addon-sdk/source/test/test-buffer.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-buffer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,566 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +
     1.9 +/*
    1.10 + * Many of these tests taken from Joyent's Node
    1.11 + * https://github.com/joyent/node/blob/master/test/simple/test-buffer.js
    1.12 + */
    1.13 +
    1.14 +// Copyright Joyent, Inc. and other Node contributors.
    1.15 +//
    1.16 +// Permission is hereby granted, free of charge, to any person obtaining a
    1.17 +// copy of this software and associated documentation files (the
    1.18 +// "Software"), to deal in the Software without restriction, including
    1.19 +// without limitation the rights to use, copy, modify, merge, publish,
    1.20 +// distribute, sublicense, and/or sell copies of the Software, and to permit
    1.21 +// persons to whom the Software is furnished to do so, subject to the
    1.22 +// following conditions:
    1.23 +//
    1.24 +// The above copyright notice and this permission notice shall be included
    1.25 +// in all copies or substantial portions of the Software.
    1.26 +//
    1.27 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    1.28 +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    1.29 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
    1.30 +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    1.31 +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
    1.32 +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
    1.33 +// USE OR OTHER DEALINGS IN THE SOFTWARE.
    1.34 +
    1.35 +const { Buffer, TextEncoder, TextDecoder } = require('sdk/io/buffer');
    1.36 +const { safeMerge } = require('sdk/util/object');
    1.37 +
    1.38 +const ENCODINGS = ['utf-8', 'utf-16le', 'utf-16be'];
    1.39 +
    1.40 +exports.testBufferMain = function (assert) {
    1.41 +  let b = Buffer('abcdef');
    1.42 +
    1.43 +  // try to create 0-length buffers
    1.44 +  new Buffer('');
    1.45 +  new Buffer(0);
    1.46 +  // test encodings supported by node;
    1.47 +  // this is different than what node supports, details
    1.48 +  // in buffer.js
    1.49 +  ENCODINGS.forEach(enc => {
    1.50 +    new Buffer('', enc);
    1.51 +    assert.pass('Creating a buffer with ' + enc + ' does not throw');
    1.52 +  });
    1.53 +
    1.54 +  ENCODINGS.forEach(function(encoding) {
    1.55 +    // Does not work with utf8
    1.56 +    if (encoding === 'utf-8') return;
    1.57 +    var b = new Buffer(10);
    1.58 +    b.write('あいうえお', encoding);
    1.59 +    assert.equal(b.toString(encoding), 'あいうえお',
    1.60 +      'encode and decodes buffer with ' + encoding);
    1.61 +  });
    1.62 +
    1.63 +  // invalid encoding for Buffer.toString
    1.64 +  assert.throws(() => {
    1.65 +    b.toString('invalid');
    1.66 +  }, TypeError, 'invalid encoding for Buffer.toString');
    1.67 +
    1.68 +  // try to toString() a 0-length slice of a buffer, both within and without the
    1.69 +  // valid buffer range
    1.70 +  assert.equal(new Buffer('abc').toString('utf8', 0, 0), '',
    1.71 +    'toString 0-length buffer, valid range');
    1.72 +  assert.equal(new Buffer('abc').toString('utf8', -100, -100), '',
    1.73 +    'toString 0-length buffer, invalid range');
    1.74 +  assert.equal(new Buffer('abc').toString('utf8', 100, 100), '',
    1.75 +    'toString 0-length buffer, invalid range');
    1.76 +
    1.77 +  // try toString() with a object as a encoding
    1.78 +  assert.equal(new Buffer('abc').toString({toString: function() {
    1.79 +    return 'utf8';
    1.80 +  }}), 'abc', 'toString with object as an encoding');
    1.81 +
    1.82 +  // test for buffer overrun
    1.83 +  var buf = new Buffer([0, 0, 0, 0, 0]); // length: 5
    1.84 +  var sub = buf.slice(0, 4); // length: 4
    1.85 +  var written = sub.write('12345', 'utf8');
    1.86 +  assert.equal(written, 4, 'correct bytes written in slice');
    1.87 +  assert.equal(buf[4], 0, 'correct origin buffer value');
    1.88 +
    1.89 +  // Check for fractional length args, junk length args, etc.
    1.90 +  // https://github.com/joyent/node/issues/1758
    1.91 +  Buffer(3.3).toString(); // throws bad argument error in commit 43cb4ec
    1.92 +  assert.equal(Buffer(-1).length, 0);
    1.93 +  assert.equal(Buffer(NaN).length, 0);
    1.94 +  assert.equal(Buffer(3.3).length, 3);
    1.95 +  assert.equal(Buffer({length: 3.3}).length, 3);
    1.96 +  assert.equal(Buffer({length: 'BAM'}).length, 0);
    1.97 +
    1.98 +  // Make sure that strings are not coerced to numbers.
    1.99 +  assert.equal(Buffer('99').length, 2);
   1.100 +  assert.equal(Buffer('13.37').length, 5);
   1.101 +};
   1.102 +
   1.103 +exports.testIsEncoding = function (assert) {
   1.104 +  ENCODINGS.map(encoding => {
   1.105 +    assert.ok(Buffer.isEncoding(encoding),
   1.106 +      'Buffer.isEncoding ' + encoding + ' truthy');
   1.107 +  });
   1.108 +  ['not-encoding', undefined, null, 100, {}].map(encoding => {
   1.109 +    assert.ok(!Buffer.isEncoding(encoding),
   1.110 +      'Buffer.isEncoding ' + encoding + ' falsy');
   1.111 +  });
   1.112 +};
   1.113 +
   1.114 +exports.testBufferCopy = function (assert) {
   1.115 +  // counter to ensure unique value is always copied
   1.116 +  var cntr = 0;
   1.117 +  var b = Buffer(1024); // safe constructor
   1.118 +
   1.119 +  assert.strictEqual(1024, b.length);
   1.120 +  b[0] = -1;
   1.121 +  assert.strictEqual(b[0], 255);
   1.122 +
   1.123 +  var shimArray = [];
   1.124 +  for (var i = 0; i < 1024; i++) {
   1.125 +    b[i] = i % 256;
   1.126 +    shimArray[i] = i % 256;
   1.127 +  }
   1.128 +
   1.129 +  compareBuffers(assert, b, shimArray, 'array notation');
   1.130 +
   1.131 +  var c = new Buffer(512);
   1.132 +  assert.strictEqual(512, c.length);
   1.133 +  // copy 512 bytes, from 0 to 512.
   1.134 +  b.fill(++cntr);
   1.135 +  c.fill(++cntr);
   1.136 +  var copied = b.copy(c, 0, 0, 512);
   1.137 +  assert.strictEqual(512, copied,
   1.138 +    'copied ' + copied + ' bytes from b into c');
   1.139 +
   1.140 +  compareBuffers(assert, b, c, 'copied to other buffer');
   1.141 +
   1.142 +  // copy c into b, without specifying sourceEnd
   1.143 +  b.fill(++cntr);
   1.144 +  c.fill(++cntr);
   1.145 +  var copied = c.copy(b, 0, 0);
   1.146 +  assert.strictEqual(c.length, copied,
   1.147 +    'copied ' + copied + ' bytes from c into b w/o sourceEnd');
   1.148 +  compareBuffers(assert, b, c,
   1.149 +    'copied to other buffer without specifying sourceEnd');
   1.150 +
   1.151 +  // copy c into b, without specifying sourceStart
   1.152 +  b.fill(++cntr);
   1.153 +  c.fill(++cntr);
   1.154 +  var copied = c.copy(b, 0);
   1.155 +  assert.strictEqual(c.length, copied,
   1.156 +    'copied ' + copied + ' bytes from c into b w/o sourceStart');
   1.157 +  compareBuffers(assert, b, c,
   1.158 +    'copied to other buffer without specifying sourceStart');
   1.159 +
   1.160 +  // copy longer buffer b to shorter c without targetStart
   1.161 +  b.fill(++cntr);
   1.162 +  c.fill(++cntr);
   1.163 +
   1.164 +  var copied = b.copy(c);
   1.165 +  assert.strictEqual(c.length, copied,
   1.166 +    'copied ' + copied + ' bytes from b into c w/o targetStart');
   1.167 +  compareBuffers(assert, b, c,
   1.168 +    'copy long buffer to shorter buffer without targetStart');
   1.169 +
   1.170 +  // copy starting near end of b to c
   1.171 +  b.fill(++cntr);
   1.172 +  c.fill(++cntr);
   1.173 +  var copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
   1.174 +  assert.strictEqual(Math.floor(c.length / 2), copied,
   1.175 +    'copied ' + copied + ' bytes from end of b into beg. of c');
   1.176 +
   1.177 +  let successStatus = true;
   1.178 +  for (var i = 0; i < Math.floor(c.length / 2); i++) {
   1.179 +    if (b[b.length - Math.floor(c.length / 2) + i] !== c[i])
   1.180 +      successStatus = false;
   1.181 +  }
   1.182 +
   1.183 +  for (var i = Math.floor(c.length /2) + 1; i < c.length; i++) {
   1.184 +    if (c[c.length-1] !== c[i])
   1.185 +      successStatus = false;
   1.186 +  }
   1.187 +  assert.ok(successStatus,
   1.188 +    'Copied bytes from end of large buffer into beginning of small buffer');
   1.189 +
   1.190 +  // try to copy 513 bytes, and check we don't overrun c
   1.191 +  b.fill(++cntr);
   1.192 +  c.fill(++cntr);
   1.193 +  var copied = b.copy(c, 0, 0, 513);
   1.194 +  assert.strictEqual(c.length, copied,
   1.195 +    'copied ' + copied + ' bytes from b trying to overrun c');
   1.196 +  compareBuffers(assert, b, c,
   1.197 +    'copying to buffer that would overflow');
   1.198 +
   1.199 +  // copy 768 bytes from b into b
   1.200 +  b.fill(++cntr);
   1.201 +  b.fill(++cntr, 256);
   1.202 +  var copied = b.copy(b, 0, 256, 1024);
   1.203 +  assert.strictEqual(768, copied,
   1.204 +    'copied ' + copied + ' bytes from b into b');
   1.205 +
   1.206 +  compareBuffers(assert, b, shimArray.map(()=>cntr),
   1.207 +    'copy partial buffer to itself');
   1.208 +
   1.209 +  // copy string longer than buffer length (failure will segfault)
   1.210 +  var bb = new Buffer(10);
   1.211 +  bb.fill('hello crazy world');
   1.212 +
   1.213 +  // copy throws at negative sourceStart
   1.214 +  assert.throws(function() {
   1.215 +    Buffer(5).copy(Buffer(5), 0, -1);
   1.216 +  }, RangeError, 'buffer copy throws at negative sourceStart');
   1.217 +
   1.218 +  // check sourceEnd resets to targetEnd if former is greater than the latter
   1.219 +  b.fill(++cntr);
   1.220 +  c.fill(++cntr);
   1.221 +  var copied = b.copy(c, 0, 0, 1025);
   1.222 +  assert.strictEqual(copied, c.length,
   1.223 +    'copied ' + copied + ' bytes from b into c');
   1.224 +  compareBuffers(assert, b, c, 'copying should reset sourceEnd if targetEnd if sourceEnd > targetEnd');
   1.225 +
   1.226 +  // throw with negative sourceEnd
   1.227 +  assert.throws(function() {
   1.228 +    b.copy(c, 0, 0, -1);
   1.229 +  }, RangeError, 'buffer copy throws at negative sourceEnd');
   1.230 +
   1.231 +  // when sourceStart is greater than sourceEnd, zero copied
   1.232 +  assert.equal(b.copy(c, 0, 100, 10), 0);
   1.233 +
   1.234 +  // when targetStart > targetLength, zero copied
   1.235 +  assert.equal(b.copy(c, 512, 0, 10), 0);
   1.236 +
   1.237 +  // try to copy 0 bytes worth of data into an empty buffer
   1.238 +  b.copy(new Buffer(0), 0, 0, 0);
   1.239 +
   1.240 +  // try to copy 0 bytes past the end of the target buffer
   1.241 +  b.copy(new Buffer(0), 1, 1, 1);
   1.242 +  b.copy(new Buffer(1), 1, 1, 1);
   1.243 +
   1.244 +  // try to copy 0 bytes from past the end of the source buffer
   1.245 +  b.copy(new Buffer(1), 0, 2048, 2048);
   1.246 +};
   1.247 +
   1.248 +exports.testBufferWrite = function (assert) {
   1.249 +  let b = Buffer(1024);
   1.250 +  b.fill(0);
   1.251 +
   1.252 +  assert.throws(() => {
   1.253 +    b.write('test string', 0, 5, 'invalid');
   1.254 +  }, TypeError, 'invalid encoding with buffer write throws');
   1.255 +  // try to write a 0-length string beyond the end of b
   1.256 +  assert.throws(function() {
   1.257 +    b.write('', 2048);
   1.258 +  }, RangeError, 'writing a 0-length string beyond buffer throws');
   1.259 +  // throw when writing to negative offset
   1.260 +  assert.throws(function() {
   1.261 +    b.write('a', -1);
   1.262 +  }, RangeError, 'writing negative offset on buffer throws');
   1.263 +
   1.264 +  // throw when writing past bounds from the pool
   1.265 +  assert.throws(function() {
   1.266 +    b.write('a', 2048);
   1.267 +  }, RangeError, 'writing past buffer bounds from pool throws');
   1.268 +
   1.269 +  // testing for smart defaults and ability to pass string values as offset
   1.270 +
   1.271 +  // previous write API was the following:
   1.272 +  // write(string, encoding, offset, length)
   1.273 +  // this is planned on being removed in node v0.13,
   1.274 +  // we will not support it
   1.275 +  var writeTest = new Buffer('abcdes');
   1.276 +  writeTest.write('n', 'utf8');
   1.277 +//  writeTest.write('o', 'utf8', '1');
   1.278 +  writeTest.write('d', '2', 'utf8');
   1.279 +  writeTest.write('e', 3, 'utf8');
   1.280 +//  writeTest.write('j', 'utf8', 4);
   1.281 +  assert.equal(writeTest.toString(), 'nbdees',
   1.282 +    'buffer write API alternative syntax works');
   1.283 +};
   1.284 +
   1.285 +exports.testBufferWriteEncoding = function (assert) {
   1.286 +
   1.287 +  // Node #1210 Test UTF-8 string includes null character
   1.288 +  var buf = new Buffer('\0');
   1.289 +  assert.equal(buf.length, 1);
   1.290 +  buf = new Buffer('\0\0');
   1.291 +  assert.equal(buf.length, 2);
   1.292 +
   1.293 +  buf = new Buffer(2);
   1.294 +  var written = buf.write(''); // 0byte
   1.295 +  assert.equal(written, 0);
   1.296 +  written = buf.write('\0'); // 1byte (v8 adds null terminator)
   1.297 +  assert.equal(written, 1);
   1.298 +  written = buf.write('a\0'); // 1byte * 2
   1.299 +  assert.equal(written, 2);
   1.300 +  // TODO, these tests write 0, possibly due to character encoding
   1.301 +/*
   1.302 +  written = buf.write('あ'); // 3bytes
   1.303 +  assert.equal(written, 0);
   1.304 +  written = buf.write('\0あ'); // 1byte + 3bytes
   1.305 +  assert.equal(written, 1);
   1.306 +*/
   1.307 +  written = buf.write('\0\0あ'); // 1byte * 2 + 3bytes
   1.308 +  buf = new Buffer(10);
   1.309 +  written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator)
   1.310 +  assert.equal(written, 9);
   1.311 +  written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
   1.312 +  assert.equal(written, 10);
   1.313 +};
   1.314 +
   1.315 +exports.testBufferWriteWithMaxLength = function (assert) {
   1.316 +  // Node #243 Test write() with maxLength
   1.317 +  var buf = new Buffer(4);
   1.318 +  buf.fill(0xFF);
   1.319 +  var written = buf.write('abcd', 1, 2, 'utf8');
   1.320 +  assert.equal(written, 2);
   1.321 +  assert.equal(buf[0], 0xFF);
   1.322 +  assert.equal(buf[1], 0x61);
   1.323 +  assert.equal(buf[2], 0x62);
   1.324 +  assert.equal(buf[3], 0xFF);
   1.325 +
   1.326 +  buf.fill(0xFF);
   1.327 +  written = buf.write('abcd', 1, 4);
   1.328 +  assert.equal(written, 3);
   1.329 +  assert.equal(buf[0], 0xFF);
   1.330 +  assert.equal(buf[1], 0x61);
   1.331 +  assert.equal(buf[2], 0x62);
   1.332 +  assert.equal(buf[3], 0x63);
   1.333 +
   1.334 +  buf.fill(0xFF);
   1.335 +  // Ignore legacy API
   1.336 +  /*
   1.337 +     written = buf.write('abcd', 'utf8', 1, 2); // legacy style
   1.338 +     console.log(buf);
   1.339 +     assert.equal(written, 2);
   1.340 +     assert.equal(buf[0], 0xFF);
   1.341 +     assert.equal(buf[1], 0x61);
   1.342 +     assert.equal(buf[2], 0x62);
   1.343 +     assert.equal(buf[3], 0xFF);
   1.344 +     */
   1.345 +};
   1.346 +
   1.347 +exports.testBufferSlice = function (assert) {
   1.348 +  var asciiString = 'hello world';
   1.349 +  var offset = 100;
   1.350 +  var b = Buffer(1024);
   1.351 +  b.fill(0);
   1.352 +
   1.353 +  for (var i = 0; i < asciiString.length; i++) {
   1.354 +    b[i] = asciiString.charCodeAt(i);
   1.355 +  }
   1.356 +  var asciiSlice = b.toString('utf8', 0, asciiString.length);
   1.357 +  assert.equal(asciiString, asciiSlice);
   1.358 +
   1.359 +  var written = b.write(asciiString, offset, 'utf8');
   1.360 +  assert.equal(asciiString.length, written);
   1.361 +  asciiSlice = b.toString('utf8', offset, offset + asciiString.length);
   1.362 +  assert.equal(asciiString, asciiSlice);
   1.363 +
   1.364 +  var sliceA = b.slice(offset, offset + asciiString.length);
   1.365 +  var sliceB = b.slice(offset, offset + asciiString.length);
   1.366 +  compareBuffers(assert, sliceA, sliceB,
   1.367 +    'slicing is idempotent');
   1.368 +
   1.369 +  let sliceTest = true;
   1.370 +  for (var j = 0; j < 100; j++) {
   1.371 +    var slice = b.slice(100, 150);
   1.372 +    if (50 !== slice.length)
   1.373 +      sliceTest = false;
   1.374 +    for (var i = 0; i < 50; i++) {
   1.375 +      if (b[100 + i] !== slice[i])
   1.376 +        sliceTest = false;
   1.377 +    }
   1.378 +  }
   1.379 +  assert.ok(sliceTest, 'massive slice runs do not affect buffer');
   1.380 +
   1.381 +  // Single argument slice
   1.382 +  let testBuf = new Buffer('abcde');
   1.383 +  assert.equal('bcde', testBuf.slice(1).toString(), 'single argument slice');
   1.384 +
   1.385 +  // slice(0,0).length === 0
   1.386 +  assert.equal(0, Buffer('hello').slice(0, 0).length, 'slice(0,0) === 0');
   1.387 +
   1.388 +  var buf = new Buffer('0123456789');
   1.389 +  assert.equal(buf.slice(-10, 10), '0123456789', 'buffer slice range correct');
   1.390 +  assert.equal(buf.slice(-20, 10), '0123456789', 'buffer slice range correct');
   1.391 +  assert.equal(buf.slice(-20, -10), '', 'buffer slice range correct');
   1.392 +  assert.equal(buf.slice(0, -1), '012345678', 'buffer slice range correct');
   1.393 +  assert.equal(buf.slice(2, -2), '234567', 'buffer slice range correct');
   1.394 +  assert.equal(buf.slice(0, 65536), '0123456789', 'buffer slice range correct');
   1.395 +  assert.equal(buf.slice(65536, 0), '', 'buffer slice range correct');
   1.396 +
   1.397 +  let sliceTest = true;
   1.398 +  for (var i = 0, s = buf.toString(); i < buf.length; ++i) {
   1.399 +    if (buf.slice(-i) != s.slice(-i)) sliceTest = false;
   1.400 +    if (buf.slice(0, -i) != s.slice(0, -i)) sliceTest = false;
   1.401 +  }
   1.402 +  assert.ok(sliceTest, 'buffer.slice should be consistent');
   1.403 +
   1.404 +  // Make sure modifying a sliced buffer, affects original and vice versa
   1.405 +  b.fill(0);
   1.406 +  let sliced = b.slice(0, 10);
   1.407 +  let babyslice = sliced.slice(0, 5);
   1.408 +
   1.409 +  for (let i = 0; i < sliced.length; i++)
   1.410 +    sliced[i] = 'jetpack'.charAt(i);
   1.411 +
   1.412 +  compareBuffers(assert, b, sliced,
   1.413 +    'modifying sliced buffer affects original');
   1.414 +
   1.415 +  compareBuffers(assert, b, babyslice,
   1.416 +    'modifying sliced buffer affects child-sliced buffer');
   1.417 +
   1.418 +  for (let i = 0; i < sliced.length; i++)
   1.419 +    b[i] = 'odinmonkey'.charAt(i);
   1.420 +
   1.421 +  compareBuffers(assert, b, sliced,
   1.422 +    'modifying original buffer affects sliced');
   1.423 +
   1.424 +  compareBuffers(assert, b, babyslice,
   1.425 +    'modifying original buffer affects grandchild sliced buffer');
   1.426 +};
   1.427 +
   1.428 +exports.testSlicingParents = function (assert) {
   1.429 +  let root = Buffer(5);
   1.430 +  let child = root.slice(0, 4);
   1.431 +  let grandchild = child.slice(0, 3);
   1.432 +
   1.433 +  assert.equal(root.parent, undefined, 'a new buffer should not have a parent');
   1.434 +
   1.435 +  // make sure a zero length slice doesn't set the .parent attribute
   1.436 +  assert.equal(root.slice(0,0).parent, undefined,
   1.437 +    '0-length slice should not have a parent');
   1.438 +
   1.439 +  assert.equal(child.parent, root,
   1.440 +    'a valid slice\'s parent should be the original buffer (child)');
   1.441 +
   1.442 +  assert.equal(grandchild.parent, root,
   1.443 +    'a valid slice\'s parent should be the original buffer (grandchild)');
   1.444 +};
   1.445 +
   1.446 +exports.testIsBuffer = function (assert) {
   1.447 +  let buffer = new Buffer('content', 'utf8');
   1.448 +  assert.ok(Buffer.isBuffer(buffer), 'isBuffer truthy on buffers');
   1.449 +  assert.ok(!Buffer.isBuffer({}), 'isBuffer falsy on objects');
   1.450 +  assert.ok(!Buffer.isBuffer(new Uint8Array()),
   1.451 +    'isBuffer falsy on Uint8Array');
   1.452 +  assert.ok(Buffer.isBuffer(buffer.slice(0)), 'Buffer#slice should be a new buffer');
   1.453 +};
   1.454 +
   1.455 +exports.testBufferConcat = function (assert) {
   1.456 +  let zero = [];
   1.457 +  let one = [ new Buffer('asdf') ];
   1.458 +  let long = [];
   1.459 +  for (let i = 0; i < 10; i++) long.push(new Buffer('asdf'));
   1.460 +
   1.461 +  let flatZero = Buffer.concat(zero);
   1.462 +  let flatOne = Buffer.concat(one);
   1.463 +  let flatLong = Buffer.concat(long);
   1.464 +  let flatLongLen = Buffer.concat(long, 40);
   1.465 +
   1.466 +  assert.equal(flatZero.length, 0);
   1.467 +  assert.equal(flatOne.toString(), 'asdf');
   1.468 +  assert.equal(flatOne, one[0]);
   1.469 +  assert.ok(flatLong.toString(), (new Array(10+1).join('asdf')));
   1.470 +  assert.equal(flatLongLen.toString(), (new Array(10+1).join('asdf')));
   1.471 +};
   1.472 +
   1.473 +exports.testBufferByteLength = function (assert) {
   1.474 +  let str = '\u00bd + \u00bc = \u00be';
   1.475 +  assert.equal(Buffer.byteLength(str), 12,
   1.476 +    'correct byteLength of string');
   1.477 +
   1.478 +  assert.equal(14, Buffer.byteLength('Il était tué'));
   1.479 +  assert.equal(14, Buffer.byteLength('Il était tué', 'utf8'));
   1.480 +  // We do not currently support these encodings
   1.481 +  /*
   1.482 +  ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
   1.483 +  assert.equal(24, Buffer.byteLength('Il était tué', encoding));
   1.484 +  });
   1.485 +  assert.equal(12, Buffer.byteLength('Il était tué', 'ascii'));
   1.486 +  assert.equal(12, Buffer.byteLength('Il était tué', 'binary'));
   1.487 +  */
   1.488 +};
   1.489 +
   1.490 +exports.testTextEncoderDecoder = function (assert) {
   1.491 +  assert.ok(TextEncoder, 'TextEncoder exists');
   1.492 +  assert.ok(TextDecoder, 'TextDecoder exists');
   1.493 +};
   1.494 +
   1.495 +exports.testOverflowedBuffers = function (assert) {
   1.496 +  assert.throws(function() {
   1.497 +    new Buffer(0xFFFFFFFF);
   1.498 +  }, RangeError, 'correctly throws buffer overflow');
   1.499 +
   1.500 +  assert.throws(function() {
   1.501 +    new Buffer(0xFFFFFFFFF);
   1.502 +  }, RangeError, 'correctly throws buffer overflow');
   1.503 +
   1.504 +  assert.throws(function() {
   1.505 +    var buf = new Buffer(8);
   1.506 +    buf.readFloatLE(0xffffffff);
   1.507 +  }, RangeError, 'correctly throws buffer overflow with readFloatLE');
   1.508 +
   1.509 +  assert.throws(function() {
   1.510 +    var buf = new Buffer(8);
   1.511 +    buf.writeFloatLE(0.0, 0xffffffff);
   1.512 +  }, RangeError, 'correctly throws buffer overflow with writeFloatLE');
   1.513 +
   1.514 +  //ensure negative values can't get past offset
   1.515 +  assert.throws(function() {
   1.516 +    var buf = new Buffer(8);
   1.517 +    buf.readFloatLE(-1);
   1.518 +  }, RangeError, 'correctly throws with readFloatLE negative values');
   1.519 +
   1.520 +  assert.throws(function() {
   1.521 +    var buf = new Buffer(8);
   1.522 +    buf.writeFloatLE(0.0, -1);
   1.523 +  }, RangeError, 'correctly throws with writeFloatLE with negative values');
   1.524 +
   1.525 +  assert.throws(function() {
   1.526 +    var buf = new Buffer(8);
   1.527 +    buf.readFloatLE(-1);
   1.528 +  }, RangeError, 'correctly throws with readFloatLE with negative values');
   1.529 +};
   1.530 +
   1.531 +exports.testReadWriteDataTypeErrors = function (assert) {
   1.532 +  var buf = new Buffer(0);
   1.533 +  assert.throws(function() { buf.readUInt8(0); }, RangeError,
   1.534 +    'readUInt8(0) throws');
   1.535 +  assert.throws(function() { buf.readInt8(0); }, RangeError,
   1.536 +    'readInt8(0) throws');
   1.537 +
   1.538 +  [16, 32].forEach(function(bits) {
   1.539 +    var buf = new Buffer(bits / 8 - 1);
   1.540 +    assert.throws(function() { buf['readUInt' + bits + 'BE'](0); },
   1.541 +      RangeError,
   1.542 +      'readUInt' + bits + 'BE');
   1.543 +
   1.544 +    assert.throws(function() { buf['readUInt' + bits + 'LE'](0); },
   1.545 +      RangeError,
   1.546 +      'readUInt' + bits + 'LE');
   1.547 +
   1.548 +    assert.throws(function() { buf['readInt' + bits + 'BE'](0); },
   1.549 +      RangeError,
   1.550 +      'readInt' + bits + 'BE()');
   1.551 +
   1.552 +    assert.throws(function() { buf['readInt' + bits + 'LE'](0); },
   1.553 +      RangeError,
   1.554 +      'readInt' + bits + 'LE()');
   1.555 +  });
   1.556 +};
   1.557 +
   1.558 +safeMerge(exports, require('./buffers/test-write-types'));
   1.559 +safeMerge(exports, require('./buffers/test-read-types'));
   1.560 +
   1.561 +function compareBuffers (assert, buf1, buf2, message) {
   1.562 +  let status = true;
   1.563 +  for (let i = 0; i < Math.min(buf1.length, buf2.length); i++) {
   1.564 +    if (buf1[i] !== buf2[i])
   1.565 +      status = false;
   1.566 +  }
   1.567 +  assert.ok(status, 'buffer successfully copied: ' + message);
   1.568 +}
   1.569 +require('sdk/test').run(exports);

mercurial