-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathheader_test.ts
More file actions
115 lines (93 loc) · 3.18 KB
/
Copy pathheader_test.ts
File metadata and controls
115 lines (93 loc) · 3.18 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import {
assert,
assertEquals,
assertFalse,
assertNotEquals,
assertThrows,
} from "./dev_deps.ts";
import { bytes, decodeBase64Url } from "./deps.ts";
import { DEFAULT_RECORD_SIZE, SALT_LENGTH } from "./const.ts";
import { Header, RecordSizeError } from "./header.ts";
import { Salt } from "./salt.ts";
Deno.test("Header/constructor/default", () => {
const h = new Header({});
assert(h.rs === DEFAULT_RECORD_SIZE);
assert(h.keyid.length === 0);
assertFalse(h.salt === null);
assertFalse(h.salt.equals(new Salt(new Uint8Array(SALT_LENGTH))));
});
Deno.test("Header/constructor/InvalidRecordSize", () => {
assertThrows(() => {
new Header({ rs: 0 });
}, RecordSizeError);
// Min value
new Header({ rs: 18 });
// Max value
new Header({ rs: 2 ** 36 - 31 });
assertThrows(() => {
new Header({ rs: 2 ** 36 - 30 });
}, RecordSizeError);
});
Deno.test("Header/equal", () => {
const h1 = new Header({});
const h2 = new Header({ salt: h1.salt, rs: h1.rs, keyid: h1.keyid });
assertEquals(h1, h2);
assert(h1.equals(h2));
const h3 = new Header({});
assertNotEquals(h1, h3);
assertFalse(h1.equals(h3));
});
Deno.test("Header/not/equal", () => {
const h1 = new Header({ keyid: Uint8Array.of(66) });
const h2 = new Header({ keyid: Uint8Array.of(67) });
assertNotEquals(h1, h2);
});
Deno.test("Header/toBytes", () => {
const innerSalt = decodeBase64Url("I1BsxtFttlv3u_Oo94xnmw");
const salt = new Salt(innerSalt);
const h = new Header({ salt, rs: 4096, keyid: new Uint8Array() });
const rs = Uint8Array.of(0, 0, 16, 0); // 4096
const idlen = Uint8Array.of(h.idlen);
assert(
bytes.equals(
new Uint8Array(h.toBytes()),
bytes.concat([innerSalt, rs, idlen, h.keyid]),
),
);
});
Deno.test("Header/toBase64", () => {
const innerSalt = decodeBase64Url("I1BsxtFttlv3u_Oo94xnmw");
const salt = new Salt(innerSalt);
const h = new Header({ salt, rs: 4096, keyid: new Uint8Array() });
assert(h.toBase64() === "I1BsxtFttlv3u/Oo94xnmwAAEAAA");
});
Deno.test("Header/toBase64Url", () => {
const innerSalt = decodeBase64Url("I1BsxtFttlv3u_Oo94xnmw");
const salt = new Salt(innerSalt);
const h = new Header({ salt, rs: 4096, keyid: new Uint8Array() });
assert(h.toBase64Url() === "I1BsxtFttlv3u_Oo94xnmwAAEAAA");
});
Deno.test("Header/fromBytes", () => {
const innerSalt = decodeBase64Url("I1BsxtFttlv3u_Oo94xnmw");
const salt = new Salt(innerSalt);
const h1 = new Header({ salt, rs: 4096, keyid: new Uint8Array() });
const h2 = Header.fromBytes(h1.toBytes());
assertFalse(h1 === h2);
assert(h1.equals(h2));
});
Deno.test("Header/fromBase64", () => {
const innerSalt = decodeBase64Url("I1BsxtFttlv3u_Oo94xnmw");
const salt = new Salt(innerSalt);
const h1 = new Header({ salt, rs: 4096, keyid: new Uint8Array() });
const h2 = Header.fromBase64(h1.toBase64());
assertFalse(h1 === h2);
assert(h1.equals(h2));
});
Deno.test("Header/fromBase64Url", () => {
const innerSalt = decodeBase64Url("I1BsxtFttlv3u_Oo94xnmw");
const salt = new Salt(innerSalt);
const h1 = new Header({ salt, rs: 4096, keyid: new Uint8Array() });
const h2 = Header.fromBase64Url(h1.toBase64Url());
assertFalse(h1 === h2);
assert(h1.equals(h2));
});