test-Parser.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var common = require('../../common');
  2. var test = require('utest');
  3. var assert = require('assert');
  4. var Parser = require(common.lib + '/protocol/Parser');
  5. function packet(bytes) {
  6. var buffer = new Buffer(bytes);
  7. var parser = new Parser();
  8. parser.append(buffer);
  9. return parser;
  10. }
  11. test('Parser', {
  12. "parseBuffer: buffer won\'t change after appending another one": function() {
  13. var startBuffer = new Buffer(5);
  14. startBuffer.fill('a');
  15. var parser = new Parser();
  16. parser.append(startBuffer);
  17. var value = parser.parseBuffer(4);
  18. assert.equal(value.toString(), 'aaaa');
  19. parser.append(new Buffer('b'));
  20. assert.equal(value.toString(), 'aaaa');
  21. },
  22. 'parseUnsignedNumber: 1 byte': function() {
  23. var value = packet([5]).parseUnsignedNumber(1);
  24. assert.equal(value, 5);
  25. },
  26. 'parseUnsignedNumber: 2 bytes': function() {
  27. var value = packet([1, 1]).parseUnsignedNumber(2);
  28. assert.equal(value, 256 + 1);
  29. },
  30. 'parseUnsignedNumber: honors offsets': function() {
  31. var parser = packet([1, 2]);
  32. assert.equal(parser.parseUnsignedNumber(1), 1);
  33. assert.equal(parser.parseUnsignedNumber(1), 2);
  34. },
  35. 'parseLengthCodedNumber: 1 byte': function() {
  36. var parser = packet([250]);
  37. assert.strictEqual(parser.parseLengthCodedNumber(), 250);
  38. },
  39. 'parseLengthCodedNumber: 251 = null': function() {
  40. var parser = packet([251]);
  41. assert.strictEqual(parser.parseLengthCodedNumber(), null);
  42. },
  43. 'parseLengthCodedNumber: 252 = 16 bit': function() {
  44. var parser = packet([252, 2, 1]);
  45. var expected =
  46. 2 * Math.pow(256, 0) +
  47. 1 * Math.pow(256, 1);
  48. assert.strictEqual(parser.parseLengthCodedNumber(), expected);
  49. },
  50. 'parseLengthCodedNumber: 253 = 24 bit': function() {
  51. var parser = packet([253, 3, 2, 1]);
  52. var expected =
  53. 3 * Math.pow(256, 0) +
  54. 2 * Math.pow(256, 1) +
  55. 1 * Math.pow(256, 2);
  56. assert.strictEqual(parser.parseLengthCodedNumber(), expected);
  57. },
  58. 'parseLengthCodedNumber: 254 = 64 bit': function() {
  59. var parser = packet([254, 8, 7, 6, 5, 4, 3, 2, 0]);
  60. var expected =
  61. 8 * Math.pow(256, 0) +
  62. 7 * Math.pow(256, 1) +
  63. 6 * Math.pow(256, 2) +
  64. 5 * Math.pow(256, 3) +
  65. 4 * Math.pow(256, 4) +
  66. 3 * Math.pow(256, 5) +
  67. 2 * Math.pow(256, 6) +
  68. 0 * Math.pow(256, 7);
  69. assert.strictEqual(parser.parseLengthCodedNumber(), expected);
  70. },
  71. 'parseLengthCodedNumber: < 53 bit = no problemo': function() {
  72. var parser = packet([254, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00]);
  73. assert.strictEqual(parser.parseLengthCodedNumber(), Math.pow(2, 53) - 1);
  74. },
  75. 'parseLengthCodedNumber: 53 bit = Error': function() {
  76. var parser = packet([254, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00]);
  77. assert.throws(function() {
  78. parser.parseLengthCodedNumber();
  79. }, /precision/i);
  80. },
  81. 'parseLengthCodedNumber: 255 = Error': function() {
  82. var parser = packet([255]);
  83. assert.throws(function() {
  84. parser.parseLengthCodedNumber();
  85. }, /unexpected/i);
  86. },
  87. 'parsePacketTerminatedString: regular case': function() {
  88. var parser = packet([0x48, 0x69]);
  89. parser._packetEnd = 2;
  90. var str = parser.parsePacketTerminatedString();
  91. assert.equal(str, 'Hi');
  92. },
  93. 'parsePacketTerminatedString: 0x00 terminated': function() {
  94. var parser = packet([0x48, 0x69, 0x00]);
  95. parser._packetEnd = 2;
  96. var str = parser.parsePacketTerminatedString();
  97. assert.equal(str, 'Hi');
  98. },
  99. });