11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst b = Buffer.allocUnsafe(1024);
71cb0ef41Sopenharmony_ciconst c = Buffer.allocUnsafe(512);
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cilet cntr = 0;
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci{
121cb0ef41Sopenharmony_ci  // copy 512 bytes, from 0 to 512.
131cb0ef41Sopenharmony_ci  b.fill(++cntr);
141cb0ef41Sopenharmony_ci  c.fill(++cntr);
151cb0ef41Sopenharmony_ci  const copied = b.copy(c, 0, 0, 512);
161cb0ef41Sopenharmony_ci  assert.strictEqual(copied, 512);
171cb0ef41Sopenharmony_ci  for (let i = 0; i < c.length; i++) {
181cb0ef41Sopenharmony_ci    assert.strictEqual(c[i], b[i]);
191cb0ef41Sopenharmony_ci  }
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci{
231cb0ef41Sopenharmony_ci  // Current behavior is to coerce values to integers.
241cb0ef41Sopenharmony_ci  b.fill(++cntr);
251cb0ef41Sopenharmony_ci  c.fill(++cntr);
261cb0ef41Sopenharmony_ci  const copied = b.copy(c, '0', '0', '512');
271cb0ef41Sopenharmony_ci  assert.strictEqual(copied, 512);
281cb0ef41Sopenharmony_ci  for (let i = 0; i < c.length; i++) {
291cb0ef41Sopenharmony_ci    assert.strictEqual(c[i], b[i]);
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci{
341cb0ef41Sopenharmony_ci  // Floats will be converted to integers via `Math.floor`
351cb0ef41Sopenharmony_ci  b.fill(++cntr);
361cb0ef41Sopenharmony_ci  c.fill(++cntr);
371cb0ef41Sopenharmony_ci  const copied = b.copy(c, 0, 0, 512.5);
381cb0ef41Sopenharmony_ci  assert.strictEqual(copied, 512);
391cb0ef41Sopenharmony_ci  for (let i = 0; i < c.length; i++) {
401cb0ef41Sopenharmony_ci    assert.strictEqual(c[i], b[i]);
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci{
451cb0ef41Sopenharmony_ci  // Copy c into b, without specifying sourceEnd
461cb0ef41Sopenharmony_ci  b.fill(++cntr);
471cb0ef41Sopenharmony_ci  c.fill(++cntr);
481cb0ef41Sopenharmony_ci  const copied = c.copy(b, 0, 0);
491cb0ef41Sopenharmony_ci  assert.strictEqual(copied, c.length);
501cb0ef41Sopenharmony_ci  for (let i = 0; i < c.length; i++) {
511cb0ef41Sopenharmony_ci    assert.strictEqual(b[i], c[i]);
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci{
561cb0ef41Sopenharmony_ci  // Copy c into b, without specifying sourceStart
571cb0ef41Sopenharmony_ci  b.fill(++cntr);
581cb0ef41Sopenharmony_ci  c.fill(++cntr);
591cb0ef41Sopenharmony_ci  const copied = c.copy(b, 0);
601cb0ef41Sopenharmony_ci  assert.strictEqual(copied, c.length);
611cb0ef41Sopenharmony_ci  for (let i = 0; i < c.length; i++) {
621cb0ef41Sopenharmony_ci    assert.strictEqual(b[i], c[i]);
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci}
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci{
671cb0ef41Sopenharmony_ci  // Copied source range greater than source length
681cb0ef41Sopenharmony_ci  b.fill(++cntr);
691cb0ef41Sopenharmony_ci  c.fill(++cntr);
701cb0ef41Sopenharmony_ci  const copied = c.copy(b, 0, 0, c.length + 1);
711cb0ef41Sopenharmony_ci  assert.strictEqual(copied, c.length);
721cb0ef41Sopenharmony_ci  for (let i = 0; i < c.length; i++) {
731cb0ef41Sopenharmony_ci    assert.strictEqual(b[i], c[i]);
741cb0ef41Sopenharmony_ci  }
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci{
781cb0ef41Sopenharmony_ci  // Copy longer buffer b to shorter c without targetStart
791cb0ef41Sopenharmony_ci  b.fill(++cntr);
801cb0ef41Sopenharmony_ci  c.fill(++cntr);
811cb0ef41Sopenharmony_ci  const copied = b.copy(c);
821cb0ef41Sopenharmony_ci  assert.strictEqual(copied, c.length);
831cb0ef41Sopenharmony_ci  for (let i = 0; i < c.length; i++) {
841cb0ef41Sopenharmony_ci    assert.strictEqual(c[i], b[i]);
851cb0ef41Sopenharmony_ci  }
861cb0ef41Sopenharmony_ci}
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci{
891cb0ef41Sopenharmony_ci  // Copy starting near end of b to c
901cb0ef41Sopenharmony_ci  b.fill(++cntr);
911cb0ef41Sopenharmony_ci  c.fill(++cntr);
921cb0ef41Sopenharmony_ci  const copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
931cb0ef41Sopenharmony_ci  assert.strictEqual(copied, Math.floor(c.length / 2));
941cb0ef41Sopenharmony_ci  for (let i = 0; i < Math.floor(c.length / 2); i++) {
951cb0ef41Sopenharmony_ci    assert.strictEqual(c[i], b[b.length - Math.floor(c.length / 2) + i]);
961cb0ef41Sopenharmony_ci  }
971cb0ef41Sopenharmony_ci  for (let i = Math.floor(c.length / 2) + 1; i < c.length; i++) {
981cb0ef41Sopenharmony_ci    assert.strictEqual(c[c.length - 1], c[i]);
991cb0ef41Sopenharmony_ci  }
1001cb0ef41Sopenharmony_ci}
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci{
1031cb0ef41Sopenharmony_ci  // Try to copy 513 bytes, and check we don't overrun c
1041cb0ef41Sopenharmony_ci  b.fill(++cntr);
1051cb0ef41Sopenharmony_ci  c.fill(++cntr);
1061cb0ef41Sopenharmony_ci  const copied = b.copy(c, 0, 0, 513);
1071cb0ef41Sopenharmony_ci  assert.strictEqual(copied, c.length);
1081cb0ef41Sopenharmony_ci  for (let i = 0; i < c.length; i++) {
1091cb0ef41Sopenharmony_ci    assert.strictEqual(c[i], b[i]);
1101cb0ef41Sopenharmony_ci  }
1111cb0ef41Sopenharmony_ci}
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci{
1141cb0ef41Sopenharmony_ci  // copy 768 bytes from b into b
1151cb0ef41Sopenharmony_ci  b.fill(++cntr);
1161cb0ef41Sopenharmony_ci  b.fill(++cntr, 256);
1171cb0ef41Sopenharmony_ci  const copied = b.copy(b, 0, 256, 1024);
1181cb0ef41Sopenharmony_ci  assert.strictEqual(copied, 768);
1191cb0ef41Sopenharmony_ci  for (let i = 0; i < b.length; i++) {
1201cb0ef41Sopenharmony_ci    assert.strictEqual(b[i], cntr);
1211cb0ef41Sopenharmony_ci  }
1221cb0ef41Sopenharmony_ci}
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci// Copy string longer than buffer length (failure will segfault)
1251cb0ef41Sopenharmony_ciconst bb = Buffer.allocUnsafe(10);
1261cb0ef41Sopenharmony_cibb.fill('hello crazy world');
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci// Try to copy from before the beginning of b. Should not throw.
1301cb0ef41Sopenharmony_cib.copy(c, 0, 100, 10);
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci// Throw with invalid source type
1331cb0ef41Sopenharmony_ciassert.throws(
1341cb0ef41Sopenharmony_ci  () => Buffer.prototype.copy.call(0),
1351cb0ef41Sopenharmony_ci  {
1361cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
1371cb0ef41Sopenharmony_ci    name: 'TypeError',
1381cb0ef41Sopenharmony_ci  }
1391cb0ef41Sopenharmony_ci);
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci// Copy throws at negative targetStart
1421cb0ef41Sopenharmony_ciassert.throws(
1431cb0ef41Sopenharmony_ci  () => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), -1, 0),
1441cb0ef41Sopenharmony_ci  {
1451cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE',
1461cb0ef41Sopenharmony_ci    name: 'RangeError',
1471cb0ef41Sopenharmony_ci    message: 'The value of "targetStart" is out of range. ' +
1481cb0ef41Sopenharmony_ci             'It must be >= 0. Received -1'
1491cb0ef41Sopenharmony_ci  }
1501cb0ef41Sopenharmony_ci);
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci// Copy throws at negative sourceStart
1531cb0ef41Sopenharmony_ciassert.throws(
1541cb0ef41Sopenharmony_ci  () => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, -1),
1551cb0ef41Sopenharmony_ci  {
1561cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE',
1571cb0ef41Sopenharmony_ci    name: 'RangeError',
1581cb0ef41Sopenharmony_ci  }
1591cb0ef41Sopenharmony_ci);
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci// Copy throws if sourceStart is greater than length of source
1621cb0ef41Sopenharmony_ciassert.throws(
1631cb0ef41Sopenharmony_ci  () => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, 100),
1641cb0ef41Sopenharmony_ci  {
1651cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE',
1661cb0ef41Sopenharmony_ci    name: 'RangeError',
1671cb0ef41Sopenharmony_ci  }
1681cb0ef41Sopenharmony_ci);
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci{
1711cb0ef41Sopenharmony_ci  // Check sourceEnd resets to targetEnd if former is greater than the latter
1721cb0ef41Sopenharmony_ci  b.fill(++cntr);
1731cb0ef41Sopenharmony_ci  c.fill(++cntr);
1741cb0ef41Sopenharmony_ci  b.copy(c, 0, 0, 1025);
1751cb0ef41Sopenharmony_ci  for (let i = 0; i < c.length; i++) {
1761cb0ef41Sopenharmony_ci    assert.strictEqual(c[i], b[i]);
1771cb0ef41Sopenharmony_ci  }
1781cb0ef41Sopenharmony_ci}
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci// Throw with negative sourceEnd
1811cb0ef41Sopenharmony_ciassert.throws(
1821cb0ef41Sopenharmony_ci  () => b.copy(c, 0, 0, -1),
1831cb0ef41Sopenharmony_ci  {
1841cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE',
1851cb0ef41Sopenharmony_ci    name: 'RangeError',
1861cb0ef41Sopenharmony_ci    message: 'The value of "sourceEnd" is out of range. ' +
1871cb0ef41Sopenharmony_ci             'It must be >= 0. Received -1'
1881cb0ef41Sopenharmony_ci  }
1891cb0ef41Sopenharmony_ci);
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci// When sourceStart is greater than sourceEnd, zero copied
1921cb0ef41Sopenharmony_ciassert.strictEqual(b.copy(c, 0, 100, 10), 0);
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci// When targetStart > targetLength, zero copied
1951cb0ef41Sopenharmony_ciassert.strictEqual(b.copy(c, 512, 0, 10), 0);
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci// Test that the `target` can be a Uint8Array.
1981cb0ef41Sopenharmony_ci{
1991cb0ef41Sopenharmony_ci  const d = new Uint8Array(c);
2001cb0ef41Sopenharmony_ci  // copy 512 bytes, from 0 to 512.
2011cb0ef41Sopenharmony_ci  b.fill(++cntr);
2021cb0ef41Sopenharmony_ci  d.fill(++cntr);
2031cb0ef41Sopenharmony_ci  const copied = b.copy(d, 0, 0, 512);
2041cb0ef41Sopenharmony_ci  assert.strictEqual(copied, 512);
2051cb0ef41Sopenharmony_ci  for (let i = 0; i < d.length; i++) {
2061cb0ef41Sopenharmony_ci    assert.strictEqual(d[i], b[i]);
2071cb0ef41Sopenharmony_ci  }
2081cb0ef41Sopenharmony_ci}
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ci// Test that the source can be a Uint8Array, too.
2111cb0ef41Sopenharmony_ci{
2121cb0ef41Sopenharmony_ci  const e = new Uint8Array(b);
2131cb0ef41Sopenharmony_ci  // copy 512 bytes, from 0 to 512.
2141cb0ef41Sopenharmony_ci  e.fill(++cntr);
2151cb0ef41Sopenharmony_ci  c.fill(++cntr);
2161cb0ef41Sopenharmony_ci  const copied = Buffer.prototype.copy.call(e, c, 0, 0, 512);
2171cb0ef41Sopenharmony_ci  assert.strictEqual(copied, 512);
2181cb0ef41Sopenharmony_ci  for (let i = 0; i < c.length; i++) {
2191cb0ef41Sopenharmony_ci    assert.strictEqual(c[i], e[i]);
2201cb0ef41Sopenharmony_ci  }
2211cb0ef41Sopenharmony_ci}
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/23668: Do not crash for invalid input.
2241cb0ef41Sopenharmony_cic.fill('c');
2251cb0ef41Sopenharmony_cib.copy(c, 'not a valid offset');
2261cb0ef41Sopenharmony_ci// Make sure this acted like a regular copy with `0` offset.
2271cb0ef41Sopenharmony_ciassert.deepStrictEqual(c, b.slice(0, c.length));
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ci{
2301cb0ef41Sopenharmony_ci  c.fill('C');
2311cb0ef41Sopenharmony_ci  assert.throws(() => {
2321cb0ef41Sopenharmony_ci    b.copy(c, { [Symbol.toPrimitive]() { throw new Error('foo'); } });
2331cb0ef41Sopenharmony_ci  }, /foo/);
2341cb0ef41Sopenharmony_ci  // No copying took place:
2351cb0ef41Sopenharmony_ci  assert.deepStrictEqual(c.toString(), 'C'.repeat(c.length));
2361cb0ef41Sopenharmony_ci}
237