forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-internal-webidl-buffer-source.js
More file actions
208 lines (178 loc) · 6.05 KB
/
test-internal-webidl-buffer-source.js
File metadata and controls
208 lines (178 loc) · 6.05 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
// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const { test } = require('node:test');
const { converters } = require('internal/webidl');
const TYPED_ARRAY_CTORS = [
Uint8Array, Int8Array, Uint8ClampedArray,
Uint16Array, Int16Array,
Uint32Array, Int32Array,
Float16Array, Float32Array, Float64Array,
BigInt64Array, BigUint64Array,
];
test('BufferSource accepts ArrayBuffer', () => {
const ab = new ArrayBuffer(8);
assert.strictEqual(converters.BufferSource(ab), ab);
});
test('BufferSource accepts all TypedArray kinds', () => {
for (const Ctor of TYPED_ARRAY_CTORS) {
const ta = new Ctor(4);
assert.strictEqual(converters.BufferSource(ta), ta);
}
});
test('BufferSource accepts Buffer', () => {
const buf = Buffer.alloc(8);
assert.strictEqual(converters.BufferSource(buf), buf);
});
test('BufferSource accepts DataView', () => {
const dv = new DataView(new ArrayBuffer(8));
assert.strictEqual(converters.BufferSource(dv), dv);
});
test('BufferSource accepts ArrayBuffer subclass instance', () => {
class MyAB extends ArrayBuffer {}
const sub = new MyAB(8);
assert.strictEqual(converters.BufferSource(sub), sub);
});
test('BufferSource accepts TypedArray with null prototype', () => {
const ta = new Uint8Array(4);
Object.setPrototypeOf(ta, null);
assert.strictEqual(converters.BufferSource(ta), ta);
});
test('BufferSource accepts DataView with null prototype', () => {
const dv = new DataView(new ArrayBuffer(4));
Object.setPrototypeOf(dv, null);
assert.strictEqual(converters.BufferSource(dv), dv);
});
test('BufferSource accepts ArrayBuffer with null prototype', () => {
const ab = new ArrayBuffer(4);
Object.setPrototypeOf(ab, null);
assert.strictEqual(converters.BufferSource(ab), ab);
});
test('BufferSource rejects SharedArrayBuffer', () => {
assert.throws(
() => converters.BufferSource(new SharedArrayBuffer(4)),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
test('BufferSource rejects SAB-backed TypedArray', () => {
const view = new Uint8Array(new SharedArrayBuffer(4));
assert.throws(
() => converters.BufferSource(view),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
test('BufferSource rejects SAB-backed DataView', () => {
const dv = new DataView(new SharedArrayBuffer(4));
assert.throws(
() => converters.BufferSource(dv),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
test('BufferSource rejects SAB view whose buffer prototype was reassigned', () => {
const sab = new SharedArrayBuffer(4);
Object.setPrototypeOf(sab, ArrayBuffer.prototype);
const view = new Uint8Array(sab);
assert.throws(
() => converters.BufferSource(view),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
test('BufferSource accepts a detached ArrayBuffer', () => {
const ab = new ArrayBuffer(8);
structuredClone(ab, { transfer: [ab] });
assert.strictEqual(ab.byteLength, 0);
assert.strictEqual(converters.BufferSource(ab), ab);
});
test('BufferSource rejects objects with a forged @@toStringTag', () => {
const fake = { [Symbol.toStringTag]: 'Uint8Array' };
assert.throws(
() => converters.BufferSource(fake),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
for (const value of [null, undefined, 0, 1, 1n, '', 'x', true, Symbol('s'), [],
{}, () => {}]) {
test(`BufferSource rejects ${typeof value} ${String(value)}`, () => {
assert.throws(
() => converters.BufferSource(value),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
}
test('ArrayBufferView accepts all TypedArray kinds', () => {
for (const Ctor of TYPED_ARRAY_CTORS) {
const ta = new Ctor(4);
assert.strictEqual(converters.ArrayBufferView(ta), ta);
}
});
test('ArrayBufferView accepts DataView', () => {
const dv = new DataView(new ArrayBuffer(8));
assert.strictEqual(converters.ArrayBufferView(dv), dv);
});
test('ArrayBufferView accepts TypedArray subclass instance', () => {
class MyU8 extends Uint8Array {}
const sub = new MyU8(4);
assert.strictEqual(converters.ArrayBufferView(sub), sub);
});
test('ArrayBufferView accepts TypedArray with null prototype', () => {
const ta = new Uint8Array(4);
Object.setPrototypeOf(ta, null);
assert.strictEqual(converters.ArrayBufferView(ta), ta);
});
test('ArrayBufferView accepts DataView with null prototype', () => {
const dv = new DataView(new ArrayBuffer(4));
Object.setPrototypeOf(dv, null);
assert.strictEqual(converters.ArrayBufferView(dv), dv);
});
test('ArrayBufferView rejects raw ArrayBuffer', () => {
assert.throws(
() => converters.ArrayBufferView(new ArrayBuffer(4)),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
test('ArrayBufferView rejects raw SharedArrayBuffer', () => {
assert.throws(
() => converters.ArrayBufferView(new SharedArrayBuffer(4)),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
test('ArrayBufferView rejects SAB-backed TypedArray', () => {
const view = new Uint8Array(new SharedArrayBuffer(4));
assert.throws(
() => converters.ArrayBufferView(view),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
test('ArrayBufferView rejects SAB-backed DataView', () => {
const dv = new DataView(new SharedArrayBuffer(4));
assert.throws(
() => converters.ArrayBufferView(dv),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
test('ArrayBufferView rejects SAB view whose buffer prototype was reassigned', () => {
const sab = new SharedArrayBuffer(4);
Object.setPrototypeOf(sab, ArrayBuffer.prototype);
const view = new Uint8Array(sab);
assert.throws(
() => converters.ArrayBufferView(view),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
test('ArrayBufferView rejects objects with a forged @@toStringTag', () => {
const fake = { [Symbol.toStringTag]: 'Uint8Array' };
assert.throws(
() => converters.ArrayBufferView(fake),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
for (const value of [null, undefined, 0, 1, 1n, '', 'x', true, Symbol('s'), [],
{}, () => {}]) {
test(`ArrayBufferView rejects ${typeof value} ${String(value)}`, () => {
assert.throws(
() => converters.ArrayBufferView(value),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
}