|
|
@ -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'), |
|
|
|