-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbase64.test.js
More file actions
200 lines (179 loc) · 8.23 KB
/
base64.test.js
File metadata and controls
200 lines (179 loc) · 8.23 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { toBase64, toBase64url, fromBase64, fromBase64url } from '@exodus/bytes/base64.js'
import * as js from '../fallback/base64.js'
import { describe, test } from 'node:test'
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 pool = raw.map((uint8) => {
const buffer = Buffer.from(uint8)
const base64 = buffer.toString('base64')
const base64nopad = base64.replaceAll('=', '')
const base64urlPadded = base64.replaceAll('+', '-').replaceAll('/', '_')
const base64urlFallback = base64urlPadded.replaceAll('=', '')
let base64url = base64urlFallback
try {
base64url = buffer.toString('base64url') // unsupported by https://npmjs.com/package/buffer
} catch {}
if (base64url !== base64urlFallback) throw new Error('Unexpected base64url mismatch with Buffer')
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, base64, base64nopad, base64url, base64urlPadded }
})
describe('toBase64', () => {
describe('invalid input', () => {
for (const method of [toBase64, toBase64url]) {
test(method.name, (t) => {
for (const input of [null, undefined, [], [1, 2], new Uint16Array(1), 'string']) {
t.assert.throws(() => method(input))
}
})
}
})
test('base64', (t) => {
for (const { uint8, shared, buffer, base64, base64nopad } of pool) {
for (const arg of [uint8, shared, buffer]) {
t.assert.strictEqual(toBase64(arg), base64)
t.assert.strictEqual(toBase64(arg, { padding: true }), base64)
t.assert.strictEqual(toBase64(arg, { padding: false }), base64nopad)
t.assert.strictEqual(js.toBase64(arg, false, false), base64nopad)
t.assert.strictEqual(js.toBase64(arg, false, true), base64)
}
}
})
test('base64url', (t) => {
for (const { uint8, shared, buffer, base64url, base64urlPadded } of pool) {
for (const arg of [uint8, shared, buffer]) {
t.assert.strictEqual(toBase64url(arg), base64url)
t.assert.strictEqual(toBase64url(arg, { padding: false }), base64url)
t.assert.strictEqual(toBase64url(arg, { padding: true }), base64urlPadded)
t.assert.strictEqual(js.toBase64(arg, true, false), base64url)
t.assert.strictEqual(js.toBase64(arg, true, true), base64urlPadded)
}
}
})
})
const INVALID_FROM_TYPES = [null, undefined, [], [1, 2], ['00'], new Uint8Array()]
const INVALID_FROM_LAX = ['aa=='] // non-strict
const INVALID_FROM_SPACES = [
...[' ', ' ', 'aaaa aaaa', 'aaaa aaaa', 'aaaa aaaa', 'aaaa aaa', 'aa== ', 'aa =='], // spaces
...['\n', '\n\n\n\n', 'aaaa\naaaa', 'aaaa\n\n\n\naaaa', 'aaaa\n', 'aaaa\n\n\n\n'], // newlines
...['\u00A0', '\u00A0\u00A0\u00A0\u00A0', 'aaaa\u00A0\u00A0\u00A0\u00A0aaaa'], // nbsp
]
const INVALID_FROM_PADDED = ['_aY=', '_aa=', '-a==', '-Q=='] // padded base64url
const INVALID_FROM_CONTENT = [
...['a', 'aaaaa'], // wrong length
...['=', '==', '===', '====', 'a=', 'a==', 'a===', 'a====', 'aa=', 'aa===', 'aa==='], // wrong padding
...['aaa==', 'aaa===', 'aaa====', 'aaaa=', 'aaaa==', 'aaaa==='], // wrong padding
...['####', '@@@@', 'aaa#', 'a%aa'], // wrong chars
...['Z−==', '✖✖✖✖'], // wrong chars
...['a-+a', 'aa+_', 'aa_/', '-a/a'], // mixed base64/base64url
...['a=aa', 'aa=a', '=aaa', 'aa==a', 'aaa=a', 'aa==aaaa', 'aaa=aaaa'], // symbols after =
]
describe('fromBase64', () => {
if (Uint8Array.fromBase64 && !['jsc', 'webkit'].includes(process.env.EXODUS_TEST_PLATFORM)) {
test('invalid input, coherence check', (t) => {
for (const input of [
...INVALID_FROM_TYPES,
...INVALID_FROM_LAX,
// but not INVALID_FROM_SPACES,
// but not INVALID_FROM_PADDED
...INVALID_FROM_CONTENT,
]) {
t.assert.throws(() => Uint8Array.fromBase64(input, { lastChunkHandling: 'strict' }))
t.assert.throws(() =>
Uint8Array.fromBase64(input, { lastChunkHandling: 'strict', alphabet: 'base64' })
)
t.assert.throws(() =>
Uint8Array.fromBase64(input, { lastChunkHandling: 'strict', alphabet: 'base64url' })
)
}
})
}
test('invalid input', (t) => {
for (const input of [
...INVALID_FROM_TYPES,
...INVALID_FROM_LAX,
...INVALID_FROM_SPACES,
...INVALID_FROM_PADDED,
...INVALID_FROM_CONTENT,
]) {
t.assert.throws(() => fromBase64(input))
t.assert.throws(() => fromBase64url(input))
for (const format of ['uint8', 'arraybuffer', 'buffer', 'hex']) {
t.assert.throws(() => fromBase64(input, { format }))
t.assert.throws(() => fromBase64url(input, { format }))
}
}
})
test('invalid input, fallback', (t) => {
for (const input of [...INVALID_FROM_SPACES, ...INVALID_FROM_LAX, ...INVALID_FROM_CONTENT]) {
t.assert.throws(() => js.fromBase64(input, false), input)
t.assert.throws(() => js.fromBase64(input, true), input)
}
})
test('uint8', (t) => {
for (const { base64, base64nopad, base64url, base64urlPadded, uint8 } of pool) {
t.assert.deepStrictEqual(fromBase64(base64), uint8)
t.assert.deepStrictEqual(fromBase64(base64, { format: 'uint8' }), uint8)
t.assert.deepStrictEqual(fromBase64(base64nopad, { format: 'uint8' }), uint8)
t.assert.deepStrictEqual(fromBase64(base64, { padding: true }), uint8)
t.assert.deepStrictEqual(fromBase64(base64nopad, { padding: false }), uint8)
t.assert.deepStrictEqual(fromBase64(base64, { padding: 'both' }), uint8)
t.assert.deepStrictEqual(fromBase64(base64nopad, { padding: 'both' }), uint8)
if (base64 !== base64nopad) {
t.assert.throws(() => fromBase64(base64, { padding: false }))
t.assert.throws(() => fromBase64(base64nopad, { padding: true }))
}
t.assert.deepStrictEqual(fromBase64url(base64url), uint8)
t.assert.deepStrictEqual(fromBase64url(base64url, { format: 'uint8' }), uint8)
t.assert.deepStrictEqual(fromBase64url(base64url, { padding: false }), uint8)
t.assert.deepStrictEqual(fromBase64url(base64urlPadded, { padding: true }), uint8)
t.assert.deepStrictEqual(fromBase64url(base64url, { padding: 'both' }), uint8)
t.assert.deepStrictEqual(fromBase64url(base64urlPadded, { padding: 'both' }), uint8)
if (base64url !== base64urlPadded) {
t.assert.throws(() => fromBase64url(base64urlPadded, { padding: false }))
t.assert.throws(() => fromBase64url(base64url, { padding: true }))
}
}
})
test('buffer', (t) => {
for (const { base64, base64url, buffer } of pool) {
t.assert.deepStrictEqual(fromBase64(base64, { format: 'buffer' }), buffer)
t.assert.deepStrictEqual(fromBase64url(base64url, { format: 'buffer' }), buffer)
}
})
test('arraybuffer', (t) => {
for (const { base64, base64url, ab } of pool) {
t.assert.deepStrictEqual(fromBase64(base64, { format: 'arraybuffer' }), ab)
t.assert.deepStrictEqual(fromBase64url(base64url, { format: 'arraybuffer' }), ab)
}
})
test('fallback', (t) => {
for (const { base64, base64nopad, base64url, base64urlPadded, uint8 } of pool) {
t.assert.deepStrictEqual(js.fromBase64(base64, false), uint8)
t.assert.deepStrictEqual(js.fromBase64(base64url, true), uint8)
t.assert.deepStrictEqual(js.fromBase64(base64nopad, false), uint8)
t.assert.deepStrictEqual(js.fromBase64(base64urlPadded, true), uint8)
}
})
})
test('fromBase64 returns non-pooled Uint8Array instances', (t) => {
for (let i = 0; i < 256; i++) {
t.assert.strictEqual(fromBase64('A'.repeat(128 * 4)).buffer.byteLength, 128 * 3)
}
for (let i = 0; i < 256; i++) {
t.assert.strictEqual(fromBase64('A'.repeat(64 * 4)).buffer.byteLength, 64 * 3)
}
for (let i = 0; i < 512; i++) {
t.assert.strictEqual(fromBase64('A'.repeat(32 * 4)).buffer.byteLength, 32 * 3)
}
for (let i = 0; i < 512; i++) {
t.assert.strictEqual(fromBase64('').buffer.byteLength, 0)
}
})