const test = require('brittle') const Realm = require('bare-realm') const Buffer = require('..') test('from', (t) => { t.is(Buffer.from('123').byteLength, 3, 'from string') t.is(Buffer.from([1, 2, 3]).byteLength, 3, 'from array') t.is(Buffer.from(Buffer.from([1, 2, 3])).byteLength, 3, 'from buffer') t.is(Buffer.from(new ArrayBuffer(8)).byteLength, 8, 'from arraybuffer') t.is(Buffer.from(Buffer.from('123').toJSON()).byteLength, 3, 'from toJSON') }) test('constructor with options', (t) => { const arrayBuffer = new ArrayBuffer(8) t.is(new Buffer(arrayBuffer, {}).byteLength, 8, 'options in place of offset') t.is(new Buffer(arrayBuffer, 2, {}).byteLength, 6, 'options in place of length') t.is(new Buffer(arrayBuffer, 2, 4, {}).byteLength, 4, 'options after length') }) test('alloc', (t) => { t.is(Buffer.alloc(42).byteLength, 42) }) test('alloc rejects a length that is not an integer in range', (t) => { const { MAX_LENGTH } = Buffer.constants const sizes = [-1, -MAX_LENGTH, NaN, Infinity, -Infinity, MAX_LENGTH + 1, 2 ** 53, 1.5, -0.5, 0.1] for (const size of sizes) { for (const method of ['alloc', 'allocUnsafe', 'allocUnsafeSlow']) { t.exception.all(() => Buffer[method](size), /RangeError/, `${method}(${size})`) } t.exception.all(() => new Buffer(size), /RangeError/, `new Buffer(${size})`) } t.is(Buffer.alloc(0).byteLength, 0, 'zero is allowed') t.is(Buffer.alloc(-0).byteLength, 0, 'negative zero is allowed') }) test('poolSize rejects anything but a size', (t) => { const { poolSize } = Buffer t.teardown(() => { Buffer.poolSize = poolSize }) for (const value of ['4', {}, null, [8], () => {}, 4n]) { t.exception.all(() => { Buffer.poolSize = value }, /TypeError/) } for (const value of [-1, NaN, Infinity, 1.5, Buffer.constants.MAX_LENGTH + 1]) { t.exception.all(() => { Buffer.poolSize = value }, /RangeError/) } t.is(Buffer.poolSize, poolSize, 'a rejected value does not take effect') Buffer.poolSize = 1024 t.is(Buffer.poolSize, 1024) t.is(Buffer.allocUnsafe(64).byteLength, 64, 'the pool still serves allocations') Buffer.poolSize = 0 t.is(Buffer.allocUnsafe(64).byteLength, 64, 'pooling can be turned off') }) test('alloc rejects a size that is not a number', (t) => { for (const size of ['4', {}, null, [8], () => {}, 4n]) { for (const method of ['alloc', 'allocUnsafe', 'allocUnsafeSlow']) { t.exception.all(() => Buffer[method](size), /TypeError/, `${method}(${String(size)})`) } } }) test('alloc rejects a fill value that cannot fill', (t) => { for (const value of [{}, Symbol('x'), null, [1, 2], 1n, () => {}]) { t.exception.all(() => Buffer.alloc(4, value), /TypeError/) } t.alike(Buffer.alloc(4, undefined), Buffer.alloc(4), 'undefined means no fill') t.alike(Buffer.alloc(4, NaN), Buffer.alloc(4), 'a number is truncated to a byte') }) test('fill rejects a value that cannot fill', (t) => { for (const value of [{}, Symbol('x'), null, undefined, [1, 2], 1n, () => {}]) { t.exception.all(() => Buffer.alloc(4).fill(value), /TypeError/) } t.exception.all(() => Buffer.alloc(4).fill(), /TypeError/) }) test('fill with a view fills with the bytes it spans', (t) => { const bytes = new Uint8Array([1, 2, 3, 4]) t.alike(Buffer.alloc(4, new Uint8Array([1, 2])), Buffer.from([1, 2, 1, 2])) t.alike(Buffer.alloc(4, new Uint16Array(bytes.buffer)), Buffer.from([1, 2, 3, 4])) t.alike(Buffer.alloc(4, new DataView(bytes.buffer, 2)), Buffer.from([3, 4, 3, 4])) t.alike(Buffer.alloc(4, new Uint8Array(0)), Buffer.alloc(4), 'an empty view zeroes') }) // A view from another realm has the internal slots and the %TypedArray% methods, // but its constructor and prototype come from that realm, so `instanceof` does // not recognize it. Anything that has to tell views apart goes by the element // size instead. const realm = new Realm() test('a view of bytes from another realm is accepted', (t) => { const bytes = realm.evaluate('new Uint8Array([1, 2, 3, 4])') t.is(bytes instanceof Uint8Array, false, 'instanceof does not recognize it') t.is(bytes.BYTES_PER_ELEMENT, 1, 'the element size still does') t.alike(Buffer.concat([bytes]), Buffer.from([1, 2, 3, 4])) t.alike(Buffer.concat([bytes], 6), Buffer.from([1, 2, 3, 4, 0, 0])) t.alike(Buffer.from(bytes), Buffer.from([1, 2, 3, 4])) t.alike(Buffer.coerce(bytes), Buffer.from([1, 2, 3, 4])) t.alike(Buffer.alloc(4).fill(bytes), Buffer.from([1, 2, 3, 4])) t.is(Buffer.from([1, 2, 3, 4]).equals(bytes), true) t.is(Buffer.from([0, 1, 2, 3, 4]).indexOf(bytes), 1) const target = realm.evaluate('new Uint8Array(4)') t.is(Buffer.from([9, 9]).copy(target), 2) t.alike([...target], [9, 9, 0, 0]) }) test('a wider view from another realm goes by its bytes', (t) => { const wide = realm.evaluate('new Uint16Array([0x1122, 0x3344])') t.is(wide.BYTES_PER_ELEMENT, 2) t.exception.all(() => Buffer.concat([wide]), /must be a view of single bytes/) t.alike(Buffer.from(wide), Buffer.from([0x22, 0x44]), 'element wise, truncated') const target = realm.evaluate('new Uint16Array(2)') t.is(Buffer.from([1, 2, 3, 4]).copy(target), 4) t.alike([...new Uint8Array(target.buffer)], [1, 2, 3, 4], 'byte wise') const pattern = new Uint16Array([1, 2]) t.alike( Buffer.alloc(4).fill(realm.evaluate('new Uint16Array([1, 2])')), Buffer.from(new Uint8Array(pattern.buffer)), 'byte wise' ) }) // A DataView reports no length in any realm, and sizing a copy by a length that // is missing rather than zero would span the rest of the allocation pool. test('a DataView from another realm copies as empty', (t) => { const view = realm.evaluate('new DataView(new ArrayBuffer(64))') t.is(view instanceof DataView, false, 'instanceof does not recognize it') t.is(typeof view.length, 'undefined', 'and it reports no length') t.is(Buffer.from(view).byteLength, 0) }) test('an encoding must be a string', (t) => { t.exception.all(() => Buffer.alloc(4, 'x', {}), /TypeError/) t.exception.all(() => Buffer.alloc(4, 'x', 42), /TypeError/) t.exception.all(() => Buffer.from('x', {}), /TypeError/) t.exception.all(() => Buffer.alloc(4).toString({}), /TypeError/) t.exception.all(() => Buffer.byteLength('x', {}), /TypeError/) }) test('an offset or length must be an integer', (t) => { const source = Buffer.from('hello world') const target = Buffer.alloc(11) const arrayBuffer = new ArrayBuffer(8) const view = new Uint16Array(4) const cases = [ ['toString start', () => source.toString('hex', 0.5, 3)], ['toString end', () => source.toString('hex', 0, 3.5)], ['write offset', () => target.write('ab', 0.5)], ['write length', () => target.write('ab', 0, 1.5)], ['fill offset', () => target.fill(1, 0.5)], ['fill end', () => target.fill(1, 0, 3.5)], ['copy targetStart', () => source.copy(target, 0.5)], ['copy sourceStart', () => source.copy(target, 0, 0.5)], ['copy sourceEnd', () => source.copy(target, 0, 0, 3.5)], ['compare targetStart', () => source.compare(target, 0.5)], ['compare targetEnd', () => source.compare(target, 0, 2.5)], ['compare sourceStart', () => source.compare(target, 0, 2, 0.5)], ['compare sourceEnd', () => source.compare(target, 0, 2, 0, 2.5)], ['indexOf offset', () => source.indexOf('o', 1.5)], ['indexOf byte offset', () => source.indexOf(0x6f, 1.5)], ['lastIndexOf offset', () => source.lastIndexOf('o', 1.5)], ['includes offset', () => source.includes('o', 1.5)], ['concat length', () => Buffer.concat([source], 3.5)], ['copyBytesFrom offset', () => Buffer.copyBytesFrom(view, 0.5)], ['copyBytesFrom length', () => Buffer.copyBytesFrom(view, 0, 1.5)], ['copyBytesFrom negative', () => Buffer.copyBytesFrom(view, -1)], ['from offset', () => Buffer.from(arrayBuffer, 0.5)], ['from length', () => Buffer.from(arrayBuffer, 0, 2.5)], ['new Buffer offset', () => new Buffer(arrayBuffer, 0.5)], ['new Buffer length', () => new Buffer(arrayBuffer, 0, 2.5)] ] for (const [label, fn] of cases) { t.exception.all(fn, /RangeError/, label) } }) test('an offset or length must be a number', (t) => { const source = Buffer.from('hello world') const target = Buffer.alloc(11) t.exception.all(() => source.toString('hex', {}, 3), /TypeError/) t.exception.all(() => target.write('ab', {}), /TypeError/) t.exception.all(() => target.fill(1, {}), /TypeError/) t.exception.all(() => source.copy(target, {}), /TypeError/) t.exception.all(() => source.compare(target, {}), /TypeError/) t.exception.all(() => Buffer.concat([source], '3'), /TypeError/) t.exception.all(() => Buffer.copyBytesFrom(new Uint16Array(4), '1'), /TypeError/) t.exception.all(() => Buffer.from(new ArrayBuffer(8), '1'), /TypeError/) }) test('a negative offset is still clamped or counted from the end', (t) => { const source = Buffer.from('hello world') t.is(source.toString('latin1', -4), 'hello world', 'a negative start clamps') t.is(source.indexOf('o', -5), 7, 'a negative offset counts from the end') t.is(source.indexOf('o', -3), -1, 'and can land past the last match') t.is(source.lastIndexOf('o', -3), 7) t.alike(Buffer.alloc(4).fill(1, -1), Buffer.from([1, 1, 1, 1])) // copy() is the exception, rejecting a negative offset outright. t.exception.all(() => source.copy(Buffer.alloc(11), -1), /RangeError/) }) test('copy rejects an offset outside its range', (t) => { const source = Buffer.from([1, 2, 3, 4]) t.exception.all(() => source.copy(Buffer.alloc(4), -1), /RangeError/, 'targetStart') t.exception.all(() => source.copy(Buffer.alloc(4), 0, -1), /RangeError/, 'sourceStart') t.exception.all(() => source.copy(Buffer.alloc(4), 0, 0, -1), /RangeError/, 'sourceEnd') t.exception.all( () => source.copy(Buffer.alloc(4), 0, 9), /RangeError/, 'sourceStart past the end' ) t.is(source.copy(Buffer.alloc(4), 0, 4), 0, 'a sourceStart at the end is allowed') t.is(source.copy(Buffer.alloc(4), 9), 0, 'a targetStart past the end copies nothing') t.is(source.copy(Buffer.alloc(4), 0, 0, 99), 4, 'a sourceEnd past the end clamps') }) test('copy requires a view as its target', (t) => { const source = Buffer.from([1, 2, 3, 4]) for (const target of [undefined, null, 'aaaa', [0, 0, 0, 0], {}, 4]) { t.exception.all(() => source.copy(target), /TypeError/, String(target)) } t.exception.all( () => source.copy({ buffer: new ArrayBuffer(8), byteOffset: 0, byteLength: 8 }), /TypeError/, 'a plain object is not a view' ) }) test('copy into a view writes the bytes it spans', (t) => { const source = Buffer.from([1, 2, 3, 4]) const wide = new Uint16Array(4) t.is(source.copy(wide), 4) t.alike(Buffer.from(wide.buffer), Buffer.from([1, 2, 3, 4, 0, 0, 0, 0])) const arrayBuffer = new ArrayBuffer(4) t.is(source.copy(new DataView(arrayBuffer)), 4) t.alike(Buffer.from(arrayBuffer), Buffer.from([1, 2, 3, 4])) const offsetView = new DataView(new ArrayBuffer(6), 2) t.is(source.copy(offsetView), 4) t.alike(Buffer.from(offsetView.buffer), Buffer.from([0, 0, 1, 2, 3, 4])) }) test('alloc with fill', (t) => { const buf = Buffer.alloc(5, 0xff) t.is(buf.byteLength, 5) for (let i = 0; i < 5; i++) t.is(buf[i], 0xff) }) test('alloc with fill leaves no byte uninitialized', (t) => { // A buffer that is filled is not zeroed first, so every fill has to cover the // whole of it. Memory is churned beforehand to make reused pages dirty. for (let i = 0; i < 100; i++) Buffer.allocUnsafeSlow(4096).fill(0xdd) const fills = [1, 255, 256, -1, true, false, '', 'a', 'ab', 'abcdefgh', Buffer.from('ab')] for (const size of [0, 1, 3, 8, 63, 64, 65, 200, 5000]) { for (const fill of fills) { const expected = Buffer.alloc(size) expected.fill(fill, 0, size) t.alike(Buffer.alloc(size, fill), expected, `alloc(${size}, ${JSON.stringify(fill)})`) } } t.test('and is still zeroed without a fill', (t) => { for (const size of [1, 64, 4096]) { for (const fill of [undefined, 0]) { t.alike(Buffer.alloc(size, fill), Buffer.alloc(size), `alloc(${size}, ${fill})`) } } }) t.test('and is never pooled', (t) => { for (const fill of [undefined, 0, 1, 'ab']) { const buffer = Buffer.alloc(64, fill) t.is( buffer.buffer.byteLength, 64, `alloc(64, ${JSON.stringify(fill)}) owns its backing store` ) t.is(buffer.byteOffset, 0) } }) }) test('allocUnsafe', (t) => { t.is(Buffer.allocUnsafe(42).byteLength, 42) }) test('allocUnsafeSlow', (t) => { t.is(Buffer.allocUnsafeSlow(42).byteLength, 42) }) test('byteLength', (t) => { t.is(Buffer.byteLength(Buffer.alloc(42)), 42) }) test('compare', (t) => { t.is(Buffer.compare(Buffer.from([1, 2, 3]), Buffer.from([1, 2, 3])), 0) t.is(Buffer.compare(Buffer.from([1, 3, 2]), Buffer.from([1, 2, 3])), 1) t.is(Buffer.compare(Buffer.from([1, 2, 3]), Buffer.from([1, 3, 2])), -1) t.is(Buffer.compare(Buffer.from([1, 2, 3, 4]), Buffer.from([1, 2, 3])), 1) t.is(Buffer.compare(Buffer.from([1, 2, 3]), Buffer.from([1, 2, 3, 4])), -1) t.is(Buffer.compare(Buffer.from([1, 2, 3, 4]), Buffer.from([1, 2, 3, 4])), 0) t.is(Buffer.compare(Buffer.from([1, 2, 4, 3]), Buffer.from([1, 2, 3, 4])), 1) t.is(Buffer.compare(Buffer.from([1, 2, 3, 4]), Buffer.from([1, 2, 4, 3])), -1) t.test('varying lengths', (t) => { for (let i = 0; i < 10; i++) { t.is(Buffer.compare(Buffer.alloc(i), Buffer.alloc(i)), 0, `length ${i}`) } }) t.test('varying alignment', (t) => { for (let i = 0; i < 10; i++) { t.is( Buffer.compare(Buffer.alloc(i).subarray(i), Buffer.alloc(i).subarray(i)), 0, `offset ${i}` ) } }) }) test('concat', (t) => { t.alike( Buffer.concat([Buffer.from([1, 2, 3]), Buffer.from([4, 5, 6])]), Buffer.from([1, 2, 3, 4, 5, 6]) ) }) test('copyBytesFrom', (t) => { const u16 = new Uint16Array([0, 0xffff]) t.alike(Buffer.copyBytesFrom(u16), Buffer.from([0, 0, 255, 255])) t.alike(Buffer.copyBytesFrom(u16, 0, 0), Buffer.from([])) t.alike(Buffer.copyBytesFrom(u16, 0, 1), Buffer.from([0, 0])) t.alike(Buffer.copyBytesFrom(u16, 1, 1), Buffer.from([255, 255])) t.exception.all(() => Buffer.copyBytesFrom(u16, 2, 1), /RangeError/) const i32 = new Int32Array([21, 31]) Buffer.copyBytesFrom(i32)[0] = 42 // Mutate copy data, original data must remain the same t.alike(Buffer.copyBytesFrom(i32, 0, 1), Buffer.from([21, 0, 0, 0])) t.alike(Buffer.copyBytesFrom(i32, 1, 1), Buffer.from([31, 0, 0, 0])) }) test('concat with length', (t) => { t.alike( Buffer.concat([Buffer.from([1, 2, 3]), Buffer.from([4, 5, 6])], 5), Buffer.from([1, 2, 3, 4, 5]) ) t.alike( Buffer.concat([Buffer.from([1, 2, 3]), Buffer.from([4, 5, 6])], 5), Buffer.from([1, 2, 3, 4, 5]) ) t.alike( Buffer.concat([Buffer.from([1, 2, 3]), Buffer.from([4, 5, 6])], 6), Buffer.from([1, 2, 3, 4, 5, 6]) ) t.alike( Buffer.concat([Buffer.from([1, 2, 3]), Buffer.from([4, 5, 6])], 7), Buffer.from([1, 2, 3, 4, 5, 6, 0]) ) }) test('copy', (t) => { const x = Buffer.from([1, 2, 3]) const y = Buffer.alloc(3) t.is(x.copy(y), 3) t.alike(y, x) t.test('within self', (t) => { const x = Buffer.from([1, 2, 3, 0, 0, 0]) t.is(x.copy(x, 3), 3) t.alike(x, Buffer.from([1, 2, 3, 1, 2, 3])) }) }) test('equals', (t) => { t.is(Buffer.from([1, 2, 3]).equals(Buffer.from([1, 2, 3])), true) t.is(Buffer.from([1, 3, 2]).equals(Buffer.from([1, 2, 3])), false) t.is(Buffer.from([1, 2, 3, 4]).equals(Buffer.from([1, 2, 3])), false) t.test('varying lengths', (t) => { for (let i = 0; i < 10; i++) { t.is(Buffer.alloc(i).equals(Buffer.alloc(i)), true, `length ${i}`) } }) t.test('varying alignment', (t) => { for (let i = 0; i < 10; i++) { t.is(Buffer.alloc(i).subarray(i).equals(Buffer.alloc(i).subarray(i)), true, `offset ${i}`) } }) }) test('fill', (t) => { t.alike(Buffer.alloc(3).fill(1), Buffer.from([1, 1, 1])) t.alike(Buffer.alloc(3).fill(1, 1), Buffer.from([0, 1, 1])) t.alike(Buffer.alloc(3).fill(1, 1, 2), Buffer.from([0, 1, 0])) t.alike(Buffer.alloc(3).fill('ab'), Buffer.from([0x61, 0x62, 0x61])) t.alike(Buffer.alloc(3).fill('abcd', 'hex'), Buffer.from([0xab, 0xcd, 0xab])) t.alike(Buffer.alloc(3).fill('abcd', 1, 'hex'), Buffer.from([0, 0xab, 0xcd])) t.alike(Buffer.alloc(3).fill('ab', 1, 2, 'hex'), Buffer.from([0, 0xab, 0])) t.alike(Buffer.alloc(3).fill(''), Buffer.alloc(3), 'empty pattern') t.alike(Buffer.alloc(3).fill('abcdef'), Buffer.from('abc'), 'pattern longer than range') t.test('pattern repeats across the range', (t) => { // Ranges either side of the point where filling switches to doubling the // pattern in place, including lengths that are not a multiple of it. for (const pattern of ['ab', 'abc', 'abcdefgh', 'abcdefgh'.repeat(9)]) { for (const size of [1, 2, 3, 7, 8, 63, 64, 65, 127, 128, 1000]) { const expected = Buffer.alloc(size) for (let i = 0; i < size; i++) expected[i] = pattern.charCodeAt(i % pattern.length) t.alike(Buffer.alloc(size).fill(pattern), expected, `${pattern} into ${size}`) if (size > 3) { const offset = Buffer.alloc(size) for (let i = 2; i < size - 1; i++) { offset[i] = pattern.charCodeAt((i - 2) % pattern.length) } t.alike( Buffer.alloc(size).fill(pattern, 2, size - 1), offset, `${pattern} into ${size} at [2, ${size - 1})` ) } } } }) }) test('indexOf', (t) => { t.is(Buffer.from([1, 2, 3]).indexOf(1), 0) t.is(Buffer.from([1, 2, 3]).indexOf(2), 1) t.is(Buffer.from([1, 2, 3]).indexOf(3), 2) t.is(Buffer.from([1, 2, 3]).indexOf(4), -1) t.is(Buffer.from([1, 2, 3]).indexOf(Buffer.from([1])), 0) t.is(Buffer.from([1, 2, 3]).indexOf(Buffer.from([1, 2])), 0) t.is(Buffer.from([1, 2, 3]).indexOf(Buffer.from([1, 2, 3])), 0) t.is(Buffer.from([1, 2, 3]).indexOf(Buffer.from([2])), 1) t.is(Buffer.from([1, 2, 3]).indexOf(Buffer.from([2, 3])), 1) t.is(Buffer.from([1, 2, 3]).indexOf(Buffer.from([3])), 2) t.is(Buffer.from([1, 2, 3]).indexOf(Buffer.from([4])), -1) t.is(Buffer.from([1, 2, 3]).indexOf(Buffer.from([1, 3])), -1) t.is(Buffer.from([1, 2, 3]).indexOf(Buffer.from([2, 1])), -1) t.is(Buffer.from([1, 2, 3]).indexOf(Buffer.from([3, 2, 1])), -1) t.is(Buffer.from([1, 2, 2, 3]).indexOf(Buffer.from([2])), 1) }) test('lastIndexOf', (t) => { t.is(Buffer.from([1, 2, 3]).lastIndexOf(1), 0) t.is(Buffer.from([1, 2, 3]).lastIndexOf(2), 1) t.is(Buffer.from([1, 2, 3]).lastIndexOf(3), 2) t.is(Buffer.from([1, 2, 3]).lastIndexOf(4), -1) t.is(Buffer.from([1, 2, 3]).lastIndexOf(Buffer.from([1])), 0) t.is(Buffer.from([1, 2, 3]).lastIndexOf(Buffer.from([1, 2])), 0) t.is(Buffer.from([1, 2, 3]).lastIndexOf(Buffer.from([1, 2, 3])), 0) t.is(Buffer.from([1, 2, 3]).lastIndexOf(Buffer.from([2])), 1) t.is(Buffer.from([1, 2, 3]).lastIndexOf(Buffer.from([2, 3])), 1) t.is(Buffer.from([1, 2, 3]).lastIndexOf(Buffer.from([3])), 2) t.is(Buffer.from([1, 2, 3]).lastIndexOf(Buffer.from([4])), -1) t.is(Buffer.from([1, 2, 3]).lastIndexOf(Buffer.from([1, 3])), -1) t.is(Buffer.from([1, 2, 3]).lastIndexOf(Buffer.from([2, 1])), -1) t.is(Buffer.from([1, 2, 3]).lastIndexOf(Buffer.from([3, 2, 1])), -1) t.is(Buffer.from([1, 2, 2, 3]).lastIndexOf(Buffer.from([2])), 2) }) test('swap16', (t) => { t.alike( Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]).swap16(), Buffer.from([0x2, 0x1, 0x4, 0x3, 0x6, 0x5, 0x8, 0x7]) ) }) test('swap32', (t) => { t.alike( Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]).swap32(), Buffer.from([0x4, 0x3, 0x2, 0x1, 0x8, 0x7, 0x6, 0x5]) ) }) test('swap64', (t) => { t.alike( Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]).swap64(), Buffer.from([0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1]) ) }) test('isBuffer', (t) => { t.ok(Buffer.isBuffer(Buffer.alloc(4))) t.absent(Buffer.isBuffer(new Uint8Array(4))) t.absent(Buffer.isBuffer()) t.absent(Buffer.isBuffer({})) t.absent(Buffer.isBuffer(null)) class MyBuffer extends Buffer {} t.ok(Buffer.isBuffer(new MyBuffer(4))) t.unlike(Buffer, global.Buffer) t.ok(Buffer.isBuffer(global.Buffer.alloc(4))) }) test('atob + btoa', (t) => { t.test('string', function (t) { const encoded = Buffer.btoa('hello world') t.is(encoded, 'aGVsbG8gd29ybGQ=') const decoded = Buffer.atob(encoded) t.is(decoded, 'hello world') }) t.test('string coercion', function (t) { const encoded = Buffer.btoa([1, 2, 3]) t.is(encoded, 'MSwyLDM=') const decoded = Buffer.atob(encoded) t.is(decoded, '1,2,3') }) }) test('toJSON', (t) => { const buffer = Buffer.from([1, 2, 3]) t.alike(buffer.toJSON(), [1, 2, 3]) }) test('transcode', (t) => { t.alike( Buffer.transcode(Buffer.of(0x21, 0x30), 'ascii', 'latin1'), Buffer.of(0x21, 0x30), 'ascii to latin1' ) t.alike( Buffer.transcode(Buffer.of(0x21, 0x30), 'ascii', 'utf8'), Buffer.of(0x21, 0x30), 'ascii to utf8' ) t.alike( Buffer.transcode(Buffer.of(0x21, 0x30), 'ascii', 'utf16le'), Buffer.of(0x21, 0x00, 0x30, 0x00), 'ascii to utf16le' ) t.alike( Buffer.transcode(Buffer.of(0xe7, 0xf8), 'latin1', 'utf8'), Buffer.of(0xc3, 0xa7, 0xc3, 0xb8), 'latin1 to utf8' ) t.alike( Buffer.transcode(Buffer.of(0xe7, 0xf8), 'latin1', 'utf16le'), Buffer.of(0xe7, 0x00, 0xf8, 0x00), 'latin1 to utf16le' ) t.alike( Buffer.transcode(Buffer.of(0xd0, 0x96), 'utf8', 'utf16le'), Buffer.of(0x16, 0x04), 'utf8 to utf16le' ) t.alike( Buffer.transcode(Buffer.of(0x16, 0x04), 'utf16le', 'utf8'), Buffer.of(0xd0, 0x96), 'utf16le to utf8' ) }) test('read and write reject bad offsets', (t) => { const buffer = Buffer.alloc(16) for (const method of ['readUint8', 'readUint16LE', 'readUint32BE', 'readDoubleLE']) { t.exception.all(() => buffer[method](1.5), /RangeError/, `${method} fractional offset`) t.exception.all(() => buffer[method](-1), /RangeError/, `${method} negative offset`) t.exception.all(() => buffer[method](16), /RangeError/, `${method} offset past the end`) } for (const method of ['writeUint8', 'writeUint16LE', 'writeUint32BE', 'writeDoubleLE']) { t.exception.all(() => buffer[method](1, 1.5), /RangeError/, `${method} fractional offset`) t.exception.all(() => buffer[method](1, -1), /RangeError/, `${method} negative offset`) t.exception.all(() => buffer[method](1, 16), /RangeError/, `${method} offset past the end`) } t.exception.all(() => buffer.readUintLE(1.5, 6), /RangeError/, 'readUintLE fractional offset') t.exception.all(() => buffer.readUintLE(11, 6), /RangeError/, 'readUintLE offset past the end') t.exception.all(() => buffer.writeUintLE(1, 11, 6), /RangeError/, 'writeUintLE past the end') t.test('a numeric string is not a number', (t) => { for (let i = 0; i < 16; i++) buffer[i] = i t.exception.all(() => buffer.readUint16LE('1'), /TypeError/) t.exception.all(() => buffer.readUint32LE('1'), /TypeError/) t.exception.all(() => buffer.readUintLE('1', 6), /TypeError/) t.exception.all(() => buffer.writeUint16LE(0xffff, '1'), /TypeError/) t.exception.all(() => buffer.readUint8(null), /TypeError/) t.exception.all(() => buffer.readDoubleLE('1'), /TypeError/) t.alike([...buffer.subarray(0, 4)], [0, 1, 2, 3], 'nothing was written') }) }) test('readInt8', (t) => assertRead(t, { byteSize: 8, signed: false })) test('readUInt8', (t) => assertRead(t, { byteSize: 8, signed: true })) test('readInt16LE', (t) => assertRead(t, { byteSize: 16, signed: true, littleEndian: true })) test('readInt32LE', (t) => assertRead(t, { byteSize: 32, signed: true, littleEndian: true })) test('readBigInt64LE', (t) => assertRead(t, { byteSize: 64, signed: true, littleEndian: true })) test('readInt16BE', (t) => assertRead(t, { byteSize: 16, signed: true, littleEndian: false })) test('readInt32BE', (t) => assertRead(t, { byteSize: 32, signed: true, littleEndian: false })) test('readBigInt64BE', (t) => assertRead(t, { byteSize: 64, signed: true, littleEndian: false })) test('readUInt16LE', (t) => assertRead(t, { byteSize: 16, signed: false, littleEndian: true })) test('readUInt32LE', (t) => assertRead(t, { byteSize: 32, signed: false, littleEndian: true })) test('readBigUInt64LE', (t) => assertRead(t, { byteSize: 64, signed: false, littleEndian: true })) test('readUInt16BE', (t) => assertRead(t, { byteSize: 16, signed: false, littleEndian: false })) test('readUInt32BE', (t) => assertRead(t, { byteSize: 32, signed: false, littleEndian: false })) test('readBigUInt64BE', (t) => assertRead(t, { byteSize: 64, signed: false, littleEndian: false })) test('readInt32BE - top bit set', (t) => { const buffer = Buffer.from([0xff, 0xff, 0xff, 0xff]) let actual = buffer.readInt32BE() let expected = -1 t.is(actual, expected) const bufferMax = Buffer.from([0x80, 0, 0, 0x1]) actual = bufferMax.readInt32BE() expected = -1 * 0x7fffffff t.is(actual, expected) const bufferSub1 = Buffer.from([0x80, 0, 0, 0]) actual = bufferSub1.readInt32BE() expected = -1 * 0x80000000 t.is(actual, expected) }) test('readFloatLE', (t) => { const buffer = Buffer.from([1, 2, 3, 4]) const actual = buffer.readFloatLE() const expected = 1.539989614439558e-36 t.is(actual, expected) }) test('readFloatBE', (t) => { const buffer = Buffer.from([4, 3, 2, 1]) const actual = buffer.readFloatBE() const expected = 1.539989614439558e-36 t.is(actual, expected) }) test('readDoubleLE', (t) => { const buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]) const actual = buffer.readDoubleLE() const expected = 5.447603722011605e-270 t.is(actual, expected) }) test('readDoubleBE', (t) => { const buffer = Buffer.from([8, 7, 6, 5, 4, 3, 2, 1]) const actual = buffer.readDoubleBE() const expected = 5.447603722011605e-270 t.is(actual, expected) }) test('writeInt8', (t) => assertWrite(t, { byteSize: 8, signed: false })) test('writeUInt8', (t) => assertWrite(t, { byteSize: 8, signed: true })) test('writeInt16LE', (t) => assertWrite(t, { byteSize: 16, signed: true, littleEndian: true })) test('writeInt32LE', (t) => assertWrite(t, { byteSize: 32, signed: true, littleEndian: true })) test('writeBigInt64LE', (t) => assertWrite(t, { byteSize: 64, signed: true, littleEndian: true })) test('writeInt16BE', (t) => assertWrite(t, { byteSize: 16, signed: true, littleEndian: false })) test('writeInt32BE', (t) => assertWrite(t, { byteSize: 32, signed: true, littleEndian: false })) test('writeBigInt64BE', (t) => assertWrite(t, { byteSize: 64, signed: true, littleEndian: false })) test('writeUInt16LE', (t) => assertWrite(t, { byteSize: 16, signed: false, littleEndian: true })) test('writeUInt32LE', (t) => assertWrite(t, { byteSize: 32, signed: false, littleEndian: true })) test('writeBigUInt64LE', (t) => assertWrite(t, { byteSize: 64, signed: false, littleEndian: true })) test('writeUInt16BE', (t) => assertWrite(t, { byteSize: 16, signed: false, littleEndian: false })) test('writeUInt32BE', (t) => assertWrite(t, { byteSize: 32, signed: false, littleEndian: false })) test('writeBigUInt64BE', (t) => assertWrite(t, { byteSize: 64, signed: false, littleEndian: false })) test('writeInt32BE - top bit set', (t) => { let value = -1 const expectedBuffer = Buffer.from('ffffffff', 'hex') const buffer = Buffer.alloc(4) let bufferOffset = buffer.writeInt32BE(value, 0) t.is(bufferOffset, 4) t.alike(buffer, expectedBuffer) const expectedMax = Buffer.from('80000000', 'hex') value = -1 * 0x80000000 bufferOffset = buffer.writeInt32BE(value, 0) t.is(bufferOffset, 4) t.alike(buffer, expectedMax) const expectedMaxSub1 = Buffer.from('80000001', 'hex') value = -1 * 0x7fffffff bufferOffset = buffer.writeInt32BE(value, 0) t.is(bufferOffset, 4) t.alike(buffer, expectedMaxSub1) }) test('writeFloatLE', (t) => { const value = 0xcafebabe const expectedBuffer = Buffer.from('bbfe4a4f', 'hex') const buffer = Buffer.alloc(4) const bufferOffset = buffer.writeFloatLE(value, 0) t.is(bufferOffset, 4) t.alike(buffer, expectedBuffer) }) test('writeFloatBE', (t) => { const value = 0xcafebabe const expectedBuffer = Buffer.from('4f4afebb', 'hex') const buffer = Buffer.alloc(4) const bufferOffset = buffer.writeFloatBE(value, 0) t.is(bufferOffset, 4) t.alike(buffer, expectedBuffer) }) test('writeDoubleLE', (t) => { const value = 123.456 const expectedBuffer = Buffer.from('77be9f1a2fdd5e40', 'hex') const buffer = Buffer.alloc(8) const bufferOffset = buffer.writeDoubleLE(value, 0) t.is(bufferOffset, 8) t.alike(buffer, expectedBuffer) }) test('writeDoubleBE', (t) => { const value = 123.456 const expectedBuffer = Buffer.from('405edd2f1a9fbe77', 'hex') const buffer = Buffer.alloc(8) const bufferOffset = buffer.writeDoubleBE(value, 0) t.is(bufferOffset, 8) t.alike(buffer, expectedBuffer) }) test('writeIntBE + readIntBE', (t) => { const buf = Buffer.alloc(6) buf.writeIntBE(-0x123456, 0, 3) t.is(buf.readIntBE(0, 3), -0x123456) buf.writeIntBE(-5000000000, 0, 5) t.is(buf.readIntBE(0, 5), -5000000000) buf.writeIntBE(-1234567890123, 0, 6) t.is(buf.readIntBE(0, 6), -1234567890123) }) test('writeIntLE + readIntLE', (t) => { const buf = Buffer.alloc(6) buf.writeIntLE(-0x654321, 0, 3) t.is(buf.readIntLE(0, 3), -0x654321) buf.writeIntLE(-4000000001, 0, 5) t.is(buf.readIntLE(0, 5), -4000000001) buf.writeIntLE(-987654321987, 0, 6) t.is(buf.readIntLE(0, 6), -987654321987) }) test('writeUintBE + readUintBE', (t) => { const buf = Buffer.alloc(6) buf.writeUintBE(0xabcdef, 0, 3) t.is(buf.readUintBE(0, 3), 0xabcdef) buf.writeUintBE(0x123456789a, 0, 5) t.is(buf.readUintBE(0, 5), 0x123456789a) buf.writeUintBE(0xfedcba987654, 0, 6) t.is(buf.readUintBE(0, 6), 0xfedcba987654) }) test('writeUintLE + readUintLE', (t) => { const buf = Buffer.alloc(6) buf.writeUintLE(0xabcdef, 0, 3) t.is(buf.readUintLE(0, 3), 0xabcdef) buf.writeUintLE(0x9876543210, 0, 5) t.is(buf.readUintLE(0, 5), 0x9876543210) buf.writeUintLE(0x123456789abc, 0, 6) t.is(buf.readUintLE(0, 6), 0x123456789abc) }) function inferMethodName({ prefix, byteSize, signed, littleEndian }) { const b = byteSize === 64 ? 'Big' : '' const s = signed ? '' : 'U' const e = byteSize === 8 ? '' : littleEndian ? 'LE' : 'BE' return prefix + b + s + 'Int' + byteSize + e } function assertRead(t, { byteSize, signed, littleEndian }) { const method = inferMethodName({ prefix: 'read', byteSize, signed, littleEndian }) t.test('endianness', (t) => { const LE = [0xff, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12] const BE = [0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff] const buffer = Buffer.from(littleEndian ? LE : BE) const offset = littleEndian ? buffer.length - byteSize / 8 : 0 const actual = buffer[method](offset).toString(16) const expected = '123456789abcdeff'.slice(0, byteSize / 4) t.is(actual, expected) }) t.test('signedness', (t) => { const buffer = Buffer.from([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) const actual = buffer[method]() const expectedUnsigned = byteSize === 64 ? 2n ** 64n - 1n : 2 ** byteSize - 1 const expectedSigned = byteSize === 64 ? -1n : -1 t.is(actual, signed ? expectedSigned : expectedUnsigned) }) } function assertWrite(t, { byteSize, signed, littleEndian }) { const method = inferMethodName({ prefix: 'write', byteSize, signed, littleEndian }) const LE = [0xde, 0xadde, 0xefbeadde, 0xefbeaddeefbeadden] const BE = [0xde, 0xdead, 0xdeadbeef, 0xdeadbeefdeadbeefn] const index = Math.log2(byteSize) - 3 const bufferInput = littleEndian ? LE[index] : BE[index] const buffer = Buffer.alloc(byteSize / 8) const bufferOffset = buffer[method](bufferInput, 0) const expected = 'deadbeefdeadbeef'.slice(0, byteSize / 4) const expectedBuffer = Buffer.from(expected, 'hex') t.is(bufferOffset, byteSize / 8) t.alike(buffer, expectedBuffer) }