decoder-test.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. jest.dontMock('../index');
  2. const erlpack = require('../index.js');
  3. const helloWorldList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
  4. const helloWorldBinary = '\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B';
  5. const helloWorldListWithNull = [1, 2, 3, 4, 5, 0, 6, 7, 8, 9, 10, 11];
  6. const helloWorldBinaryWithNull = '\x01\x02\x03\x04\x05\x00\x06\x07\x08\x09\x0A\x0B';
  7. describe('unpacks', () => {
  8. it('short list via string with null byte', () => {
  9. expect(erlpack.unpack(new Buffer('\x83k\x00\x0c' + helloWorldBinaryWithNull, 'binary'))).toEqual(helloWorldListWithNull);
  10. });
  11. it('short list via string without byte', () => {
  12. expect(erlpack.unpack(new Buffer('\x83k\x00\x0b' + helloWorldBinary, 'binary'))).toEqual(helloWorldList);
  13. });
  14. it('binary with null byte', () => {
  15. expect(erlpack.unpack(new Buffer('\x83m\x00\x00\x00\x0chello\x00 world', 'binary'))).toEqual('hello\x00 world');
  16. });
  17. it('binary without null byte', () => {
  18. expect(erlpack.unpack(new Buffer('\x83m\x00\x00\x00\x0bhello world', 'binary'))).toEqual('hello world');
  19. });
  20. it('dictionary', () => {
  21. const data = new Buffer(
  22. '\x83t\x00\x00\x00\x03a\x02a\x02a\x03l\x00\x00\x00\x03a\x01a\x02a\x03jm\x00\x00\x00\x01aa\x01',
  23. 'binary'
  24. );
  25. const unpacked = erlpack.unpack(data);
  26. expect({'a': 1, 2: 2, 3: [1, 2, 3]}).toEqual(unpacked);
  27. });
  28. it('false', () => {
  29. expect(erlpack.unpack(new Buffer('\x83s\x05false', 'binary'))).toEqual(false);
  30. });
  31. it('true', () => {
  32. expect(erlpack.unpack(new Buffer('\x83s\x04true', 'binary'))).toEqual(true);
  33. });
  34. it('nil token is array', () => {
  35. expect(erlpack.unpack(new Buffer('\x83j', 'binary'))).toEqual([]);
  36. });
  37. it('nil atom is null', () => {
  38. expect(erlpack.unpack(new Buffer('\x83s\x03nil', 'binary'))).toBeNull();
  39. });
  40. it('null is null', () => {
  41. expect(erlpack.unpack(new Buffer('\x83s\x04null', 'binary'))).toBeNull();
  42. });
  43. it('floats', () => {
  44. expect(erlpack.unpack(new Buffer('\x83c2.50000000000000000000e+00\x00\x00\x00\x00\x00', 'binary'))).toEqual(2.5);
  45. expect(erlpack.unpack(new Buffer('\x83c5.15121238412343125000e+13\x00\x00\x00\x00\x00', 'binary'))).toEqual(51512123841234.31423412341435123412341342);
  46. });
  47. it('new floats', () => {
  48. expect(erlpack.unpack(new Buffer('\x83F\x40\x04\x00\x00\x00\x00\x00\x00', 'binary'))).toEqual(2.5);
  49. expect(erlpack.unpack(new Buffer('\x83F\x42\xC7\x6C\xCC\xEB\xED\x69\x28', 'binary'))).toEqual(51512123841234.31423412341435123412341342);
  50. });
  51. it('small int', () => {
  52. function check(small_int) {
  53. const expected = new Buffer(3);
  54. expected.write('\x83a', 0, 2, 'binary');
  55. expected.writeUInt8(small_int, 2);
  56. expect(erlpack.unpack(expected)).toEqual(small_int);
  57. }
  58. for(var i = 0; i < 256; ++i) {
  59. check(i);
  60. }
  61. });
  62. it('int32', () => {
  63. expect(erlpack.unpack(new Buffer('\x83b\x00\x00\x04\x00', 'binary'))).toEqual(1024);
  64. expect(erlpack.unpack(new Buffer('\x83b\x80\x00\x00\x00', 'binary'))).toEqual(-2147483648);
  65. expect(erlpack.unpack(new Buffer('\x83b\x7f\xff\xff\xff', 'binary'))).toEqual(2147483647);
  66. });
  67. it('small big ints', () => {
  68. expect(erlpack.unpack(new Buffer('\x83n\x04\x01\x01\x02\x03\x04', 'binary'))).toEqual(-67305985);
  69. expect(erlpack.unpack(new Buffer('\x83n\x04\x00\x01\x02\x03\x04', 'binary'))).toEqual(67305985);
  70. expect(erlpack.unpack(new Buffer('\x83n\x08\x01\x01\x02\x03\x04\x05\x06\x07\x08', 'binary'))).toEqual("-578437695752307201");
  71. expect(erlpack.unpack(new Buffer('\x83n\x08\x00\x01\x02\x03\x04\x05\x06\x07\x08', 'binary'))).toEqual("578437695752307201");
  72. expect(() => erlpack.unpack(new Buffer('\x83n\x0A\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A', 'binary'))).toThrow("Unable to decode big ints larger than 8 bytes");
  73. });
  74. it('large big ints', () => {
  75. expect(erlpack.unpack(new Buffer('\x83o\x00\x00\x00\x04\x01\x01\x02\x03\x04', 'binary'))).toEqual(-67305985);
  76. expect(erlpack.unpack(new Buffer('\x83o\x00\x00\x00\x04\x00\x01\x02\x03\x04', 'binary'))).toEqual(67305985);
  77. expect(erlpack.unpack(new Buffer('\x83o\x00\x00\x00\x08\x01\x01\x02\x03\x04\x05\x06\x07\x08', 'binary'))).toEqual("-578437695752307201");
  78. expect(erlpack.unpack(new Buffer('\x83o\x00\x00\x00\x08\x00\x01\x02\x03\x04\x05\x06\x07\x08', 'binary'))).toEqual("578437695752307201");
  79. expect(() => erlpack.unpack(new Buffer('\x83o\x00\x00\x00\x0A\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A', 'binary'))).toThrow("Unable to decode big ints larger than 8 bytes");
  80. });
  81. it('atoms', () => {
  82. expect(erlpack.unpack(new Buffer('\x83d\x00\x0Dguild members', 'binary'))).toEqual("guild members");
  83. });
  84. it('tuples', () => {
  85. expect(erlpack.unpack(new Buffer('\x83h\x03m\x00\x00\x00\x06vanisha\x01a\x04', 'binary'))).toEqual(['vanish', 1, 4]);
  86. expect(erlpack.unpack(new Buffer('\x83i\x00\x00\x00\x03m\x00\x00\x00\x06vanisha\x01a\x04', 'binary'))).toEqual(['vanish', 1, 4]);
  87. });
  88. it('compressed', () => {
  89. const expected = [2, Array.from("it's getting hot in here.").map(x => x.charCodeAt(0))];
  90. expect(erlpack.unpack(new Buffer('\x83l\x00\x00\x00\x02a\x02k\x00\x19it\'s getting hot in here.j', 'binary'))).toEqual(expected);
  91. expect(erlpack.unpack(new Buffer('\x83P\x00\x00\x00\x24\x78\x9C\xCB\x61\x60\x60\x60\x4A\x64\xCA\x66\x90\xCC\x2C\x51\x2F\x56\x48\x4F\x2D\x29\xC9\xCC\x4B\x57\xC8\xC8\x2F\x51\xC8\xCC\x53\xC8\x48\x2D\x4A\xD5\xCB\x02\x00\xA8\xA8\x0A\x9D', 'binary'))).toEqual(expected);
  92. });
  93. it('nested compressed', () => {
  94. const expected = [[2, Array.from("it's getting hot in here.").map(x => x.charCodeAt(0))], 3];
  95. expect(erlpack.unpack(new Buffer('\x83l\x00\x00\x00\x02l\x00\x00\x00\x02a\x02k\x00\x19it\'s getting hot in here.ja\x03j', 'binary'))).toEqual(expected);
  96. expect(erlpack.unpack(new Buffer('\x83P\x00\x00\x00\x2C\x78\x9C\xCB\x61\x60\x60\x60\xCA\x01\x11\x89\x4C\xD9\x0C\x92\x99\x25\xEA\xC5\x0A\xE9\xA9\x25\x25\x99\x79\xE9\x0A\x19\xF9\x25\x0A\x99\x79\x0A\x19\xA9\x45\xA9\x7A\x59\x89\xCC\x59\x00\xDC\xF7\x0B\xD9', 'binary'))).toEqual(expected);
  97. });
  98. it('references', () => {
  99. var reference = {
  100. "node" : "Hello",
  101. "id": [1245],
  102. "creation": 1
  103. };
  104. expect(erlpack.unpack(new Buffer('\x83em\x00\x00\x00\x05Hello\x00\x00\x04\xDD\x01', 'binary'))).toEqual(reference);
  105. reference = {
  106. "node" : "Hello",
  107. "id": [10, 15, 1245],
  108. "creation": 1
  109. };
  110. expect(erlpack.unpack(new Buffer('\x83r\x00\x03m\x00\x00\x00\x05Hello\x01\x00\x00\x00\x0A\x00\x00\x00\x0F\x00\x00\x04\xDD', 'binary'))).toEqual(reference);
  111. });
  112. it('port', () => {
  113. const port = {
  114. "node" : "Hello",
  115. "id": 1245,
  116. "creation": 1
  117. };
  118. expect(erlpack.unpack(new Buffer('\x83fm\x00\x00\x00\x05Hello\x00\x00\x04\xDD\x01', 'binary'))).toEqual(port);
  119. });
  120. it('pid', () => {
  121. const pid = {
  122. "node" : "Hello",
  123. "id": 1245,
  124. "serial": 123456,
  125. "creation": 1
  126. };
  127. expect(erlpack.unpack(new Buffer('\x83gm\x00\x00\x00\x05Hello\x00\x00\x04\xDD\x00\x01\xE2\x40\x01', 'binary'))).toEqual(pid);
  128. });
  129. it('export', () => {
  130. const exp = {
  131. "mod" : "guild_members",
  132. "fun": "append",
  133. "arity": 1
  134. };
  135. expect(erlpack.unpack(new Buffer('\x83qd\x00\x0Dguild_membersd\x00\x06appenda\x01', 'binary'))).toEqual(exp);
  136. });
  137. it('can unpack from ArrayBuffers', () => {
  138. const data = new Buffer('\x83k\x00\x0b' + helloWorldBinary, 'binary');
  139. var byteBuffer = new Uint8Array(data.length);
  140. for(var i = 0; i < data.length; ++i) {
  141. byteBuffer[i] = data[i];
  142. }
  143. expect(erlpack.unpack(byteBuffer)).toEqual(helloWorldList);
  144. });
  145. it('excepts from malformed token', () => {
  146. const data = new Buffer(
  147. '\x83q\x00\x00\x00\x03a\x02a\x02a\x03l\x00\x00\x00\x03a\x01a\x02a\x03jm\x00\x00\x00\x01aa\x01',
  148. 'binary'
  149. );
  150. expect(() => erlpack.unpack(data)).toThrow("Unsupported erlang term type identifier found");
  151. expect(() => erlpack.unpack(new Buffer('\x83k\x00', 'binary'))).toThrow("Reading two bytes passes the end of the buffer.");
  152. });
  153. it('excepts from malformed array', () => {
  154. expect(() => erlpack.unpack(new Buffer('\x83t\x00\x00\x00\x03a\x02a\x02a\x03', 'binary'))).toThrow("Unpacking beyond the end of the buffer");
  155. });
  156. it('excepts from malformed object', () => {
  157. const data = new Buffer(
  158. '\x83t\x00\x00\x00\x04a\x02a\x02a\x03l\x00\x00\x00\x03a\x01a\x02a\x03jm\x00\x00\x00\x01aa\x01',
  159. 'binary'
  160. );
  161. expect(() => erlpack.unpack(data)).toThrow("Unpacking beyond the end of the buffer");
  162. });
  163. it('excepts from malformed atom', () => {
  164. expect(() => erlpack.unpack(new Buffer('\x83s\x09true', 'binary'))).toThrow("Reading sequence past the end of the buffer.");
  165. });
  166. it('excepts from malformed integer', () => {
  167. expect(() => erlpack.unpack(new Buffer('\x83b\x00\x00\x04', 'binary'))).toThrow("Reading three bytes passes the end of the buffer.");
  168. });
  169. it('excepts from malformed float', () => {
  170. expect(() => erlpack.unpack(new Buffer('\x83c2.500000000000000e+00\x00\x00\x00\x00\x00', 'binary'))).toThrow("Reading sequence past the end of the buffer.");
  171. });
  172. it('excepts from malformed string ', () => {
  173. expect(() => erlpack.unpack(new Buffer('\x83k\x00\x0bworld', 'binary'))).toThrow("Reading sequence past the end of the buffer.");
  174. });
  175. it('excepts from malformed binary', () => {
  176. expect(() => erlpack.unpack(new Buffer('\x83m\x00\x00\x00\x0chel', 'binary'))).toThrow("Reading sequence past the end of the buffer.");
  177. });
  178. });