From 7479d657683103ed33e564489f675176c57aee7e Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 15 Feb 2018 23:35:03 -0800 Subject: [PATCH] Pull in new test files from Node.js core that are passing! --- test/node/test-buffer-arraybuffer.js | 110 +++++--- test/node/test-buffer-ascii.js | 46 +++- test/node/test-buffer-badhex.js | 80 +++--- .../test-buffer-failed-alloc-typed-arrays.js | 35 +++ test/node/test-buffer-inheritance.js | 27 +- test/node/test-buffer-isencoding.js | 39 +++ test/node/test-buffer-iterator.js | 11 +- test/node/test-buffer-parent-property.js | 25 ++ test/node/test-buffer-prototype-inspect.js | 25 ++ test/node/test-buffer-safe-unsafe.js | 9 +- test/node/test-buffer-slice.js | 132 +++++++++ test/node/test-buffer-swap.js | 255 +++++++++--------- test/node/test-buffer-tojson.js | 37 +++ test/node/test-buffer-tostring.js | 39 +++ test/node/test-buffer-write.js | 74 +++++ test/node/test-buffer-zero-fill-cli.js | 15 +- test/node/test-buffer-zero-fill-reset.js | 15 +- test/node/test-buffer-zero-fill.js | 15 ++ 18 files changed, 741 insertions(+), 248 deletions(-) create mode 100644 test/node/test-buffer-failed-alloc-typed-arrays.js create mode 100644 test/node/test-buffer-isencoding.js create mode 100644 test/node/test-buffer-parent-property.js create mode 100644 test/node/test-buffer-prototype-inspect.js create mode 100644 test/node/test-buffer-slice.js create mode 100644 test/node/test-buffer-tojson.js create mode 100644 test/node/test-buffer-tostring.js create mode 100644 test/node/test-buffer-write.js create mode 100644 test/node/test-buffer-zero-fill.js diff --git a/test/node/test-buffer-arraybuffer.js b/test/node/test-buffer-arraybuffer.js index d264e7f..b3b1a48 100644 --- a/test/node/test-buffer-arraybuffer.js +++ b/test/node/test-buffer-arraybuffer.js @@ -1,41 +1,37 @@ 'use strict'; var Buffer = require('../../').Buffer; -var common = require('./common'); +const common = require('./common'); +const assert = require('assert'); -var assert = require('assert'); +const LENGTH = 16; -var Buffer = require('../../').Buffer; -var LENGTH = 16; - -var ab = new ArrayBuffer(LENGTH); -var dv = new DataView(ab); -var ui = new Uint8Array(ab); -var buf = Buffer.from(ab); +const ab = new ArrayBuffer(LENGTH); +const dv = new DataView(ab); +const ui = new Uint8Array(ab); +const buf = Buffer.from(ab); assert.ok(buf instanceof Buffer); -// For backwards compatibility of old .parent property test that if buf is not -// a slice then .parent should be undefined. -assert.equal(buf.parent, buf.buffer); -assert.equal(buf.buffer, ab); -assert.equal(buf.length, ab.byteLength); +assert.strictEqual(buf.parent, buf.buffer); +assert.strictEqual(buf.buffer, ab); +assert.strictEqual(buf.length, ab.byteLength); buf.fill(0xC); -for (var i = 0; i < LENGTH; i++) { - assert.equal(ui[i], 0xC); +for (let i = 0; i < LENGTH; i++) { + assert.strictEqual(ui[i], 0xC); ui[i] = 0xF; - assert.equal(buf[i], 0xF); + assert.strictEqual(buf[i], 0xF); } buf.writeUInt32LE(0xF00, 0); buf.writeUInt32BE(0xB47, 4); buf.writeDoubleLE(3.1415, 8); -assert.equal(dv.getUint32(0, true), 0xF00); -assert.equal(dv.getUint32(4), 0xB47); -assert.equal(dv.getFloat64(8, true), 3.1415); +assert.strictEqual(dv.getUint32(0, true), 0xF00); +assert.strictEqual(dv.getUint32(4), 0xB47); +assert.strictEqual(dv.getFloat64(8, true), 3.1415); // Now test protecting users from doing stupid things @@ -48,7 +44,7 @@ assert.throws(function() { }, TypeError); // write{Double,Float}{LE,BE} with noAssert should not crash, cf. #3766 -var b = Buffer.allocUnsafe(1); +const b = Buffer.allocUnsafe(1); b.writeFloatLE(11.11, 0, true); b.writeFloatBE(11.11, 0, true); b.writeDoubleLE(11.11, 0, true); @@ -56,19 +52,19 @@ b.writeDoubleBE(11.11, 0, true); // Test the byteOffset and length arguments { - var ab = new Uint8Array(5); + const ab = new Uint8Array(5); ab[0] = 1; ab[1] = 2; ab[2] = 3; ab[3] = 4; ab[4] = 5; - var buf = Buffer.from(ab.buffer, 1, 3); - assert.equal(buf.length, 3); - assert.equal(buf[0], 2); - assert.equal(buf[1], 3); - assert.equal(buf[2], 4); + const buf = Buffer.from(ab.buffer, 1, 3); + assert.strictEqual(buf.length, 3); + assert.strictEqual(buf[0], 2); + assert.strictEqual(buf[1], 3); + assert.strictEqual(buf[2], 4); buf[0] = 9; - assert.equal(ab[1], 9); + assert.strictEqual(ab[1], 9); common.expectsError(() => Buffer.from(ab.buffer, 6), { code: 'ERR_BUFFER_OUT_OF_BOUNDS', @@ -84,19 +80,19 @@ b.writeDoubleBE(11.11, 0, true); // Test the deprecated Buffer() version also { - var ab = new Uint8Array(5); + const ab = new Uint8Array(5); ab[0] = 1; ab[1] = 2; ab[2] = 3; ab[3] = 4; ab[4] = 5; - var buf = Buffer(ab.buffer, 1, 3); - assert.equal(buf.length, 3); - assert.equal(buf[0], 2); - assert.equal(buf[1], 3); - assert.equal(buf[2], 4); + const buf = Buffer(ab.buffer, 1, 3); + assert.strictEqual(buf.length, 3); + assert.strictEqual(buf[0], 2); + assert.strictEqual(buf[1], 3); + assert.strictEqual(buf[2], 4); buf[0] = 9; - assert.equal(ab[1], 9); + assert.strictEqual(ab[1], 9); common.expectsError(() => Buffer(ab.buffer, 6), { code: 'ERR_BUFFER_OUT_OF_BOUNDS', @@ -110,3 +106,47 @@ b.writeDoubleBE(11.11, 0, true); }); } +{ + // If byteOffset is not numeric, it defaults to 0. + const ab = new ArrayBuffer(10); + const expected = Buffer.from(ab, 0); + assert.deepStrictEqual(Buffer.from(ab, 'fhqwhgads'), expected); + assert.deepStrictEqual(Buffer.from(ab, NaN), expected); + assert.deepStrictEqual(Buffer.from(ab, {}), expected); + assert.deepStrictEqual(Buffer.from(ab, []), expected); + + // If byteOffset can be converted to a number, it will be. + assert.deepStrictEqual(Buffer.from(ab, [1]), Buffer.from(ab, 1)); + + // If byteOffset is Infinity, throw. + common.expectsError(() => { + Buffer.from(ab, Infinity); + }, { + code: 'ERR_BUFFER_OUT_OF_BOUNDS', + type: RangeError, + message: '"offset" is outside of buffer bounds' + }); +} + +{ + // If length is not numeric, it defaults to 0. + const ab = new ArrayBuffer(10); + const expected = Buffer.from(ab, 0, 0); + assert.deepStrictEqual(Buffer.from(ab, 0, 'fhqwhgads'), expected); + assert.deepStrictEqual(Buffer.from(ab, 0, NaN), expected); + assert.deepStrictEqual(Buffer.from(ab, 0, {}), expected); + assert.deepStrictEqual(Buffer.from(ab, 0, []), expected); + + // If length can be converted to a number, it will be. + assert.deepStrictEqual(Buffer.from(ab, 0, [1]), Buffer.from(ab, 0, 1)); + + //If length is Infinity, throw. + common.expectsError(() => { + Buffer.from(ab, 0, Infinity); + }, { + code: 'ERR_BUFFER_OUT_OF_BOUNDS', + type: RangeError, + message: '"length" is outside of buffer bounds' + }); +} + diff --git a/test/node/test-buffer-ascii.js b/test/node/test-buffer-ascii.js index 4a06098..6c462e7 100644 --- a/test/node/test-buffer-ascii.js +++ b/test/node/test-buffer-ascii.js @@ -1,25 +1,45 @@ -'use strict'; -var Buffer = require('../../').Buffer; - +// Copyright Joyent, Inc. and other Node contributors.var Buffer = require('../../').Buffer; +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. -var assert = require('assert'); +'use strict'; +require('./common'); +const assert = require('assert'); // ASCII conversion in node.js simply masks off the high bits, // it doesn't do transliteration. -assert.equal(Buffer.from('hérité').toString('ascii'), 'hC)ritC)'); +assert.strictEqual(Buffer.from('hérité').toString('ascii'), 'hC)ritC)'); // 71 characters, 78 bytes. The ’ character is a triple-byte sequence. -var input = 'C’est, graphiquement, la réunion d’un accent aigu ' + - 'et d’un accent grave.'; +const input = 'C’est, graphiquement, la réunion d’un accent aigu ' + + 'et d’un accent grave.'; -var expected = 'Cb\u0000\u0019est, graphiquement, la rC)union ' + - 'db\u0000\u0019un accent aigu et db\u0000\u0019un ' + - 'accent grave.'; +const expected = 'Cb\u0000\u0019est, graphiquement, la rC)union ' + + 'db\u0000\u0019un accent aigu et db\u0000\u0019un ' + + 'accent grave.'; -var buf = Buffer.from(input); +const buf = Buffer.from(input); -for (var i = 0; i < expected.length; ++i) { - assert.equal(buf.slice(i).toString('ascii'), expected.slice(i)); +for (let i = 0; i < expected.length; ++i) { + assert.strictEqual(buf.slice(i).toString('ascii'), expected.slice(i)); // Skip remainder of multi-byte sequence. if (input.charCodeAt(i) > 65535) ++i; diff --git a/test/node/test-buffer-badhex.js b/test/node/test-buffer-badhex.js index fd7851d..a6388e3 100644 --- a/test/node/test-buffer-badhex.js +++ b/test/node/test-buffer-badhex.js @@ -1,46 +1,50 @@ 'use strict'; var Buffer = require('../../').Buffer; +require('./common'); +const assert = require('assert'); +// Test hex strings and bad hex strings +{ + const buf = Buffer.alloc(4); + assert.strictEqual(buf.length, 4); + assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0])); + assert.strictEqual(buf.write('abcdxx', 0, 'hex'), 2); + assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0x00, 0x00])); + assert.strictEqual(buf.toString('hex'), 'abcd0000'); + assert.strictEqual(buf.write('abcdef01', 0, 'hex'), 4); + assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0xef, 0x01])); + assert.strictEqual(buf.toString('hex'), 'abcdef01'); + + const copy = Buffer.from(buf.toString('hex'), 'hex'); + assert.strictEqual(buf.toString('hex'), copy.toString('hex')); +} -var assert = require('assert'); -var Buffer = require('../../').Buffer; +{ + const buf = Buffer.alloc(5); + assert.strictEqual(buf.write('abcdxx', 1, 'hex'), 2); + assert.strictEqual(buf.toString('hex'), '00abcd0000'); +} + +{ + const buf = Buffer.alloc(4); + assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0])); + assert.strictEqual(buf.write('xxabcd', 0, 'hex'), 0); + assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0])); + assert.strictEqual(buf.write('xxab', 1, 'hex'), 0); + assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0])); + assert.strictEqual(buf.write('cdxxab', 0, 'hex'), 1); + assert.deepStrictEqual(buf, new Buffer([0xcd, 0, 0, 0])); +} -// Test hex strings and bad hex strings { - var buf1 = Buffer.alloc(4); - assert.strictEqual(buf1.length, 4); - assert.deepStrictEqual(buf1, new Buffer([0, 0, 0, 0])); - assert.strictEqual(buf1.write('abcdxx', 0, 'hex'), 2); - assert.deepStrictEqual(buf1, new Buffer([0xab, 0xcd, 0x00, 0x00])); - assert.strictEqual(buf1.toString('hex'), 'abcd0000'); - assert.strictEqual(buf1.write('abcdef01', 0, 'hex'), 4); - assert.deepStrictEqual(buf1, new Buffer([0xab, 0xcd, 0xef, 0x01])); - assert.strictEqual(buf1.toString('hex'), 'abcdef01'); - - var buf2 = Buffer.from(buf1.toString('hex'), 'hex'); - assert.strictEqual(buf1.toString('hex'), buf2.toString('hex')); - - var buf3 = Buffer.alloc(5); - assert.strictEqual(buf3.write('abcdxx', 1, 'hex'), 2); - assert.strictEqual(buf3.toString('hex'), '00abcd0000'); - - var buf4 = Buffer.alloc(4); - assert.deepStrictEqual(buf4, new Buffer([0, 0, 0, 0])); - assert.strictEqual(buf4.write('xxabcd', 0, 'hex'), 0); - assert.deepStrictEqual(buf4, new Buffer([0, 0, 0, 0])); - assert.strictEqual(buf4.write('xxab', 1, 'hex'), 0); - assert.deepStrictEqual(buf4, new Buffer([0, 0, 0, 0])); - assert.strictEqual(buf4.write('cdxxab', 0, 'hex'), 1); - assert.deepStrictEqual(buf4, new Buffer([0xcd, 0, 0, 0])); - - var buf5 = Buffer.alloc(256); - for (var i = 0; i < 256; i++) - buf5[i] = i; - - var hex = buf5.toString('hex'); - assert.deepStrictEqual(Buffer.from(hex, 'hex'), buf5); - - var badHex = hex.slice(0, 256) + 'xx' + hex.slice(256, 510); - assert.deepStrictEqual(Buffer.from(badHex, 'hex'), buf5.slice(0, 128)); + const buf = Buffer.alloc(256); + for (let i = 0; i < 256; i++) + buf[i] = i; + + const hex = buf.toString('hex'); + assert.deepStrictEqual(Buffer.from(hex, 'hex'), buf); + + const badHex = `${hex.slice(0, 256)}xx${hex.slice(256, 510)}`; + assert.deepStrictEqual(Buffer.from(badHex, 'hex'), buf.slice(0, 128)); } diff --git a/test/node/test-buffer-failed-alloc-typed-arrays.js b/test/node/test-buffer-failed-alloc-typed-arrays.js new file mode 100644 index 0000000..0e01955 --- /dev/null +++ b/test/node/test-buffer-failed-alloc-typed-arrays.js @@ -0,0 +1,35 @@ +'use strict'; +var Buffer = require('../../').Buffer; + +require('./common'); +const assert = require('assert'); +const SlowBuffer = require('../../').SlowBuffer; + +// Test failed or zero-sized Buffer allocations not affecting typed arrays. +// This test exists because of a regression that occurred. Because Buffer +// instances are allocated with the same underlying allocator as TypedArrays, +// but Buffer's can optional be non-zero filled, there was a regression that +// occurred when a Buffer allocated failed, the internal flag specifying +// whether or not to zero-fill was not being reset, causing TypedArrays to +// allocate incorrectly. +const zeroArray = new Uint32Array(10).fill(0); +const sizes = [1e10, 0, 0.1, -1, 'a', undefined, null, NaN]; +const allocators = [ + Buffer, + SlowBuffer, + Buffer.alloc, + Buffer.allocUnsafe, + Buffer.allocUnsafeSlow +]; +for (const allocator of allocators) { + for (const size of sizes) { + try { + // These allocations are known to fail. If they do, + // Uint32Array should still produce a zeroed out result. + allocator(size); + } catch (e) { + assert.deepStrictEqual(new Uint32Array(10), zeroArray); + } + } +} + diff --git a/test/node/test-buffer-inheritance.js b/test/node/test-buffer-inheritance.js index 711d9f2..26ac8f9 100644 --- a/test/node/test-buffer-inheritance.js +++ b/test/node/test-buffer-inheritance.js @@ -1,13 +1,12 @@ 'use strict'; var Buffer = require('../../').Buffer; - - -var assert = require('assert'); +require('./common'); +const assert = require('assert'); function T(n) { - var ui8 = new Uint8Array(n); + const ui8 = new Uint8Array(n); Object.setPrototypeOf(ui8, T.prototype); return ui8; } @@ -15,26 +14,26 @@ Object.setPrototypeOf(T.prototype, Buffer.prototype); Object.setPrototypeOf(T, Buffer); T.prototype.sum = function sum() { - var cntr = 0; - for (var i = 0; i < this.length; i++) + let cntr = 0; + for (let i = 0; i < this.length; i++) cntr += this[i]; return cntr; }; -var vals = [new T(4), T(4)]; +const vals = [new T(4), T(4)]; vals.forEach(function(t) { - assert.equal(t.constructor, T); - assert.equal(Object.getPrototypeOf(t), T.prototype); - assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(t)), - Buffer.prototype); + assert.strictEqual(t.constructor, T); + assert.strictEqual(Object.getPrototypeOf(t), T.prototype); + assert.strictEqual(Object.getPrototypeOf(Object.getPrototypeOf(t)), + Buffer.prototype); t.fill(5); - var cntr = 0; - for (var i = 0; i < t.length; i++) + let cntr = 0; + for (let i = 0; i < t.length; i++) cntr += t[i]; - assert.equal(t.length * 5, cntr); + assert.strictEqual(t.length * 5, cntr); // Check this does not throw t.toString(); diff --git a/test/node/test-buffer-isencoding.js b/test/node/test-buffer-isencoding.js new file mode 100644 index 0000000..0d79140 --- /dev/null +++ b/test/node/test-buffer-isencoding.js @@ -0,0 +1,39 @@ +'use strict'; +var Buffer = require('../../').Buffer; + +require('./common'); +const assert = require('assert'); + +[ + 'hex', + 'utf8', + 'utf-8', + 'ascii', + 'latin1', + 'binary', + 'base64', + 'ucs2', + 'ucs-2', + 'utf16le', + 'utf-16le' +].forEach((enc) => { + assert.strictEqual(Buffer.isEncoding(enc), true); +}); + +[ + 'utf9', + 'utf-7', + 'Unicode-FTW', + 'new gnu gun', + false, + NaN, + {}, + Infinity, + [], + 1, + 0, + -1 +].forEach((enc) => { + assert.strictEqual(Buffer.isEncoding(enc), false); +}); + diff --git a/test/node/test-buffer-iterator.js b/test/node/test-buffer-iterator.js index f27709f..3e08473 100644 --- a/test/node/test-buffer-iterator.js +++ b/test/node/test-buffer-iterator.js @@ -1,12 +1,11 @@ 'use strict'; var Buffer = require('../../').Buffer; +require('./common'); +const assert = require('assert'); - -var assert = require('assert'); - -var buffer = Buffer.from([1, 2, 3, 4, 5]); -var arr; -var b; +const buffer = Buffer.from([1, 2, 3, 4, 5]); +let arr; +let b; // buffers should be iterable diff --git a/test/node/test-buffer-parent-property.js b/test/node/test-buffer-parent-property.js new file mode 100644 index 0000000..d01376a --- /dev/null +++ b/test/node/test-buffer-parent-property.js @@ -0,0 +1,25 @@ +'use strict'; +var Buffer = require('../../').Buffer; + +/* + * Fix for https://github.com/nodejs/node/issues/8266 + * + * Zero length Buffer objects should expose the `buffer` property of the + * TypedArrays, via the `parent` property. + */ +require('./common'); +const assert = require('assert'); + +// If the length of the buffer object is zero +assert((new Buffer(0)).parent instanceof ArrayBuffer); + +// If the length of the buffer object is equal to the underlying ArrayBuffer +assert((new Buffer(Buffer.poolSize)).parent instanceof ArrayBuffer); + +// Same as the previous test, but with user created buffer +const arrayBuffer = new ArrayBuffer(0); +assert.strictEqual(new Buffer(arrayBuffer).parent, arrayBuffer); +assert.strictEqual(new Buffer(arrayBuffer).buffer, arrayBuffer); +assert.strictEqual(Buffer.from(arrayBuffer).parent, arrayBuffer); +assert.strictEqual(Buffer.from(arrayBuffer).buffer, arrayBuffer); + diff --git a/test/node/test-buffer-prototype-inspect.js b/test/node/test-buffer-prototype-inspect.js new file mode 100644 index 0000000..2c643a7 --- /dev/null +++ b/test/node/test-buffer-prototype-inspect.js @@ -0,0 +1,25 @@ +'use strict'; +var Buffer = require('../../').Buffer; +require('./common'); + +// lib/buffer.js defines Buffer.prototype.inspect() to override how buffers are +// presented by util.inspect(). + +const assert = require('assert'); +const util = require('util'); + +{ + const buf = Buffer.from('fhqwhgads'); + assert.strictEqual(util.inspect(buf), ''); +} + +{ + const buf = Buffer.from(''); + assert.strictEqual(util.inspect(buf), ''); +} + +{ + const buf = Buffer.from('x'.repeat(51)); + assert.ok(/^$/.test(util.inspect(buf))); +} + diff --git a/test/node/test-buffer-safe-unsafe.js b/test/node/test-buffer-safe-unsafe.js index 22ba29e..ca171b6 100644 --- a/test/node/test-buffer-safe-unsafe.js +++ b/test/node/test-buffer-safe-unsafe.js @@ -1,14 +1,13 @@ 'use strict'; var Buffer = require('../../').Buffer; +require('./common'); +const assert = require('assert'); - -var assert = require('assert'); - -var safe = Buffer.alloc(10); +const safe = Buffer.alloc(10); function isZeroFilled(buf) { - for (var n = 0; n < buf.length; n++) + for (let n = 0; n < buf.length; n++) if (buf[n] !== 0) return false; return true; } diff --git a/test/node/test-buffer-slice.js b/test/node/test-buffer-slice.js new file mode 100644 index 0000000..c2d3282 --- /dev/null +++ b/test/node/test-buffer-slice.js @@ -0,0 +1,132 @@ +// Copyright Joyent, Inc. and other Node contributors.var Buffer = require('../../').Buffer; +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'use strict'; + +require('./common'); +const assert = require('assert'); + +assert.strictEqual(0, Buffer.from('hello', 'utf8').slice(0, 0).length); +assert.strictEqual(0, Buffer('hello', 'utf8').slice(0, 0).length); + +const buf = Buffer.from('0123456789', 'utf8'); +const expectedSameBufs = [ + [buf.slice(-10, 10), Buffer.from('0123456789', 'utf8')], + [buf.slice(-20, 10), Buffer.from('0123456789', 'utf8')], + [buf.slice(-20, -10), Buffer.from('', 'utf8')], + [buf.slice(), Buffer.from('0123456789', 'utf8')], + [buf.slice(0), Buffer.from('0123456789', 'utf8')], + [buf.slice(0, 0), Buffer.from('', 'utf8')], + [buf.slice(undefined), Buffer.from('0123456789', 'utf8')], + [buf.slice('foobar'), Buffer.from('0123456789', 'utf8')], + [buf.slice(undefined, undefined), Buffer.from('0123456789', 'utf8')], + [buf.slice(2), Buffer.from('23456789', 'utf8')], + [buf.slice(5), Buffer.from('56789', 'utf8')], + [buf.slice(10), Buffer.from('', 'utf8')], + [buf.slice(5, 8), Buffer.from('567', 'utf8')], + [buf.slice(8, -1), Buffer.from('8', 'utf8')], + [buf.slice(-10), Buffer.from('0123456789', 'utf8')], + [buf.slice(0, -9), Buffer.from('0', 'utf8')], + [buf.slice(0, -10), Buffer.from('', 'utf8')], + [buf.slice(0, -1), Buffer.from('012345678', 'utf8')], + [buf.slice(2, -2), Buffer.from('234567', 'utf8')], + [buf.slice(0, 65536), Buffer.from('0123456789', 'utf8')], + [buf.slice(65536, 0), Buffer.from('', 'utf8')], + [buf.slice(-5, -8), Buffer.from('', 'utf8')], + [buf.slice(-5, -3), Buffer.from('56', 'utf8')], + [buf.slice(-10, 10), Buffer.from('0123456789', 'utf8')], + [buf.slice('0', '1'), Buffer.from('0', 'utf8')], + [buf.slice('-5', '10'), Buffer.from('56789', 'utf8')], + [buf.slice('-10', '10'), Buffer.from('0123456789', 'utf8')], + [buf.slice('-10', '-5'), Buffer.from('01234', 'utf8')], + [buf.slice('-10', '-0'), Buffer.from('', 'utf8')], + [buf.slice('111'), Buffer.from('', 'utf8')], + [buf.slice('0', '-111'), Buffer.from('', 'utf8')] +]; + +for (let i = 0, s = buf.toString(); i < buf.length; ++i) { + expectedSameBufs.push( + [buf.slice(i), Buffer.from(s.slice(i))], + [buf.slice(0, i), Buffer.from(s.slice(0, i))], + [buf.slice(-i), Buffer.from(s.slice(-i))], + [buf.slice(0, -i), Buffer.from(s.slice(0, -i))] + ); +} + +expectedSameBufs.forEach(([buf1, buf2]) => { + assert.strictEqual(0, Buffer.compare(buf1, buf2)); +}); + +const utf16Buf = Buffer.from('0123456789', 'utf16le'); +assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer.from('012', 'utf16le')); +// try to slice a zero length Buffer +// see https://github.com/joyent/node/issues/5881 +assert.doesNotThrow(() => Buffer.alloc(0).slice(0, 1)); +assert.strictEqual(Buffer.alloc(0).slice(0, 1).length, 0); + +{ + // Single argument slice + assert.strictEqual('bcde', + Buffer.from('abcde', 'utf8').slice(1).toString('utf8')); +} + +// slice(0,0).length === 0 +assert.strictEqual(0, Buffer.from('hello', 'utf8').slice(0, 0).length); + +{ + // Regression tests for https://github.com/nodejs/node/issues/9096 + const buf = Buffer.from('abcd', 'utf8'); + assert.strictEqual(buf.slice(buf.length / 3).toString('utf8'), 'bcd'); + assert.strictEqual( + buf.slice(buf.length / 3, buf.length).toString(), + 'bcd' + ); +} + +{ + const buf = Buffer.from('abcdefg', 'utf8'); + assert.strictEqual(buf.slice(-(-1 >>> 0) - 1).toString('utf8'), + buf.toString('utf8')); +} + +{ + const buf = Buffer.from('abc', 'utf8'); + assert.strictEqual(buf.slice(-0.5).toString('utf8'), buf.toString('utf8')); +} + +{ + const buf = Buffer.from([ + 1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0, + 0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0 + ]); + const chunk1 = Buffer.from([ + 1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0 + ]); + const chunk2 = Buffer.from([ + 0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0 + ]); + const middle = buf.length / 2; + + assert.deepStrictEqual(buf.slice(0, middle), chunk1); + assert.deepStrictEqual(buf.slice(middle), chunk2); +} + diff --git a/test/node/test-buffer-swap.js b/test/node/test-buffer-swap.js index 2ec1a7a..9af5755 100644 --- a/test/node/test-buffer-swap.js +++ b/test/node/test-buffer-swap.js @@ -1,141 +1,154 @@ 'use strict'; var Buffer = require('../../').Buffer; - - -var assert = require('assert'); +require('./common'); +const assert = require('assert'); // Test buffers small enough to use the JS implementation -var buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, - 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]); - -assert.strictEqual(buf, buf.swap16()); -assert.deepStrictEqual(buf, Buffer.from([0x02, 0x01, 0x04, 0x03, 0x06, 0x05, - 0x08, 0x07, 0x0a, 0x09, 0x0c, 0x0b, - 0x0e, 0x0d, 0x10, 0x0f])); -buf.swap16(); // restore - -assert.strictEqual(buf, buf.swap32()); -assert.deepStrictEqual(buf, Buffer.from([0x04, 0x03, 0x02, 0x01, 0x08, 0x07, - 0x06, 0x05, 0x0c, 0x0b, 0x0a, 0x09, - 0x10, 0x0f, 0x0e, 0x0d])); -buf.swap32(); // restore - -assert.strictEqual(buf, buf.swap64()); -assert.deepStrictEqual(buf, Buffer.from([0x08, 0x07, 0x06, 0x05, 0x04, 0x03, - 0x02, 0x01, 0x10, 0x0f, 0x0e, 0x0d, - 0x0c, 0x0b, 0x0a, 0x09])); +{ + const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]); + + assert.strictEqual(buf, buf.swap16()); + assert.deepStrictEqual(buf, Buffer.from([0x02, 0x01, 0x04, 0x03, 0x06, 0x05, + 0x08, 0x07, 0x0a, 0x09, 0x0c, 0x0b, + 0x0e, 0x0d, 0x10, 0x0f])); + buf.swap16(); // restore + + assert.strictEqual(buf, buf.swap32()); + assert.deepStrictEqual(buf, Buffer.from([0x04, 0x03, 0x02, 0x01, 0x08, 0x07, + 0x06, 0x05, 0x0c, 0x0b, 0x0a, 0x09, + 0x10, 0x0f, 0x0e, 0x0d])); + buf.swap32(); // restore + + assert.strictEqual(buf, buf.swap64()); + assert.deepStrictEqual(buf, Buffer.from([0x08, 0x07, 0x06, 0x05, 0x04, 0x03, + 0x02, 0x01, 0x10, 0x0f, 0x0e, 0x0d, + 0x0c, 0x0b, 0x0a, 0x09])); +} // Operates in-place -var buf3 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7]); -buf3.slice(1, 5).swap32(); -assert.deepStrictEqual(buf3, Buffer.from([0x1, 0x5, 0x4, 0x3, 0x2, 0x6, 0x7])); - -buf3.slice(1, 5).swap16(); -assert.deepStrictEqual(buf3, Buffer.from([0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7])); - -var buf3_64 = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, - 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]); -buf3_64.slice(2, 18).swap64(); -assert.deepStrictEqual(buf3_64, Buffer.from([0x01, 0x02, 0x0a, 0x09, 0x08, 0x07, - 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, - 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, - 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, - 0x0f, 0x10])); - -// Force use of native code (Buffer size above threshold limit for js impl) -var buf4A = new Uint32Array(256).fill(0x04030201); -var buf4 = Buffer.from(buf4A.buffer, buf4A.byteOffset); -var buf5A = new Uint32Array(256).fill(0x03040102); -var buf5 = Buffer.from(buf5A.buffer, buf5A.byteOffset); - -buf4.swap16(); -assert.deepStrictEqual(buf4, buf5); - -var buf6A = new Uint32Array(256).fill(0x04030201); -var buf6 = Buffer.from(buf6A.buffer); -var bu7A = new Uint32Array(256).fill(0x01020304); -var buf7 = Buffer.from(bu7A.buffer, bu7A.byteOffset); - -buf6.swap32(); -assert.deepStrictEqual(buf6, buf7); - -var buf8A = new Uint8Array(256 * 8); -var buf9A = new Uint8Array(256 * 8); -for (var i = 0; i < buf8A.length; i++) { - buf8A[i] = i % 8; - buf9A[buf9A.length - i - 1] = i % 8; +{ + const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7]); + buf.slice(1, 5).swap32(); + assert.deepStrictEqual(buf, Buffer.from([0x1, 0x5, 0x4, 0x3, 0x2, 0x6, 0x7])); + buf.slice(1, 5).swap16(); + assert.deepStrictEqual(buf, Buffer.from([0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7])); + + // Length assertions + const re16 = /Buffer size must be a multiple of 16-bits/; + const re32 = /Buffer size must be a multiple of 32-bits/; + const re64 = /Buffer size must be a multiple of 64-bits/; + + assert.throws(() => Buffer.from(buf).swap16(), re16); + assert.throws(() => Buffer.alloc(1025).swap16(), re16); + assert.throws(() => Buffer.from(buf).swap32(), re32); + assert.throws(() => buf.slice(1, 3).swap32(), re32); + assert.throws(() => Buffer.alloc(1025).swap32(), re32); + assert.throws(() => buf.slice(1, 3).swap64(), re64); + assert.throws(() => Buffer.alloc(1025).swap64(), re64); } -var buf8 = Buffer.from(buf8A.buffer, buf8A.byteOffset); -var buf9 = Buffer.from(buf9A.buffer, buf9A.byteOffset); - -buf8.swap64(); -assert.deepStrictEqual(buf8, buf9); -// Test native code with buffers that are not memory-aligned -var buf10A = new Uint8Array(256 * 8); -var buf11A = new Uint8Array(256 * 8 - 2); -for (var i = 0; i < buf10A.length; i++) { - buf10A[i] = i % 2; -} -for (var i = 1; i < buf11A.length; i++) { - buf11A[buf11A.length - i] = (i + 1) % 2; -} -var buf10 = Buffer.from(buf10A.buffer, buf10A.byteOffset); -// 0|1 0|1 0|1... -var buf11 = Buffer.from(buf11A.buffer, buf11A.byteOffset); -// 0|0 1|0 1|0... +{ + const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]); -buf10.slice(1, buf10.length - 1).swap16(); -assert.deepStrictEqual(buf10.slice(0, buf11.length), buf11); + buf.slice(2, 18).swap64(); + assert.deepStrictEqual(buf, Buffer.from([0x01, 0x02, 0x0a, 0x09, 0x08, 0x07, + 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, + 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, + 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10])); +} -var buf12A = new Uint8Array(256 * 8); -var buf13A = new Uint8Array(256 * 8 - 4); -for (var i = 0; i < buf12A.length; i++) { - buf12A[i] = i % 4; +// Force use of native code (Buffer size above threshold limit for js impl) +{ + const bufData = new Uint32Array(256).fill(0x04030201); + const buf = Buffer.from(bufData.buffer, bufData.byteOffset); + const otherBufData = new Uint32Array(256).fill(0x03040102); + const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); + buf.swap16(); + assert.deepStrictEqual(buf, otherBuf); } -for (var i = 1; i < buf13A.length; i++) { - buf13A[buf13A.length - i] = (i + 1) % 4; + +{ + const bufData = new Uint32Array(256).fill(0x04030201); + const buf = Buffer.from(bufData.buffer); + const otherBufData = new Uint32Array(256).fill(0x01020304); + const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); + buf.swap32(); + assert.deepStrictEqual(buf, otherBuf); } -var buf12 = Buffer.from(buf12A.buffer, buf12A.byteOffset); -// 0|1 2 3 0|1 2 3... -var buf13 = Buffer.from(buf13A.buffer, buf13A.byteOffset); -// 0|0 3 2 1|0 3 2... -buf12.slice(1, buf12.length - 3).swap32(); -assert.deepStrictEqual(buf12.slice(0, buf13.length), buf13); +{ + const bufData = new Uint8Array(256 * 8); + const otherBufData = new Uint8Array(256 * 8); + for (let i = 0; i < bufData.length; i++) { + bufData[i] = i % 8; + otherBufData[otherBufData.length - i - 1] = i % 8; + } + const buf = Buffer.from(bufData.buffer, bufData.byteOffset); + const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); + buf.swap64(); + assert.deepStrictEqual(buf, otherBuf); +} +// Test native code with buffers that are not memory-aligned +{ + const bufData = new Uint8Array(256 * 8); + const otherBufData = new Uint8Array(256 * 8 - 2); + for (let i = 0; i < bufData.length; i++) { + bufData[i] = i % 2; + } + for (let i = 1; i < otherBufData.length; i++) { + otherBufData[otherBufData.length - i] = (i + 1) % 2; + } + const buf = Buffer.from(bufData.buffer, bufData.byteOffset); + // 0|1 0|1 0|1... + const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); + // 0|0 1|0 1|0... + + buf.slice(1, buf.length - 1).swap16(); + assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf); +} -var buf14A = new Uint8Array(256 * 8); -var buf15A = new Uint8Array(256 * 8 - 8); -for (var i = 0; i < buf14A.length; i++) { - buf14A[i] = i % 8; +{ + const bufData = new Uint8Array(256 * 8); + const otherBufData = new Uint8Array(256 * 8 - 4); + for (let i = 0; i < bufData.length; i++) { + bufData[i] = i % 4; + } + for (let i = 1; i < otherBufData.length; i++) { + otherBufData[otherBufData.length - i] = (i + 1) % 4; + } + const buf = Buffer.from(bufData.buffer, bufData.byteOffset); + // 0|1 2 3 0|1 2 3... + const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); + // 0|0 3 2 1|0 3 2... + + buf.slice(1, buf.length - 3).swap32(); + assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf); } -for (var i = 1; i < buf15A.length; i++) { - buf15A[buf15A.length - i] = (i + 1) % 8; + +{ + const bufData = new Uint8Array(256 * 8); + const otherBufData = new Uint8Array(256 * 8 - 8); + for (let i = 0; i < bufData.length; i++) { + bufData[i] = i % 8; + } + for (let i = 1; i < otherBufData.length; i++) { + otherBufData[otherBufData.length - i] = (i + 1) % 8; + } + const buf = Buffer.from(bufData.buffer, bufData.byteOffset); + // 0|1 2 3 4 5 6 7 0|1 2 3 4... + const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset); + // 0|0 7 6 5 4 3 2 1|0 7 6 5... + + buf.slice(1, buf.length - 7).swap64(); + assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf); } -var buf14 = Buffer.from(buf14A.buffer, buf14A.byteOffset); -// 0|1 2 3 4 5 6 7 0|1 2 3 4... -var buf15 = Buffer.from(buf15A.buffer, buf15A.byteOffset); -// 0|0 7 6 5 4 3 2 1|0 7 6 5... - -buf14.slice(1, buf14.length - 7).swap64(); -assert.deepStrictEqual(buf14.slice(0, buf15.length), buf15); - -// Length assertions -var re16 = /Buffer size must be a multiple of 16-bits/; -var re32 = /Buffer size must be a multiple of 32-bits/; -var re64 = /Buffer size must be a multiple of 64-bits/; - -assert.throws(() => Buffer.from(buf3).swap16(), re16); -assert.throws(() => Buffer.alloc(1025).swap16(), re16); -assert.throws(() => Buffer.from(buf3).swap32(), re32); -assert.throws(() => buf3.slice(1, 3).swap32(), re32); -assert.throws(() => Buffer.alloc(1025).swap32(), re32); -assert.throws(() => buf3.slice(1, 3).swap64(), re64); -assert.throws(() => Buffer.alloc(1025).swap64(), re64); diff --git a/test/node/test-buffer-tojson.js b/test/node/test-buffer-tojson.js new file mode 100644 index 0000000..dfb94d6 --- /dev/null +++ b/test/node/test-buffer-tojson.js @@ -0,0 +1,37 @@ +'use strict'; +var Buffer = require('../../').Buffer; + +require('./common'); +const assert = require('assert'); + +{ + assert.strictEqual(JSON.stringify(Buffer.alloc(0)), + '{"type":"Buffer","data":[]}'); + assert.strictEqual(JSON.stringify(Buffer.from([1, 2, 3, 4])), + '{"type":"Buffer","data":[1,2,3,4]}'); +} + +// issue GH-7849 +{ + const buf = Buffer.from('test'); + const json = JSON.stringify(buf); + const obj = JSON.parse(json); + const copy = Buffer.from(obj); + + assert.deepStrictEqual(buf, copy); +} + +// GH-5110 +{ + const buffer = Buffer.from('test'); + const string = JSON.stringify(buffer); + + assert.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}'); + + function receiver(key, value) { + return value && value.type === 'Buffer' ? Buffer.from(value.data) : value; + } + + assert.deepStrictEqual(buffer, JSON.parse(string, receiver)); +} + diff --git a/test/node/test-buffer-tostring.js b/test/node/test-buffer-tostring.js new file mode 100644 index 0000000..ac0102c --- /dev/null +++ b/test/node/test-buffer-tostring.js @@ -0,0 +1,39 @@ +'use strict'; +var Buffer = require('../../').Buffer; + +const common = require('./common'); +const assert = require('assert'); + +// utf8, ucs2, ascii, latin1, utf16le +const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1', + 'binary', 'utf16le', 'utf-16le']; + +encodings + .reduce((es, e) => es.concat(e, e.toUpperCase()), []) + .forEach((encoding) => { + assert.strictEqual(Buffer.from('foo', encoding).toString(encoding), 'foo'); + }); + +// base64 +['base64', 'BASE64'].forEach((encoding) => { + assert.strictEqual(Buffer.from('Zm9v', encoding).toString(encoding), 'Zm9v'); +}); + +// hex +['hex', 'HEX'].forEach((encoding) => { + assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding), + '666f6f'); +}); + +// Invalid encodings +for (let i = 1; i < 10; i++) { + const encoding = String(i).repeat(i); + const error = common.expectsError({ + code: 'ERR_UNKNOWN_ENCODING', + type: TypeError, + message: `Unknown encoding: ${encoding}` + }); + assert.ok(!Buffer.isEncoding(encoding)); + assert.throws(() => Buffer.from('foo').toString(encoding), error); +} + diff --git a/test/node/test-buffer-write.js b/test/node/test-buffer-write.js new file mode 100644 index 0000000..85a87e8 --- /dev/null +++ b/test/node/test-buffer-write.js @@ -0,0 +1,74 @@ +'use strict'; +var Buffer = require('../../').Buffer; + +const common = require('./common'); +const assert = require('assert'); + +const outsideBounds = common.expectsError({ + code: 'ERR_BUFFER_OUT_OF_BOUNDS', + type: RangeError, + message: 'Attempt to write outside buffer bounds' +}, 2); + +assert.throws(() => Buffer.alloc(9).write('foo', -1), outsideBounds); +assert.throws(() => Buffer.alloc(9).write('foo', 10), outsideBounds); + +const resultMap = new Map([ + ['utf8', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], + ['ucs2', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])], + ['ascii', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], + ['latin1', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], + ['binary', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], + ['utf16le', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])], + ['base64', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], + ['hex', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])] +]); + +// utf8, ucs2, ascii, latin1, utf16le +const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1', + 'binary', 'utf16le', 'utf-16le']; + +encodings + .reduce((es, e) => es.concat(e, e.toUpperCase()), []) + .forEach((encoding) => { + const buf = Buffer.alloc(9); + const len = Buffer.byteLength('foo', encoding); + assert.strictEqual(buf.write('foo', 0, len, encoding), len); + + if (encoding.includes('-')) + encoding = encoding.replace('-', ''); + + assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); + }); + +// base64 +['base64', 'BASE64'].forEach((encoding) => { + const buf = Buffer.alloc(9); + const len = Buffer.byteLength('Zm9v', encoding); + + assert.strictEqual(buf.write('Zm9v', 0, len, encoding), len); + assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); +}); + +// hex +['hex', 'HEX'].forEach((encoding) => { + const buf = Buffer.alloc(9); + const len = Buffer.byteLength('666f6f', encoding); + + assert.strictEqual(buf.write('666f6f', 0, len, encoding), len); + assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); +}); + +// Invalid encodings +for (let i = 1; i < 10; i++) { + const encoding = String(i).repeat(i); + const error = common.expectsError({ + code: 'ERR_UNKNOWN_ENCODING', + type: TypeError, + message: `Unknown encoding: ${encoding}` + }); + + assert.ok(!Buffer.isEncoding(encoding)); + assert.throws(() => Buffer.alloc(9).write('foo', encoding), error); +} + diff --git a/test/node/test-buffer-zero-fill-cli.js b/test/node/test-buffer-zero-fill-cli.js index 744b334..1e86b27 100644 --- a/test/node/test-buffer-zero-fill-cli.js +++ b/test/node/test-buffer-zero-fill-cli.js @@ -1,17 +1,16 @@ 'use strict'; var Buffer = require('../../').Buffer; - // Flags: --zero-fill-buffers // when using --zero-fill-buffers, every Buffer and SlowBuffer // instance must be zero filled upon creation - -var SlowBuffer = require('../../').SlowBuffer; -var assert = require('assert'); +require('./common'); +const SlowBuffer = require('../../').SlowBuffer; +const assert = require('assert'); function isZeroFilled(buf) { - for (var n = 0; n < buf.length; n++) + for (let n = 0; n < buf.length; n++) if (buf[n] > 0) return false; return true; } @@ -20,15 +19,15 @@ function isZeroFilled(buf) { // allocated memory might just already happen to // contain all zeroes. The test is run multiple // times to improve the reliability. -for (var i = 0; i < 50; i++) { - var bufs = [ +for (let i = 0; i < 50; i++) { + const bufs = [ Buffer.alloc(20), Buffer.allocUnsafe(20), SlowBuffer(20), Buffer(20), new SlowBuffer(20) ]; - for (var buf of bufs) { + for (const buf of bufs) { assert(isZeroFilled(buf)); } } diff --git a/test/node/test-buffer-zero-fill-reset.js b/test/node/test-buffer-zero-fill-reset.js index b21a6b2..875a64b 100644 --- a/test/node/test-buffer-zero-fill-reset.js +++ b/test/node/test-buffer-zero-fill-reset.js @@ -1,22 +1,21 @@ 'use strict'; var Buffer = require('../../').Buffer; - - -var assert = require('assert'); +require('./common'); +const assert = require('assert'); function testUint8Array(ui) { - var length = ui.length; - for (var i = 0; i < length; i++) + const length = ui.length; + for (let i = 0; i < length; i++) if (ui[i] !== 0) return false; return true; } -for (var i = 0; i < 100; i++) { +for (let i = 0; i < 100; i++) { Buffer.alloc(0); - var ui = new Uint8Array(65); - assert.ok(testUint8Array(ui), 'Uint8Array is not zero-filled'); + const ui = new Uint8Array(65); + assert.ok(testUint8Array(ui), `Uint8Array is not zero-filled: ${ui}`); } diff --git a/test/node/test-buffer-zero-fill.js b/test/node/test-buffer-zero-fill.js new file mode 100644 index 0000000..f5c1a13 --- /dev/null +++ b/test/node/test-buffer-zero-fill.js @@ -0,0 +1,15 @@ +'use strict'; +var Buffer = require('../../').Buffer; + +require('./common'); +const assert = require('assert'); + +const buf1 = Buffer(100); +const buf2 = new Buffer(100); + +for (let n = 0; n < buf1.length; n++) + assert.strictEqual(buf1[n], 0); + +for (let n = 0; n < buf2.length; n++) + assert.strictEqual(buf2[n], 0); + -- 2.34.1