2015-09-01 08:51:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
2024-10-15 21:24:39 +00:00
|
|
|
const common = require('../common');
|
2015-09-01 08:51:17 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const buffer = require('buffer');
|
|
|
|
const SlowBuffer = buffer.SlowBuffer;
|
|
|
|
|
|
|
|
const ones = [1, 1, 1, 1];
|
|
|
|
|
2024-10-15 21:24:39 +00:00
|
|
|
common.expectWarning(
|
|
|
|
'DeprecationWarning',
|
|
|
|
'SlowBuffer() is deprecated. Please use Buffer.allocUnsafeSlow()',
|
|
|
|
'DEP0030'
|
|
|
|
);
|
|
|
|
|
2019-03-22 02:44:26 +00:00
|
|
|
// Should create a Buffer
|
2016-01-25 23:00:06 +00:00
|
|
|
let sb = SlowBuffer(4);
|
2015-09-01 08:51:17 +00:00
|
|
|
assert(sb instanceof Buffer);
|
|
|
|
assert.strictEqual(sb.length, 4);
|
|
|
|
sb.fill(1);
|
2016-04-19 22:37:45 +00:00
|
|
|
for (const [key, value] of sb.entries()) {
|
|
|
|
assert.deepStrictEqual(value, ones[key]);
|
|
|
|
}
|
2015-09-01 08:51:17 +00:00
|
|
|
|
|
|
|
// underlying ArrayBuffer should have the same length
|
|
|
|
assert.strictEqual(sb.buffer.byteLength, 4);
|
|
|
|
|
2019-03-22 02:44:26 +00:00
|
|
|
// Should work without new
|
2015-09-01 08:51:17 +00:00
|
|
|
sb = SlowBuffer(4);
|
|
|
|
assert(sb instanceof Buffer);
|
|
|
|
assert.strictEqual(sb.length, 4);
|
|
|
|
sb.fill(1);
|
2016-04-19 22:37:45 +00:00
|
|
|
for (const [key, value] of sb.entries()) {
|
|
|
|
assert.deepStrictEqual(value, ones[key]);
|
|
|
|
}
|
2015-09-01 08:51:17 +00:00
|
|
|
|
2019-03-22 02:44:26 +00:00
|
|
|
// Should work with edge cases
|
2015-09-01 08:51:17 +00:00
|
|
|
assert.strictEqual(SlowBuffer(0).length, 0);
|
|
|
|
|
2019-03-04 03:02:53 +00:00
|
|
|
// Should throw with invalid length type
|
|
|
|
const bufferInvalidTypeMsg = {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'TypeError',
|
2019-03-04 03:02:53 +00:00
|
|
|
message: /^The "size" argument must be of type number/,
|
|
|
|
};
|
|
|
|
assert.throws(() => SlowBuffer(), bufferInvalidTypeMsg);
|
|
|
|
assert.throws(() => SlowBuffer({}), bufferInvalidTypeMsg);
|
|
|
|
assert.throws(() => SlowBuffer('6'), bufferInvalidTypeMsg);
|
|
|
|
assert.throws(() => SlowBuffer(true), bufferInvalidTypeMsg);
|
|
|
|
|
|
|
|
// Should throw with invalid length value
|
|
|
|
const bufferMaxSizeMsg = {
|
2023-01-18 10:36:27 +00:00
|
|
|
code: 'ERR_OUT_OF_RANGE',
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'RangeError',
|
2019-03-04 03:02:53 +00:00
|
|
|
};
|
2019-02-17 13:59:10 +00:00
|
|
|
assert.throws(() => SlowBuffer(NaN), bufferMaxSizeMsg);
|
|
|
|
assert.throws(() => SlowBuffer(Infinity), bufferMaxSizeMsg);
|
|
|
|
assert.throws(() => SlowBuffer(-1), bufferMaxSizeMsg);
|
|
|
|
assert.throws(() => SlowBuffer(buffer.kMaxLength + 1), bufferMaxSizeMsg);
|