-
Notifications
You must be signed in to change notification settings - Fork 1
/
util_test.ts
93 lines (86 loc) · 2.55 KB
/
util_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {
bytesToUInt32BE,
cleanUserInputFormatAndAddBase32Padding,
numberToBytes,
} from "./util.ts";
import { assertEquals } from "./test_deps.ts";
Deno.test({
name: "Converts a number to a Uint8Array representing its bytes",
fn(): void {
const uint32MaxValue = 4294967295;
assertEquals(
numberToBytes(uint32MaxValue),
new Uint8Array([0, 0, 0, 0, 255, 255, 255, 255]),
);
assertEquals(
numberToBytes(uint32MaxValue, false),
new Uint8Array([255, 255, 255, 255]),
);
assertEquals(numberToBytes(600), new Uint8Array([0, 0, 0, 0, 0, 0, 2, 88]));
assertEquals(numberToBytes(0), new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]));
assertEquals(numberToBytes(0, false), new Uint8Array([0]));
},
});
Deno.test({
name: "Converts a Uint8Array to a uint32",
fn(): void {
const uint32MaxValue = 4294967295;
// Ignores last bytes of array which contain more than 32 bit
assertEquals(
bytesToUInt32BE(new Uint8Array([255, 255, 255, 255, 255, 255, 255, 255])),
uint32MaxValue,
);
assertEquals(
bytesToUInt32BE(new Uint8Array([127, 127, 127, 127, 255, 255, 255, 255])),
2139062143,
);
assertEquals(
bytesToUInt32BE(new Uint8Array([255, 255, 255, 255, 127, 127, 127, 127])),
uint32MaxValue,
);
assertEquals(
bytesToUInt32BE(new Uint8Array([255, 255, 255, 255])),
uint32MaxValue,
);
assertEquals(
bytesToUInt32BE(
new Uint8Array([0, 0, 16, 12] /**That's the code to my heart */),
),
4108,
);
assertEquals(bytesToUInt32BE(new Uint8Array([0, 0, 0, 0])), 0);
},
});
Deno.test({
name: "Secret is modified to fit Base32 requirements",
fn(): void {
const falselyEncodedSecret =
"GEZ DGN bvg Y3T QOJ QGE ZDG NBV GY3 TQO JQG EZDG";
const treatedSecret = cleanUserInputFormatAndAddBase32Padding(
falselyEncodedSecret,
);
assertEquals(
treatedSecret,
"GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDG===",
);
const correctlyEncodedSecret = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBV";
assertEquals(
cleanUserInputFormatAndAddBase32Padding(correctlyEncodedSecret),
correctlyEncodedSecret,
);
},
});
Deno.test({
name: "Padded secrets do not get extra padding",
fn(): void {
const falselyEncodedSecret =
"GEZ DGN bvg Y3T QOJ QGE ZDG NBV GY3 TQO JQG EZDG ===";
const treatedSecret = cleanUserInputFormatAndAddBase32Padding(
falselyEncodedSecret,
);
assertEquals(
treatedSecret,
"GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDG===",
);
},
});