Repository navigation
Expand file tree
/
Copy pathWebCryptV06.test.js
More file actions
81 lines (69 loc) · 2.82 KB
/
Copy pathWebCryptV06.test.js
File metadata and controls
81 lines (69 loc) · 2.82 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
// __tests__/WebCryptV06.test.js
import { WebCrypt } from "../src/WebCrypt.js";
import { WebCryptAsym } from "../src/WebCryptAsym.js";
import { WebCryptPQC } from "../src/WebCryptPQC.js";
import TimingSafeHelper from "../src/TimingSafeHelper.js";
import fs from "fs";
import path from "path";
describe("WebCrypt v0.7.x Features & Verification", () => {
test("package.json version matches semver", () => {
const pkgPath = path.join(process.cwd(), "package.json");
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
expect(pkg.version).toMatch(/^\d+\.\d+\.\d+$/);
expect(pkg.author).toBe("PuterVision");
expect(pkg.homepage).toBe(pkg.homepage);
});
test("TimingSafeHelper.sleepWithDummyOps is non-blocking", async () => {
const start = performance.now();
await TimingSafeHelper.sleepWithDummyOps(15);
const elapsed = performance.now() - start;
expect(elapsed).toBeGreaterThanOrEqual(10);
});
test("Chunked Base64 handles large buffers cleanly", async () => {
const wc = new WebCrypt();
const largeBuffer = new Uint8Array(100 * 1024); // 100KB
for (let i = 0; i < largeBuffer.length; i++) {
largeBuffer[i] = i % 256;
}
const b64 = wc._arrayBufferToBase64(largeBuffer.buffer);
expect(typeof b64).toBe("string");
expect(b64.length).toBeGreaterThan(0);
const recoveredBuffer = new Uint8Array(wc._base64ToArrayBuffer(b64));
expect(recoveredBuffer.length).toBe(largeBuffer.length);
expect(recoveredBuffer[0]).toBe(largeBuffer[0]);
expect(recoveredBuffer[50000]).toBe(largeBuffer[50000]);
});
test("TimingSafeHelper branch tests", async () => {
const diff1 = await TimingSafeHelper.constantTimeCompareStrings("abc", "defg");
expect(diff1).toBe(false);
const diff2 = await TimingSafeHelper.constantTimeCompareBuffers(
new Uint8Array([1, 2]),
new Uint8Array([1, 2, 3])
);
expect(diff2).toBe(false);
const valid = await TimingSafeHelper.timingSafeVerify(
globalThis.crypto,
{ name: "HMAC" },
null,
new Uint8Array(32),
new Uint8Array([1, 2, 3])
);
expect(valid).toBe(false);
});
test("WebCrypt static helpers and error branches", async () => {
const wc = new WebCrypt();
const salt = WebCrypt.generateHmacSalt(32);
expect(salt.length).toBe(32);
await expect(wc.decryptData("invalid-base64-json", "password")).rejects.toThrow();
const circular = {};
circular.self = circular;
await expect(wc.encryptData(circular, "password")).rejects.toThrow("Failed to serialize data");
});
test("WebCryptAsym deprecated stubs", () => {
const wca = new WebCryptAsym();
expect(wca._getCachedKey()).toBeNull();
expect(() => wca._cacheKey()).not.toThrow();
expect(() => wca.clearKeyCache()).not.toThrow();
expect(() => wca.stopAutoCleanup()).not.toThrow();
});
});