-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
58 lines (51 loc) · 1.78 KB
/
test.js
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
"use strict";
const assert = require("assert");
const blns = require("blns");
const { secureCompare, secureCompareBuffer } = require("./index");
const strings = [
"",
"a",
"ab",
"AB",
"abc",
"hello world",
"你好世界",
"你好,世界",
"สวัสดีชาวโลก",
"\u00e8",
"\u01e8",
"47b604fe6993fdb942917db92ae836eba179b69dc5b5ea2a983caf5bac1119f6",
"hello\r\nworld",
];
describe("secureCompare()", () => {
it("should return true if the strings are identical", () => {
strings.forEach((str) => assert.ok(secureCompare(str, str), `"${str}"`));
blns.forEach((str) => assert.ok(secureCompare(str, str), `"${str}"`));
});
it("should return false if the strings are different", () => {
assert.ok(!secureCompare("hello", "abc"));
assert.ok(!secureCompare("abc", "ab"));
assert.ok(!secureCompare("abc", "abd"));
assert.ok(!secureCompare("", "abc"));
assert.ok(!secureCompare("你好世界", "abc"));
});
});
describe("secureCompareBuffer()", () => {
const buff1 = Buffer.from("abc");
const buff2 = Buffer.from("hello");
it("should return true if the buffers are identical", () => {
strings
.map((x) => Buffer.from(x))
.forEach((str) => assert.ok(secureCompareBuffer(str, str), `"${str}"`));
blns
.map((x) => Buffer.from(x))
.forEach((str) => assert.ok(secureCompareBuffer(str, str), `"${str}"`));
assert.ok(secureCompareBuffer(buff1, buff1));
});
it("should return false if the buffers are different", () => {
assert.ok(!secureCompareBuffer(buff2, buff1));
assert.ok(!secureCompareBuffer(Buffer.from("abc"), Buffer.from("ab")));
assert.ok(!secureCompareBuffer(Buffer.from("abc"), Buffer.from("abd")));
assert.ok(!secureCompareBuffer(Buffer.from(""), Buffer.from("abc")));
});
});