-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
134 lines (124 loc) · 5.29 KB
/
index.test.js
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
import { encode, decode } from './index.js';
const REPL_CHAR = '\uFFFD';
test('encode-and-decode', () => {
const testCases = [
'$£Иह€한𐍈',
'abcd😊efgh\n012345689\t€\r🧑🏽🍳helloworld!',
];
testCases.forEach(s => {
const arr = encode(s);
expect(decode(arr)).toBe(s);
{
// test against TextEncoder
const expected = Array.from(new TextEncoder().encode(s));
const actual = Array.from(arr);
expect(actual.length).toBe(expected.length);
expect(expected.every((x, i) => x === actual[i])).toBe(true);
}
{
// test against TextDecoder
const actual = new TextDecoder().decode(arr);
expect(actual).toBe(s);
}
});
});
test('utf-8-broken-beginning', () => {
const textEnc = new TextEncoder();
const textDec = new TextDecoder();
const s = 'abcd😊efgh\n012345689\t€\r🧑🏽🍳helloworld!';
const arr = new Uint8Array([
0x80, 0x80, 0x80, // continuation byte x 3
...Array.from(textEnc.encode(s)),
]);
const expected = REPL_CHAR + REPL_CHAR + REPL_CHAR + s;
expect(textDec.decode(arr)).toBe(expected);
expect(decode(arr)).toBe(expected);
});
test('utf-8-broken-beginning-invalid-continuation-byte', () => {
const textEnc = new TextEncoder();
const textDec = new TextDecoder();
const s = 'abcd😊efgh\n012345689\t€\r🧑🏽🍳helloworld!';
const arr = new Uint8Array([
0x80, 0xFF, 0x80, // continuation byte, invalid byte, continuation byte
...Array.from(textEnc.encode(s)),
]);
expect(textDec.decode(arr)).toBe(REPL_CHAR + REPL_CHAR + REPL_CHAR + s);
expect(() => decode(arr)).toThrow('invalid utf-8. Expected a leading byte at index 1 actual ff');
});
test('utf-8-broken-beginning-too-many-continuation-bytes', () => {
const textEnc = new TextEncoder();
const textDec = new TextDecoder();
const s = 'abcd😊efgh\n012345689\t€\r🧑🏽🍳helloworld!';
const arr = new Uint8Array([
0x80, 0x80, 0x80, 0x80, // continuation byte x 4
...Array.from(textEnc.encode(s)),
]);
expect(textDec.decode(arr)).toBe(REPL_CHAR + REPL_CHAR + REPL_CHAR + REPL_CHAR + s);
expect(() => decode(arr)).toThrow('invalid utf-8. Expected a leading byte at index 3 actual 80');
});
test('utf-8-broken-beginning-invalid-leading-byte', () => {
const textEnc = new TextEncoder();
const textDec = new TextDecoder();
const s = 'abcd😊efgh\n012345689\t€\r🧑🏽🍳helloworld!';
const arr = new Uint8Array([
0x80, 0x80, 0x80, 0xFF, // continuation byte x 4
...Array.from(textEnc.encode(s)),
]);
expect(textDec.decode(arr)).toBe(REPL_CHAR + REPL_CHAR + REPL_CHAR + REPL_CHAR + s);
expect(() => decode(arr)).toThrow('invalid utf-8. Expected a leading byte at index 3 actual ff');
});
test('utf-8-broken-ending-partial-multi-byte-character', () => {
const textEnc = new TextEncoder();
const textDec = new TextDecoder();
const s = 'abcd😊efgh\n012345689\t€\r🧑🏽🍳helloworld!';
const arr = new Uint8Array([
0x80, 0x80, 0x80, // continuation byte x 4
...Array.from(textEnc.encode(s)),
0xF0,
0x80,
]);
const expected = REPL_CHAR + REPL_CHAR + REPL_CHAR + s + REPL_CHAR + REPL_CHAR;
expect(textDec.decode(arr)).toBe(expected);
expect(decode(arr)).toBe(expected);
});
test('utf-8-broken-middle-invalid-character', () => {
const textEnc = new TextEncoder();
const textDec = new TextDecoder();
const s = 'abcd😊efgh\n012345689\t€\r🧑🏽🍳helloworld!';
const arr = new Uint8Array([
0x80, 0x80, 0x80, // continuation byte x 4
...Array.from(textEnc.encode(s)),
0xFF,
...Array.from(textEnc.encode(s)),
]);
expect(textDec.decode(arr)).toBe(REPL_CHAR + REPL_CHAR + REPL_CHAR + s + REPL_CHAR + s);
expect(() => decode(arr)).toThrow('invalid utf-8. Expected a leading byte at index 56 actual ff');
});
test('utf-8-broken-middle-invalid-4-byte-character-not-continuation-byte', () => {
const textEnc = new TextEncoder();
const textDec = new TextDecoder();
const s = 'abcd😊efgh\n012345689\t€\r🧑🏽🍳helloworld!';
const arr = new Uint8Array([
0x80, 0x80, 0x80, // continuation byte x 4
...Array.from(textEnc.encode(s)),
0xF0,
0x80,
...Array.from(textEnc.encode(s)),
]);
expect(textDec.decode(arr)).toBe(REPL_CHAR + REPL_CHAR + REPL_CHAR + s + REPL_CHAR + REPL_CHAR + s);
expect(() => decode(arr)).toThrow('invalid utf-8. Expected a continuation byte at index 58 actual 61');
});
test('utf-8-broken-middle-invalid-2-byte-character-out-of-range', () => {
const textEnc = new TextEncoder();
const textDec = new TextDecoder();
const s = 'abcd😊efgh\n012345689\t€\r🧑🏽🍳helloworld!';
const arr = new Uint8Array([
0x80, 0x80, 0x80, // continuation byte x 4
...Array.from(textEnc.encode(s)),
0xC0,
0x80,
...Array.from(textEnc.encode(s)),
]);
expect(textDec.decode(arr)).toBe(REPL_CHAR + REPL_CHAR + REPL_CHAR + s + REPL_CHAR + REPL_CHAR + s);
expect(() => decode(arr)).toThrow('invalid utf-8. Expected an integer between 0x80 and 0x800 at index 56 actual 0');
});