test-buffer-arraybuffer.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict';
  2. var Buffer = require('../../').Buffer;
  3. var assert = require('assert');
  4. var Buffer = require('../../').Buffer;
  5. var LENGTH = 16;
  6. var ab = new ArrayBuffer(LENGTH);
  7. var dv = new DataView(ab);
  8. var ui = new Uint8Array(ab);
  9. var buf = Buffer.from(ab);
  10. assert.ok(buf instanceof Buffer);
  11. // For backwards compatibility of old .parent property test that if buf is not
  12. // a slice then .parent should be undefined.
  13. assert.equal(buf.parent, undefined);
  14. assert.equal(buf.buffer, ab);
  15. assert.equal(buf.length, ab.byteLength);
  16. buf.fill(0xC);
  17. for (var i = 0; i < LENGTH; i++) {
  18. assert.equal(ui[i], 0xC);
  19. ui[i] = 0xF;
  20. assert.equal(buf[i], 0xF);
  21. }
  22. buf.writeUInt32LE(0xF00, 0);
  23. buf.writeUInt32BE(0xB47, 4);
  24. buf.writeDoubleLE(3.1415, 8);
  25. assert.equal(dv.getUint32(0, true), 0xF00);
  26. assert.equal(dv.getUint32(4), 0xB47);
  27. assert.equal(dv.getFloat64(8, true), 3.1415);
  28. // Now test protecting users from doing stupid things
  29. assert.throws(function() {
  30. function AB() { }
  31. Object.setPrototypeOf(AB, ArrayBuffer);
  32. Object.setPrototypeOf(AB.prototype, ArrayBuffer.prototype);
  33. Buffer.from(new AB());
  34. }, TypeError);
  35. // write{Double,Float}{LE,BE} with noAssert should not crash, cf. #3766
  36. var b = Buffer.allocUnsafe(1);
  37. b.writeFloatLE(11.11, 0, true);
  38. b.writeFloatBE(11.11, 0, true);
  39. b.writeDoubleLE(11.11, 0, true);
  40. b.writeDoubleBE(11.11, 0, true);
  41. // Test the byteOffset and length arguments
  42. {
  43. var ab = new Uint8Array(5);
  44. ab[0] = 1;
  45. ab[1] = 2;
  46. ab[2] = 3;
  47. ab[3] = 4;
  48. ab[4] = 5;
  49. var buf = Buffer.from(ab.buffer, 1, 3);
  50. assert.equal(buf.length, 3);
  51. assert.equal(buf[0], 2);
  52. assert.equal(buf[1], 3);
  53. assert.equal(buf[2], 4);
  54. buf[0] = 9;
  55. assert.equal(ab[1], 9);
  56. assert.throws(() => Buffer.from(ab.buffer, 6), (err) => {
  57. assert(err instanceof RangeError);
  58. assert(/'offset' is out of bounds/.test(err.message));
  59. return true;
  60. });
  61. assert.throws(() => Buffer.from(ab.buffer, 3, 6), (err) => {
  62. assert(err instanceof RangeError);
  63. assert(/'length' is out of bounds/.test(err.message));
  64. return true;
  65. });
  66. }
  67. // Test the deprecated Buffer() version also
  68. {
  69. var ab = new Uint8Array(5);
  70. ab[0] = 1;
  71. ab[1] = 2;
  72. ab[2] = 3;
  73. ab[3] = 4;
  74. ab[4] = 5;
  75. var buf = Buffer(ab.buffer, 1, 3);
  76. assert.equal(buf.length, 3);
  77. assert.equal(buf[0], 2);
  78. assert.equal(buf[1], 3);
  79. assert.equal(buf[2], 4);
  80. buf[0] = 9;
  81. assert.equal(ab[1], 9);
  82. assert.throws(() => Buffer(ab.buffer, 6), (err) => {
  83. assert(err instanceof RangeError);
  84. assert(/'offset' is out of bounds/.test(err.message));
  85. return true;
  86. });
  87. assert.throws(() => Buffer(ab.buffer, 3, 6), (err) => {
  88. assert(err instanceof RangeError);
  89. assert(/'length' is out of bounds/.test(err.message));
  90. return true;
  91. });
  92. }