-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathtest-multibase.spec.ts
More file actions
227 lines (185 loc) · 7.6 KB
/
Copy pathtest-multibase.spec.ts
File metadata and controls
227 lines (185 loc) · 7.6 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* globals describe, it */
import { assert } from 'aegir/chai'
import * as b10 from '../src/bases/base10.ts'
import * as b16 from '../src/bases/base16.ts'
import * as b2 from '../src/bases/base2.ts'
import * as b32 from '../src/bases/base32.ts'
import * as b36 from '../src/bases/base36.ts'
import * as b58 from '../src/bases/base58.ts'
import * as b64 from '../src/bases/base64.ts'
import * as b8 from '../src/bases/base8.ts'
import * as id from '../src/bases/identity.ts'
import * as bytes from '../src/bytes.ts'
const { base16, base32, base58btc, base64 } = { ...b16, ...b32, ...b58, ...b64 }
describe('multibase', () => {
for (const base of [base16, base32, base58btc, base64]) {
describe(`basics ${base.name}`, () => {
it('encode/decode', () => {
const string = base.encode(bytes.fromString('test'))
assert.deepStrictEqual(string[0], base.prefix)
const buffer = base.decode(string)
assert.deepStrictEqual(buffer, bytes.fromString('test'))
})
it('pristine backing buffer', () => {
// some deepEqual() libraries go as deep as the backing buffer, make sure it's pristine
const string = base.encode(bytes.fromString('test'))
const buffer = base.decode(string)
const expected = bytes.fromString('test')
assert.deepStrictEqual(new Uint8Array(buffer).join(','), new Uint8Array(expected.buffer).join(','))
})
it('empty', () => {
const str = base.encode(bytes.fromString(''))
assert.deepStrictEqual(str, base.prefix)
assert.deepStrictEqual(base.decode(str), bytes.fromString(''))
})
it('bad chars', () => {
const str = base.prefix + '#$%^&*&^%$#'
const msg = `Non-${base.name} character`
assert.throws(() => base.decode(str), msg)
})
})
}
it('encode string failure', () => {
const msg = 'Unknown type, must be binary type'
// @ts-expect-error - expects bytes
assert.throws(() => base32.encode('asdf'), msg)
// @ts-expect-error - expects bytes
assert.throws(() => base32.encoder.encode('asdf'), msg)
})
it('decode int failure', () => {
const msg = 'Can only multibase decode strings'
// @ts-expect-error - 'number' is not assignable to parameter of type 'string'
assert.throws(() => base32.decode(1), msg)
// @ts-expect-error - 'number' is not assignable to parameter of type 'string'
assert.throws(() => base32.decoder.decode(1), msg)
})
const buff = bytes.fromString('test')
const nonPrintableBuff = Uint8Array.from([239, 250, 254])
const baseTest = (bases: typeof b2 | typeof b8 | typeof b10 | typeof b16 | typeof b32 | typeof b36 | typeof b58 | typeof b64 | typeof id): void => {
for (const base of Object.values(bases)) {
if (((base as { name: string })?.name) !== '') {
it(`encode/decode ${base.name}`, () => {
const encoded = base.encode(buff)
const decoded = base.decode(encoded)
assert.deepStrictEqual(decoded, buff)
assert.deepStrictEqual(encoded, base.encoder.encode(buff))
assert.deepStrictEqual(buff, base.decoder.decode(encoded))
})
it(`encode/decode ${base.name} with non-printable values`, () => {
const encoded = base.encode(nonPrintableBuff)
const decoded = base.decode(encoded)
assert.deepStrictEqual(decoded, nonPrintableBuff)
assert.deepStrictEqual(encoded, base.encoder.encode(nonPrintableBuff))
assert.deepStrictEqual(nonPrintableBuff, base.decoder.decode(encoded))
})
}
}
}
describe('base2', () => {
baseTest(b2)
})
describe('base8', () => {
baseTest(b8)
})
describe('base10', () => {
baseTest(b10)
})
describe('base16', () => {
baseTest(b16)
})
describe('base32', () => {
baseTest(b32)
})
describe('base36', () => {
baseTest(b36)
})
describe('base58', () => {
baseTest(b58)
})
describe('base64', () => {
baseTest(b64)
})
describe('identity', () => {
baseTest(id)
it('should round-trip unprintable characters', () => {
const u = new Uint8Array([
6, 22, 184, 240, 237, 178,
112, 0, 150, 137, 182, 54,
220, 1, 217, 221
])
const s = id.identity.encode(u)
const b = id.identity.decode(s)
assert.equalBytes(b, u)
})
it('should round-trip emojis', () => {
const input = '😵💫🎉'
const u = new TextEncoder().encode(input)
const s = id.identity.encode(u)
const b = id.identity.decode(s)
const output = new TextDecoder().decode(b)
assert.equalBytes(b, u)
assert.equal(output, input)
})
it('should round-trip multi-byte characters', () => {
// https://www.kanshudo.com/kanji/%F0%A0%AE%B7
const input = '𠮷'
const u = new TextEncoder().encode(input)
const s = id.identity.encode(u)
const b = id.identity.decode(s)
const output = new TextDecoder().decode(b)
assert.equalBytes(b, u)
assert.equal(output, input)
})
})
it('multibase mismatch', () => {
const b64 = base64.encode(bytes.fromString('test'))
const msg = `Unable to decode multibase string "${b64}", base32 decoder only supports inputs prefixed with ${base32.prefix}`
assert.throws(() => base32.decode(b64), msg)
})
it('decoder composition', () => {
const base = base32.decoder.or(base58btc.decoder)
const b32 = base32.encode(bytes.fromString('test'))
assert.deepStrictEqual(base.decode(b32), bytes.fromString('test'))
const b58 = base58btc.encode(bytes.fromString('test'))
assert.deepStrictEqual(base.decode(b58), bytes.fromString('test'))
const b64 = base64.encode(bytes.fromString('test'))
const msg = `Unable to decode multibase string "${b64}", only inputs prefixed with ${base32.prefix},${base58btc.prefix} are supported`
assert.throws(() => base.decode(b64), msg)
const baseExt = base.or(base64)
assert.deepStrictEqual(baseExt.decode(b64), bytes.fromString('test'))
// original composition stays intact
assert.throws(() => base.decode(b64), msg)
// non-composed combined with composed
const baseExt2 = base32.decoder.or(base64.decoder.or(base16.decoder))
assert.deepStrictEqual(baseExt2.decode(b64), bytes.fromString('test'))
// composed combined with composed
const baseExt3 = base.or(base64.decoder.or(base16.decoder))
assert.deepStrictEqual(baseExt3.decode(b64), bytes.fromString('test'))
})
it('truncated data', () => {
const b64 = base64.encode(Uint8Array.from([245, 250]))
assert.throws(() => base64.decode(b64.substring(0, b64.length - 1)), 'Unexpected end of data')
})
it('rejects interior padding (RFC 4648 section 3.2)', () => {
const padded = { ...b32, ...b64 }
for (const base of [padded.base64pad, padded.base64urlpad, padded.base32pad, padded.base32hexpad]) {
const value = Uint8Array.from([0x41, 0x42])
// A valid encoding, with legitimate trailing padding, still round-trips.
const encoded = base.encode(value)
assert.deepStrictEqual(base.decode(encoded), value)
// A '=' spliced into the data run (padding may only be a trailing run) is
// invalid and must be rejected, not silently decoded to bogus bytes.
const tampered = encoded.slice(0, 2) + '=' + encoded.slice(2)
assert.throws(() => base.decode(tampered), `Non-${base.name} character`)
}
})
it('infers prefix and name correctly', () => {
const name = base32.name
// @ts-expect-error - TS catches mismatch
const name2: 'base16' = base32.name
const prefix = base32.prefix
assert.equal(prefix, 'b')
assert.equal(name, 'base32')
assert.equal(name2, name)
})
})