forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-buffer-creation-regression.js
More file actions
41 lines (33 loc) · 1.05 KB
/
Copy pathtest-buffer-creation-regression.js
File metadata and controls
41 lines (33 loc) · 1.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
'use strict';
const common = require('../common');
const assert = require('assert');
function test(arrayBuffer, offset, length) {
const uint8Array = new Uint8Array(arrayBuffer, offset, length);
for (let i = 0; i < length; i += 1) {
uint8Array[i] = 1;
}
const buffer = Buffer.from(arrayBuffer, offset, length);
for (let i = 0; i < length; i += 1) {
assert.strictEqual(buffer[i], 1);
}
}
const acceptableOOMErrors = [
'Array buffer allocation failed',
'Invalid array buffer length'
];
const testCases = [
[200, 50, 100],
[4294967296 /* 1 << 32 */, 2147483648 /* 1 << 31 */, 1000],
[8589934592 /* 1 << 33 */, 4294967296 /* 1 << 32 */, 1000]
];
for (let index = 0, arrayBuffer; index < testCases.length; index += 1) {
const [size, offset, length] = testCases[index];
try {
arrayBuffer = new ArrayBuffer(size);
} catch (e) {
if (e instanceof RangeError && acceptableOOMErrors.includes(e.message))
return common.skip(`Unable to allocate ${size} bytes for ArrayBuffer`);
throw e;
}
test(arrayBuffer, offset, length);
}