Beispiel #1
0
exports.testWriteInt8 = helper('writeInt8', function (assert) {
  var buffer = new Buffer(2);

  buffer.writeInt8(0x23, 0);
  buffer.writeInt8(-5, 1);

  assert.equal(0x23, buffer[0]);
  assert.equal(0xfb, buffer[1]);

  /* Make sure we handle truncation correctly */
  assert.throws(function() {
    buffer.writeInt8(0xabc, 0);
  });
  assert.throws(function() {
    buffer.writeInt8(0xabc, 0);
  });

  /* Make sure we handle min/max correctly */
  buffer.writeInt8(0x7f, 0);
  buffer.writeInt8(-0x80, 1);

  assert.equal(0x7f, buffer[0]);
  assert.equal(0x80, buffer[1]);
  assert.throws(function() {
    buffer.writeInt8(0x7f + 1, 0);
  });
  assert.throws(function() {
    buffer.writeInt8(-0x80 - 1, 0);
  });
});
Beispiel #2
0
 ENCODINGS.forEach(function(encoding) {
   // Does not work with utf8
   if (encoding === 'utf-8') return;
   var b = new Buffer(10);
   b.write('あいうえお', encoding);
   assert.equal(b.toString(encoding), 'あいうえお',
     'encode and decodes buffer with ' + encoding);
 });
Beispiel #3
0
exports.testBufferByteLength = function (assert) {
  let str = '\u00bd + \u00bc = \u00be';
  assert.equal(Buffer.byteLength(str), 12,
    'correct byteLength of string');

  assert.equal(14, Buffer.byteLength('Il était tué'));
  assert.equal(14, Buffer.byteLength('Il était tué', 'utf8'));
  // We do not currently support these encodings
  /*
  ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
  assert.equal(24, Buffer.byteLength('Il était tué', encoding));
  });
  assert.equal(12, Buffer.byteLength('Il était tué', 'ascii'));
  assert.equal(12, Buffer.byteLength('Il était tué', 'binary'));
  */
};
Beispiel #4
0
exports.testReadWriteDataTypeErrors = function (assert) {
  var buf = new Buffer(0);
  assert.throws(function() { buf.readUInt8(0); }, RangeError,
    'readUInt8(0) throws');
  assert.throws(function() { buf.readInt8(0); }, RangeError,
    'readInt8(0) throws');

  [16, 32].forEach(function(bits) {
    var buf = new Buffer(bits / 8 - 1);
    assert.throws(function() { buf['readUInt' + bits + 'BE'](0); },
      RangeError,
      'readUInt' + bits + 'BE');

    assert.throws(function() { buf['readUInt' + bits + 'LE'](0); },
      RangeError,
      'readUInt' + bits + 'LE');

    assert.throws(function() { buf['readInt' + bits + 'BE'](0); },
      RangeError,
      'readInt' + bits + 'BE()');

    assert.throws(function() { buf['readInt' + bits + 'LE'](0); },
      RangeError,
      'readInt' + bits + 'LE()');
  });
};
Beispiel #5
0
exports.testBufferConcat = function (assert) {
  let zero = [];
  let one = [ new Buffer('asdf') ];
  let long = [];
  for (let i = 0; i < 10; i++) long.push(new Buffer('asdf'));

  let flatZero = Buffer.concat(zero);
  let flatOne = Buffer.concat(one);
  let flatLong = Buffer.concat(long);
  let flatLongLen = Buffer.concat(long, 40);

  assert.equal(flatZero.length, 0);
  assert.equal(flatOne.toString(), 'asdf');
  assert.equal(flatOne, one[0]);
  assert.equal(flatLong.toString(), (new Array(10+1).join('asdf')));
  assert.equal(flatLongLen.toString(), (new Array(10+1).join('asdf')));
};
Beispiel #6
0
exports.testBufferWriteEncoding = function (assert) {

  // Node #1210 Test UTF-8 string includes null character
  var buf = new Buffer('\0');
  assert.equal(buf.length, 1);
  buf = new Buffer('\0\0');
  assert.equal(buf.length, 2);

  buf = new Buffer(2);
  var written = buf.write(''); // 0byte
  assert.equal(written, 0);
  written = buf.write('\0'); // 1byte (v8 adds null terminator)
  assert.equal(written, 1);
  written = buf.write('a\0'); // 1byte * 2
  assert.equal(written, 2);
  // TODO, these tests write 0, possibly due to character encoding
/*
  written = buf.write('あ'); // 3bytes
  assert.equal(written, 0);
  written = buf.write('\0あ'); // 1byte + 3bytes
  assert.equal(written, 1);
*/
  written = buf.write('\0\0あ'); // 1byte * 2 + 3bytes
  buf = new Buffer(10);
  written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator)
  assert.equal(written, 9);
  written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
  assert.equal(written, 10);
};
Beispiel #7
0
exports.testBufferWriteWithMaxLength = function (assert) {
  // Node #243 Test write() with maxLength
  var buf = new Buffer(4);
  buf.fill(0xFF);
  var written = buf.write('abcd', 1, 2, 'utf8');
  assert.equal(written, 2);
  assert.equal(buf[0], 0xFF);
  assert.equal(buf[1], 0x61);
  assert.equal(buf[2], 0x62);
  assert.equal(buf[3], 0xFF);

  buf.fill(0xFF);
  written = buf.write('abcd', 1, 4);
  assert.equal(written, 3);
  assert.equal(buf[0], 0xFF);
  assert.equal(buf[1], 0x61);
  assert.equal(buf[2], 0x62);
  assert.equal(buf[3], 0x63);

  buf.fill(0xFF);
  // Ignore legacy API
  /*
     written = buf.write('abcd', 'utf8', 1, 2); // legacy style
     console.log(buf);
     assert.equal(written, 2);
     assert.equal(buf[0], 0xFF);
     assert.equal(buf[1], 0x61);
     assert.equal(buf[2], 0x62);
     assert.equal(buf[3], 0xFF);
     */
};
Beispiel #8
0
exports.testIsBuffer = function (assert) {
  let buffer = new Buffer('content', 'utf8');
  assert.ok(Buffer.isBuffer(buffer), 'isBuffer truthy on buffers');
  assert.ok(!Buffer.isBuffer({}), 'isBuffer falsy on objects');
  assert.ok(!Buffer.isBuffer(new Uint8Array()),
    'isBuffer falsy on Uint8Array');
  assert.ok(Buffer.isBuffer(buffer.slice(0)), 'Buffer#slice should be a new buffer');
};
Beispiel #9
0
exports.testBufferWrite = function (assert) {
  let b = Buffer(1024);
  b.fill(0);

  assert.throws(() => {
    b.write('test string', 0, 5, 'invalid');
  }, RangeError, 'invalid encoding with buffer write throws');
  // try to write a 0-length string beyond the end of b
  assert.throws(function() {
    b.write('', 2048);
  }, RangeError, 'writing a 0-length string beyond buffer throws');
  // throw when writing to negative offset
  assert.throws(function() {
    b.write('a', -1);
  }, RangeError, 'writing negative offset on buffer throws');

  // throw when writing past bounds from the pool
  assert.throws(function() {
    b.write('a', 2048);
  }, RangeError, 'writing past buffer bounds from pool throws');

  // testing for smart defaults and ability to pass string values as offset

  // previous write API was the following:
  // write(string, encoding, offset, length)
  // this is planned on being removed in node v0.13,
  // we will not support it
  var writeTest = new Buffer('abcdes');
  writeTest.write('n', 'utf8');
//  writeTest.write('o', 'utf8', '1');
  writeTest.write('d', '2', 'utf8');
  writeTest.write('e', 3, 'utf8');
//  writeTest.write('j', 'utf8', 4);
  assert.equal(writeTest.toString(), 'nbdees',
    'buffer write API alternative syntax works');
};
Beispiel #10
0
exports.testWriteUInt16 = helper('writeUInt16BE/writeUInt16LE', function (assert) {
  var value = 0x2343;
  var data = new Buffer(4);

  data.writeUInt16BE(value, 0);
  assert.equal(0x23, data[0]);
  assert.equal(0x43, data[1]);

  data.writeUInt16BE(value, 1);
  assert.equal(0x23, data[1]);
  assert.equal(0x43, data[2]);

  data.writeUInt16BE(value, 2);
  assert.equal(0x23, data[2]);
  assert.equal(0x43, data[3]);

  data.writeUInt16LE(value, 0);
  assert.equal(0x23, data[1]);
  assert.equal(0x43, data[0]);

  data.writeUInt16LE(value, 1);
  assert.equal(0x23, data[2]);
  assert.equal(0x43, data[1]);

  data.writeUInt16LE(value, 2);
  assert.equal(0x23, data[3]);
  assert.equal(0x43, data[2]);

  value = 0xff80;
  data.writeUInt16LE(value, 0);
  assert.equal(0xff, data[1]);
  assert.equal(0x80, data[0]);

  data.writeUInt16BE(value, 0);
  assert.equal(0xff, data[0]);
  assert.equal(0x80, data[1]);
});
Beispiel #11
0
exports.testWriteUInt32 = helper('writeUInt32BE/writeUInt32LE', function (assert) {
  var data = new Buffer(6);
  var value = 0xe7f90a6d;

  data.writeUInt32BE(value, 0);
  assert.equal(0xe7, data[0]);
  assert.equal(0xf9, data[1]);
  assert.equal(0x0a, data[2]);
  assert.equal(0x6d, data[3]);

  data.writeUInt32BE(value, 1);
  assert.equal(0xe7, data[1]);
  assert.equal(0xf9, data[2]);
  assert.equal(0x0a, data[3]);
  assert.equal(0x6d, data[4]);

  data.writeUInt32BE(value, 2);
  assert.equal(0xe7, data[2]);
  assert.equal(0xf9, data[3]);
  assert.equal(0x0a, data[4]);
  assert.equal(0x6d, data[5]);

  data.writeUInt32LE(value, 0);
  assert.equal(0xe7, data[3]);
  assert.equal(0xf9, data[2]);
  assert.equal(0x0a, data[1]);
  assert.equal(0x6d, data[0]);

  data.writeUInt32LE(value, 1);
  assert.equal(0xe7, data[4]);
  assert.equal(0xf9, data[3]);
  assert.equal(0x0a, data[2]);
  assert.equal(0x6d, data[1]);

  data.writeUInt32LE(value, 2);
  assert.equal(0xe7, data[5]);
  assert.equal(0xf9, data[4]);
  assert.equal(0x0a, data[3]);
  assert.equal(0x6d, data[2]);
});
Beispiel #12
0
exports.testWriteUInt8 = helper('writeUInt8', function (assert) {
  var data = new Buffer(4);

  data.writeUInt8(23, 0);
  data.writeUInt8(23, 1);
  data.writeUInt8(23, 2);
  data.writeUInt8(23, 3);
  assert.equal(23, data[0]);
  assert.equal(23, data[1]);
  assert.equal(23, data[2]);
  assert.equal(23, data[3]);

  data.writeUInt8(23, 0);
  data.writeUInt8(23, 1);
  data.writeUInt8(23, 2);
  data.writeUInt8(23, 3);
  assert.equal(23, data[0]);
  assert.equal(23, data[1]);
  assert.equal(23, data[2]);
  assert.equal(23, data[3]);

  data.writeUInt8(255, 0);
  assert.equal(255, data[0]);

  data.writeUInt8(255, 0);
  assert.equal(255, data[0]);
});
Beispiel #13
0
 assert.throws(function() {
   buffer.writeInt32LE(0x7fffffff + 1, 0);
 });
Beispiel #14
0
 assert.throws(function() {
   buffer.writeInt32LE(-0x80000000 - 1, 0);
 });
Beispiel #15
0
exports.testWriteInt16 = helper('writeInt16LE/writeInt16BE', function (assert) {
  var buffer = new Buffer(6);

  buffer.writeInt16BE(0x0023, 0);
  buffer.writeInt16LE(0x0023, 2);
  assert.equal(0x00, buffer[0]);
  assert.equal(0x23, buffer[1]);
  assert.equal(0x23, buffer[2]);
  assert.equal(0x00, buffer[3]);

  buffer.writeInt16BE(-5, 0);
  buffer.writeInt16LE(-5, 2);
  assert.equal(0xff, buffer[0]);
  assert.equal(0xfb, buffer[1]);
  assert.equal(0xfb, buffer[2]);
  assert.equal(0xff, buffer[3]);

  buffer.writeInt16BE(-1679, 1);
  buffer.writeInt16LE(-1679, 3);
  assert.equal(0xf9, buffer[1]);
  assert.equal(0x71, buffer[2]);
  assert.equal(0x71, buffer[3]);
  assert.equal(0xf9, buffer[4]);

  /* Make sure we handle min/max correctly */
  buffer.writeInt16BE(0x7fff, 0);
  buffer.writeInt16BE(-0x8000, 2);
  assert.equal(0x7f, buffer[0]);
  assert.equal(0xff, buffer[1]);
  assert.equal(0x80, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.throws(function() {
    buffer.writeInt16BE(0x7fff + 1, 0);
  });
  assert.throws(function() {
    buffer.writeInt16BE(-0x8000 - 1, 0);
  });

  buffer.writeInt16LE(0x7fff, 0);
  buffer.writeInt16LE(-0x8000, 2);
  assert.equal(0xff, buffer[0]);
  assert.equal(0x7f, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x80, buffer[3]);
  assert.throws(function() {
    buffer.writeInt16LE(0x7fff + 1, 0);
  });
  assert.throws(function() {
    buffer.writeInt16LE(-0x8000 - 1, 0);
  });
});
Beispiel #16
0
exports.testWriteInt32 = helper('writeInt32BE/writeInt32LE', function (assert) {
  var buffer = new Buffer(8);

  buffer.writeInt32BE(0x23, 0);
  buffer.writeInt32LE(0x23, 4);
  assert.equal(0x00, buffer[0]);
  assert.equal(0x00, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x23, buffer[3]);
  assert.equal(0x23, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x00, buffer[6]);
  assert.equal(0x00, buffer[7]);

  buffer.writeInt32BE(-5, 0);
  buffer.writeInt32LE(-5, 4);
  assert.equal(0xff, buffer[0]);
  assert.equal(0xff, buffer[1]);
  assert.equal(0xff, buffer[2]);
  assert.equal(0xfb, buffer[3]);
  assert.equal(0xfb, buffer[4]);
  assert.equal(0xff, buffer[5]);
  assert.equal(0xff, buffer[6]);
  assert.equal(0xff, buffer[7]);

  buffer.writeInt32BE(-805306713, 0);
  buffer.writeInt32LE(-805306713, 4);
  assert.equal(0xcf, buffer[0]);
  assert.equal(0xff, buffer[1]);
  assert.equal(0xfe, buffer[2]);
  assert.equal(0xa7, buffer[3]);
  assert.equal(0xa7, buffer[4]);
  assert.equal(0xfe, buffer[5]);
  assert.equal(0xff, buffer[6]);
  assert.equal(0xcf, buffer[7]);

  /* Make sure we handle min/max correctly */
  buffer.writeInt32BE(0x7fffffff, 0);
  buffer.writeInt32BE(-0x80000000, 4);
  assert.equal(0x7f, buffer[0]);
  assert.equal(0xff, buffer[1]);
  assert.equal(0xff, buffer[2]);
  assert.equal(0xff, buffer[3]);
  assert.equal(0x80, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x00, buffer[6]);
  assert.equal(0x00, buffer[7]);
  assert.throws(function() {
    buffer.writeInt32BE(0x7fffffff + 1, 0);
  });
  assert.throws(function() {
    buffer.writeInt32BE(-0x80000000 - 1, 0);
  });

  buffer.writeInt32LE(0x7fffffff, 0);
  buffer.writeInt32LE(-0x80000000, 4);
  assert.equal(0xff, buffer[0]);
  assert.equal(0xff, buffer[1]);
  assert.equal(0xff, buffer[2]);
  assert.equal(0x7f, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x00, buffer[6]);
  assert.equal(0x80, buffer[7]);
  assert.throws(function() {
    buffer.writeInt32LE(0x7fffffff + 1, 0);
  });
  assert.throws(function() {
    buffer.writeInt32LE(-0x80000000 - 1, 0);
  });
});
Beispiel #17
0
 assert.throws(function() {
   var buf = new Buffer(8);
   buf.writeFloatLE(0.0, 0xffffffff);
 }, RangeError, 'correctly throws buffer overflow with writeFloatLE');
Beispiel #18
0
 assert.throws(function() {
   buffer.writeInt8(-0x80 - 1, 0);
 });
Beispiel #19
0
exports.testWriteDouble = helper('writeDoubleBE/writeDoubleLE', function (assert) {
  var buffer = new Buffer(16);

  buffer.writeDoubleBE(2.225073858507201e-308, 0);
  buffer.writeDoubleLE(2.225073858507201e-308, 8);
  assert.equal(0x00, buffer[0]);
  assert.equal(0x0f, buffer[1]);
  assert.equal(0xff, buffer[2]);
  assert.equal(0xff, buffer[3]);
  assert.equal(0xff, buffer[4]);
  assert.equal(0xff, buffer[5]);
  assert.equal(0xff, buffer[6]);
  assert.equal(0xff, buffer[7]);
  assert.equal(0xff, buffer[8]);
  assert.equal(0xff, buffer[9]);
  assert.equal(0xff, buffer[10]);
  assert.equal(0xff, buffer[11]);
  assert.equal(0xff, buffer[12]);
  assert.equal(0xff, buffer[13]);
  assert.equal(0x0f, buffer[14]);
  assert.equal(0x00, buffer[15]);

  buffer.writeDoubleBE(1.0000000000000004, 0);
  buffer.writeDoubleLE(1.0000000000000004, 8);
  assert.equal(0x3f, buffer[0]);
  assert.equal(0xf0, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x00, buffer[6]);
  assert.equal(0x02, buffer[7]);
  assert.equal(0x02, buffer[8]);
  assert.equal(0x00, buffer[9]);
  assert.equal(0x00, buffer[10]);
  assert.equal(0x00, buffer[11]);
  assert.equal(0x00, buffer[12]);
  assert.equal(0x00, buffer[13]);
  assert.equal(0xf0, buffer[14]);
  assert.equal(0x3f, buffer[15]);

  buffer.writeDoubleBE(-2, 0);
  buffer.writeDoubleLE(-2, 8);
  assert.equal(0xc0, buffer[0]);
  assert.equal(0x00, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x00, buffer[6]);
  assert.equal(0x00, buffer[7]);
  assert.equal(0x00, buffer[8]);
  assert.equal(0x00, buffer[9]);
  assert.equal(0x00, buffer[10]);
  assert.equal(0x00, buffer[11]);
  assert.equal(0x00, buffer[12]);
  assert.equal(0x00, buffer[13]);
  assert.equal(0x00, buffer[14]);
  assert.equal(0xc0, buffer[15]);

  buffer.writeDoubleBE(1.7976931348623157e+308, 0);
  buffer.writeDoubleLE(1.7976931348623157e+308, 8);
  assert.equal(0x7f, buffer[0]);
  assert.equal(0xef, buffer[1]);
  assert.equal(0xff, buffer[2]);
  assert.equal(0xff, buffer[3]);
  assert.equal(0xff, buffer[4]);
  assert.equal(0xff, buffer[5]);
  assert.equal(0xff, buffer[6]);
  assert.equal(0xff, buffer[7]);
  assert.equal(0xff, buffer[8]);
  assert.equal(0xff, buffer[9]);
  assert.equal(0xff, buffer[10]);
  assert.equal(0xff, buffer[11]);
  assert.equal(0xff, buffer[12]);
  assert.equal(0xff, buffer[13]);
  assert.equal(0xef, buffer[14]);
  assert.equal(0x7f, buffer[15]);

  buffer.writeDoubleBE(0 * -1, 0);
  buffer.writeDoubleLE(0 * -1, 8);
  assert.equal(0x80, buffer[0]);
  assert.equal(0x00, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x00, buffer[6]);
  assert.equal(0x00, buffer[7]);
  assert.equal(0x00, buffer[8]);
  assert.equal(0x00, buffer[9]);
  assert.equal(0x00, buffer[10]);
  assert.equal(0x00, buffer[11]);
  assert.equal(0x00, buffer[12]);
  assert.equal(0x00, buffer[13]);
  assert.equal(0x00, buffer[14]);
  assert.equal(0x80, buffer[15]);

  buffer.writeDoubleBE(Infinity, 0);
  buffer.writeDoubleLE(Infinity, 8);
  assert.equal(0x7F, buffer[0]);
  assert.equal(0xF0, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x00, buffer[6]);
  assert.equal(0x00, buffer[7]);
  assert.equal(0x00, buffer[8]);
  assert.equal(0x00, buffer[9]);
  assert.equal(0x00, buffer[10]);
  assert.equal(0x00, buffer[11]);
  assert.equal(0x00, buffer[12]);
  assert.equal(0x00, buffer[13]);
  assert.equal(0xF0, buffer[14]);
  assert.equal(0x7F, buffer[15]);
  assert.equal(Infinity, buffer.readDoubleBE(0));
  assert.equal(Infinity, buffer.readDoubleLE(8));

  buffer.writeDoubleBE(-Infinity, 0);
  buffer.writeDoubleLE(-Infinity, 8);
  assert.equal(0xFF, buffer[0]);
  assert.equal(0xF0, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x00, buffer[6]);
  assert.equal(0x00, buffer[7]);
  assert.equal(0x00, buffer[8]);
  assert.equal(0x00, buffer[9]);
  assert.equal(0x00, buffer[10]);
  assert.equal(0x00, buffer[11]);
  assert.equal(0x00, buffer[12]);
  assert.equal(0x00, buffer[13]);
  assert.equal(0xF0, buffer[14]);
  assert.equal(0xFF, buffer[15]);
  assert.equal(-Infinity, buffer.readDoubleBE(0));
  assert.equal(-Infinity, buffer.readDoubleLE(8));

  buffer.writeDoubleBE(NaN, 0);
  buffer.writeDoubleLE(NaN, 8);
  // Darwin ia32 does the other kind of NaN.
  // Compiler bug.  No one really cares.
  assert.ok(0x7F === buffer[0] || 0xFF === buffer[0]);
  assert.equal(0xF8, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x00, buffer[6]);
  assert.equal(0x00, buffer[7]);
  assert.equal(0x00, buffer[8]);
  assert.equal(0x00, buffer[9]);
  assert.equal(0x00, buffer[10]);
  assert.equal(0x00, buffer[11]);
  assert.equal(0x00, buffer[12]);
  assert.equal(0x00, buffer[13]);
  assert.equal(0xF8, buffer[14]);
  // Darwin ia32 does the other kind of NaN.
  // Compiler bug.  No one really cares.
  assert.ok(0x7F === buffer[15] || 0xFF === buffer[15]);
  assert.ok(isNaN(buffer.readDoubleBE(0)));
  assert.ok(isNaN(buffer.readDoubleLE(8)));
});
Beispiel #20
0
exports.testBufferSlice = function (assert) {
  var asciiString = 'hello world';
  var offset = 100;
  var b = Buffer(1024);
  b.fill(0);

  for (var i = 0; i < asciiString.length; i++) {
    b[i] = asciiString.charCodeAt(i);
  }
  var asciiSlice = b.toString('utf8', 0, asciiString.length);
  assert.equal(asciiString, asciiSlice);

  var written = b.write(asciiString, offset, 'utf8');
  assert.equal(asciiString.length, written);
  asciiSlice = b.toString('utf8', offset, offset + asciiString.length);
  assert.equal(asciiString, asciiSlice);

  var sliceA = b.slice(offset, offset + asciiString.length);
  var sliceB = b.slice(offset, offset + asciiString.length);
  compareBuffers(assert, sliceA, sliceB,
    'slicing is idempotent');

  let sliceTest = true;
  for (var j = 0; j < 100; j++) {
    var slice = b.slice(100, 150);
    if (50 !== slice.length)
      sliceTest = false;
    for (var i = 0; i < 50; i++) {
      if (b[100 + i] !== slice[i])
        sliceTest = false;
    }
  }
  assert.ok(sliceTest, 'massive slice runs do not affect buffer');

  // Single argument slice
  let testBuf = new Buffer('abcde');
  assert.equal('bcde', testBuf.slice(1).toString(), 'single argument slice');

  // slice(0,0).length === 0
  assert.equal(0, Buffer('hello').slice(0, 0).length, 'slice(0,0) === 0');

  var buf = new Buffer('0123456789');
  assert.equal(buf.slice(-10, 10), '0123456789', 'buffer slice range correct');
  assert.equal(buf.slice(-20, 10), '0123456789', 'buffer slice range correct');
  assert.equal(buf.slice(-20, -10), '', 'buffer slice range correct');
  assert.equal(buf.slice(0, -1), '012345678', 'buffer slice range correct');
  assert.equal(buf.slice(2, -2), '234567', 'buffer slice range correct');
  assert.equal(buf.slice(0, 65536), '0123456789', 'buffer slice range correct');
  assert.equal(buf.slice(65536, 0), '', 'buffer slice range correct');

  sliceTest = true;
  for (var i = 0, s = buf.toString(); i < buf.length; ++i) {
    if (buf.slice(-i) != s.slice(-i)) sliceTest = false;
    if (buf.slice(0, -i) != s.slice(0, -i)) sliceTest = false;
  }
  assert.ok(sliceTest, 'buffer.slice should be consistent');

  // Make sure modifying a sliced buffer, affects original and vice versa
  b.fill(0);
  let sliced = b.slice(0, 10);
  let babyslice = sliced.slice(0, 5);

  for (let i = 0; i < sliced.length; i++)
    sliced[i] = 'jetpack'.charAt(i);

  compareBuffers(assert, b, sliced,
    'modifying sliced buffer affects original');

  compareBuffers(assert, b, babyslice,
    'modifying sliced buffer affects child-sliced buffer');

  for (let i = 0; i < sliced.length; i++)
    b[i] = 'odinmonkey'.charAt(i);

  compareBuffers(assert, b, sliced,
    'modifying original buffer affects sliced');

  compareBuffers(assert, b, babyslice,
    'modifying original buffer affects grandchild sliced buffer');
};
    initialize: function(data, options = {}) {
        var stream, size;
        if("size" in options)
            size = options.size;

        if(data instanceof Ci.nsIInputStream) {
            if(!size) {
                // preform the size calculation on a clone to avoid closing it.
                if(data instanceof Ci.nsICloneableInputStream)
                    size = data.clone().available();
                else
                    size = data.available();
            }
            this.data = data;
            stream = data;
        }
        else if(data instanceof InputStream) {
            this.data = data.data;
            size = data.length;
            stream = data.stream;
        }
        else if(data instanceof ArrayBuffer) {
            this.data = data;
            size = data.byteLength;
            this.stream = new ArrayBufferInputStream(data, 0, data.byteLength);
        }
        else if(Buffer.isBuffer(data) || isStrictTypedArray(data)) {
            this.data = data;
            size = data.length;
            this.stream = new ArrayBufferInputStream(data.buffer, 0, data.length);
        }
        else {
            let string = data;
            if(data === null) {
                string = "";
            }
            else if(typeof data === "object") {
                string = JSON.stringify(data);
            }
            this.data = data;
            if("charset" in options) {
                let converter = new Converter();
                converter.charset = options.charset;
                stream = converter.convertToInputStream(string);
                size = stream.available();
            }
            else {
                stream = string;
                size = string.length;
            }
        }
        let storageSize = STORAGE_BLOCK_SIZE;
        if(size < storageSize)
            storageSize = Math.pow(2, Math.ceil(Math.log2(size)));
        if(storageSize == 0)
            storageSize = 1;

        this._storage = new StorageStream(storageSize, size);
        if(size > 0) {
            let bytes;
            if(stream instanceof Ci.nsIInputStream)
                bytes = NetUtil.readInputStreamToString(stream, size);
            else
                bytes = stream;
            let oStream = new BinaryOutputStream(this._storage.getOutputStream(0));
            oStream.writeBytes(bytes, size);

            if(this.data instanceof Ci.nsIInputStream)
                this.data = bytes;
        }
        else if(this.data instanceof Ci.nsIInputStream) {
            this.data = null;
        }
    },
Beispiel #22
0
exports.testWriteFloat = helper('writeFloatBE/writeFloatLE', function (assert) {
  var buffer = new Buffer(8);

  buffer.writeFloatBE(1, 0);
  buffer.writeFloatLE(1, 4);
  assert.equal(0x3f, buffer[0]);
  assert.equal(0x80, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x80, buffer[6]);
  assert.equal(0x3f, buffer[7]);

  buffer.writeFloatBE(1 / 3, 0);
  buffer.writeFloatLE(1 / 3, 4);
  assert.equal(0x3e, buffer[0]);
  assert.equal(0xaa, buffer[1]);
  assert.equal(0xaa, buffer[2]);
  assert.equal(0xab, buffer[3]);
  assert.equal(0xab, buffer[4]);
  assert.equal(0xaa, buffer[5]);
  assert.equal(0xaa, buffer[6]);
  assert.equal(0x3e, buffer[7]);

  buffer.writeFloatBE(3.4028234663852886e+38, 0);
  buffer.writeFloatLE(3.4028234663852886e+38, 4);
  assert.equal(0x7f, buffer[0]);
  assert.equal(0x7f, buffer[1]);
  assert.equal(0xff, buffer[2]);
  assert.equal(0xff, buffer[3]);
  assert.equal(0xff, buffer[4]);
  assert.equal(0xff, buffer[5]);
  assert.equal(0x7f, buffer[6]);
  assert.equal(0x7f, buffer[7]);

  buffer.writeFloatLE(1.1754943508222875e-38, 0);
  buffer.writeFloatBE(1.1754943508222875e-38, 4);
  assert.equal(0x00, buffer[0]);
  assert.equal(0x00, buffer[1]);
  assert.equal(0x80, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x80, buffer[5]);
  assert.equal(0x00, buffer[6]);
  assert.equal(0x00, buffer[7]);

  buffer.writeFloatBE(0 * -1, 0);
  buffer.writeFloatLE(0 * -1, 4);
  assert.equal(0x80, buffer[0]);
  assert.equal(0x00, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x00, buffer[6]);
  assert.equal(0x80, buffer[7]);

  buffer.writeFloatBE(Infinity, 0);
  buffer.writeFloatLE(Infinity, 4);
  assert.equal(0x7F, buffer[0]);
  assert.equal(0x80, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x80, buffer[6]);
  assert.equal(0x7F, buffer[7]);
  assert.equal(Infinity, buffer.readFloatBE(0));
  assert.equal(Infinity, buffer.readFloatLE(4));

  buffer.writeFloatBE(-Infinity, 0);
  buffer.writeFloatLE(-Infinity, 4);
  // Darwin ia32 does the other kind of NaN.
  // Compiler bug.  No one really cares.
  assert.ok(0xFF === buffer[0] || 0x7F === buffer[0]);
  assert.equal(0x80, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0x80, buffer[6]);
  assert.equal(0xFF, buffer[7]);
  assert.equal(-Infinity, buffer.readFloatBE(0));
  assert.equal(-Infinity, buffer.readFloatLE(4));

  buffer.writeFloatBE(NaN, 0);
  buffer.writeFloatLE(NaN, 4);
  // Darwin ia32 does the other kind of NaN.
  // Compiler bug.  No one really cares.
  assert.ok(0x7F === buffer[0] || 0xFF === buffer[0]);
  assert.equal(0xc0, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0xc0, buffer[6]);
  // Darwin ia32 does the other kind of NaN.
  // Compiler bug.  No one really cares.
  assert.ok(0x7F === buffer[7] || 0xFF === buffer[7]);
  assert.ok(isNaN(buffer.readFloatBE(0)));
  assert.ok(isNaN(buffer.readFloatLE(4)));
});
Beispiel #23
0
 assert.throws(function() { buf.readInt8(0); }, RangeError,
Beispiel #24
0
 ['not-encoding', undefined, null, 100, {}].map(encoding => {
   assert.ok(!Buffer.isEncoding(encoding),
     'Buffer.isEncoding ' + encoding + ' falsy');
 });
Beispiel #25
0
 assert.throws(function() {
   var buf = new Buffer(8);
   buf.readFloatLE(-1);
 }, RangeError, 'correctly throws with readFloatLE with negative values');
Beispiel #26
0
exports.testBufferCopy = function (assert) {
  // counter to ensure unique value is always copied
  var cntr = 0;
  var b = Buffer(1024); // safe constructor

  assert.strictEqual(1024, b.length);
  b[0] = -1;
  assert.strictEqual(b[0], 255);

  var shimArray = [];
  for (var i = 0; i < 1024; i++) {
    b[i] = i % 256;
    shimArray[i] = i % 256;
  }

  compareBuffers(assert, b, shimArray, 'array notation');

  var c = new Buffer(512);
  assert.strictEqual(512, c.length);
  // copy 512 bytes, from 0 to 512.
  b.fill(++cntr);
  c.fill(++cntr);
  var copied = b.copy(c, 0, 0, 512);
  assert.strictEqual(512, copied,
    'copied ' + copied + ' bytes from b into c');

  compareBuffers(assert, b, c, 'copied to other buffer');

  // copy c into b, without specifying sourceEnd
  b.fill(++cntr);
  c.fill(++cntr);
  var copied = c.copy(b, 0, 0);
  assert.strictEqual(c.length, copied,
    'copied ' + copied + ' bytes from c into b w/o sourceEnd');
  compareBuffers(assert, b, c,
    'copied to other buffer without specifying sourceEnd');

  // copy c into b, without specifying sourceStart
  b.fill(++cntr);
  c.fill(++cntr);
  var copied = c.copy(b, 0);
  assert.strictEqual(c.length, copied,
    'copied ' + copied + ' bytes from c into b w/o sourceStart');
  compareBuffers(assert, b, c,
    'copied to other buffer without specifying sourceStart');

  // copy longer buffer b to shorter c without targetStart
  b.fill(++cntr);
  c.fill(++cntr);

  var copied = b.copy(c);
  assert.strictEqual(c.length, copied,
    'copied ' + copied + ' bytes from b into c w/o targetStart');
  compareBuffers(assert, b, c,
    'copy long buffer to shorter buffer without targetStart');

  // copy starting near end of b to c
  b.fill(++cntr);
  c.fill(++cntr);
  var copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
  assert.strictEqual(Math.floor(c.length / 2), copied,
    'copied ' + copied + ' bytes from end of b into beg. of c');

  let successStatus = true;
  for (var i = 0; i < Math.floor(c.length / 2); i++) {
    if (b[b.length - Math.floor(c.length / 2) + i] !== c[i])
      successStatus = false;
  }

  for (var i = Math.floor(c.length /2) + 1; i < c.length; i++) {
    if (c[c.length-1] !== c[i])
      successStatus = false;
  }
  assert.ok(successStatus,
    'Copied bytes from end of large buffer into beginning of small buffer');

  // try to copy 513 bytes, and check we don't overrun c
  b.fill(++cntr);
  c.fill(++cntr);
  var copied = b.copy(c, 0, 0, 513);
  assert.strictEqual(c.length, copied,
    'copied ' + copied + ' bytes from b trying to overrun c');
  compareBuffers(assert, b, c,
    'copying to buffer that would overflow');

  // copy 768 bytes from b into b
  b.fill(++cntr);
  b.fill(++cntr, 256);
  var copied = b.copy(b, 0, 256, 1024);
  assert.strictEqual(768, copied,
    'copied ' + copied + ' bytes from b into b');

  compareBuffers(assert, b, shimArray.map(()=>cntr),
    'copy partial buffer to itself');

  // copy string longer than buffer length (failure will segfault)
  var bb = new Buffer(10);
  bb.fill('hello crazy world');

  // copy throws at negative sourceStart
  assert.throws(function() {
    Buffer(5).copy(Buffer(5), 0, -1);
  }, RangeError, 'buffer copy throws at negative sourceStart');

  // check sourceEnd resets to targetEnd if former is greater than the latter
  b.fill(++cntr);
  c.fill(++cntr);
  var copied = b.copy(c, 0, 0, 1025);
  assert.strictEqual(copied, c.length,
    'copied ' + copied + ' bytes from b into c');
  compareBuffers(assert, b, c, 'copying should reset sourceEnd if targetEnd if sourceEnd > targetEnd');

  // throw with negative sourceEnd
  assert.throws(function() {
    b.copy(c, 0, 0, -1);
  }, RangeError, 'buffer copy throws at negative sourceEnd');

  // when sourceStart is greater than sourceEnd, zero copied
  assert.equal(b.copy(c, 0, 100, 10), 0);

  // when targetStart > targetLength, zero copied
  assert.equal(b.copy(c, 512, 0, 10), 0);

  // try to copy 0 bytes worth of data into an empty buffer
  b.copy(new Buffer(0), 0, 0, 0);

  // try to copy 0 bytes past the end of the target buffer
  b.copy(new Buffer(0), 1, 1, 1);
  b.copy(new Buffer(1), 1, 1, 1);

  // try to copy 0 bytes from past the end of the source buffer
  b.copy(new Buffer(1), 0, 2048, 2048);
};
Beispiel #27
0
 assert.throws(function() {
   buffer.writeInt8(0x7f + 1, 0);
 });
Beispiel #28
0
 ENCODINGS.map(encoding => {
   assert.ok(Buffer.isEncoding(encoding),
     'Buffer.isEncoding ' + encoding + ' truthy');
 });
Beispiel #29
0
exports.testBufferMain = function (assert) {
  let b = Buffer('abcdef');

  // try to create 0-length buffers
  new Buffer('');
  new Buffer(0);
  // test encodings supported by node;
  // this is different than what node supports, details
  // in buffer.js
  ENCODINGS.forEach(enc => {
    new Buffer('', enc);
    assert.pass('Creating a buffer with ' + enc + ' does not throw');
  });

  ENCODINGS.forEach(function(encoding) {
    // Does not work with utf8
    if (encoding === 'utf-8') return;
    var b = new Buffer(10);
    b.write('あいうえお', encoding);
    assert.equal(b.toString(encoding), 'あいうえお',
      'encode and decodes buffer with ' + encoding);
  });

  // invalid encoding for Buffer.toString
  assert.throws(() => {
    b.toString('invalid');
  }, RangeError, 'invalid encoding for Buffer.toString');

  // try to toString() a 0-length slice of a buffer, both within and without the
  // valid buffer range
  assert.equal(new Buffer('abc').toString('utf8', 0, 0), '',
    'toString 0-length buffer, valid range');
  assert.equal(new Buffer('abc').toString('utf8', -100, -100), '',
    'toString 0-length buffer, invalid range');
  assert.equal(new Buffer('abc').toString('utf8', 100, 100), '',
    'toString 0-length buffer, invalid range');

  // try toString() with a object as a encoding
  assert.equal(new Buffer('abc').toString({toString: function() {
    return 'utf8';
  }}), 'abc', 'toString with object as an encoding');

  // test for buffer overrun
  var buf = new Buffer([0, 0, 0, 0, 0]); // length: 5
  var sub = buf.slice(0, 4); // length: 4
  var written = sub.write('12345', 'utf8');
  assert.equal(written, 4, 'correct bytes written in slice');
  assert.equal(buf[4], 0, 'correct origin buffer value');

  // 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
  assert.equal(Buffer(-1).length, 0);
  assert.equal(Buffer(NaN).length, 0);
  assert.equal(Buffer(3.3).length, 3);
  assert.equal(Buffer({length: 3.3}).length, 3);
  assert.equal(Buffer({length: 'BAM'}).length, 0);

  // Make sure that strings are not coerced to numbers.
  assert.equal(Buffer('99').length, 2);
  assert.equal(Buffer('13.37').length, 5);
};