Skip to content

Commit 0e98fdc

Browse files
committed
crypto: simplify test with expectAsync()
1 parent c1a4224 commit 0e98fdc

File tree

2 files changed

+23
-78
lines changed

2 files changed

+23
-78
lines changed

packages/crypto/src/libsodium.spec.ts

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -393,51 +393,31 @@ describe("Libsodium", () => {
393393
{
394394
// empty
395395
const key = fromHex("");
396-
await Xchacha20poly1305Ietf.encrypt(message, key, nonce).then(
397-
() => {
398-
throw new Error("encryption must not succeed");
399-
},
400-
(error) => {
401-
expect(error).toMatch(/key, got length=0/);
402-
},
396+
await expectAsync(Xchacha20poly1305Ietf.encrypt(message, key, nonce)).toBeRejectedWithError(
397+
/key, got length=0/,
403398
);
404399
}
405400
{
406401
// 31 bytes
407402
const key = fromHex("1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916");
408-
await Xchacha20poly1305Ietf.encrypt(message, key, nonce).then(
409-
() => {
410-
throw new Error("encryption must not succeed");
411-
},
412-
(error) => {
413-
expect(error).toMatch(/key, got length=31/);
414-
},
403+
await expectAsync(Xchacha20poly1305Ietf.encrypt(message, key, nonce)).toBeRejectedWithError(
404+
/key, got length=31/,
415405
);
416406
}
417407
{
418408
// 33 bytes
419409
const key = fromHex("1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d8aa");
420-
await Xchacha20poly1305Ietf.encrypt(message, key, nonce).then(
421-
() => {
422-
throw new Error("encryption must not succeed");
423-
},
424-
(error) => {
425-
expect(error).toMatch(/key, got length=33/);
426-
},
410+
await expectAsync(Xchacha20poly1305Ietf.encrypt(message, key, nonce)).toBeRejectedWithError(
411+
/key, got length=33/,
427412
);
428413
}
429414
{
430415
// 64 bytes
431416
const key = fromHex(
432417
"1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d81324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d8",
433418
);
434-
await Xchacha20poly1305Ietf.encrypt(message, key, nonce).then(
435-
() => {
436-
throw new Error("encryption must not succeed");
437-
},
438-
(error) => {
439-
expect(error).toMatch(/key, got length=64/);
440-
},
419+
await expectAsync(Xchacha20poly1305Ietf.encrypt(message, key, nonce)).toBeRejectedWithError(
420+
/key, got length=64/,
441421
);
442422
}
443423
});

packages/crypto/src/secp256k1.spec.ts

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -68,58 +68,23 @@ describe("Secp256k1", () => {
6868
).toBeTruthy();
6969

7070
// too short and too long
71-
await Secp256k1.makeKeypair(
72-
fromHex("e4ade2a5232a7c6f37e7b854a774e25e6047ee7c6d63e8304ae04fa190bc17"),
73-
).then(
74-
() => {
75-
throw new Error("promise must be rejected");
76-
},
77-
(error) => {
78-
expect(error.message).toContain("not a valid secp256k1 private key");
79-
},
80-
);
81-
await Secp256k1.makeKeypair(
82-
fromHex("e4ade2a5232a7c6f37e7b854a774e25e6047ee7c6d63e8304ae04fa190bc1732aa"),
83-
).then(
84-
() => {
85-
throw new Error("promise must be rejected");
86-
},
87-
(error) => {
88-
expect(error.message).toContain("not a valid secp256k1 private key");
89-
},
90-
);
71+
await expectAsync(
72+
Secp256k1.makeKeypair(fromHex("e4ade2a5232a7c6f37e7b854a774e25e6047ee7c6d63e8304ae04fa190bc17")),
73+
).toBeRejectedWithError("input data is not a valid secp256k1 private key");
74+
await expectAsync(
75+
Secp256k1.makeKeypair(fromHex("e4ade2a5232a7c6f37e7b854a774e25e6047ee7c6d63e8304ae04fa190bc1732aa")),
76+
).toBeRejectedWithError("input data is not a valid secp256k1 private key");
9177
// value out of range (too small)
92-
await Secp256k1.makeKeypair(
93-
fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
94-
).then(
95-
() => {
96-
throw new Error("promise must be rejected");
97-
},
98-
(error) => {
99-
expect(error.message).toContain("not a valid secp256k1 private key");
100-
},
101-
);
78+
await expectAsync(
79+
Secp256k1.makeKeypair(fromHex("0000000000000000000000000000000000000000000000000000000000000000")),
80+
).toBeRejectedWithError("input data is not a valid secp256k1 private key");
10281
// value out of range (>= n)
103-
await Secp256k1.makeKeypair(
104-
fromHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
105-
).then(
106-
() => {
107-
throw new Error("promise must be rejected");
108-
},
109-
(error) => {
110-
expect(error.message).toContain("not a valid secp256k1 private key");
111-
},
112-
);
113-
await Secp256k1.makeKeypair(
114-
fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"),
115-
).then(
116-
() => {
117-
throw new Error("promise must be rejected");
118-
},
119-
(error) => {
120-
expect(error.message).toContain("not a valid secp256k1 private key");
121-
},
122-
);
82+
await expectAsync(
83+
Secp256k1.makeKeypair(fromHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")),
84+
).toBeRejectedWithError("input data is not a valid secp256k1 private key");
85+
await expectAsync(
86+
Secp256k1.makeKeypair(fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141")),
87+
).toBeRejectedWithError("input data is not a valid secp256k1 private key");
12388
});
12489

12590
it("creates signatures", async () => {

0 commit comments

Comments
 (0)