test-buffer.js 19 KB

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