Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * Many of these tests taken from Joyent's Node |
michael@0 | 8 | * https://github.com/joyent/node/blob/master/test/simple/test-buffer.js |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | // Copyright Joyent, Inc. and other Node contributors. |
michael@0 | 12 | // |
michael@0 | 13 | // Permission is hereby granted, free of charge, to any person obtaining a |
michael@0 | 14 | // copy of this software and associated documentation files (the |
michael@0 | 15 | // "Software"), to deal in the Software without restriction, including |
michael@0 | 16 | // without limitation the rights to use, copy, modify, merge, publish, |
michael@0 | 17 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
michael@0 | 18 | // persons to whom the Software is furnished to do so, subject to the |
michael@0 | 19 | // following conditions: |
michael@0 | 20 | // |
michael@0 | 21 | // The above copyright notice and this permission notice shall be included |
michael@0 | 22 | // in all copies or substantial portions of the Software. |
michael@0 | 23 | // |
michael@0 | 24 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
michael@0 | 25 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
michael@0 | 26 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
michael@0 | 27 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
michael@0 | 28 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
michael@0 | 29 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
michael@0 | 30 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
michael@0 | 31 | |
michael@0 | 32 | const { Buffer, TextEncoder, TextDecoder } = require('sdk/io/buffer'); |
michael@0 | 33 | const { safeMerge } = require('sdk/util/object'); |
michael@0 | 34 | |
michael@0 | 35 | const ENCODINGS = ['utf-8', 'utf-16le', 'utf-16be']; |
michael@0 | 36 | |
michael@0 | 37 | exports.testBufferMain = function (assert) { |
michael@0 | 38 | let b = Buffer('abcdef'); |
michael@0 | 39 | |
michael@0 | 40 | // try to create 0-length buffers |
michael@0 | 41 | new Buffer(''); |
michael@0 | 42 | new Buffer(0); |
michael@0 | 43 | // test encodings supported by node; |
michael@0 | 44 | // this is different than what node supports, details |
michael@0 | 45 | // in buffer.js |
michael@0 | 46 | ENCODINGS.forEach(enc => { |
michael@0 | 47 | new Buffer('', enc); |
michael@0 | 48 | assert.pass('Creating a buffer with ' + enc + ' does not throw'); |
michael@0 | 49 | }); |
michael@0 | 50 | |
michael@0 | 51 | ENCODINGS.forEach(function(encoding) { |
michael@0 | 52 | // Does not work with utf8 |
michael@0 | 53 | if (encoding === 'utf-8') return; |
michael@0 | 54 | var b = new Buffer(10); |
michael@0 | 55 | b.write('あいうえお', encoding); |
michael@0 | 56 | assert.equal(b.toString(encoding), 'あいうえお', |
michael@0 | 57 | 'encode and decodes buffer with ' + encoding); |
michael@0 | 58 | }); |
michael@0 | 59 | |
michael@0 | 60 | // invalid encoding for Buffer.toString |
michael@0 | 61 | assert.throws(() => { |
michael@0 | 62 | b.toString('invalid'); |
michael@0 | 63 | }, TypeError, 'invalid encoding for Buffer.toString'); |
michael@0 | 64 | |
michael@0 | 65 | // try to toString() a 0-length slice of a buffer, both within and without the |
michael@0 | 66 | // valid buffer range |
michael@0 | 67 | assert.equal(new Buffer('abc').toString('utf8', 0, 0), '', |
michael@0 | 68 | 'toString 0-length buffer, valid range'); |
michael@0 | 69 | assert.equal(new Buffer('abc').toString('utf8', -100, -100), '', |
michael@0 | 70 | 'toString 0-length buffer, invalid range'); |
michael@0 | 71 | assert.equal(new Buffer('abc').toString('utf8', 100, 100), '', |
michael@0 | 72 | 'toString 0-length buffer, invalid range'); |
michael@0 | 73 | |
michael@0 | 74 | // try toString() with a object as a encoding |
michael@0 | 75 | assert.equal(new Buffer('abc').toString({toString: function() { |
michael@0 | 76 | return 'utf8'; |
michael@0 | 77 | }}), 'abc', 'toString with object as an encoding'); |
michael@0 | 78 | |
michael@0 | 79 | // test for buffer overrun |
michael@0 | 80 | var buf = new Buffer([0, 0, 0, 0, 0]); // length: 5 |
michael@0 | 81 | var sub = buf.slice(0, 4); // length: 4 |
michael@0 | 82 | var written = sub.write('12345', 'utf8'); |
michael@0 | 83 | assert.equal(written, 4, 'correct bytes written in slice'); |
michael@0 | 84 | assert.equal(buf[4], 0, 'correct origin buffer value'); |
michael@0 | 85 | |
michael@0 | 86 | // Check for fractional length args, junk length args, etc. |
michael@0 | 87 | // https://github.com/joyent/node/issues/1758 |
michael@0 | 88 | Buffer(3.3).toString(); // throws bad argument error in commit 43cb4ec |
michael@0 | 89 | assert.equal(Buffer(-1).length, 0); |
michael@0 | 90 | assert.equal(Buffer(NaN).length, 0); |
michael@0 | 91 | assert.equal(Buffer(3.3).length, 3); |
michael@0 | 92 | assert.equal(Buffer({length: 3.3}).length, 3); |
michael@0 | 93 | assert.equal(Buffer({length: 'BAM'}).length, 0); |
michael@0 | 94 | |
michael@0 | 95 | // Make sure that strings are not coerced to numbers. |
michael@0 | 96 | assert.equal(Buffer('99').length, 2); |
michael@0 | 97 | assert.equal(Buffer('13.37').length, 5); |
michael@0 | 98 | }; |
michael@0 | 99 | |
michael@0 | 100 | exports.testIsEncoding = function (assert) { |
michael@0 | 101 | ENCODINGS.map(encoding => { |
michael@0 | 102 | assert.ok(Buffer.isEncoding(encoding), |
michael@0 | 103 | 'Buffer.isEncoding ' + encoding + ' truthy'); |
michael@0 | 104 | }); |
michael@0 | 105 | ['not-encoding', undefined, null, 100, {}].map(encoding => { |
michael@0 | 106 | assert.ok(!Buffer.isEncoding(encoding), |
michael@0 | 107 | 'Buffer.isEncoding ' + encoding + ' falsy'); |
michael@0 | 108 | }); |
michael@0 | 109 | }; |
michael@0 | 110 | |
michael@0 | 111 | exports.testBufferCopy = function (assert) { |
michael@0 | 112 | // counter to ensure unique value is always copied |
michael@0 | 113 | var cntr = 0; |
michael@0 | 114 | var b = Buffer(1024); // safe constructor |
michael@0 | 115 | |
michael@0 | 116 | assert.strictEqual(1024, b.length); |
michael@0 | 117 | b[0] = -1; |
michael@0 | 118 | assert.strictEqual(b[0], 255); |
michael@0 | 119 | |
michael@0 | 120 | var shimArray = []; |
michael@0 | 121 | for (var i = 0; i < 1024; i++) { |
michael@0 | 122 | b[i] = i % 256; |
michael@0 | 123 | shimArray[i] = i % 256; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | compareBuffers(assert, b, shimArray, 'array notation'); |
michael@0 | 127 | |
michael@0 | 128 | var c = new Buffer(512); |
michael@0 | 129 | assert.strictEqual(512, c.length); |
michael@0 | 130 | // copy 512 bytes, from 0 to 512. |
michael@0 | 131 | b.fill(++cntr); |
michael@0 | 132 | c.fill(++cntr); |
michael@0 | 133 | var copied = b.copy(c, 0, 0, 512); |
michael@0 | 134 | assert.strictEqual(512, copied, |
michael@0 | 135 | 'copied ' + copied + ' bytes from b into c'); |
michael@0 | 136 | |
michael@0 | 137 | compareBuffers(assert, b, c, 'copied to other buffer'); |
michael@0 | 138 | |
michael@0 | 139 | // copy c into b, without specifying sourceEnd |
michael@0 | 140 | b.fill(++cntr); |
michael@0 | 141 | c.fill(++cntr); |
michael@0 | 142 | var copied = c.copy(b, 0, 0); |
michael@0 | 143 | assert.strictEqual(c.length, copied, |
michael@0 | 144 | 'copied ' + copied + ' bytes from c into b w/o sourceEnd'); |
michael@0 | 145 | compareBuffers(assert, b, c, |
michael@0 | 146 | 'copied to other buffer without specifying sourceEnd'); |
michael@0 | 147 | |
michael@0 | 148 | // copy c into b, without specifying sourceStart |
michael@0 | 149 | b.fill(++cntr); |
michael@0 | 150 | c.fill(++cntr); |
michael@0 | 151 | var copied = c.copy(b, 0); |
michael@0 | 152 | assert.strictEqual(c.length, copied, |
michael@0 | 153 | 'copied ' + copied + ' bytes from c into b w/o sourceStart'); |
michael@0 | 154 | compareBuffers(assert, b, c, |
michael@0 | 155 | 'copied to other buffer without specifying sourceStart'); |
michael@0 | 156 | |
michael@0 | 157 | // copy longer buffer b to shorter c without targetStart |
michael@0 | 158 | b.fill(++cntr); |
michael@0 | 159 | c.fill(++cntr); |
michael@0 | 160 | |
michael@0 | 161 | var copied = b.copy(c); |
michael@0 | 162 | assert.strictEqual(c.length, copied, |
michael@0 | 163 | 'copied ' + copied + ' bytes from b into c w/o targetStart'); |
michael@0 | 164 | compareBuffers(assert, b, c, |
michael@0 | 165 | 'copy long buffer to shorter buffer without targetStart'); |
michael@0 | 166 | |
michael@0 | 167 | // copy starting near end of b to c |
michael@0 | 168 | b.fill(++cntr); |
michael@0 | 169 | c.fill(++cntr); |
michael@0 | 170 | var copied = b.copy(c, 0, b.length - Math.floor(c.length / 2)); |
michael@0 | 171 | assert.strictEqual(Math.floor(c.length / 2), copied, |
michael@0 | 172 | 'copied ' + copied + ' bytes from end of b into beg. of c'); |
michael@0 | 173 | |
michael@0 | 174 | let successStatus = true; |
michael@0 | 175 | for (var i = 0; i < Math.floor(c.length / 2); i++) { |
michael@0 | 176 | if (b[b.length - Math.floor(c.length / 2) + i] !== c[i]) |
michael@0 | 177 | successStatus = false; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | for (var i = Math.floor(c.length /2) + 1; i < c.length; i++) { |
michael@0 | 181 | if (c[c.length-1] !== c[i]) |
michael@0 | 182 | successStatus = false; |
michael@0 | 183 | } |
michael@0 | 184 | assert.ok(successStatus, |
michael@0 | 185 | 'Copied bytes from end of large buffer into beginning of small buffer'); |
michael@0 | 186 | |
michael@0 | 187 | // try to copy 513 bytes, and check we don't overrun c |
michael@0 | 188 | b.fill(++cntr); |
michael@0 | 189 | c.fill(++cntr); |
michael@0 | 190 | var copied = b.copy(c, 0, 0, 513); |
michael@0 | 191 | assert.strictEqual(c.length, copied, |
michael@0 | 192 | 'copied ' + copied + ' bytes from b trying to overrun c'); |
michael@0 | 193 | compareBuffers(assert, b, c, |
michael@0 | 194 | 'copying to buffer that would overflow'); |
michael@0 | 195 | |
michael@0 | 196 | // copy 768 bytes from b into b |
michael@0 | 197 | b.fill(++cntr); |
michael@0 | 198 | b.fill(++cntr, 256); |
michael@0 | 199 | var copied = b.copy(b, 0, 256, 1024); |
michael@0 | 200 | assert.strictEqual(768, copied, |
michael@0 | 201 | 'copied ' + copied + ' bytes from b into b'); |
michael@0 | 202 | |
michael@0 | 203 | compareBuffers(assert, b, shimArray.map(()=>cntr), |
michael@0 | 204 | 'copy partial buffer to itself'); |
michael@0 | 205 | |
michael@0 | 206 | // copy string longer than buffer length (failure will segfault) |
michael@0 | 207 | var bb = new Buffer(10); |
michael@0 | 208 | bb.fill('hello crazy world'); |
michael@0 | 209 | |
michael@0 | 210 | // copy throws at negative sourceStart |
michael@0 | 211 | assert.throws(function() { |
michael@0 | 212 | Buffer(5).copy(Buffer(5), 0, -1); |
michael@0 | 213 | }, RangeError, 'buffer copy throws at negative sourceStart'); |
michael@0 | 214 | |
michael@0 | 215 | // check sourceEnd resets to targetEnd if former is greater than the latter |
michael@0 | 216 | b.fill(++cntr); |
michael@0 | 217 | c.fill(++cntr); |
michael@0 | 218 | var copied = b.copy(c, 0, 0, 1025); |
michael@0 | 219 | assert.strictEqual(copied, c.length, |
michael@0 | 220 | 'copied ' + copied + ' bytes from b into c'); |
michael@0 | 221 | compareBuffers(assert, b, c, 'copying should reset sourceEnd if targetEnd if sourceEnd > targetEnd'); |
michael@0 | 222 | |
michael@0 | 223 | // throw with negative sourceEnd |
michael@0 | 224 | assert.throws(function() { |
michael@0 | 225 | b.copy(c, 0, 0, -1); |
michael@0 | 226 | }, RangeError, 'buffer copy throws at negative sourceEnd'); |
michael@0 | 227 | |
michael@0 | 228 | // when sourceStart is greater than sourceEnd, zero copied |
michael@0 | 229 | assert.equal(b.copy(c, 0, 100, 10), 0); |
michael@0 | 230 | |
michael@0 | 231 | // when targetStart > targetLength, zero copied |
michael@0 | 232 | assert.equal(b.copy(c, 512, 0, 10), 0); |
michael@0 | 233 | |
michael@0 | 234 | // try to copy 0 bytes worth of data into an empty buffer |
michael@0 | 235 | b.copy(new Buffer(0), 0, 0, 0); |
michael@0 | 236 | |
michael@0 | 237 | // try to copy 0 bytes past the end of the target buffer |
michael@0 | 238 | b.copy(new Buffer(0), 1, 1, 1); |
michael@0 | 239 | b.copy(new Buffer(1), 1, 1, 1); |
michael@0 | 240 | |
michael@0 | 241 | // try to copy 0 bytes from past the end of the source buffer |
michael@0 | 242 | b.copy(new Buffer(1), 0, 2048, 2048); |
michael@0 | 243 | }; |
michael@0 | 244 | |
michael@0 | 245 | exports.testBufferWrite = function (assert) { |
michael@0 | 246 | let b = Buffer(1024); |
michael@0 | 247 | b.fill(0); |
michael@0 | 248 | |
michael@0 | 249 | assert.throws(() => { |
michael@0 | 250 | b.write('test string', 0, 5, 'invalid'); |
michael@0 | 251 | }, TypeError, 'invalid encoding with buffer write throws'); |
michael@0 | 252 | // try to write a 0-length string beyond the end of b |
michael@0 | 253 | assert.throws(function() { |
michael@0 | 254 | b.write('', 2048); |
michael@0 | 255 | }, RangeError, 'writing a 0-length string beyond buffer throws'); |
michael@0 | 256 | // throw when writing to negative offset |
michael@0 | 257 | assert.throws(function() { |
michael@0 | 258 | b.write('a', -1); |
michael@0 | 259 | }, RangeError, 'writing negative offset on buffer throws'); |
michael@0 | 260 | |
michael@0 | 261 | // throw when writing past bounds from the pool |
michael@0 | 262 | assert.throws(function() { |
michael@0 | 263 | b.write('a', 2048); |
michael@0 | 264 | }, RangeError, 'writing past buffer bounds from pool throws'); |
michael@0 | 265 | |
michael@0 | 266 | // testing for smart defaults and ability to pass string values as offset |
michael@0 | 267 | |
michael@0 | 268 | // previous write API was the following: |
michael@0 | 269 | // write(string, encoding, offset, length) |
michael@0 | 270 | // this is planned on being removed in node v0.13, |
michael@0 | 271 | // we will not support it |
michael@0 | 272 | var writeTest = new Buffer('abcdes'); |
michael@0 | 273 | writeTest.write('n', 'utf8'); |
michael@0 | 274 | // writeTest.write('o', 'utf8', '1'); |
michael@0 | 275 | writeTest.write('d', '2', 'utf8'); |
michael@0 | 276 | writeTest.write('e', 3, 'utf8'); |
michael@0 | 277 | // writeTest.write('j', 'utf8', 4); |
michael@0 | 278 | assert.equal(writeTest.toString(), 'nbdees', |
michael@0 | 279 | 'buffer write API alternative syntax works'); |
michael@0 | 280 | }; |
michael@0 | 281 | |
michael@0 | 282 | exports.testBufferWriteEncoding = function (assert) { |
michael@0 | 283 | |
michael@0 | 284 | // Node #1210 Test UTF-8 string includes null character |
michael@0 | 285 | var buf = new Buffer('\0'); |
michael@0 | 286 | assert.equal(buf.length, 1); |
michael@0 | 287 | buf = new Buffer('\0\0'); |
michael@0 | 288 | assert.equal(buf.length, 2); |
michael@0 | 289 | |
michael@0 | 290 | buf = new Buffer(2); |
michael@0 | 291 | var written = buf.write(''); // 0byte |
michael@0 | 292 | assert.equal(written, 0); |
michael@0 | 293 | written = buf.write('\0'); // 1byte (v8 adds null terminator) |
michael@0 | 294 | assert.equal(written, 1); |
michael@0 | 295 | written = buf.write('a\0'); // 1byte * 2 |
michael@0 | 296 | assert.equal(written, 2); |
michael@0 | 297 | // TODO, these tests write 0, possibly due to character encoding |
michael@0 | 298 | /* |
michael@0 | 299 | written = buf.write('あ'); // 3bytes |
michael@0 | 300 | assert.equal(written, 0); |
michael@0 | 301 | written = buf.write('\0あ'); // 1byte + 3bytes |
michael@0 | 302 | assert.equal(written, 1); |
michael@0 | 303 | */ |
michael@0 | 304 | written = buf.write('\0\0あ'); // 1byte * 2 + 3bytes |
michael@0 | 305 | buf = new Buffer(10); |
michael@0 | 306 | written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator) |
michael@0 | 307 | assert.equal(written, 9); |
michael@0 | 308 | written = buf.write('あいう\0'); // 3bytes * 3 + 1byte |
michael@0 | 309 | assert.equal(written, 10); |
michael@0 | 310 | }; |
michael@0 | 311 | |
michael@0 | 312 | exports.testBufferWriteWithMaxLength = function (assert) { |
michael@0 | 313 | // Node #243 Test write() with maxLength |
michael@0 | 314 | var buf = new Buffer(4); |
michael@0 | 315 | buf.fill(0xFF); |
michael@0 | 316 | var written = buf.write('abcd', 1, 2, 'utf8'); |
michael@0 | 317 | assert.equal(written, 2); |
michael@0 | 318 | assert.equal(buf[0], 0xFF); |
michael@0 | 319 | assert.equal(buf[1], 0x61); |
michael@0 | 320 | assert.equal(buf[2], 0x62); |
michael@0 | 321 | assert.equal(buf[3], 0xFF); |
michael@0 | 322 | |
michael@0 | 323 | buf.fill(0xFF); |
michael@0 | 324 | written = buf.write('abcd', 1, 4); |
michael@0 | 325 | assert.equal(written, 3); |
michael@0 | 326 | assert.equal(buf[0], 0xFF); |
michael@0 | 327 | assert.equal(buf[1], 0x61); |
michael@0 | 328 | assert.equal(buf[2], 0x62); |
michael@0 | 329 | assert.equal(buf[3], 0x63); |
michael@0 | 330 | |
michael@0 | 331 | buf.fill(0xFF); |
michael@0 | 332 | // Ignore legacy API |
michael@0 | 333 | /* |
michael@0 | 334 | written = buf.write('abcd', 'utf8', 1, 2); // legacy style |
michael@0 | 335 | console.log(buf); |
michael@0 | 336 | assert.equal(written, 2); |
michael@0 | 337 | assert.equal(buf[0], 0xFF); |
michael@0 | 338 | assert.equal(buf[1], 0x61); |
michael@0 | 339 | assert.equal(buf[2], 0x62); |
michael@0 | 340 | assert.equal(buf[3], 0xFF); |
michael@0 | 341 | */ |
michael@0 | 342 | }; |
michael@0 | 343 | |
michael@0 | 344 | exports.testBufferSlice = function (assert) { |
michael@0 | 345 | var asciiString = 'hello world'; |
michael@0 | 346 | var offset = 100; |
michael@0 | 347 | var b = Buffer(1024); |
michael@0 | 348 | b.fill(0); |
michael@0 | 349 | |
michael@0 | 350 | for (var i = 0; i < asciiString.length; i++) { |
michael@0 | 351 | b[i] = asciiString.charCodeAt(i); |
michael@0 | 352 | } |
michael@0 | 353 | var asciiSlice = b.toString('utf8', 0, asciiString.length); |
michael@0 | 354 | assert.equal(asciiString, asciiSlice); |
michael@0 | 355 | |
michael@0 | 356 | var written = b.write(asciiString, offset, 'utf8'); |
michael@0 | 357 | assert.equal(asciiString.length, written); |
michael@0 | 358 | asciiSlice = b.toString('utf8', offset, offset + asciiString.length); |
michael@0 | 359 | assert.equal(asciiString, asciiSlice); |
michael@0 | 360 | |
michael@0 | 361 | var sliceA = b.slice(offset, offset + asciiString.length); |
michael@0 | 362 | var sliceB = b.slice(offset, offset + asciiString.length); |
michael@0 | 363 | compareBuffers(assert, sliceA, sliceB, |
michael@0 | 364 | 'slicing is idempotent'); |
michael@0 | 365 | |
michael@0 | 366 | let sliceTest = true; |
michael@0 | 367 | for (var j = 0; j < 100; j++) { |
michael@0 | 368 | var slice = b.slice(100, 150); |
michael@0 | 369 | if (50 !== slice.length) |
michael@0 | 370 | sliceTest = false; |
michael@0 | 371 | for (var i = 0; i < 50; i++) { |
michael@0 | 372 | if (b[100 + i] !== slice[i]) |
michael@0 | 373 | sliceTest = false; |
michael@0 | 374 | } |
michael@0 | 375 | } |
michael@0 | 376 | assert.ok(sliceTest, 'massive slice runs do not affect buffer'); |
michael@0 | 377 | |
michael@0 | 378 | // Single argument slice |
michael@0 | 379 | let testBuf = new Buffer('abcde'); |
michael@0 | 380 | assert.equal('bcde', testBuf.slice(1).toString(), 'single argument slice'); |
michael@0 | 381 | |
michael@0 | 382 | // slice(0,0).length === 0 |
michael@0 | 383 | assert.equal(0, Buffer('hello').slice(0, 0).length, 'slice(0,0) === 0'); |
michael@0 | 384 | |
michael@0 | 385 | var buf = new Buffer('0123456789'); |
michael@0 | 386 | assert.equal(buf.slice(-10, 10), '0123456789', 'buffer slice range correct'); |
michael@0 | 387 | assert.equal(buf.slice(-20, 10), '0123456789', 'buffer slice range correct'); |
michael@0 | 388 | assert.equal(buf.slice(-20, -10), '', 'buffer slice range correct'); |
michael@0 | 389 | assert.equal(buf.slice(0, -1), '012345678', 'buffer slice range correct'); |
michael@0 | 390 | assert.equal(buf.slice(2, -2), '234567', 'buffer slice range correct'); |
michael@0 | 391 | assert.equal(buf.slice(0, 65536), '0123456789', 'buffer slice range correct'); |
michael@0 | 392 | assert.equal(buf.slice(65536, 0), '', 'buffer slice range correct'); |
michael@0 | 393 | |
michael@0 | 394 | let sliceTest = true; |
michael@0 | 395 | for (var i = 0, s = buf.toString(); i < buf.length; ++i) { |
michael@0 | 396 | if (buf.slice(-i) != s.slice(-i)) sliceTest = false; |
michael@0 | 397 | if (buf.slice(0, -i) != s.slice(0, -i)) sliceTest = false; |
michael@0 | 398 | } |
michael@0 | 399 | assert.ok(sliceTest, 'buffer.slice should be consistent'); |
michael@0 | 400 | |
michael@0 | 401 | // Make sure modifying a sliced buffer, affects original and vice versa |
michael@0 | 402 | b.fill(0); |
michael@0 | 403 | let sliced = b.slice(0, 10); |
michael@0 | 404 | let babyslice = sliced.slice(0, 5); |
michael@0 | 405 | |
michael@0 | 406 | for (let i = 0; i < sliced.length; i++) |
michael@0 | 407 | sliced[i] = 'jetpack'.charAt(i); |
michael@0 | 408 | |
michael@0 | 409 | compareBuffers(assert, b, sliced, |
michael@0 | 410 | 'modifying sliced buffer affects original'); |
michael@0 | 411 | |
michael@0 | 412 | compareBuffers(assert, b, babyslice, |
michael@0 | 413 | 'modifying sliced buffer affects child-sliced buffer'); |
michael@0 | 414 | |
michael@0 | 415 | for (let i = 0; i < sliced.length; i++) |
michael@0 | 416 | b[i] = 'odinmonkey'.charAt(i); |
michael@0 | 417 | |
michael@0 | 418 | compareBuffers(assert, b, sliced, |
michael@0 | 419 | 'modifying original buffer affects sliced'); |
michael@0 | 420 | |
michael@0 | 421 | compareBuffers(assert, b, babyslice, |
michael@0 | 422 | 'modifying original buffer affects grandchild sliced buffer'); |
michael@0 | 423 | }; |
michael@0 | 424 | |
michael@0 | 425 | exports.testSlicingParents = function (assert) { |
michael@0 | 426 | let root = Buffer(5); |
michael@0 | 427 | let child = root.slice(0, 4); |
michael@0 | 428 | let grandchild = child.slice(0, 3); |
michael@0 | 429 | |
michael@0 | 430 | assert.equal(root.parent, undefined, 'a new buffer should not have a parent'); |
michael@0 | 431 | |
michael@0 | 432 | // make sure a zero length slice doesn't set the .parent attribute |
michael@0 | 433 | assert.equal(root.slice(0,0).parent, undefined, |
michael@0 | 434 | '0-length slice should not have a parent'); |
michael@0 | 435 | |
michael@0 | 436 | assert.equal(child.parent, root, |
michael@0 | 437 | 'a valid slice\'s parent should be the original buffer (child)'); |
michael@0 | 438 | |
michael@0 | 439 | assert.equal(grandchild.parent, root, |
michael@0 | 440 | 'a valid slice\'s parent should be the original buffer (grandchild)'); |
michael@0 | 441 | }; |
michael@0 | 442 | |
michael@0 | 443 | exports.testIsBuffer = function (assert) { |
michael@0 | 444 | let buffer = new Buffer('content', 'utf8'); |
michael@0 | 445 | assert.ok(Buffer.isBuffer(buffer), 'isBuffer truthy on buffers'); |
michael@0 | 446 | assert.ok(!Buffer.isBuffer({}), 'isBuffer falsy on objects'); |
michael@0 | 447 | assert.ok(!Buffer.isBuffer(new Uint8Array()), |
michael@0 | 448 | 'isBuffer falsy on Uint8Array'); |
michael@0 | 449 | assert.ok(Buffer.isBuffer(buffer.slice(0)), 'Buffer#slice should be a new buffer'); |
michael@0 | 450 | }; |
michael@0 | 451 | |
michael@0 | 452 | exports.testBufferConcat = function (assert) { |
michael@0 | 453 | let zero = []; |
michael@0 | 454 | let one = [ new Buffer('asdf') ]; |
michael@0 | 455 | let long = []; |
michael@0 | 456 | for (let i = 0; i < 10; i++) long.push(new Buffer('asdf')); |
michael@0 | 457 | |
michael@0 | 458 | let flatZero = Buffer.concat(zero); |
michael@0 | 459 | let flatOne = Buffer.concat(one); |
michael@0 | 460 | let flatLong = Buffer.concat(long); |
michael@0 | 461 | let flatLongLen = Buffer.concat(long, 40); |
michael@0 | 462 | |
michael@0 | 463 | assert.equal(flatZero.length, 0); |
michael@0 | 464 | assert.equal(flatOne.toString(), 'asdf'); |
michael@0 | 465 | assert.equal(flatOne, one[0]); |
michael@0 | 466 | assert.ok(flatLong.toString(), (new Array(10+1).join('asdf'))); |
michael@0 | 467 | assert.equal(flatLongLen.toString(), (new Array(10+1).join('asdf'))); |
michael@0 | 468 | }; |
michael@0 | 469 | |
michael@0 | 470 | exports.testBufferByteLength = function (assert) { |
michael@0 | 471 | let str = '\u00bd + \u00bc = \u00be'; |
michael@0 | 472 | assert.equal(Buffer.byteLength(str), 12, |
michael@0 | 473 | 'correct byteLength of string'); |
michael@0 | 474 | |
michael@0 | 475 | assert.equal(14, Buffer.byteLength('Il était tué')); |
michael@0 | 476 | assert.equal(14, Buffer.byteLength('Il était tué', 'utf8')); |
michael@0 | 477 | // We do not currently support these encodings |
michael@0 | 478 | /* |
michael@0 | 479 | ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) { |
michael@0 | 480 | assert.equal(24, Buffer.byteLength('Il était tué', encoding)); |
michael@0 | 481 | }); |
michael@0 | 482 | assert.equal(12, Buffer.byteLength('Il était tué', 'ascii')); |
michael@0 | 483 | assert.equal(12, Buffer.byteLength('Il était tué', 'binary')); |
michael@0 | 484 | */ |
michael@0 | 485 | }; |
michael@0 | 486 | |
michael@0 | 487 | exports.testTextEncoderDecoder = function (assert) { |
michael@0 | 488 | assert.ok(TextEncoder, 'TextEncoder exists'); |
michael@0 | 489 | assert.ok(TextDecoder, 'TextDecoder exists'); |
michael@0 | 490 | }; |
michael@0 | 491 | |
michael@0 | 492 | exports.testOverflowedBuffers = function (assert) { |
michael@0 | 493 | assert.throws(function() { |
michael@0 | 494 | new Buffer(0xFFFFFFFF); |
michael@0 | 495 | }, RangeError, 'correctly throws buffer overflow'); |
michael@0 | 496 | |
michael@0 | 497 | assert.throws(function() { |
michael@0 | 498 | new Buffer(0xFFFFFFFFF); |
michael@0 | 499 | }, RangeError, 'correctly throws buffer overflow'); |
michael@0 | 500 | |
michael@0 | 501 | assert.throws(function() { |
michael@0 | 502 | var buf = new Buffer(8); |
michael@0 | 503 | buf.readFloatLE(0xffffffff); |
michael@0 | 504 | }, RangeError, 'correctly throws buffer overflow with readFloatLE'); |
michael@0 | 505 | |
michael@0 | 506 | assert.throws(function() { |
michael@0 | 507 | var buf = new Buffer(8); |
michael@0 | 508 | buf.writeFloatLE(0.0, 0xffffffff); |
michael@0 | 509 | }, RangeError, 'correctly throws buffer overflow with writeFloatLE'); |
michael@0 | 510 | |
michael@0 | 511 | //ensure negative values can't get past offset |
michael@0 | 512 | assert.throws(function() { |
michael@0 | 513 | var buf = new Buffer(8); |
michael@0 | 514 | buf.readFloatLE(-1); |
michael@0 | 515 | }, RangeError, 'correctly throws with readFloatLE negative values'); |
michael@0 | 516 | |
michael@0 | 517 | assert.throws(function() { |
michael@0 | 518 | var buf = new Buffer(8); |
michael@0 | 519 | buf.writeFloatLE(0.0, -1); |
michael@0 | 520 | }, RangeError, 'correctly throws with writeFloatLE with negative values'); |
michael@0 | 521 | |
michael@0 | 522 | assert.throws(function() { |
michael@0 | 523 | var buf = new Buffer(8); |
michael@0 | 524 | buf.readFloatLE(-1); |
michael@0 | 525 | }, RangeError, 'correctly throws with readFloatLE with negative values'); |
michael@0 | 526 | }; |
michael@0 | 527 | |
michael@0 | 528 | exports.testReadWriteDataTypeErrors = function (assert) { |
michael@0 | 529 | var buf = new Buffer(0); |
michael@0 | 530 | assert.throws(function() { buf.readUInt8(0); }, RangeError, |
michael@0 | 531 | 'readUInt8(0) throws'); |
michael@0 | 532 | assert.throws(function() { buf.readInt8(0); }, RangeError, |
michael@0 | 533 | 'readInt8(0) throws'); |
michael@0 | 534 | |
michael@0 | 535 | [16, 32].forEach(function(bits) { |
michael@0 | 536 | var buf = new Buffer(bits / 8 - 1); |
michael@0 | 537 | assert.throws(function() { buf['readUInt' + bits + 'BE'](0); }, |
michael@0 | 538 | RangeError, |
michael@0 | 539 | 'readUInt' + bits + 'BE'); |
michael@0 | 540 | |
michael@0 | 541 | assert.throws(function() { buf['readUInt' + bits + 'LE'](0); }, |
michael@0 | 542 | RangeError, |
michael@0 | 543 | 'readUInt' + bits + 'LE'); |
michael@0 | 544 | |
michael@0 | 545 | assert.throws(function() { buf['readInt' + bits + 'BE'](0); }, |
michael@0 | 546 | RangeError, |
michael@0 | 547 | 'readInt' + bits + 'BE()'); |
michael@0 | 548 | |
michael@0 | 549 | assert.throws(function() { buf['readInt' + bits + 'LE'](0); }, |
michael@0 | 550 | RangeError, |
michael@0 | 551 | 'readInt' + bits + 'LE()'); |
michael@0 | 552 | }); |
michael@0 | 553 | }; |
michael@0 | 554 | |
michael@0 | 555 | safeMerge(exports, require('./buffers/test-write-types')); |
michael@0 | 556 | safeMerge(exports, require('./buffers/test-read-types')); |
michael@0 | 557 | |
michael@0 | 558 | function compareBuffers (assert, buf1, buf2, message) { |
michael@0 | 559 | let status = true; |
michael@0 | 560 | for (let i = 0; i < Math.min(buf1.length, buf2.length); i++) { |
michael@0 | 561 | if (buf1[i] !== buf2[i]) |
michael@0 | 562 | status = false; |
michael@0 | 563 | } |
michael@0 | 564 | assert.ok(status, 'buffer successfully copied: ' + message); |
michael@0 | 565 | } |
michael@0 | 566 | require('sdk/test').run(exports); |