index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. var assert = require('assert')
  2. var Reader = require('../')
  3. describe('packet-reader', function() {
  4. beforeEach(function() {
  5. this.reader = new Reader(1)
  6. })
  7. it('reads perfect 1 length buffer', function() {
  8. this.reader.addChunk(Buffer.from([0, 0, 0, 0, 1, 1]))
  9. var result = this.reader.read()
  10. assert.equal(result.length, 1)
  11. assert.equal(result[0], 1)
  12. assert.strictEqual(false, this.reader.read())
  13. })
  14. it('reads perfect longer buffer', function() {
  15. this.reader.addChunk(Buffer.from([0, 0, 0, 0, 4, 1, 2, 3, 4]))
  16. var result = this.reader.read()
  17. assert.equal(result.length, 4)
  18. assert.strictEqual(false, this.reader.read())
  19. })
  20. it('reads two parts', function() {
  21. this.reader.addChunk(Buffer.from([0, 0, 0, 0, 1]))
  22. var result = this.reader.read()
  23. assert.strictEqual(false, result)
  24. this.reader.addChunk(Buffer.from([2]))
  25. var result = this.reader.read()
  26. assert.equal(result.length, 1, 'should return 1 length buffer')
  27. assert.equal(result[0], 2)
  28. assert.strictEqual(this.reader.read(), false)
  29. })
  30. it('reads multi-part', function() {
  31. this.reader.addChunk(Buffer.from([0, 0, 0, 0, 16]))
  32. assert.equal(false, this.reader.read())
  33. this.reader.addChunk(Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]))
  34. assert.equal(false, this.reader.read())
  35. this.reader.addChunk(Buffer.from([9, 10, 11, 12, 13, 14, 15, 16]))
  36. var result = this.reader.read()
  37. assert.equal(result.length, 16)
  38. })
  39. it('resets internal buffer at end of packet', function() {
  40. this.reader.addChunk(Buffer.from([0, 0, 0, 0, 16]))
  41. this.reader.addChunk(Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]))
  42. this.reader.addChunk(Buffer.from([9, 10, 11, 12, 13, 14, 15, 16]))
  43. var result = this.reader.read()
  44. assert.equal(result.length, 16)
  45. var newChunk = Buffer.from([0, 0, 0, 0, 16])
  46. this.reader.addChunk(newChunk)
  47. assert.equal(this.reader.offset, 0, 'should have been reset to 0.')
  48. assert.strictEqual(this.reader.chunk, newChunk)
  49. })
  50. it('reads multiple messages from single chunk', function() {
  51. this.reader.addChunk(Buffer.from([0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 2, 1, 2]))
  52. var result = this.reader.read()
  53. assert.equal(result.length, 1, 'should have 1 length buffer')
  54. assert.equal(result[0], 1)
  55. var result = this.reader.read()
  56. assert.equal(result.length, 2, 'should have 2 length buffer but was ' + result.length)
  57. assert.equal(result[0], 1)
  58. assert.equal(result[1], 2)
  59. assert.strictEqual(false, this.reader.read())
  60. })
  61. it('reads 1 and a split', function() {
  62. this.reader.addChunk(Buffer.from([0, 0, 0, 0, 1, 1, 0, 0]))//, 0, 0, 2, 1, 2]))
  63. var result = this.reader.read()
  64. assert.equal(result.length, 1, 'should have 1 length buffer')
  65. assert.equal(result[0], 1)
  66. var result = this.reader.read()
  67. assert.strictEqual(result, false)
  68. this.reader.addChunk(Buffer.from([0, 0, 2, 1, 2]))
  69. var result = this.reader.read()
  70. assert.equal(result.length, 2, 'should have 2 length buffer but was ' + result.length)
  71. assert.equal(result[0], 1)
  72. assert.equal(result[1], 2)
  73. assert.strictEqual(false, this.reader.read())
  74. })
  75. })
  76. describe('variable length header', function() {
  77. beforeEach(function() {
  78. this.reader = new Reader()
  79. })
  80. it('reads double message buffers', function() {
  81. this.reader.addChunk(Buffer.from([
  82. 0, 0, 0, 1, 1,
  83. 0, 0, 0, 2, 1, 2]))
  84. var result = this.reader.read()
  85. assert(result)
  86. assert.equal(result.length, 1)
  87. assert.equal(result[0], 1)
  88. result = this.reader.read()
  89. assert(result)
  90. assert.equal(result.length, 2)
  91. assert.equal(result[0], 1)
  92. assert.equal(result[1], 2)
  93. assert.strictEqual(this.reader.read(), false)
  94. })
  95. })
  96. describe('1 length code', function() {
  97. beforeEach(function() {
  98. this.reader = new Reader(1)
  99. })
  100. it('reads code', function() {
  101. this.reader.addChunk(Buffer.from([9, 0, 0, 0, 1, 1]))
  102. var result = this.reader.read()
  103. assert(result)
  104. assert.equal(this.reader.header, 9)
  105. assert.equal(result.length, 1)
  106. assert.equal(result[0], 1)
  107. })
  108. it('is set on uncompleted read', function() {
  109. assert.equal(this.reader.header, null)
  110. this.reader.addChunk(Buffer.from([2, 0, 0, 0, 1]))
  111. assert.strictEqual(this.reader.read(), false)
  112. assert.equal(this.reader.header, 2)
  113. })
  114. })
  115. describe('postgres style packet', function() {
  116. beforeEach(function() {
  117. this.reader = new Reader({
  118. headerSize: 1,
  119. lengthPadding: -4
  120. })
  121. })
  122. it('reads with padded length', function() {
  123. this.reader.addChunk(Buffer.from([1, 0, 0, 0, 8, 0, 0, 2, 0]))
  124. var result = this.reader.read()
  125. assert(result)
  126. assert.equal(result.length, 4)
  127. assert.equal(result[0], 0)
  128. assert.equal(result[1], 0)
  129. assert.equal(result[2], 2)
  130. assert.equal(result[3], 0)
  131. })
  132. })