summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorlarissayvette <larissayvette55@gmail.com>2017-02-10 15:31:14 +0100
committerRich Trott <rtrott@gmail.com>2017-05-27 21:11:26 -0700
commit234353a1b81a474ef93e1039afc2441ca81810d5 (patch)
treeb48a63d36993d67e6af67f8d1b81876358137df1 /test
parent1474b7a0328350670e7351096c72a280ed9140ea (diff)
downloadandroid-node-v8-234353a1b81a474ef93e1039afc2441ca81810d5.tar.gz
android-node-v8-234353a1b81a474ef93e1039afc2441ca81810d5.tar.bz2
android-node-v8-234353a1b81a474ef93e1039afc2441ca81810d5.zip
lib,src: refactor buffer out of range index
PR-URL: https://github.com/nodejs/node/pull/11296 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-buffer-alloc.js3
-rw-r--r--test/parallel/test-buffer-compare-offset.js4
-rw-r--r--test/parallel/test-buffer-fill.js77
-rw-r--r--test/parallel/test-buffer-read.js4
-rw-r--r--test/parallel/test-buffer-write-noassert.js2
5 files changed, 51 insertions, 39 deletions
diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js
index ca68a27a90..a2095c31c0 100644
--- a/test/parallel/test-buffer-alloc.js
+++ b/test/parallel/test-buffer-alloc.js
@@ -963,7 +963,8 @@ assert.throws(() => {
const a = Buffer.alloc(1);
const b = Buffer.alloc(1);
a.copy(b, 0, 0x100000000, 0x100000001);
-}, /out of range index/);
+}, common.expectsError(
+ {code: undefined, type: RangeError, message: 'Index out of range'}));
// Unpooled buffer (replaces SlowBuffer)
{
diff --git a/test/parallel/test-buffer-compare-offset.js b/test/parallel/test-buffer-compare-offset.js
index 037e82e055..e5c7a69110 100644
--- a/test/parallel/test-buffer-compare-offset.js
+++ b/test/parallel/test-buffer-compare-offset.js
@@ -1,6 +1,6 @@
'use strict';
-require('../common');
+const common = require('../common');
const assert = require('assert');
const a = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
@@ -57,7 +57,7 @@ assert.strictEqual(1, a.compare(b, Infinity, -Infinity));
// zero length target because default for targetEnd <= targetSource
assert.strictEqual(1, a.compare(b, '0xff'));
-const oor = /out of range index/;
+const oor = common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'});
assert.throws(() => a.compare(b, 0, 100, 0), oor);
assert.throws(() => a.compare(b, 0, 1, 0, 100), oor);
diff --git a/test/parallel/test-buffer-fill.js b/test/parallel/test-buffer-fill.js
index 46ebc79b62..d73aa006a9 100644
--- a/test/parallel/test-buffer-fill.js
+++ b/test/parallel/test-buffer-fill.js
@@ -1,7 +1,8 @@
+// Flags: --expose-internals
'use strict';
-
-require('../common');
+const common = require('../common');
const assert = require('assert');
+const errors = require('internal/errors');
const SIZE = 28;
const buf1 = Buffer.allocUnsafe(SIZE);
@@ -191,25 +192,30 @@ deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, -1]), [0, 0, 0, 0]);
// Check exceptions
-assert.throws(() => buf1.fill(0, -1), /^RangeError: Out of range index$/);
-assert.throws(() =>
- buf1.fill(0, 0, buf1.length + 1),
- /^RangeError: Out of range index$/);
-assert.throws(() => buf1.fill('', -1), /^RangeError: Out of range index$/);
-assert.throws(() =>
- buf1.fill('', 0, buf1.length + 1),
- /^RangeError: Out of range index$/);
-assert.throws(() =>
- buf1.fill('a', 0, buf1.length, 'node rocks!'),
- /^TypeError: Unknown encoding: node rocks!$/);
-assert.throws(() =>
- buf1.fill('a', 0, 0, NaN),
- /^TypeError: encoding must be a string$/);
-assert.throws(() =>
- buf1.fill('a', 0, 0, null),
- /^TypeError: encoding must be a string$/);
-assert.throws(() =>
- buf1.fill('a', 0, 0, 'foo'), /^TypeError: Unknown encoding: foo$/);
+assert.throws(
+ () => buf1.fill(0, -1),
+ common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
+assert.throws(
+ () => buf1.fill(0, 0, buf1.length + 1),
+ common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
+assert.throws(
+ () => buf1.fill('', -1),
+ common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
+assert.throws(
+ () => buf1.fill('', 0, buf1.length + 1),
+ common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
+assert.throws(
+ () => buf1.fill('a', 0, buf1.length, 'node rocks!'),
+ /^TypeError: Unknown encoding: node rocks!$/);
+assert.throws(
+ () => buf1.fill('a', 0, 0, NaN),
+ /^TypeError: encoding must be a string$/);
+assert.throws(
+ () => buf1.fill('a', 0, 0, null),
+ /^TypeError: encoding must be a string$/);
+assert.throws(
+ () => buf1.fill('a', 0, 0, 'foo'),
+ /^TypeError: Unknown encoding: foo$/);
function genBuffer(size, args) {
@@ -241,7 +247,7 @@ function writeToFill(string, offset, end, encoding) {
}
if (offset < 0 || end > buf2.length)
- throw new RangeError('Out of range index');
+ throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
if (end <= offset)
return buf2;
@@ -279,12 +285,12 @@ function testBufs(string, offset, length, encoding) {
}
// Make sure these throw.
-assert.throws(() =>
- Buffer.allocUnsafe(8).fill('a', -1),
- /^RangeError: Out of range index$/);
-assert.throws(() =>
- Buffer.allocUnsafe(8).fill('a', 0, 9),
- /^RangeError: Out of range index$/);
+assert.throws(
+ () => Buffer.allocUnsafe(8).fill('a', -1),
+ common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
+assert.throws(
+ () => Buffer.allocUnsafe(8).fill('a', 0, 9),
+ common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
// Make sure this doesn't hang indefinitely.
Buffer.allocUnsafe(8).fill('');
@@ -350,7 +356,8 @@ Buffer.alloc(8, '');
}
};
Buffer.alloc(1).fill(Buffer.alloc(1), start, 1);
- }, /out of range index/);
+ }, common.expectsError(
+ {code: undefined, type: RangeError, message: 'Index out of range'}));
// Make sure -1 is making it to Buffer::Fill().
assert.ok(elseWasLast,
'internal API changed, -1 no longer in correct location');
@@ -360,7 +367,8 @@ Buffer.alloc(8, '');
// around.
assert.throws(() => {
process.binding('buffer').fill(Buffer.alloc(1), 1, -1, 0, 1);
-}, /out of range index/);
+}, common.expectsError(
+ {code: undefined, type: RangeError, message: 'Index out of range'}));
// Make sure "end" is properly checked, even if it's magically mangled using
// Symbol.toPrimitive.
@@ -383,7 +391,8 @@ assert.throws(() => {
}
};
Buffer.alloc(1).fill(Buffer.alloc(1), 0, end);
- }, /^RangeError: out of range index$/);
+ }, common.expectsError(
+ {code: undefined, type: RangeError, message: 'Index out of range'}));
// Make sure -1 is making it to Buffer::Fill().
assert.ok(elseWasLast,
'internal API changed, -1 no longer in correct location');
@@ -393,7 +402,8 @@ assert.throws(() => {
// around.
assert.throws(() => {
process.binding('buffer').fill(Buffer.alloc(1), 1, 1, -2, 1);
-}, /out of range index/);
+}, common.expectsError(
+ { code: undefined, type: RangeError, message: 'Index out of range'}));
// Test that bypassing 'length' won't cause an abort.
assert.throws(() => {
@@ -403,7 +413,8 @@ assert.throws(() => {
enumerable: true
});
buf.fill('');
-}, /^RangeError: out of range index$/);
+}, common.expectsError(
+ { code: undefined, type: RangeError, message: 'Index out of range'}));
assert.deepStrictEqual(
Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'),
diff --git a/test/parallel/test-buffer-read.js b/test/parallel/test-buffer-read.js
index 53e53dd49e..e55bc3435a 100644
--- a/test/parallel/test-buffer-read.js
+++ b/test/parallel/test-buffer-read.js
@@ -1,5 +1,5 @@
'use strict';
-require('../common');
+const common = require('../common');
const assert = require('assert');
// testing basic buffer read functions
@@ -10,7 +10,7 @@ function read(buff, funx, args, expected) {
assert.strictEqual(buff[funx](...args), expected);
assert.throws(
() => buff[funx](-1),
- /^RangeError: Index out of range$/
+ common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'})
);
assert.doesNotThrow(
diff --git a/test/parallel/test-buffer-write-noassert.js b/test/parallel/test-buffer-write-noassert.js
index 9a5e4e6671..0730aaa354 100644
--- a/test/parallel/test-buffer-write-noassert.js
+++ b/test/parallel/test-buffer-write-noassert.js
@@ -4,7 +4,7 @@ const assert = require('assert');
// testing buffer write functions
-const outOfRange = /^RangeError: (?:Index )?out of range(?: index)?$/;
+const outOfRange = /^RangeError\b.*\bIndex out of range$/;
function write(funx, args, result, res) {
{