Repository navigation
Expand file tree
/
Copy pathWebCryptAsym100Coverage.test.js
More file actions
67 lines (56 loc) · 2.65 KB
/
Copy pathWebCryptAsym100Coverage.test.js
File metadata and controls
67 lines (56 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// __tests__/WebCryptAsym100Coverage.test.js
import { WebCryptAsym } from "../src/WebCryptAsym.js";
describe("WebCryptAsym 100% Coverage Expansion", () => {
let wca;
let rsaKeyPair;
beforeAll(async () => {
wca = new WebCryptAsym();
rsaKeyPair = await wca.generateKeyPair();
});
test("decryptJWE error handling traps across all validation branches", async () => {
// 1. Invalid input type
await expect(wca.decryptJWE(12345, rsaKeyPair.privateKey)).rejects.toThrow(
"Invalid JWE token format"
);
// 2. Invalid number of parts
await expect(wca.decryptJWE("part1.part2.part3", rsaKeyPair.privateKey)).rejects.toThrow(
"Invalid JWE token structure"
);
// 3. Invalid header JSON
await expect(
wca.decryptJWE("!!!invalid-header!!!.part2.part3.part4.part5", rsaKeyPair.privateKey)
).rejects.toThrow("Failed to parse JWE header");
// 4. Unsupported algorithm in header
const badAlgHeader = wca._arrayBufferToBase64Url(
new TextEncoder().encode(JSON.stringify({ alg: "INVALID", enc: "A256GCM" })).buffer
);
await expect(
wca.decryptJWE(`${badAlgHeader}.part2.part3.part4.part5`, rsaKeyPair.privateKey)
).rejects.toThrow("Unsupported JWE algorithm");
// 5. Unsupported encryption in header
const badEncHeader = wca._arrayBufferToBase64Url(
new TextEncoder().encode(JSON.stringify({ alg: "RSA-OAEP", enc: "INVALID" })).buffer
);
await expect(
wca.decryptJWE(`${badEncHeader}.part2.part3.part4.part5`, rsaKeyPair.privateKey)
).rejects.toThrow("Unsupported JWE encryption");
// 6. Failed CEK decryption
const validHeader = wca._arrayBufferToBase64Url(
new TextEncoder().encode(JSON.stringify({ alg: "RSA-OAEP", enc: "A256GCM" })).buffer
);
const badCek = wca._arrayBufferToBase64Url(new Uint8Array(16).buffer);
await expect(
wca.decryptJWE(`${validHeader}.${badCek}.part3.part4.part5`, rsaKeyPair.privateKey)
).rejects.toThrow("Failed to decrypt Content Encryption Key");
});
test("encryptText and decryptText invalid input error handling", async () => {
await expect(wca.decryptText(null, rsaKeyPair.privateKey)).rejects.toThrow();
await expect(wca.decryptText("invalid-b64-rsa", rsaKeyPair.privateKey)).rejects.toThrow();
});
test("encryptFile and decryptFile invalid input error handling", async () => {
await expect(wca.encryptFile(null, rsaKeyPair.publicKey)).rejects.toThrow();
await expect(wca.decryptFile(null, rsaKeyPair.privateKey)).rejects.toThrow();
const corruptBlob = new Blob([new Uint8Array(10)]);
await expect(wca.decryptFile(corruptBlob, rsaKeyPair.privateKey)).rejects.toThrow();
});
});