-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
61 lines (52 loc) · 1.71 KB
/
test.ts
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
import secureCompare from "./mod.ts";
const a = Math.random().toString(36).substring(2, 15);
const b = Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15);
Deno.test("Should take the same amount of time comparing different string sizes", () => {
let now = Date.now();
secureCompare(b, a);
const t1 = Date.now() - now;
now = Date.now();
secureCompare(b, b);
const t2 = Date.now() - now;
if (Math.abs(t1 - t2) > 1) {
throw Error(
"Constant time test failed - greater than a 1 millisecond difference.",
);
}
});
Deno.test("Should return true for equal strings", () => {
if (!secureCompare(a, a)) {
throw Error(
"Same string test failed - returned false for identical strings.",
);
}
});
Deno.test("Should return false for different strings (size, a < b)", () => {
if (secureCompare(a, a + "x")) {
throw Error(
"Different string w/ different sizes (a < b) test failed - returned true for different strings.",
);
}
});
Deno.test("Should return false for different strings (size, a > b)", () => {
if (secureCompare(a + "x", a)) {
throw Error(
"Different string w/ different sizes (a > b) test failed - returned true for different strings.",
);
}
});
Deno.test("Should return false for different strings (size, a = b)", () => {
if (secureCompare(a + "x", a + "y")) {
throw Error(
"Different string w/ same size test failed - returned true for different strings.",
);
}
});
Deno.test("Should return true if the strings are identical in utf8", () => {
if (!secureCompare("你好世界", "你好世界")) {
throw Error(
"UTF8 test failed - returned false for identical strings.",
);
}
});