Skip to content

Commit 6f49b66

Browse files
committed
test: ensure any Uint8Array is accepted in base64/base32
1 parent c085e1b commit 6f49b66

4 files changed

Lines changed: 42 additions & 38 deletions

File tree

base32.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface FromBase32Options {
4747
* @param options - Encoding options
4848
* @returns The base32 encoded string
4949
*/
50-
export function toBase32(arr: Uint8ArrayBuffer, options?: ToBase32Options): string;
50+
export function toBase32(arr: Uint8Array, options?: ToBase32Options): string;
5151

5252
/**
5353
* Encode a `Uint8Array` to a base32hex string (RFC 4648)
@@ -56,7 +56,7 @@ export function toBase32(arr: Uint8ArrayBuffer, options?: ToBase32Options): stri
5656
* @param options - Encoding options (padding defaults to false)
5757
* @returns The base32hex encoded string
5858
*/
59-
export function toBase32hex(arr: Uint8ArrayBuffer, options?: ToBase32Options): string;
59+
export function toBase32hex(arr: Uint8Array, options?: ToBase32Options): string;
6060

6161
/**
6262
* Decode a base32 string to bytes

base64.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export interface FromBase64Options {
4848
* @param options - Encoding options
4949
* @returns The base64 encoded string
5050
*/
51-
export function toBase64(arr: Uint8ArrayBuffer, options?: ToBase64Options): string;
51+
export function toBase64(arr: Uint8Array, options?: ToBase64Options): string;
5252

5353
/**
5454
* Encode a `Uint8Array` to a base64url string (RFC 4648)
@@ -57,7 +57,7 @@ export function toBase64(arr: Uint8ArrayBuffer, options?: ToBase64Options): stri
5757
* @param options - Encoding options (padding defaults to false)
5858
* @returns The base64url encoded string
5959
*/
60-
export function toBase64url(arr: Uint8ArrayBuffer, options?: ToBase64Options): string;
60+
export function toBase64url(arr: Uint8Array, options?: ToBase64Options): string;
6161

6262
/**
6363
* Decode a base64 string to bytes

tests/base32.test.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { describe, test } from 'node:test'
33
import base32js from 'base32.js'
44
import hiBase32 from 'hi-base32'
55

6+
const SharedArrayBuffer = globalThis.SharedArrayBuffer ?? ArrayBuffer
7+
68
const raw = [new Uint8Array(), new Uint8Array([0]), new Uint8Array([1]), new Uint8Array([255])]
79
for (let i = 0; i < 50; i++) {
810
const size = Math.floor(Math.random() * 100)
@@ -19,7 +21,9 @@ const pool = raw.map((uint8) => {
1921
if (base32padded !== hiBase32.encode(uint8)) throw new Error('Unexpected mismatch with hiBase32')
2022
const base32hexPadded = pad(base32hex)
2123
const hex = buffer.toString('hex')
22-
return { uint8, buffer, hex, base32, base32padded, base32hex, base32hexPadded }
24+
const shared = new Uint8Array(new SharedArrayBuffer(buffer.length))
25+
shared.set(uint8)
26+
return { uint8, shared, buffer, hex, base32, base32padded, base32hex, base32hexPadded }
2327
})
2428

2529
describe('toBase32', () => {
@@ -34,20 +38,22 @@ describe('toBase32', () => {
3438
})
3539

3640
test('base32', (t) => {
37-
for (const { uint8, buffer, base32, base32padded } of pool) {
38-
t.assert.strictEqual(toBase32(uint8), base32)
39-
t.assert.strictEqual(toBase32(buffer), base32)
40-
t.assert.strictEqual(toBase32(uint8, { padding: false }), base32)
41-
t.assert.strictEqual(toBase32(uint8, { padding: true }), base32padded)
41+
for (const { uint8, shared, buffer, base32, base32padded } of pool) {
42+
for (const arg of [uint8, shared, buffer]) {
43+
t.assert.strictEqual(toBase32(arg), base32)
44+
t.assert.strictEqual(toBase32(arg, { padding: false }), base32)
45+
t.assert.strictEqual(toBase32(arg, { padding: true }), base32padded)
46+
}
4247
}
4348
})
4449

4550
test('base32hex', (t) => {
46-
for (const { uint8, buffer, base32hex, base32hexPadded } of pool) {
47-
t.assert.strictEqual(toBase32hex(uint8), base32hex)
48-
t.assert.strictEqual(toBase32hex(buffer), base32hex)
49-
t.assert.strictEqual(toBase32hex(uint8, { padding: false }), base32hex)
50-
t.assert.strictEqual(toBase32hex(uint8, { padding: true }), base32hexPadded)
51+
for (const { uint8, shared, buffer, base32hex, base32hexPadded } of pool) {
52+
for (const arg of [uint8, shared, buffer]) {
53+
t.assert.strictEqual(toBase32hex(arg), base32hex)
54+
t.assert.strictEqual(toBase32hex(arg, { padding: false }), base32hex)
55+
t.assert.strictEqual(toBase32hex(arg, { padding: true }), base32hexPadded)
56+
}
5157
}
5258
})
5359
})

tests/base64.test.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { toBase64, toBase64url, fromBase64, fromBase64url } from '@exodus/bytes/
22
import * as js from '../fallback/base64.js'
33
import { describe, test } from 'node:test'
44

5+
const SharedArrayBuffer = globalThis.SharedArrayBuffer ?? ArrayBuffer
6+
57
const raw = [new Uint8Array(), new Uint8Array([0]), new Uint8Array([1]), new Uint8Array([255])]
68
for (let i = 0; i < 50; i++) {
79
const size = Math.floor(Math.random() * 100)
@@ -21,7 +23,9 @@ const pool = raw.map((uint8) => {
2123

2224
if (base64url !== base64urlFallback) throw new Error('Unexpected base64url mismatch with Buffer')
2325
const hex = buffer.toString('hex')
24-
return { uint8, buffer, hex, base64, base64nopad, base64url, base64urlPadded }
26+
const shared = new Uint8Array(new SharedArrayBuffer(buffer.length))
27+
shared.set(uint8)
28+
return { uint8, shared, buffer, hex, base64, base64nopad, base64url, base64urlPadded }
2529
})
2630

2731
describe('toBase64', () => {
@@ -36,32 +40,26 @@ describe('toBase64', () => {
3640
})
3741

3842
test('base64', (t) => {
39-
for (const { uint8, buffer, base64, base64nopad } of pool) {
40-
t.assert.strictEqual(toBase64(uint8), base64)
41-
t.assert.strictEqual(toBase64(uint8, { padding: true }), base64)
42-
t.assert.strictEqual(toBase64(uint8, { padding: false }), base64nopad)
43-
t.assert.strictEqual(js.toBase64(uint8, false, false), base64nopad)
44-
t.assert.strictEqual(js.toBase64(uint8, false, true), base64)
45-
t.assert.strictEqual(toBase64(buffer), base64)
46-
t.assert.strictEqual(toBase64(buffer, { padding: true }), base64)
47-
t.assert.strictEqual(toBase64(buffer, { padding: false }), base64nopad)
48-
t.assert.strictEqual(js.toBase64(buffer, false, false), base64nopad)
49-
t.assert.strictEqual(js.toBase64(buffer, false, true), base64)
43+
for (const { uint8, shared, buffer, base64, base64nopad } of pool) {
44+
for (const arg of [uint8, shared, buffer]) {
45+
t.assert.strictEqual(toBase64(arg), base64)
46+
t.assert.strictEqual(toBase64(arg, { padding: true }), base64)
47+
t.assert.strictEqual(toBase64(arg, { padding: false }), base64nopad)
48+
t.assert.strictEqual(js.toBase64(arg, false, false), base64nopad)
49+
t.assert.strictEqual(js.toBase64(arg, false, true), base64)
50+
}
5051
}
5152
})
5253

5354
test('base64url', (t) => {
54-
for (const { uint8, buffer, base64url, base64urlPadded } of pool) {
55-
t.assert.strictEqual(toBase64url(uint8), base64url)
56-
t.assert.strictEqual(toBase64url(uint8, { padding: false }), base64url)
57-
t.assert.strictEqual(toBase64url(uint8, { padding: true }), base64urlPadded)
58-
t.assert.strictEqual(js.toBase64(uint8, true, false), base64url)
59-
t.assert.strictEqual(js.toBase64(uint8, true, true), base64urlPadded)
60-
t.assert.strictEqual(toBase64url(buffer), base64url)
61-
t.assert.strictEqual(toBase64url(buffer, { padding: false }), base64url)
62-
t.assert.strictEqual(toBase64url(buffer, { padding: true }), base64urlPadded)
63-
t.assert.strictEqual(js.toBase64(buffer, true, false), base64url)
64-
t.assert.strictEqual(js.toBase64(buffer, true, true), base64urlPadded)
55+
for (const { uint8, shared, buffer, base64url, base64urlPadded } of pool) {
56+
for (const arg of [uint8, shared, buffer]) {
57+
t.assert.strictEqual(toBase64url(arg), base64url)
58+
t.assert.strictEqual(toBase64url(arg, { padding: false }), base64url)
59+
t.assert.strictEqual(toBase64url(arg, { padding: true }), base64urlPadded)
60+
t.assert.strictEqual(js.toBase64(arg, true, false), base64url)
61+
t.assert.strictEqual(js.toBase64(arg, true, true), base64urlPadded)
62+
}
6563
}
6664
})
6765
})

0 commit comments

Comments
 (0)