1'use strict';
2
3const common = require('../common');
4const { deepStrictEqual, strictEqual, throws } = require('assert');
5const { runInNewContext } = require('vm');
6
7const checkString = 'test';
8
9const check = Buffer.from(checkString);
10
11class MyString extends String {
12  constructor() {
13    super(checkString);
14  }
15}
16
17class MyPrimitive {
18  [Symbol.toPrimitive]() {
19    return checkString;
20  }
21}
22
23class MyBadPrimitive {
24  [Symbol.toPrimitive]() {
25    return 1;
26  }
27}
28
29deepStrictEqual(Buffer.from(new String(checkString)), check);
30deepStrictEqual(Buffer.from(new MyString()), check);
31deepStrictEqual(Buffer.from(new MyPrimitive()), check);
32deepStrictEqual(
33  Buffer.from(runInNewContext('new String(checkString)', { checkString })),
34  check
35);
36
37[
38  {},
39  new Boolean(true),
40  { valueOf() { return null; } },
41  { valueOf() { return undefined; } },
42  { valueOf: null },
43  Object.create(null),
44  new Number(true),
45  new MyBadPrimitive(),
46  Symbol(),
47  5n,
48  (one, two, three) => {},
49  undefined,
50  null,
51].forEach((input) => {
52  const errObj = {
53    code: 'ERR_INVALID_ARG_TYPE',
54    name: 'TypeError',
55    message: 'The first argument must be of type string or an instance of ' +
56             'Buffer, ArrayBuffer, or Array or an Array-like Object.' +
57             common.invalidArgTypeHelper(input)
58  };
59  throws(() => Buffer.from(input), errObj);
60  throws(() => Buffer.from(input, 'hex'), errObj);
61});
62
63Buffer.allocUnsafe(10); // Should not throw.
64Buffer.from('deadbeaf', 'hex'); // Should not throw.
65
66
67{
68  const u16 = new Uint16Array([0xffff]);
69  const b16 = Buffer.copyBytesFrom(u16);
70  u16[0] = 0;
71  strictEqual(b16.length, 2);
72  strictEqual(b16[0], 255);
73  strictEqual(b16[1], 255);
74}
75
76{
77  const u16 = new Uint16Array([0, 0xffff]);
78  const b16 = Buffer.copyBytesFrom(u16, 1, 5);
79  u16[0] = 0xffff;
80  u16[1] = 0;
81  strictEqual(b16.length, 2);
82  strictEqual(b16[0], 255);
83  strictEqual(b16[1], 255);
84}
85
86{
87  const u32 = new Uint32Array([0xffffffff]);
88  const b32 = Buffer.copyBytesFrom(u32);
89  u32[0] = 0;
90  strictEqual(b32.length, 4);
91  strictEqual(b32[0], 255);
92  strictEqual(b32[1], 255);
93  strictEqual(b32[2], 255);
94  strictEqual(b32[3], 255);
95}
96
97throws(() => {
98  Buffer.copyBytesFrom();
99}, {
100  code: 'ERR_INVALID_ARG_TYPE',
101});
102
103['', Symbol(), true, false, {}, [], () => {}, 1, 1n, null, undefined].forEach(
104  (notTypedArray) => throws(() => {
105    Buffer.copyBytesFrom('nope');
106  }, {
107    code: 'ERR_INVALID_ARG_TYPE',
108  })
109);
110
111['', Symbol(), true, false, {}, [], () => {}, 1n].forEach((notANumber) =>
112  throws(() => {
113    Buffer.copyBytesFrom(new Uint8Array(1), notANumber);
114  }, {
115    code: 'ERR_INVALID_ARG_TYPE',
116  })
117);
118
119[-1, NaN, 1.1, -Infinity].forEach((outOfRange) =>
120  throws(() => {
121    Buffer.copyBytesFrom(new Uint8Array(1), outOfRange);
122  }, {
123    code: 'ERR_OUT_OF_RANGE',
124  })
125);
126
127['', Symbol(), true, false, {}, [], () => {}, 1n].forEach((notANumber) =>
128  throws(() => {
129    Buffer.copyBytesFrom(new Uint8Array(1), 0, notANumber);
130  }, {
131    code: 'ERR_INVALID_ARG_TYPE',
132  })
133);
134
135[-1, NaN, 1.1, -Infinity].forEach((outOfRange) =>
136  throws(() => {
137    Buffer.copyBytesFrom(new Uint8Array(1), 0, outOfRange);
138  }, {
139    code: 'ERR_OUT_OF_RANGE',
140  })
141);
142