• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3require('../common');
4const assert = require('assert');
5
6const b = Buffer.allocUnsafe(1024);
7const c = Buffer.allocUnsafe(512);
8
9let cntr = 0;
10
11{
12  // copy 512 bytes, from 0 to 512.
13  b.fill(++cntr);
14  c.fill(++cntr);
15  const copied = b.copy(c, 0, 0, 512);
16  assert.strictEqual(copied, 512);
17  for (let i = 0; i < c.length; i++) {
18    assert.strictEqual(c[i], b[i]);
19  }
20}
21
22{
23  // Current behavior is to coerce values to integers.
24  b.fill(++cntr);
25  c.fill(++cntr);
26  const copied = b.copy(c, '0', '0', '512');
27  assert.strictEqual(copied, 512);
28  for (let i = 0; i < c.length; i++) {
29    assert.strictEqual(c[i], b[i]);
30  }
31}
32
33{
34  // Copy c into b, without specifying sourceEnd
35  b.fill(++cntr);
36  c.fill(++cntr);
37  const copied = c.copy(b, 0, 0);
38  assert.strictEqual(copied, c.length);
39  for (let i = 0; i < c.length; i++) {
40    assert.strictEqual(b[i], c[i]);
41  }
42}
43
44{
45  // Copy c into b, without specifying sourceStart
46  b.fill(++cntr);
47  c.fill(++cntr);
48  const copied = c.copy(b, 0);
49  assert.strictEqual(copied, c.length);
50  for (let i = 0; i < c.length; i++) {
51    assert.strictEqual(b[i], c[i]);
52  }
53}
54
55{
56  // Copy longer buffer b to shorter c without targetStart
57  b.fill(++cntr);
58  c.fill(++cntr);
59  const copied = b.copy(c);
60  assert.strictEqual(copied, c.length);
61  for (let i = 0; i < c.length; i++) {
62    assert.strictEqual(c[i], b[i]);
63  }
64}
65
66{
67  // Copy starting near end of b to c
68  b.fill(++cntr);
69  c.fill(++cntr);
70  const copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
71  assert.strictEqual(copied, Math.floor(c.length / 2));
72  for (let i = 0; i < Math.floor(c.length / 2); i++) {
73    assert.strictEqual(c[i], b[b.length - Math.floor(c.length / 2) + i]);
74  }
75  for (let i = Math.floor(c.length / 2) + 1; i < c.length; i++) {
76    assert.strictEqual(c[c.length - 1], c[i]);
77  }
78}
79
80{
81  // Try to copy 513 bytes, and check we don't overrun c
82  b.fill(++cntr);
83  c.fill(++cntr);
84  const copied = b.copy(c, 0, 0, 513);
85  assert.strictEqual(copied, c.length);
86  for (let i = 0; i < c.length; i++) {
87    assert.strictEqual(c[i], b[i]);
88  }
89}
90
91{
92  // copy 768 bytes from b into b
93  b.fill(++cntr);
94  b.fill(++cntr, 256);
95  const copied = b.copy(b, 0, 256, 1024);
96  assert.strictEqual(copied, 768);
97  for (let i = 0; i < b.length; i++) {
98    assert.strictEqual(b[i], cntr);
99  }
100}
101
102// Copy string longer than buffer length (failure will segfault)
103const bb = Buffer.allocUnsafe(10);
104bb.fill('hello crazy world');
105
106
107// Try to copy from before the beginning of b. Should not throw.
108b.copy(c, 0, 100, 10);
109
110// Copy throws at negative sourceStart
111assert.throws(
112  () => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, -1),
113  {
114    code: 'ERR_OUT_OF_RANGE',
115    name: 'RangeError',
116    message: 'The value of "sourceStart" is out of range. ' +
117             'It must be >= 0. Received -1'
118  }
119);
120
121{
122  // Check sourceEnd resets to targetEnd if former is greater than the latter
123  b.fill(++cntr);
124  c.fill(++cntr);
125  b.copy(c, 0, 0, 1025);
126  for (let i = 0; i < c.length; i++) {
127    assert.strictEqual(c[i], b[i]);
128  }
129}
130
131// Throw with negative sourceEnd
132assert.throws(
133  () => b.copy(c, 0, 0, -1),
134  {
135    code: 'ERR_OUT_OF_RANGE',
136    name: 'RangeError',
137    message: 'The value of "sourceEnd" is out of range. ' +
138             'It must be >= 0. Received -1'
139  }
140);
141
142// When sourceStart is greater than sourceEnd, zero copied
143assert.strictEqual(b.copy(c, 0, 100, 10), 0);
144
145// When targetStart > targetLength, zero copied
146assert.strictEqual(b.copy(c, 512, 0, 10), 0);
147
148// Test that the `target` can be a Uint8Array.
149{
150  const d = new Uint8Array(c);
151  // copy 512 bytes, from 0 to 512.
152  b.fill(++cntr);
153  d.fill(++cntr);
154  const copied = b.copy(d, 0, 0, 512);
155  assert.strictEqual(copied, 512);
156  for (let i = 0; i < d.length; i++) {
157    assert.strictEqual(d[i], b[i]);
158  }
159}
160
161// Test that the source can be a Uint8Array, too.
162{
163  const e = new Uint8Array(b);
164  // copy 512 bytes, from 0 to 512.
165  e.fill(++cntr);
166  c.fill(++cntr);
167  const copied = Buffer.prototype.copy.call(e, c, 0, 0, 512);
168  assert.strictEqual(copied, 512);
169  for (let i = 0; i < c.length; i++) {
170    assert.strictEqual(c[i], e[i]);
171  }
172}
173
174// https://github.com/nodejs/node/issues/23668: Do not crash for invalid input.
175c.fill('c');
176b.copy(c, 'not a valid offset');
177// Make sure this acted like a regular copy with `0` offset.
178assert.deepStrictEqual(c, b.slice(0, c.length));
179
180{
181  c.fill('C');
182  assert.throws(() => {
183    b.copy(c, { [Symbol.toPrimitive]() { throw new Error('foo'); } });
184  }, /foo/);
185  // No copying took place:
186  assert.deepStrictEqual(c.toString(), 'C'.repeat(c.length));
187}
188