Repository navigation
Expand file tree
/
Copy pathWebCryptFullCoverage.test.js
More file actions
90 lines (70 loc) · 3.12 KB
/
Copy pathWebCryptFullCoverage.test.js
File metadata and controls
90 lines (70 loc) · 3.12 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// __tests__/WebCryptFullCoverage.test.js
import { WebCrypt } from "../src/WebCrypt.js";
describe("WebCrypt Full Coverage Expansion", () => {
test("Key cache LRU eviction and manual cleanup", async () => {
const wc = new WebCrypt();
const fakeKey = {};
// Populate key cache with 10 items directly
for (let i = 0; i < 10; i++) {
wc.keyCache.set(`pass-${i}:salt`, { key: fakeKey, createdAt: Date.now() - (10 - i) * 1000 });
}
expect(wc.keyCache.size).toBe(10);
// Deriving key 11 triggers LRU eviction of oldest key
await wc._deriveKey("new-pass", new Uint8Array(16));
expect(wc.keyCache.size).toBeLessThanOrEqual(10);
wc._cleanupExpiredKeys();
wc.clearKeyCache();
expect(wc.keyCache.size).toBe(0);
wc.stopAutoCleanup();
});
test("generateRandomPassword and static helpers", () => {
const wc = new WebCrypt();
const pwd16 = wc.generateRandomPassword(16);
expect(pwd16.length).toBe(32); // 16 bytes = 32 hex chars
const pwd64 = wc.generateRandomPassword(64);
expect(pwd64.length).toBe(128); // 64 bytes = 128 hex chars
});
test("generateHmacKeySHA3 with random key generation across SHA3-256/384/512", async () => {
const wc = new WebCrypt();
// Random HMAC key generation (instant execution)
const key1 = await wc.generateHmacKeySHA3(null, "SHA3-256");
expect(key1).toBeDefined();
const key2 = await wc.generateHmacKeySHA3(null, "SHA3-384");
expect(key2).toBeDefined();
const key3 = await wc.generateHmacKeySHA3(null, "SHA3-512");
expect(key3).toBeDefined();
});
test("WebRTC Insertable Streams createEncryptTransform & createDecryptTransform error handling", async () => {
const wc = new WebCrypt();
const encryptTransform = await wc.createEncryptTransform("webrtc-pass");
const decryptTransform = await wc.createDecryptTransform("webrtc-pass");
const rawData = new Uint8Array([10, 20, 30, 40, 50]).buffer;
const frame = { data: rawData };
const enqueued = [];
const controller = {
enqueue: f => enqueued.push(f),
};
// Encrypt frame
await encryptTransform(frame, controller);
expect(enqueued.length).toBe(1);
expect(enqueued[0].data.byteLength).toBeGreaterThan(rawData.byteLength);
// Decrypt frame
const encryptedFrame = { data: enqueued[0].data };
const decryptedEnqueued = [];
const decryptController = {
enqueue: f => decryptedEnqueued.push(f),
};
await decryptTransform(encryptedFrame, decryptController);
expect(decryptedEnqueued.length).toBe(1);
expect(new Uint8Array(decryptedEnqueued[0].data)).toEqual(new Uint8Array([10, 20, 30, 40, 50]));
// Corrupted frame decryption error handling
const corruptFrame = { data: new Uint8Array(20).buffer };
const corruptEnqueued = [];
await decryptTransform(corruptFrame, { enqueue: f => corruptEnqueued.push(f) });
});
test("decryptFile handles invalid ciphertext and oversized payloads", async () => {
const wc = new WebCrypt();
const invalidCiphertext = new Uint8Array(10); // Too short
await expect(wc.decryptFile(invalidCiphertext, "password")).rejects.toThrow();
});
});