• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3require('../common');
4const assert = require('assert');
5
6const LENGTH = 16;
7
8const ab = new ArrayBuffer(LENGTH);
9const dv = new DataView(ab);
10const ui = new Uint8Array(ab);
11const buf = Buffer.from(ab);
12
13
14assert.ok(buf instanceof Buffer);
15assert.strictEqual(buf.parent, buf.buffer);
16assert.strictEqual(buf.buffer, ab);
17assert.strictEqual(buf.length, ab.byteLength);
18
19
20buf.fill(0xC);
21for (let i = 0; i < LENGTH; i++) {
22  assert.strictEqual(ui[i], 0xC);
23  ui[i] = 0xF;
24  assert.strictEqual(buf[i], 0xF);
25}
26
27buf.writeUInt32LE(0xF00, 0);
28buf.writeUInt32BE(0xB47, 4);
29buf.writeDoubleLE(3.1415, 8);
30
31assert.strictEqual(dv.getUint32(0, true), 0xF00);
32assert.strictEqual(dv.getUint32(4), 0xB47);
33assert.strictEqual(dv.getFloat64(8, true), 3.1415);
34
35
36// Now test protecting users from doing stupid things
37
38assert.throws(function() {
39  function AB() { }
40  Object.setPrototypeOf(AB, ArrayBuffer);
41  Object.setPrototypeOf(AB.prototype, ArrayBuffer.prototype);
42  Buffer.from(new AB());
43}, {
44  code: 'ERR_INVALID_ARG_TYPE',
45  name: 'TypeError',
46  message: 'The first argument must be of type string or an instance of ' +
47           'Buffer, ArrayBuffer, or Array or an Array-like Object. Received ' +
48           'an instance of AB'
49});
50
51// Test the byteOffset and length arguments
52{
53  const ab = new Uint8Array(5);
54  ab[0] = 1;
55  ab[1] = 2;
56  ab[2] = 3;
57  ab[3] = 4;
58  ab[4] = 5;
59  const buf = Buffer.from(ab.buffer, 1, 3);
60  assert.strictEqual(buf.length, 3);
61  assert.strictEqual(buf[0], 2);
62  assert.strictEqual(buf[1], 3);
63  assert.strictEqual(buf[2], 4);
64  buf[0] = 9;
65  assert.strictEqual(ab[1], 9);
66
67  assert.throws(() => Buffer.from(ab.buffer, 6), {
68    code: 'ERR_BUFFER_OUT_OF_BOUNDS',
69    name: 'RangeError',
70    message: '"offset" is outside of buffer bounds'
71  });
72  assert.throws(() => Buffer.from(ab.buffer, 3, 6), {
73    code: 'ERR_BUFFER_OUT_OF_BOUNDS',
74    name: 'RangeError',
75    message: '"length" is outside of buffer bounds'
76  });
77}
78
79// Test the deprecated Buffer() version also
80{
81  const ab = new Uint8Array(5);
82  ab[0] = 1;
83  ab[1] = 2;
84  ab[2] = 3;
85  ab[3] = 4;
86  ab[4] = 5;
87  const buf = Buffer(ab.buffer, 1, 3);
88  assert.strictEqual(buf.length, 3);
89  assert.strictEqual(buf[0], 2);
90  assert.strictEqual(buf[1], 3);
91  assert.strictEqual(buf[2], 4);
92  buf[0] = 9;
93  assert.strictEqual(ab[1], 9);
94
95  assert.throws(() => Buffer(ab.buffer, 6), {
96    code: 'ERR_BUFFER_OUT_OF_BOUNDS',
97    name: 'RangeError',
98    message: '"offset" is outside of buffer bounds'
99  });
100  assert.throws(() => Buffer(ab.buffer, 3, 6), {
101    code: 'ERR_BUFFER_OUT_OF_BOUNDS',
102    name: 'RangeError',
103    message: '"length" is outside of buffer bounds'
104  });
105}
106
107{
108  // If byteOffset is not numeric, it defaults to 0.
109  const ab = new ArrayBuffer(10);
110  const expected = Buffer.from(ab, 0);
111  assert.deepStrictEqual(Buffer.from(ab, 'fhqwhgads'), expected);
112  assert.deepStrictEqual(Buffer.from(ab, NaN), expected);
113  assert.deepStrictEqual(Buffer.from(ab, {}), expected);
114  assert.deepStrictEqual(Buffer.from(ab, []), expected);
115
116  // If byteOffset can be converted to a number, it will be.
117  assert.deepStrictEqual(Buffer.from(ab, [1]), Buffer.from(ab, 1));
118
119  // If byteOffset is Infinity, throw.
120  assert.throws(() => {
121    Buffer.from(ab, Infinity);
122  }, {
123    code: 'ERR_BUFFER_OUT_OF_BOUNDS',
124    name: 'RangeError',
125    message: '"offset" is outside of buffer bounds'
126  });
127}
128
129{
130  // If length is not numeric, it defaults to 0.
131  const ab = new ArrayBuffer(10);
132  const expected = Buffer.from(ab, 0, 0);
133  assert.deepStrictEqual(Buffer.from(ab, 0, 'fhqwhgads'), expected);
134  assert.deepStrictEqual(Buffer.from(ab, 0, NaN), expected);
135  assert.deepStrictEqual(Buffer.from(ab, 0, {}), expected);
136  assert.deepStrictEqual(Buffer.from(ab, 0, []), expected);
137
138  // If length can be converted to a number, it will be.
139  assert.deepStrictEqual(Buffer.from(ab, 0, [1]), Buffer.from(ab, 0, 1));
140
141  // If length is Infinity, throw.
142  assert.throws(() => {
143    Buffer.from(ab, 0, Infinity);
144  }, {
145    code: 'ERR_BUFFER_OUT_OF_BOUNDS',
146    name: 'RangeError',
147    message: '"length" is outside of buffer bounds'
148  });
149}
150
151// Test an array like entry with the length set to NaN.
152assert.deepStrictEqual(Buffer.from({ length: NaN }), Buffer.alloc(0));
153