From a53911b166c5db738330e6cd84062959528998f7 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Tue, 4 May 2021 22:41:55 +0800 Subject: [PATCH] test: improve buffer coverage PR-URL: https://github.com/nodejs/node/pull/38538 Reviewed-By: Michael Dawson Reviewed-By: Khaidi Chu Reviewed-By: Colin Ihrig Reviewed-By: Zijian Liu Reviewed-By: Rich Trott Reviewed-By: James M Snell --- test/parallel/test-buffer-copy.js | 42 ++++++++++++++++++++++++++++ test/parallel/test-buffer-indexof.js | 28 +++++++++++++------ 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/test/parallel/test-buffer-copy.js b/test/parallel/test-buffer-copy.js index d6f734614471e5..f7ccfff9d0cbbe 100644 --- a/test/parallel/test-buffer-copy.js +++ b/test/parallel/test-buffer-copy.js @@ -30,6 +30,17 @@ let cntr = 0; } } +{ + // Floats will be converted to integers via `Math.floor` + b.fill(++cntr); + c.fill(++cntr); + const copied = b.copy(c, 0, 0, 512.5); + assert.strictEqual(copied, 512); + for (let i = 0; i < c.length; i++) { + assert.strictEqual(c[i], b[i]); + } +} + { // Copy c into b, without specifying sourceEnd b.fill(++cntr); @@ -52,6 +63,17 @@ let cntr = 0; } } +{ + // Copied source range greater than source length + b.fill(++cntr); + c.fill(++cntr); + const copied = c.copy(b, 0, 0, c.length + 1); + assert.strictEqual(copied, c.length); + for (let i = 0; i < c.length; i++) { + assert.strictEqual(b[i], c[i]); + } +} + { // Copy longer buffer b to shorter c without targetStart b.fill(++cntr); @@ -107,6 +129,26 @@ bb.fill('hello crazy world'); // Try to copy from before the beginning of b. Should not throw. b.copy(c, 0, 100, 10); +// Throw with invalid source type +assert.throws( + () => Buffer.prototype.copy.call(0), + { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + } +); + +// Copy throws at negative targetStart +assert.throws( + () => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), -1, 0), + { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + message: 'The value of "targetStart" is out of range. ' + + 'It must be >= 0. Received -1' + } +); + // Copy throws at negative sourceStart assert.throws( () => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, -1), diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js index 82d359ca213f2c..01cbd3dd5ab186 100644 --- a/test/parallel/test-buffer-indexof.js +++ b/test/parallel/test-buffer-indexof.js @@ -108,6 +108,13 @@ assert.strictEqual( 3 ); +// Test base64url encoding +assert.strictEqual( + Buffer.from(b.toString('base64url'), 'base64url') + .indexOf('ZA==', 0, 'base64url'), + 3 +); + // test ascii encoding assert.strictEqual( Buffer.from(b.toString('ascii'), 'ascii') @@ -180,15 +187,18 @@ assert.strictEqual(Buffer.from('aaaa0').indexOf('30', 'hex'), 4); assert.strictEqual(Buffer.from('aaaa00a').indexOf('3030', 'hex'), 4); { - // test usc2 encoding - const twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); - - assert.strictEqual(twoByteString.indexOf('\u0395', 4, 'ucs2'), 8); - assert.strictEqual(twoByteString.indexOf('\u03a3', -4, 'ucs2'), 6); - assert.strictEqual(twoByteString.indexOf('\u03a3', -6, 'ucs2'), 4); - assert.strictEqual(twoByteString.indexOf( - Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2'), 4); - assert.strictEqual(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2')); + // Test usc2 and utf16le encoding + ['ucs2', 'utf16le'].forEach((encoding) => { + const twoByteString = Buffer.from( + '\u039a\u0391\u03a3\u03a3\u0395', encoding); + + assert.strictEqual(twoByteString.indexOf('\u0395', 4, encoding), 8); + assert.strictEqual(twoByteString.indexOf('\u03a3', -4, encoding), 6); + assert.strictEqual(twoByteString.indexOf('\u03a3', -6, encoding), 4); + assert.strictEqual(twoByteString.indexOf( + Buffer.from('\u03a3', encoding), -6, encoding), 4); + assert.strictEqual(-1, twoByteString.indexOf('\u03a3', -2, encoding)); + }); } const mixedByteStringUcs2 =