michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /* michael@0: * Many of these tests taken from Joyent's Node michael@0: * https://github.com/joyent/node/blob/master/test/simple/test-buffer.js michael@0: */ michael@0: michael@0: // Copyright Joyent, Inc. and other Node contributors. michael@0: // michael@0: // Permission is hereby granted, free of charge, to any person obtaining a michael@0: // copy of this software and associated documentation files (the michael@0: // "Software"), to deal in the Software without restriction, including michael@0: // without limitation the rights to use, copy, modify, merge, publish, michael@0: // distribute, sublicense, and/or sell copies of the Software, and to permit michael@0: // persons to whom the Software is furnished to do so, subject to the michael@0: // following conditions: michael@0: // michael@0: // The above copyright notice and this permission notice shall be included michael@0: // in all copies or substantial portions of the Software. michael@0: // michael@0: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS michael@0: // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF michael@0: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN michael@0: // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, michael@0: // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR michael@0: // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE michael@0: // USE OR OTHER DEALINGS IN THE SOFTWARE. michael@0: michael@0: const { Buffer, TextEncoder, TextDecoder } = require('sdk/io/buffer'); michael@0: const { safeMerge } = require('sdk/util/object'); michael@0: michael@0: const ENCODINGS = ['utf-8', 'utf-16le', 'utf-16be']; michael@0: michael@0: exports.testBufferMain = function (assert) { michael@0: let b = Buffer('abcdef'); michael@0: michael@0: // try to create 0-length buffers michael@0: new Buffer(''); michael@0: new Buffer(0); michael@0: // test encodings supported by node; michael@0: // this is different than what node supports, details michael@0: // in buffer.js michael@0: ENCODINGS.forEach(enc => { michael@0: new Buffer('', enc); michael@0: assert.pass('Creating a buffer with ' + enc + ' does not throw'); michael@0: }); michael@0: michael@0: ENCODINGS.forEach(function(encoding) { michael@0: // Does not work with utf8 michael@0: if (encoding === 'utf-8') return; michael@0: var b = new Buffer(10); michael@0: b.write('あいうえお', encoding); michael@0: assert.equal(b.toString(encoding), 'あいうえお', michael@0: 'encode and decodes buffer with ' + encoding); michael@0: }); michael@0: michael@0: // invalid encoding for Buffer.toString michael@0: assert.throws(() => { michael@0: b.toString('invalid'); michael@0: }, TypeError, 'invalid encoding for Buffer.toString'); michael@0: michael@0: // try to toString() a 0-length slice of a buffer, both within and without the michael@0: // valid buffer range michael@0: assert.equal(new Buffer('abc').toString('utf8', 0, 0), '', michael@0: 'toString 0-length buffer, valid range'); michael@0: assert.equal(new Buffer('abc').toString('utf8', -100, -100), '', michael@0: 'toString 0-length buffer, invalid range'); michael@0: assert.equal(new Buffer('abc').toString('utf8', 100, 100), '', michael@0: 'toString 0-length buffer, invalid range'); michael@0: michael@0: // try toString() with a object as a encoding michael@0: assert.equal(new Buffer('abc').toString({toString: function() { michael@0: return 'utf8'; michael@0: }}), 'abc', 'toString with object as an encoding'); michael@0: michael@0: // test for buffer overrun michael@0: var buf = new Buffer([0, 0, 0, 0, 0]); // length: 5 michael@0: var sub = buf.slice(0, 4); // length: 4 michael@0: var written = sub.write('12345', 'utf8'); michael@0: assert.equal(written, 4, 'correct bytes written in slice'); michael@0: assert.equal(buf[4], 0, 'correct origin buffer value'); michael@0: michael@0: // Check for fractional length args, junk length args, etc. michael@0: // https://github.com/joyent/node/issues/1758 michael@0: Buffer(3.3).toString(); // throws bad argument error in commit 43cb4ec michael@0: assert.equal(Buffer(-1).length, 0); michael@0: assert.equal(Buffer(NaN).length, 0); michael@0: assert.equal(Buffer(3.3).length, 3); michael@0: assert.equal(Buffer({length: 3.3}).length, 3); michael@0: assert.equal(Buffer({length: 'BAM'}).length, 0); michael@0: michael@0: // Make sure that strings are not coerced to numbers. michael@0: assert.equal(Buffer('99').length, 2); michael@0: assert.equal(Buffer('13.37').length, 5); michael@0: }; michael@0: michael@0: exports.testIsEncoding = function (assert) { michael@0: ENCODINGS.map(encoding => { michael@0: assert.ok(Buffer.isEncoding(encoding), michael@0: 'Buffer.isEncoding ' + encoding + ' truthy'); michael@0: }); michael@0: ['not-encoding', undefined, null, 100, {}].map(encoding => { michael@0: assert.ok(!Buffer.isEncoding(encoding), michael@0: 'Buffer.isEncoding ' + encoding + ' falsy'); michael@0: }); michael@0: }; michael@0: michael@0: exports.testBufferCopy = function (assert) { michael@0: // counter to ensure unique value is always copied michael@0: var cntr = 0; michael@0: var b = Buffer(1024); // safe constructor michael@0: michael@0: assert.strictEqual(1024, b.length); michael@0: b[0] = -1; michael@0: assert.strictEqual(b[0], 255); michael@0: michael@0: var shimArray = []; michael@0: for (var i = 0; i < 1024; i++) { michael@0: b[i] = i % 256; michael@0: shimArray[i] = i % 256; michael@0: } michael@0: michael@0: compareBuffers(assert, b, shimArray, 'array notation'); michael@0: michael@0: var c = new Buffer(512); michael@0: assert.strictEqual(512, c.length); michael@0: // copy 512 bytes, from 0 to 512. michael@0: b.fill(++cntr); michael@0: c.fill(++cntr); michael@0: var copied = b.copy(c, 0, 0, 512); michael@0: assert.strictEqual(512, copied, michael@0: 'copied ' + copied + ' bytes from b into c'); michael@0: michael@0: compareBuffers(assert, b, c, 'copied to other buffer'); michael@0: michael@0: // copy c into b, without specifying sourceEnd michael@0: b.fill(++cntr); michael@0: c.fill(++cntr); michael@0: var copied = c.copy(b, 0, 0); michael@0: assert.strictEqual(c.length, copied, michael@0: 'copied ' + copied + ' bytes from c into b w/o sourceEnd'); michael@0: compareBuffers(assert, b, c, michael@0: 'copied to other buffer without specifying sourceEnd'); michael@0: michael@0: // copy c into b, without specifying sourceStart michael@0: b.fill(++cntr); michael@0: c.fill(++cntr); michael@0: var copied = c.copy(b, 0); michael@0: assert.strictEqual(c.length, copied, michael@0: 'copied ' + copied + ' bytes from c into b w/o sourceStart'); michael@0: compareBuffers(assert, b, c, michael@0: 'copied to other buffer without specifying sourceStart'); michael@0: michael@0: // copy longer buffer b to shorter c without targetStart michael@0: b.fill(++cntr); michael@0: c.fill(++cntr); michael@0: michael@0: var copied = b.copy(c); michael@0: assert.strictEqual(c.length, copied, michael@0: 'copied ' + copied + ' bytes from b into c w/o targetStart'); michael@0: compareBuffers(assert, b, c, michael@0: 'copy long buffer to shorter buffer without targetStart'); michael@0: michael@0: // copy starting near end of b to c michael@0: b.fill(++cntr); michael@0: c.fill(++cntr); michael@0: var copied = b.copy(c, 0, b.length - Math.floor(c.length / 2)); michael@0: assert.strictEqual(Math.floor(c.length / 2), copied, michael@0: 'copied ' + copied + ' bytes from end of b into beg. of c'); michael@0: michael@0: let successStatus = true; michael@0: for (var i = 0; i < Math.floor(c.length / 2); i++) { michael@0: if (b[b.length - Math.floor(c.length / 2) + i] !== c[i]) michael@0: successStatus = false; michael@0: } michael@0: michael@0: for (var i = Math.floor(c.length /2) + 1; i < c.length; i++) { michael@0: if (c[c.length-1] !== c[i]) michael@0: successStatus = false; michael@0: } michael@0: assert.ok(successStatus, michael@0: 'Copied bytes from end of large buffer into beginning of small buffer'); michael@0: michael@0: // try to copy 513 bytes, and check we don't overrun c michael@0: b.fill(++cntr); michael@0: c.fill(++cntr); michael@0: var copied = b.copy(c, 0, 0, 513); michael@0: assert.strictEqual(c.length, copied, michael@0: 'copied ' + copied + ' bytes from b trying to overrun c'); michael@0: compareBuffers(assert, b, c, michael@0: 'copying to buffer that would overflow'); michael@0: michael@0: // copy 768 bytes from b into b michael@0: b.fill(++cntr); michael@0: b.fill(++cntr, 256); michael@0: var copied = b.copy(b, 0, 256, 1024); michael@0: assert.strictEqual(768, copied, michael@0: 'copied ' + copied + ' bytes from b into b'); michael@0: michael@0: compareBuffers(assert, b, shimArray.map(()=>cntr), michael@0: 'copy partial buffer to itself'); michael@0: michael@0: // copy string longer than buffer length (failure will segfault) michael@0: var bb = new Buffer(10); michael@0: bb.fill('hello crazy world'); michael@0: michael@0: // copy throws at negative sourceStart michael@0: assert.throws(function() { michael@0: Buffer(5).copy(Buffer(5), 0, -1); michael@0: }, RangeError, 'buffer copy throws at negative sourceStart'); michael@0: michael@0: // check sourceEnd resets to targetEnd if former is greater than the latter michael@0: b.fill(++cntr); michael@0: c.fill(++cntr); michael@0: var copied = b.copy(c, 0, 0, 1025); michael@0: assert.strictEqual(copied, c.length, michael@0: 'copied ' + copied + ' bytes from b into c'); michael@0: compareBuffers(assert, b, c, 'copying should reset sourceEnd if targetEnd if sourceEnd > targetEnd'); michael@0: michael@0: // throw with negative sourceEnd michael@0: assert.throws(function() { michael@0: b.copy(c, 0, 0, -1); michael@0: }, RangeError, 'buffer copy throws at negative sourceEnd'); michael@0: michael@0: // when sourceStart is greater than sourceEnd, zero copied michael@0: assert.equal(b.copy(c, 0, 100, 10), 0); michael@0: michael@0: // when targetStart > targetLength, zero copied michael@0: assert.equal(b.copy(c, 512, 0, 10), 0); michael@0: michael@0: // try to copy 0 bytes worth of data into an empty buffer michael@0: b.copy(new Buffer(0), 0, 0, 0); michael@0: michael@0: // try to copy 0 bytes past the end of the target buffer michael@0: b.copy(new Buffer(0), 1, 1, 1); michael@0: b.copy(new Buffer(1), 1, 1, 1); michael@0: michael@0: // try to copy 0 bytes from past the end of the source buffer michael@0: b.copy(new Buffer(1), 0, 2048, 2048); michael@0: }; michael@0: michael@0: exports.testBufferWrite = function (assert) { michael@0: let b = Buffer(1024); michael@0: b.fill(0); michael@0: michael@0: assert.throws(() => { michael@0: b.write('test string', 0, 5, 'invalid'); michael@0: }, TypeError, 'invalid encoding with buffer write throws'); michael@0: // try to write a 0-length string beyond the end of b michael@0: assert.throws(function() { michael@0: b.write('', 2048); michael@0: }, RangeError, 'writing a 0-length string beyond buffer throws'); michael@0: // throw when writing to negative offset michael@0: assert.throws(function() { michael@0: b.write('a', -1); michael@0: }, RangeError, 'writing negative offset on buffer throws'); michael@0: michael@0: // throw when writing past bounds from the pool michael@0: assert.throws(function() { michael@0: b.write('a', 2048); michael@0: }, RangeError, 'writing past buffer bounds from pool throws'); michael@0: michael@0: // testing for smart defaults and ability to pass string values as offset michael@0: michael@0: // previous write API was the following: michael@0: // write(string, encoding, offset, length) michael@0: // this is planned on being removed in node v0.13, michael@0: // we will not support it michael@0: var writeTest = new Buffer('abcdes'); michael@0: writeTest.write('n', 'utf8'); michael@0: // writeTest.write('o', 'utf8', '1'); michael@0: writeTest.write('d', '2', 'utf8'); michael@0: writeTest.write('e', 3, 'utf8'); michael@0: // writeTest.write('j', 'utf8', 4); michael@0: assert.equal(writeTest.toString(), 'nbdees', michael@0: 'buffer write API alternative syntax works'); michael@0: }; michael@0: michael@0: exports.testBufferWriteEncoding = function (assert) { michael@0: michael@0: // Node #1210 Test UTF-8 string includes null character michael@0: var buf = new Buffer('\0'); michael@0: assert.equal(buf.length, 1); michael@0: buf = new Buffer('\0\0'); michael@0: assert.equal(buf.length, 2); michael@0: michael@0: buf = new Buffer(2); michael@0: var written = buf.write(''); // 0byte michael@0: assert.equal(written, 0); michael@0: written = buf.write('\0'); // 1byte (v8 adds null terminator) michael@0: assert.equal(written, 1); michael@0: written = buf.write('a\0'); // 1byte * 2 michael@0: assert.equal(written, 2); michael@0: // TODO, these tests write 0, possibly due to character encoding michael@0: /* michael@0: written = buf.write('あ'); // 3bytes michael@0: assert.equal(written, 0); michael@0: written = buf.write('\0あ'); // 1byte + 3bytes michael@0: assert.equal(written, 1); michael@0: */ michael@0: written = buf.write('\0\0あ'); // 1byte * 2 + 3bytes michael@0: buf = new Buffer(10); michael@0: written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator) michael@0: assert.equal(written, 9); michael@0: written = buf.write('あいう\0'); // 3bytes * 3 + 1byte michael@0: assert.equal(written, 10); michael@0: }; michael@0: michael@0: exports.testBufferWriteWithMaxLength = function (assert) { michael@0: // Node #243 Test write() with maxLength michael@0: var buf = new Buffer(4); michael@0: buf.fill(0xFF); michael@0: var written = buf.write('abcd', 1, 2, 'utf8'); michael@0: assert.equal(written, 2); michael@0: assert.equal(buf[0], 0xFF); michael@0: assert.equal(buf[1], 0x61); michael@0: assert.equal(buf[2], 0x62); michael@0: assert.equal(buf[3], 0xFF); michael@0: michael@0: buf.fill(0xFF); michael@0: written = buf.write('abcd', 1, 4); michael@0: assert.equal(written, 3); michael@0: assert.equal(buf[0], 0xFF); michael@0: assert.equal(buf[1], 0x61); michael@0: assert.equal(buf[2], 0x62); michael@0: assert.equal(buf[3], 0x63); michael@0: michael@0: buf.fill(0xFF); michael@0: // Ignore legacy API michael@0: /* michael@0: written = buf.write('abcd', 'utf8', 1, 2); // legacy style michael@0: console.log(buf); michael@0: assert.equal(written, 2); michael@0: assert.equal(buf[0], 0xFF); michael@0: assert.equal(buf[1], 0x61); michael@0: assert.equal(buf[2], 0x62); michael@0: assert.equal(buf[3], 0xFF); michael@0: */ michael@0: }; michael@0: michael@0: exports.testBufferSlice = function (assert) { michael@0: var asciiString = 'hello world'; michael@0: var offset = 100; michael@0: var b = Buffer(1024); michael@0: b.fill(0); michael@0: michael@0: for (var i = 0; i < asciiString.length; i++) { michael@0: b[i] = asciiString.charCodeAt(i); michael@0: } michael@0: var asciiSlice = b.toString('utf8', 0, asciiString.length); michael@0: assert.equal(asciiString, asciiSlice); michael@0: michael@0: var written = b.write(asciiString, offset, 'utf8'); michael@0: assert.equal(asciiString.length, written); michael@0: asciiSlice = b.toString('utf8', offset, offset + asciiString.length); michael@0: assert.equal(asciiString, asciiSlice); michael@0: michael@0: var sliceA = b.slice(offset, offset + asciiString.length); michael@0: var sliceB = b.slice(offset, offset + asciiString.length); michael@0: compareBuffers(assert, sliceA, sliceB, michael@0: 'slicing is idempotent'); michael@0: michael@0: let sliceTest = true; michael@0: for (var j = 0; j < 100; j++) { michael@0: var slice = b.slice(100, 150); michael@0: if (50 !== slice.length) michael@0: sliceTest = false; michael@0: for (var i = 0; i < 50; i++) { michael@0: if (b[100 + i] !== slice[i]) michael@0: sliceTest = false; michael@0: } michael@0: } michael@0: assert.ok(sliceTest, 'massive slice runs do not affect buffer'); michael@0: michael@0: // Single argument slice michael@0: let testBuf = new Buffer('abcde'); michael@0: assert.equal('bcde', testBuf.slice(1).toString(), 'single argument slice'); michael@0: michael@0: // slice(0,0).length === 0 michael@0: assert.equal(0, Buffer('hello').slice(0, 0).length, 'slice(0,0) === 0'); michael@0: michael@0: var buf = new Buffer('0123456789'); michael@0: assert.equal(buf.slice(-10, 10), '0123456789', 'buffer slice range correct'); michael@0: assert.equal(buf.slice(-20, 10), '0123456789', 'buffer slice range correct'); michael@0: assert.equal(buf.slice(-20, -10), '', 'buffer slice range correct'); michael@0: assert.equal(buf.slice(0, -1), '012345678', 'buffer slice range correct'); michael@0: assert.equal(buf.slice(2, -2), '234567', 'buffer slice range correct'); michael@0: assert.equal(buf.slice(0, 65536), '0123456789', 'buffer slice range correct'); michael@0: assert.equal(buf.slice(65536, 0), '', 'buffer slice range correct'); michael@0: michael@0: let sliceTest = true; michael@0: for (var i = 0, s = buf.toString(); i < buf.length; ++i) { michael@0: if (buf.slice(-i) != s.slice(-i)) sliceTest = false; michael@0: if (buf.slice(0, -i) != s.slice(0, -i)) sliceTest = false; michael@0: } michael@0: assert.ok(sliceTest, 'buffer.slice should be consistent'); michael@0: michael@0: // Make sure modifying a sliced buffer, affects original and vice versa michael@0: b.fill(0); michael@0: let sliced = b.slice(0, 10); michael@0: let babyslice = sliced.slice(0, 5); michael@0: michael@0: for (let i = 0; i < sliced.length; i++) michael@0: sliced[i] = 'jetpack'.charAt(i); michael@0: michael@0: compareBuffers(assert, b, sliced, michael@0: 'modifying sliced buffer affects original'); michael@0: michael@0: compareBuffers(assert, b, babyslice, michael@0: 'modifying sliced buffer affects child-sliced buffer'); michael@0: michael@0: for (let i = 0; i < sliced.length; i++) michael@0: b[i] = 'odinmonkey'.charAt(i); michael@0: michael@0: compareBuffers(assert, b, sliced, michael@0: 'modifying original buffer affects sliced'); michael@0: michael@0: compareBuffers(assert, b, babyslice, michael@0: 'modifying original buffer affects grandchild sliced buffer'); michael@0: }; michael@0: michael@0: exports.testSlicingParents = function (assert) { michael@0: let root = Buffer(5); michael@0: let child = root.slice(0, 4); michael@0: let grandchild = child.slice(0, 3); michael@0: michael@0: assert.equal(root.parent, undefined, 'a new buffer should not have a parent'); michael@0: michael@0: // make sure a zero length slice doesn't set the .parent attribute michael@0: assert.equal(root.slice(0,0).parent, undefined, michael@0: '0-length slice should not have a parent'); michael@0: michael@0: assert.equal(child.parent, root, michael@0: 'a valid slice\'s parent should be the original buffer (child)'); michael@0: michael@0: assert.equal(grandchild.parent, root, michael@0: 'a valid slice\'s parent should be the original buffer (grandchild)'); michael@0: }; michael@0: michael@0: exports.testIsBuffer = function (assert) { michael@0: let buffer = new Buffer('content', 'utf8'); michael@0: assert.ok(Buffer.isBuffer(buffer), 'isBuffer truthy on buffers'); michael@0: assert.ok(!Buffer.isBuffer({}), 'isBuffer falsy on objects'); michael@0: assert.ok(!Buffer.isBuffer(new Uint8Array()), michael@0: 'isBuffer falsy on Uint8Array'); michael@0: assert.ok(Buffer.isBuffer(buffer.slice(0)), 'Buffer#slice should be a new buffer'); michael@0: }; michael@0: michael@0: exports.testBufferConcat = function (assert) { michael@0: let zero = []; michael@0: let one = [ new Buffer('asdf') ]; michael@0: let long = []; michael@0: for (let i = 0; i < 10; i++) long.push(new Buffer('asdf')); michael@0: michael@0: let flatZero = Buffer.concat(zero); michael@0: let flatOne = Buffer.concat(one); michael@0: let flatLong = Buffer.concat(long); michael@0: let flatLongLen = Buffer.concat(long, 40); michael@0: michael@0: assert.equal(flatZero.length, 0); michael@0: assert.equal(flatOne.toString(), 'asdf'); michael@0: assert.equal(flatOne, one[0]); michael@0: assert.ok(flatLong.toString(), (new Array(10+1).join('asdf'))); michael@0: assert.equal(flatLongLen.toString(), (new Array(10+1).join('asdf'))); michael@0: }; michael@0: michael@0: exports.testBufferByteLength = function (assert) { michael@0: let str = '\u00bd + \u00bc = \u00be'; michael@0: assert.equal(Buffer.byteLength(str), 12, michael@0: 'correct byteLength of string'); michael@0: michael@0: assert.equal(14, Buffer.byteLength('Il était tué')); michael@0: assert.equal(14, Buffer.byteLength('Il était tué', 'utf8')); michael@0: // We do not currently support these encodings michael@0: /* michael@0: ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) { michael@0: assert.equal(24, Buffer.byteLength('Il était tué', encoding)); michael@0: }); michael@0: assert.equal(12, Buffer.byteLength('Il était tué', 'ascii')); michael@0: assert.equal(12, Buffer.byteLength('Il était tué', 'binary')); michael@0: */ michael@0: }; michael@0: michael@0: exports.testTextEncoderDecoder = function (assert) { michael@0: assert.ok(TextEncoder, 'TextEncoder exists'); michael@0: assert.ok(TextDecoder, 'TextDecoder exists'); michael@0: }; michael@0: michael@0: exports.testOverflowedBuffers = function (assert) { michael@0: assert.throws(function() { michael@0: new Buffer(0xFFFFFFFF); michael@0: }, RangeError, 'correctly throws buffer overflow'); michael@0: michael@0: assert.throws(function() { michael@0: new Buffer(0xFFFFFFFFF); michael@0: }, RangeError, 'correctly throws buffer overflow'); michael@0: michael@0: assert.throws(function() { michael@0: var buf = new Buffer(8); michael@0: buf.readFloatLE(0xffffffff); michael@0: }, RangeError, 'correctly throws buffer overflow with readFloatLE'); michael@0: michael@0: assert.throws(function() { michael@0: var buf = new Buffer(8); michael@0: buf.writeFloatLE(0.0, 0xffffffff); michael@0: }, RangeError, 'correctly throws buffer overflow with writeFloatLE'); michael@0: michael@0: //ensure negative values can't get past offset michael@0: assert.throws(function() { michael@0: var buf = new Buffer(8); michael@0: buf.readFloatLE(-1); michael@0: }, RangeError, 'correctly throws with readFloatLE negative values'); michael@0: michael@0: assert.throws(function() { michael@0: var buf = new Buffer(8); michael@0: buf.writeFloatLE(0.0, -1); michael@0: }, RangeError, 'correctly throws with writeFloatLE with negative values'); michael@0: michael@0: assert.throws(function() { michael@0: var buf = new Buffer(8); michael@0: buf.readFloatLE(-1); michael@0: }, RangeError, 'correctly throws with readFloatLE with negative values'); michael@0: }; michael@0: michael@0: exports.testReadWriteDataTypeErrors = function (assert) { michael@0: var buf = new Buffer(0); michael@0: assert.throws(function() { buf.readUInt8(0); }, RangeError, michael@0: 'readUInt8(0) throws'); michael@0: assert.throws(function() { buf.readInt8(0); }, RangeError, michael@0: 'readInt8(0) throws'); michael@0: michael@0: [16, 32].forEach(function(bits) { michael@0: var buf = new Buffer(bits / 8 - 1); michael@0: assert.throws(function() { buf['readUInt' + bits + 'BE'](0); }, michael@0: RangeError, michael@0: 'readUInt' + bits + 'BE'); michael@0: michael@0: assert.throws(function() { buf['readUInt' + bits + 'LE'](0); }, michael@0: RangeError, michael@0: 'readUInt' + bits + 'LE'); michael@0: michael@0: assert.throws(function() { buf['readInt' + bits + 'BE'](0); }, michael@0: RangeError, michael@0: 'readInt' + bits + 'BE()'); michael@0: michael@0: assert.throws(function() { buf['readInt' + bits + 'LE'](0); }, michael@0: RangeError, michael@0: 'readInt' + bits + 'LE()'); michael@0: }); michael@0: }; michael@0: michael@0: safeMerge(exports, require('./buffers/test-write-types')); michael@0: safeMerge(exports, require('./buffers/test-read-types')); michael@0: michael@0: function compareBuffers (assert, buf1, buf2, message) { michael@0: let status = true; michael@0: for (let i = 0; i < Math.min(buf1.length, buf2.length); i++) { michael@0: if (buf1[i] !== buf2[i]) michael@0: status = false; michael@0: } michael@0: assert.ok(status, 'buffer successfully copied: ' + message); michael@0: } michael@0: require('sdk/test').run(exports);