
































@@ -20,6 +20,16 @@ function createCollectingMetrics() {
2020};
2121}
222223+function expectThrowsError(run: () => unknown): void {
24+let error: unknown;
25+try {
26+run();
27+} catch (caught) {
28+error = caught;
29+}
30+expect(error).toBeInstanceOf(Error);
31+}
32+2333// ============================================================================
2434// Fuzz Tests for validatePrivateKey
2535// ============================================================================
@@ -28,7 +38,7 @@ describe("validatePrivateKey fuzz", () => {
2838describe("validatePrivateKey type confusion", () => {
2939it("rejects non-string input", () => {
3040for (const value of [null, undefined, 123, true, {}, [], () => {}]) {
31-expect(() => validatePrivateKey(value as unknown as string)).toThrow();
41+expectThrowsError(() => validatePrivateKey(value as unknown as string));
3242}
3343});
3444});
@@ -49,42 +59,42 @@ describe("validatePrivateKey fuzz", () => {
4959];
50605161for (const key of invalidKeys) {
52-expect(() => validatePrivateKey(key)).toThrow();
62+expectThrowsError(() => validatePrivateKey(key));
5363}
5464});
5565});
56665767describe("edge cases", () => {
5868it("rejects very long string", () => {
5969const veryLong = "a".repeat(10000);
60-expect(() => validatePrivateKey(veryLong)).toThrow();
70+expectThrowsError(() => validatePrivateKey(veryLong));
6171});
62726373it("rejects string of spaces matching length", () => {
6474const spaces = " ".repeat(64);
65-expect(() => validatePrivateKey(spaces)).toThrow();
75+expectThrowsError(() => validatePrivateKey(spaces));
6676});
67776878it("rejects hex with spaces between characters", () => {
6979const withSpaces =
7080"01 23 45 67 89 ab cd ef 01 23 45 67 89 ab cd ef 01 23 45 67 89 ab cd ef 01 23 45 67 89 ab cd ef";
71-expect(() => validatePrivateKey(withSpaces)).toThrow();
81+expectThrowsError(() => validatePrivateKey(withSpaces));
7282});
7383});
74847585describe("nsec format edge cases", () => {
7686it("rejects nsec with invalid bech32 characters", () => {
7787// 'b', 'i', 'o' are not valid bech32 characters
7888const invalidBech32 = "nsec1qypqxpq9qtpqscx7peytbfwtdjmcv0mrz5rjpej8vjppfkqfqy8skqfv3l";
79-expect(() => validatePrivateKey(invalidBech32)).toThrow();
89+expectThrowsError(() => validatePrivateKey(invalidBech32));
8090});
81918292it("rejects nsec with wrong prefix", () => {
83-expect(() => validatePrivateKey("nsec0aaaa")).toThrow();
93+expectThrowsError(() => validatePrivateKey("nsec0aaaa"));
8494});
85958696it("rejects partial nsec", () => {
87-expect(() => validatePrivateKey("nsec1")).toThrow();
97+expectThrowsError(() => validatePrivateKey("nsec1"));
8898});
8999});
90100});
@@ -119,7 +129,7 @@ describe("normalizePubkey fuzz", () => {
119129describe("prototype pollution attempts", () => {
120130it("throws for prototype property names", () => {
121131for (const value of ["__proto__", "constructor", "prototype"]) {
122-expect(() => normalizePubkey(value)).toThrow();
132+expectThrowsError(() => normalizePubkey(value));
123133}
124134});
125135});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。