From: Feross Aboukhadijeh Date: Wed, 5 Aug 2015 08:40:33 +0000 (+0200) Subject: test: update tests for iojs 3.0.0 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=3c26158b9b1f3d51cd8afc58659e220f8f748f7e;p=buffer.git test: update tests for iojs 3.0.0 --- diff --git a/bin/download-node-tests.js b/bin/download-node-tests.js index da100d1..2556faa 100755 --- a/bin/download-node-tests.js +++ b/bin/download-node-tests.js @@ -71,20 +71,26 @@ function testfixer (filename) { firstline = false } - // comment out require('common') - line = line.replace(/((var|const) common = require.*)/, 'var common = {};') + // use `var` instead of `const`/`let` + line = line.replace(/(const|let) /g, 'var ') + + // make `require('common')` work + line = line.replace(/(var common = require.*)/g, 'var common = {};') + + // use `Buffer.isBuffer` instead of `instanceof Buffer` + line = line.replace(/buf instanceof Buffer/g, 'Buffer.isBuffer(buf)') // require browser buffer - line = line.replace(/(.*)require\('buffer'\)(.*)/, '$1require(\'../../\')$2') + line = line.replace(/(.*)require\('buffer'\)(.*)/g, '$1require(\'../../\')$2') // smalloc is only used for kMaxLength line = line.replace( - /require\('smalloc'\)/, + /require\('smalloc'\)/g, '{ kMaxLength: process.env.OBJECT_IMPL ? 0x3fffffff : 0x7fffffff }' ) // comment out console logs - line = line.replace(/(.*console\..*)/, '// $1') + line = line.replace(/(.*console\..*)/g, '// $1') // we can't reliably test typed array max-sizes in the browser if (filename === 'test-buffer-big.js') { diff --git a/test/node/test-buffer-arraybuffer.js b/test/node/test-buffer-arraybuffer.js new file mode 100644 index 0000000..b5e014e --- /dev/null +++ b/test/node/test-buffer-arraybuffer.js @@ -0,0 +1,49 @@ +'use strict'; +var Buffer = require('../../').Buffer; +if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; + +var common = {}; +var assert = require('assert'); + +var Buffer = require('../../').Buffer; +var LENGTH = 16; + +var ab = new ArrayBuffer(LENGTH); +var dv = new DataView(ab); +var ui = new Uint8Array(ab); +var buf = new Buffer(ab); + + +assert.ok(Buffer.isBuffer(buf)); +// For backwards compatibility of old .parent property test that if buf is not +// a slice then .parent should be undefined. +assert.equal(buf.parent, undefined); +assert.equal(buf.buffer, ab); +assert.equal(buf.length, ab.byteLength); + + +buf.fill(0xC); +for (var i = 0; i < LENGTH; i++) { + assert.equal(ui[i], 0xC); + ui[i] = 0xF; + assert.equal(buf[i], 0xF); +} + +buf.writeUInt32LE(0xF00, 0); +buf.writeUInt32BE(0xB47, 4); +buf.writeDoubleLE(3.1415, 8); + +assert.equal(dv.getUint32(0, true), 0xF00); +assert.equal(dv.getUint32(4), 0xB47); +assert.equal(dv.getFloat64(8, true), 3.1415); + + +// Now test protecting users from doing stupid things + +assert.throws(function() { + function AB() { } + AB.__proto__ = ArrayBuffer; + AB.prototype.__proto__ = ArrayBuffer.prototype; + new Buffer(new AB()); +}, TypeError); + diff --git a/test/node/test-buffer-big.js b/test/node/test-buffer-big.js deleted file mode 100644 index 777233d..0000000 --- a/test/node/test-buffer-big.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict'; -var Buffer = require('../../').Buffer; -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; -var common = {}; -var assert = require('assert'); - -// The tests below should throw an error, not abort the process... -assert.throws(function() { new Buffer(0x3fffffff + 1); }, RangeError); -// assert.throws(function() { new Int8Array(0x3fffffff + 1); }, RangeError); -// assert.throws(function() { new ArrayBuffer(0x3fffffff + 1); }, RangeError); -// assert.throws(function() { new Float64Array(0x7ffffff + 1); }, RangeError); - diff --git a/test/node/test-buffer-concat.js b/test/node/test-buffer-concat.js index ae04925..d9fb48e 100644 --- a/test/node/test-buffer-concat.js +++ b/test/node/test-buffer-concat.js @@ -16,9 +16,15 @@ var flatLongLen = Buffer.concat(long, 40); assert(flatZero.length === 0); assert(flatOne.toString() === 'asdf'); -assert(flatOne === one[0]); +// A special case where concat used to return the first item, +// if the length is one. This check is to make sure that we don't do that. +assert(flatOne !== one[0]); assert(flatLong.toString() === (new Array(10 + 1).join('asdf'))); assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf'))); +assert.throws(function() { + Buffer.concat([42]); +}, TypeError); + // console.log('ok'); diff --git a/test/node/test-buffer-slice.js b/test/node/test-buffer-slice.js deleted file mode 100644 index 2967195..0000000 --- a/test/node/test-buffer-slice.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; -var Buffer = require('../../').Buffer; -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; -var common = {}; -var assert = require('assert'); - -var Buffer = require('../../').Buffer; - -var buff = new Buffer(Buffer.poolSize + 1); -var slicedBuffer = buff.slice(); -assert.equal(slicedBuffer.parent, - buff, - 'slicedBufffer should have its parent set to the original ' + - ' buffer'); - diff --git a/test/node/test-buffer.js b/test/node/test-buffer.js index 551f883..b895336 100644 --- a/test/node/test-buffer.js +++ b/test/node/test-buffer.js @@ -6,7 +6,6 @@ var assert = require('assert'); var Buffer = require('../../').Buffer; var SlowBuffer = require('../../').SlowBuffer; -var smalloc = { kMaxLength: process.env.OBJECT_IMPL ? 0x3fffffff : 0x7fffffff }; // counter to ensure unique value is always copied var cntr = 0; @@ -331,8 +330,6 @@ assert.equal(b.parent, d.parent); var b = new SlowBuffer(5); var c = b.slice(0, 4); var d = c.slice(0, 2); -assert.equal(b, c.parent); -assert.equal(b, d.parent); // Bug regression test @@ -804,7 +801,9 @@ assert.equal(buf[4], 0); // Check for fractional length args, junk length args, etc. // https://github.com/joyent/node/issues/1758 -Buffer(3.3).toString(); // throws bad argument error in commit 43cb4ec + +// Call .fill() first, stops valgrind warning about uninitialized memory reads. +Buffer(3.3).fill().toString(); // throws bad argument error in commit 43cb4ec assert.equal(Buffer(-1).length, 0); assert.equal(Buffer(NaN).length, 0); assert.equal(Buffer(3.3).length, 3); @@ -1075,10 +1074,6 @@ assert.equal(buf.readInt8(0), -1); // try to slice a zero length Buffer // see https://github.com/joyent/node/issues/5881 SlowBuffer(0).slice(0, 1); - // make sure a zero length slice doesn't set the .parent attribute - assert.equal(Buffer(5).slice(0, 0).parent, undefined); - // and make sure a proper slice does have a parent - assert.ok(typeof Buffer(5).slice(0, 5).parent === 'object'); })(); // Regression test for #5482: should throw but not assert in C++ land. @@ -1105,11 +1100,11 @@ assert.throws(function() { assert.throws(function() { - new Buffer(smalloc.kMaxLength + 1); + new Buffer((-1 >>> 0) + 1); }, RangeError); assert.throws(function() { - new SlowBuffer(smalloc.kMaxLength + 1); + new SlowBuffer((-1 >>> 0) + 1); }, RangeError); if (common.hasCrypto) { @@ -1189,3 +1184,11 @@ assert.throws(function() { Buffer(10).copy(); }); +assert.throws(function() { + new Buffer(); +}, /must start with number, buffer, array or string/); + +assert.throws(function() { + new Buffer(null); +}, /must start with number, buffer, array or string/); +