-
-
Notifications
You must be signed in to change notification settings - Fork 619
/
buffer.test.ts
116 lines (103 loc) · 4.32 KB
/
buffer.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { SHA256 as sha256CryptoJS } from 'crypto-js'
import { bufferToFormData, bufferToString, equal, timingSafeEqual } from './buffer'
describe('equal', () => {
it('should return true for identical ArrayBuffers', () => {
const buffer1 = new ArrayBuffer(1)
const buffer2 = buffer1
expect(equal(buffer1, buffer2)).toBe(true)
})
it('should return false for ArrayBuffers of different lengths', () => {
const buffer1 = new ArrayBuffer(1)
const buffer2 = new ArrayBuffer(2)
expect(equal(buffer1, buffer2)).toBe(false)
})
it('should return false for ArrayBuffers with different content', () => {
const buffer1 = new Uint8Array([1, 2, 3, 4]).buffer
const buffer2 = new Uint8Array([2, 2, 3, 4]).buffer
expect(equal(buffer1, buffer2)).toBe(false)
})
it('should return true for ArrayBuffers with identical content', () => {
const buffer1 = new Uint8Array([1, 2, 3, 4]).buffer
const buffer2 = new Uint8Array([1, 2, 3, 4]).buffer
expect(equal(buffer1, buffer2)).toBe(true)
})
})
describe('buffer', () => {
it('positive', async () => {
expect(
await timingSafeEqual(
'127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935',
'127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935'
)
).toBe(true)
expect(await timingSafeEqual('a', 'a')).toBe(true)
expect(await timingSafeEqual('', '')).toBe(true)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(await timingSafeEqual(undefined, undefined)).toBe(true)
expect(await timingSafeEqual(true, true)).toBe(true)
expect(await timingSafeEqual(false, false)).toBe(true)
expect(await timingSafeEqual(true, true, (d: string) => sha256CryptoJS(d).toString()))
})
it('negative', async () => {
expect(await timingSafeEqual('a', 'b')).toBe(false)
expect(
await timingSafeEqual('a', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
).toBe(false)
expect(
await timingSafeEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a')
).toBe(false)
expect(await timingSafeEqual('alpha', 'beta')).toBe(false)
expect(await timingSafeEqual(false, true)).toBe(false)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(await timingSafeEqual(false, undefined)).toBe(false)
expect(
await timingSafeEqual(
() => {},
() => {}
)
).toBe(false)
expect(await timingSafeEqual({}, {})).toBe(false)
expect(await timingSafeEqual({ a: 1 }, { a: 1 })).toBe(false)
expect(await timingSafeEqual({ a: 1 }, { a: 2 })).toBe(false)
expect(await timingSafeEqual([1, 2], [1, 2])).toBe(false)
expect(await timingSafeEqual([1, 2], [1, 2, 3])).toBe(false)
expect(await timingSafeEqual('a', 'b', () => undefined)).toBe(false)
})
})
describe('bufferToString', () => {
it('Should return あいうえお', () => {
const bytes = [227, 129, 130, 227, 129, 132, 227, 129, 134, 227, 129, 136, 227, 129, 138]
const buffer = Uint8Array.from(bytes).buffer
expect(bufferToString(buffer)).toBe('あいうえお')
})
it('should return the passed arguments as is ', () => {
const notBuffer = 'あいうえお' as unknown as ArrayBuffer
expect(bufferToString(notBuffer)).toBe('あいうえお')
})
})
describe('bufferToFormData', () => {
it('Should parse multipart/form-data from ArrayBuffer', async () => {
const encoder = new TextEncoder()
const testData =
'--sampleboundary\r\nContent-Disposition: form-data; name="test"\r\n\r\nHello\r\n--sampleboundary--'
const arrayBuffer = encoder.encode(testData).buffer
const result = await bufferToFormData(
arrayBuffer,
'multipart/form-data; boundary=sampleboundary'
)
expect(result.get('test')).toBe('Hello')
})
it('Should parse application/x-www-form-urlencoded from ArrayBuffer', async () => {
const encoder = new TextEncoder()
const searchParams = new URLSearchParams()
searchParams.append('id', '123')
searchParams.append('title', 'Good title')
const testData = searchParams.toString()
const arrayBuffer = encoder.encode(testData).buffer
const result = await bufferToFormData(arrayBuffer, 'application/x-www-form-urlencoded')
expect(result.get('id')).toBe('123')
expect(result.get('title')).toBe('Good title')
})
})