-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsalt_test.ts
74 lines (59 loc) · 2 KB
/
salt_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
62
63
64
65
66
67
68
69
70
71
72
73
74
import { bytes } from "./deps.ts";
import { assert, assertFalse, assertThrows } from "./dev_deps.ts";
import { SALT_LENGTH } from "./const.ts";
import { Salt, SaltLengthError } from "./salt.ts";
Deno.test("Salt/constructor/default", () => {
const salt = new Salt();
assert(salt.length === SALT_LENGTH);
const other = new Salt();
assertFalse(bytes.equals(salt, other));
});
Deno.test("Salt/constructor/Uint8Array", () => {
const buf = new Uint8Array(SALT_LENGTH);
crypto.getRandomValues(buf);
const salt = new Salt(buf);
assert(salt.length === SALT_LENGTH);
assert(salt.every((_, i) => salt.at(i) === buf.at(i)));
// Ensure buffer was copied
crypto.getRandomValues(buf);
assertFalse(salt.every((_, i) => salt.at(i) === buf.at(i)));
});
Deno.test("Salt/constructor/Uint8Array/InvalidLength", () => {
for (const saltLength of [SALT_LENGTH - 1, SALT_LENGTH + 1]) {
const buf = new Uint8Array(saltLength);
crypto.getRandomValues(buf);
assertThrows(() => {
new Salt(buf);
}, SaltLengthError);
}
});
Deno.test("Salt/constructor/ArrayBuffer", () => {
const arr = new Uint8Array(SALT_LENGTH);
crypto.getRandomValues(arr);
const salt = new Salt(arr.buffer);
assert(salt.length === SALT_LENGTH);
assert(salt.every((_, i) => salt.at(i) === arr.at(i)));
// Ensure buffer was copied
crypto.getRandomValues(arr);
assertFalse(salt.every((_, i) => salt.at(i) === arr.at(i)));
});
Deno.test("Salt/constructor/ArrayBuffer/InvalidLength", () => {
for (const saltLength of [SALT_LENGTH - 1, SALT_LENGTH + 1]) {
const buf = new Uint8Array(saltLength);
const dv = new DataView(buf.buffer);
for (let i = 0; i < saltLength; i++) {
dv.setUint8(i, Math.floor(Math.random() * 255));
}
assertThrows(() => {
new Salt(buf.buffer);
}, SaltLengthError);
}
});
Deno.test("Salt/equal", () => {
const h1 = new Salt();
assert(h1.equals(h1));
const h2 = new Salt(h1);
assert(h1.equals(h2));
crypto.getRandomValues(h2);
assertFalse(h1.equals(h2));
});