You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
8.6 KiB

  1. var assert = require('assert')
  2. , Receiver = require('../lib/Receiver');
  3. require('should');
  4. require('./hybi-common');
  5. describe('Receiver', function() {
  6. it('can parse unmasked text message', function() {
  7. var p = new Receiver();
  8. var packet = '81 05 48 65 6c 6c 6f';
  9. var gotData = false;
  10. p.ontext = function(data) {
  11. gotData = true;
  12. assert.equal('Hello', data);
  13. };
  14. p.add(getBufferFromHexString(packet));
  15. gotData.should.be.ok;
  16. });
  17. it('can parse close message', function() {
  18. var p = new Receiver();
  19. var packet = '88 00';
  20. var gotClose = false;
  21. p.onclose = function(data) {
  22. gotClose = true;
  23. };
  24. p.add(getBufferFromHexString(packet));
  25. gotClose.should.be.ok;
  26. });
  27. it('can parse masked text message', function() {
  28. var p = new Receiver();
  29. var packet = '81 93 34 83 a8 68 01 b9 92 52 4f a1 c6 09 59 e6 8a 52 16 e6 cb 00 5b a1 d5';
  30. var gotData = false;
  31. p.ontext = function(data) {
  32. gotData = true;
  33. assert.equal('5:::{"name":"echo"}', data);
  34. };
  35. p.add(getBufferFromHexString(packet));
  36. gotData.should.be.ok;
  37. });
  38. it('can parse a masked text message longer than 125 bytes', function() {
  39. var p = new Receiver();
  40. var message = 'A';
  41. for (var i = 0; i < 300; ++i) message += (i % 5).toString();
  42. var packet = '81 FE ' + pack(4, message.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68'));
  43. var gotData = false;
  44. p.ontext = function(data) {
  45. gotData = true;
  46. assert.equal(message, data);
  47. };
  48. p.add(getBufferFromHexString(packet));
  49. gotData.should.be.ok;
  50. });
  51. it('can parse a really long masked text message', function() {
  52. var p = new Receiver();
  53. var message = 'A';
  54. for (var i = 0; i < 64*1024; ++i) message += (i % 5).toString();
  55. var packet = '81 FF ' + pack(16, message.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68'));
  56. var gotData = false;
  57. p.ontext = function(data) {
  58. gotData = true;
  59. assert.equal(message, data);
  60. };
  61. p.add(getBufferFromHexString(packet));
  62. gotData.should.be.ok;
  63. });
  64. it('can parse a fragmented masked text message of 300 bytes', function() {
  65. var p = new Receiver();
  66. var message = 'A';
  67. for (var i = 0; i < 300; ++i) message += (i % 5).toString();
  68. var msgpiece1 = message.substr(0, 150);
  69. var msgpiece2 = message.substr(150);
  70. var packet1 = '01 FE ' + pack(4, msgpiece1.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece1, '34 83 a8 68'));
  71. var packet2 = '80 FE ' + pack(4, msgpiece2.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece2, '34 83 a8 68'));
  72. var gotData = false;
  73. p.ontext = function(data) {
  74. gotData = true;
  75. assert.equal(message, data);
  76. };
  77. p.add(getBufferFromHexString(packet1));
  78. p.add(getBufferFromHexString(packet2));
  79. gotData.should.be.ok;
  80. });
  81. it('can parse a ping message', function() {
  82. var p = new Receiver();
  83. var message = 'Hello';
  84. var packet = '89 ' + getHybiLengthAsHexString(message.length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68'));
  85. var gotPing = false;
  86. p.onping = function(data) {
  87. gotPing = true;
  88. assert.equal(message, data);
  89. };
  90. p.add(getBufferFromHexString(packet));
  91. gotPing.should.be.ok;
  92. });
  93. it('can parse a ping with no data', function() {
  94. var p = new Receiver();
  95. var packet = '89 00';
  96. var gotPing = false;
  97. p.onping = function(data) {
  98. gotPing = true;
  99. };
  100. p.add(getBufferFromHexString(packet));
  101. gotPing.should.be.ok;
  102. });
  103. it('can parse a fragmented masked text message of 300 bytes with a ping in the middle', function() {
  104. var p = new Receiver();
  105. var message = 'A';
  106. for (var i = 0; i < 300; ++i) message += (i % 5).toString();
  107. var msgpiece1 = message.substr(0, 150);
  108. var packet1 = '01 FE ' + pack(4, msgpiece1.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece1, '34 83 a8 68'));
  109. var pingMessage = 'Hello';
  110. var pingPacket = '89 ' + getHybiLengthAsHexString(pingMessage.length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(pingMessage, '34 83 a8 68'));
  111. var msgpiece2 = message.substr(150);
  112. var packet2 = '80 FE ' + pack(4, msgpiece2.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece2, '34 83 a8 68'));
  113. var gotData = false;
  114. p.ontext = function(data) {
  115. gotData = true;
  116. assert.equal(message, data);
  117. };
  118. var gotPing = false;
  119. p.onping = function(data) {
  120. gotPing = true;
  121. assert.equal(pingMessage, data);
  122. };
  123. p.add(getBufferFromHexString(packet1));
  124. p.add(getBufferFromHexString(pingPacket));
  125. p.add(getBufferFromHexString(packet2));
  126. gotData.should.be.ok;
  127. gotPing.should.be.ok;
  128. });
  129. it('can parse a fragmented masked text message of 300 bytes with a ping in the middle, which is delievered over sevaral tcp packets', function() {
  130. var p = new Receiver();
  131. var message = 'A';
  132. for (var i = 0; i < 300; ++i) message += (i % 5).toString();
  133. var msgpiece1 = message.substr(0, 150);
  134. var packet1 = '01 FE ' + pack(4, msgpiece1.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece1, '34 83 a8 68'));
  135. var pingMessage = 'Hello';
  136. var pingPacket = '89 ' + getHybiLengthAsHexString(pingMessage.length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(pingMessage, '34 83 a8 68'));
  137. var msgpiece2 = message.substr(150);
  138. var packet2 = '80 FE ' + pack(4, msgpiece2.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece2, '34 83 a8 68'));
  139. var gotData = false;
  140. p.ontext = function(data) {
  141. gotData = true;
  142. assert.equal(message, data);
  143. };
  144. var gotPing = false;
  145. p.onping = function(data) {
  146. gotPing = true;
  147. assert.equal(pingMessage, data);
  148. };
  149. var buffers = [];
  150. buffers = buffers.concat(splitBuffer(getBufferFromHexString(packet1)));
  151. buffers = buffers.concat(splitBuffer(getBufferFromHexString(pingPacket)));
  152. buffers = buffers.concat(splitBuffer(getBufferFromHexString(packet2)));
  153. for (var i = 0; i < buffers.length; ++i) {
  154. p.add(buffers[i]);
  155. }
  156. gotData.should.be.ok;
  157. gotPing.should.be.ok;
  158. });
  159. it('can parse a 100 byte long masked binary message', function() {
  160. var p = new Receiver();
  161. var length = 100;
  162. var message = new Buffer(length);
  163. for (var i = 0; i < length; ++i) message[i] = i % 256;
  164. var originalMessage = getHexStringFromBuffer(message);
  165. var packet = '82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68'));
  166. var gotData = false;
  167. p.onbinary = function(data) {
  168. gotData = true;
  169. assert.equal(originalMessage, getHexStringFromBuffer(data));
  170. };
  171. p.add(getBufferFromHexString(packet));
  172. gotData.should.be.ok;
  173. });
  174. it('can parse a 256 byte long masked binary message', function() {
  175. var p = new Receiver();
  176. var length = 256;
  177. var message = new Buffer(length);
  178. for (var i = 0; i < length; ++i) message[i] = i % 256;
  179. var originalMessage = getHexStringFromBuffer(message);
  180. var packet = '82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68'));
  181. var gotData = false;
  182. p.onbinary = function(data) {
  183. gotData = true;
  184. assert.equal(originalMessage, getHexStringFromBuffer(data));
  185. };
  186. p.add(getBufferFromHexString(packet));
  187. gotData.should.be.ok;
  188. });
  189. it('can parse a 200kb long masked binary message', function() {
  190. var p = new Receiver();
  191. var length = 200 * 1024;
  192. var message = new Buffer(length);
  193. for (var i = 0; i < length; ++i) message[i] = i % 256;
  194. var originalMessage = getHexStringFromBuffer(message);
  195. var packet = '82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68'));
  196. var gotData = false;
  197. p.onbinary = function(data) {
  198. gotData = true;
  199. assert.equal(originalMessage, getHexStringFromBuffer(data));
  200. };
  201. p.add(getBufferFromHexString(packet));
  202. gotData.should.be.ok;
  203. });
  204. it('can parse a 200kb long unmasked binary message', function() {
  205. var p = new Receiver();
  206. var length = 200 * 1024;
  207. var message = new Buffer(length);
  208. for (var i = 0; i < length; ++i) message[i] = i % 256;
  209. var originalMessage = getHexStringFromBuffer(message);
  210. var packet = '82 ' + getHybiLengthAsHexString(length, false) + ' ' + getHexStringFromBuffer(message);
  211. var gotData = false;
  212. p.onbinary = function(data) {
  213. gotData = true;
  214. assert.equal(originalMessage, getHexStringFromBuffer(data));
  215. };
  216. p.add(getBufferFromHexString(packet));
  217. gotData.should.be.ok;
  218. });
  219. });