-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbase32.test.js
More file actions
164 lines (149 loc) · 7.01 KB
/
base32.test.js
File metadata and controls
164 lines (149 loc) · 7.01 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { toBase32, toBase32hex, fromBase32, fromBase32hex } from '@exodus/bytes/base32.js'
import { describe, test } from 'node:test'
import base32js from 'base32.js'
import hiBase32 from 'hi-base32'
const SharedArrayBuffer = globalThis.SharedArrayBuffer ?? ArrayBuffer
const raw = [new Uint8Array(), new Uint8Array([0]), new Uint8Array([1]), new Uint8Array([255])]
for (let i = 0; i < 50; i++) {
const size = Math.floor(Math.random() * 100)
raw.push(crypto.getRandomValues(new Uint8Array(size)))
}
const pad = (x) => (x.length % 8 === 0 ? x : x + '='.repeat(8 - (x.length % 8)))
const pool = raw.map((uint8) => {
const buffer = Buffer.from(uint8)
const base32 = base32js.encode(uint8)
const base32hex = base32js.encode(uint8, { type: 'base32hex' })
const base32padded = pad(base32)
if (base32hex.length !== base32.length) throw new Error('Unexpected base32hex length')
if (base32padded !== hiBase32.encode(uint8)) throw new Error('Unexpected mismatch with hiBase32')
const base32hexPadded = pad(base32hex)
const hex = buffer.toString('hex')
const shared = new Uint8Array(new SharedArrayBuffer(uint8.length))
shared.set(uint8)
const ab = uint8.buffer
if (ab.byteLength !== uint8.byteLength) throw new Error('Unexpected pooled Uint8Array')
return { uint8, ab, shared, buffer, hex, base32, base32padded, base32hex, base32hexPadded }
})
describe('toBase32', () => {
describe('invalid input', () => {
for (const method of [toBase32, toBase32hex]) {
test(method.name, (t) => {
for (const input of [null, undefined, [], [1, 2], new Uint16Array(1), 'string']) {
t.assert.throws(() => method(input))
}
})
}
})
test('base32', (t) => {
for (const { uint8, shared, buffer, base32, base32padded } of pool) {
for (const arg of [uint8, shared, buffer]) {
t.assert.strictEqual(toBase32(arg), base32)
t.assert.strictEqual(toBase32(arg, { padding: false }), base32)
t.assert.strictEqual(toBase32(arg, { padding: true }), base32padded)
}
}
})
test('base32hex', (t) => {
for (const { uint8, shared, buffer, base32hex, base32hexPadded } of pool) {
for (const arg of [uint8, shared, buffer]) {
t.assert.strictEqual(toBase32hex(arg), base32hex)
t.assert.strictEqual(toBase32hex(arg, { padding: false }), base32hex)
t.assert.strictEqual(toBase32hex(arg, { padding: true }), base32hexPadded)
}
}
})
})
const INVALID_FROM_TYPES = [null, undefined, [], [1, 2], ['00'], new Uint8Array()]
const INVALID_FROM_LAX = ['AB======'] // non-strict
const INVALID_FROM_SPACES = [' ', 'aaaa aaaa', 'aaaa aaa', 'AE====== ', 'ae ======'] // spaces
const INVALID_FROM_LENGTH = [
...['a', 'aaa', 'AAAAAA'],
...['a=', 'a==', 'a===', 'a====', 'a=====', 'a======', 'a=======', 'a========'],
...['aaa=', 'aaa==', 'aaa===', 'aaa====', 'aaa=====', 'aaa======', 'aaa=======', 'aaa======='],
]
const INVALID_FROM_PADDING = [
...['=', '==', '===', '====', '=====', '======', '=======', '========', '========='],
...['aa=', 'aa===', 'aa===', 'AA====', 'AA=====', 'AA======='],
...['aaaa=', 'aaaa==', 'aaaa==='],
...['a=aa', 'aa=a', '=aaa', 'aa==a', 'aaa=a', 'aa==aaaa', 'aaa=aaaa'], // symbols after =
]
const INVALID_FROM_CONTENT = [
...['########', '@@@@@@@@', 'aa######', 'aa%aaaa', 'aa!aaaaa'], // wrong chars
...['✖✖✖✖✖✖✖✖', '✖✖✖✖✖✖✖=', '✖✖✖✖✖===', '✖✖✖✖✖===', '✖✖======'], // wrong chars
...['✖✖✖✖✖✖==', '✖✖✖=====', '✖======='], // wrong chars and padding
...['x0', 'xxxx0000'], // mixed base32/base32hex
]
describe('fromBase32', () => {
test('invalid input, coherence check', (t) => {
for (const input of [
...INVALID_FROM_TYPES,
// but not INVALID_FROM_LAX
...INVALID_FROM_SPACES,
// but not INVALID_FROM_LENGTH
// but not INVALID_FROM_PADDING
...INVALID_FROM_CONTENT,
]) {
t.assert.throws(() => hiBase32.decode.asBytes(input.toUpperCase()))
}
})
test('invalid input', (t) => {
for (const input of [
...INVALID_FROM_TYPES,
...INVALID_FROM_LAX, // it's lax in both alphabets
...INVALID_FROM_SPACES,
...INVALID_FROM_LENGTH,
...INVALID_FROM_PADDING,
...INVALID_FROM_CONTENT,
]) {
t.assert.throws(() => fromBase32(input))
t.assert.throws(() => fromBase32hex(input))
t.assert.throws(() => fromBase32(input.toUpperCase()))
t.assert.throws(() => fromBase32hex(input.toUpperCase()))
t.assert.throws(() => fromBase32(input.toLowerCase()))
t.assert.throws(() => fromBase32hex(input.toLowerCase()))
for (const format of ['uint8', 'buffer', 'arraybuffer', 'hex']) {
t.assert.throws(() => fromBase32(input, { format }))
t.assert.throws(() => fromBase32hex(input, { format }))
}
}
})
test('uint8', (t) => {
for (const { base32, base32padded, base32hex, base32hexPadded, uint8 } of pool) {
t.assert.deepStrictEqual(fromBase32(base32), uint8)
t.assert.deepStrictEqual(fromBase32(base32, { format: 'uint8' }), uint8)
t.assert.deepStrictEqual(fromBase32(base32padded, { format: 'uint8' }), uint8)
t.assert.deepStrictEqual(fromBase32(base32, { padding: false }), uint8)
t.assert.deepStrictEqual(fromBase32(base32padded, { padding: true }), uint8)
t.assert.deepStrictEqual(fromBase32(base32, { padding: 'both' }), uint8)
t.assert.deepStrictEqual(fromBase32(base32padded, { padding: 'both' }), uint8)
if (base32 !== base32padded) {
t.assert.throws(() => fromBase32(base32, { padding: true }))
t.assert.throws(() => fromBase32(base32padded, { padding: false }))
}
t.assert.deepStrictEqual(fromBase32hex(base32hex), uint8)
t.assert.deepStrictEqual(fromBase32hex(base32hex, { format: 'uint8' }), uint8)
t.assert.deepStrictEqual(fromBase32hex(base32hex, { padding: false }), uint8)
t.assert.deepStrictEqual(fromBase32hex(base32hexPadded, { padding: true }), uint8)
t.assert.deepStrictEqual(fromBase32hex(base32hex, { padding: 'both' }), uint8)
t.assert.deepStrictEqual(fromBase32hex(base32hexPadded, { padding: 'both' }), uint8)
if (base32hex !== base32hexPadded) {
t.assert.throws(() => fromBase32hex(base32hex, { padding: true }))
t.assert.throws(() => fromBase32hex(base32hexPadded, { padding: false }))
}
}
})
test('buffer', (t) => {
for (const { base32, base32padded, base32hex, buffer } of pool) {
t.assert.deepStrictEqual(fromBase32(base32, { format: 'buffer' }), buffer)
t.assert.deepStrictEqual(fromBase32(base32padded, { format: 'buffer' }), buffer)
t.assert.deepStrictEqual(fromBase32hex(base32hex, { format: 'buffer' }), buffer)
}
})
test('arraybuffer', (t) => {
for (const { base32, base32padded, base32hex, ab } of pool) {
t.assert.deepStrictEqual(fromBase32(base32, { format: 'arraybuffer' }), ab)
t.assert.deepStrictEqual(fromBase32(base32padded, { format: 'arraybuffer' }), ab)
t.assert.deepStrictEqual(fromBase32hex(base32hex, { format: 'arraybuffer' }), ab)
}
})
})