writer-tests.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. var Writer = require(__dirname + "/../");
  2. var assert = require('assert');
  3. var util = require('util');
  4. assert.equalBuffers = function (actual, expected) {
  5. var spit = function (actual, expected) {
  6. console.log("");
  7. console.log("actual " + util.inspect(actual));
  8. console.log("expect " + util.inspect(expected));
  9. console.log("");
  10. };
  11. if (actual.length != expected.length) {
  12. spit(actual, expected);
  13. assert.strictEqual(actual.length, expected.length);
  14. }
  15. for (var i = 0; i < actual.length; i++) {
  16. if (actual[i] != expected[i]) {
  17. spit(actual, expected);
  18. }
  19. assert.strictEqual(actual[i], expected[i]);
  20. }
  21. };
  22. suite('adding int32', function () {
  23. var testAddingInt32 = function (int, expectedBuffer) {
  24. test('writes ' + int, function () {
  25. var subject = new Writer();
  26. var result = subject.addInt32(int).join();
  27. assert.equalBuffers(result, expectedBuffer);
  28. });
  29. };
  30. testAddingInt32(0, [0, 0, 0, 0]);
  31. testAddingInt32(1, [0, 0, 0, 1]);
  32. testAddingInt32(256, [0, 0, 1, 0]);
  33. test('writes largest int32', function () {
  34. //todo need to find largest int32 when I have internet access
  35. return false;
  36. });
  37. test('writing multiple int32s', function () {
  38. var subject = new Writer();
  39. var result = subject.addInt32(1).addInt32(10).addInt32(0).join();
  40. assert.equalBuffers(result, [0, 0, 0, 1, 0, 0, 0, 0x0a, 0, 0, 0, 0]);
  41. });
  42. suite('having to resize the buffer', function () {
  43. test('after resize correct result returned', function () {
  44. var subject = new Writer(10);
  45. subject.addInt32(1).addInt32(1).addInt32(1);
  46. assert.equalBuffers(subject.join(), [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1]);
  47. });
  48. });
  49. });
  50. suite('int16', function () {
  51. test('writes 0', function () {
  52. var subject = new Writer();
  53. var result = subject.addInt16(0).join();
  54. assert.equalBuffers(result, [0, 0]);
  55. });
  56. test('writes 400', function () {
  57. var subject = new Writer();
  58. var result = subject.addInt16(400).join();
  59. assert.equalBuffers(result, [1, 0x90]);
  60. });
  61. test('writes many', function () {
  62. var subject = new Writer();
  63. var result = subject.addInt16(0).addInt16(1).addInt16(2).join();
  64. assert.equalBuffers(result, [0, 0, 0, 1, 0, 2]);
  65. });
  66. test('resizes if internal buffer fills up', function () {
  67. var subject = new Writer(3);
  68. var result = subject.addInt16(2).addInt16(3).join();
  69. assert.equalBuffers(result, [0, 2, 0, 3]);
  70. });
  71. });
  72. suite('cString', function () {
  73. test('writes empty cstring', function () {
  74. var subject = new Writer();
  75. var result = subject.addCString().join();
  76. assert.equalBuffers(result, [0]);
  77. });
  78. test('writes two empty cstrings', function () {
  79. var subject = new Writer();
  80. var result = subject.addCString("").addCString("").join();
  81. assert.equalBuffers(result, [0, 0]);
  82. });
  83. test('writes non-empty cstring', function () {
  84. var subject = new Writer();
  85. var result = subject.addCString("!!!").join();
  86. assert.equalBuffers(result, [33, 33, 33, 0]);
  87. });
  88. test('resizes if reached end', function () {
  89. var subject = new Writer(3);
  90. var result = subject.addCString("!!!").join();
  91. assert.equalBuffers(result, [33, 33, 33, 0]);
  92. });
  93. test('writes multiple cstrings', function () {
  94. var subject = new Writer();
  95. var result = subject.addCString("!").addCString("!").join();
  96. assert.equalBuffers(result, [33, 0, 33, 0]);
  97. });
  98. });
  99. test('writes char', function () {
  100. var subject = new Writer(2);
  101. var result = subject.addChar('a').addChar('b').addChar('c').join();
  102. assert.equalBuffers(result, [0x61, 0x62, 0x63]);
  103. });
  104. test('gets correct byte length', function () {
  105. var subject = new Writer(5);
  106. assert.strictEqual(subject.getByteLength(), 0);
  107. subject.addInt32(0);
  108. assert.strictEqual(subject.getByteLength(), 4);
  109. subject.addCString("!");
  110. assert.strictEqual(subject.getByteLength(), 6);
  111. });
  112. test('can add arbitrary buffer to the end', function () {
  113. var subject = new Writer(4);
  114. subject.addCString("!!!")
  115. var result = subject.add(Buffer.from("@@@")).join();
  116. assert.equalBuffers(result, [33, 33, 33, 0, 0x40, 0x40, 0x40]);
  117. });
  118. suite('can write normal string', function () {
  119. var subject = new Writer(4);
  120. var result = subject.addString("!").join();
  121. assert.equalBuffers(result, [33]);
  122. test('can write cString too', function () {
  123. var result = subject.addCString("!").join();
  124. assert.equalBuffers(result, [33, 33, 0]);
  125. });
  126. test('can resize', function () {
  127. var result = subject.addString("!!").join();
  128. assert.equalBuffers(result, [33, 33, 0, 33, 33]);
  129. });
  130. });
  131. suite('clearing', function () {
  132. var subject = new Writer();
  133. subject.addCString("@!!#!#");
  134. subject.addInt32(10401);
  135. test('clears', function () {
  136. subject.clear();
  137. assert.equalBuffers(subject.join(), []);
  138. });
  139. test('writing more', function () {
  140. var joinedResult = subject.addCString("!").addInt32(9).addInt16(2).join();
  141. assert.equalBuffers(joinedResult, [33, 0, 0, 0, 0, 9, 0, 2]);
  142. });
  143. test('returns result', function () {
  144. var flushedResult = subject.flush();
  145. assert.equalBuffers(flushedResult, [33, 0, 0, 0, 0, 9, 0, 2])
  146. });
  147. test('clears the writer', function () {
  148. assert.equalBuffers(subject.join(), [])
  149. assert.equalBuffers(subject.flush(), [])
  150. });
  151. });
  152. test("resizing to much larger", function () {
  153. var subject = new Writer(2);
  154. var string = "!!!!!!!!";
  155. var result = subject.addCString(string).flush();
  156. assert.equalBuffers(result, [33, 33, 33, 33, 33, 33, 33, 33, 0]);
  157. });
  158. suite("flush", function () {
  159. test('added as a hex code to a full writer', function () {
  160. var subject = new Writer(2);
  161. var result = subject.addCString("!").flush(0x50);
  162. assert.equalBuffers(result, [0x50, 0, 0, 0, 6, 33, 0]);
  163. });
  164. test('added as a hex code to a non-full writer', function () {
  165. var subject = new Writer(10).addCString("!");
  166. var joinedResult = subject.join(0x50);
  167. var result = subject.flush(0x50);
  168. assert.equalBuffers(result, [0x50, 0, 0, 0, 6, 33, 0]);
  169. });
  170. test('added as a hex code to a buffer which requires resizing', function () {
  171. var result = new Writer(2).addCString("!!!!!!!!").flush(0x50);
  172. assert.equalBuffers(result, [0x50, 0, 0, 0, 0x0D, 33, 33, 33, 33, 33, 33, 33, 33, 0]);
  173. });
  174. });
  175. suite("header", function () {
  176. test('adding two packets with headers', function () {
  177. var subject = new Writer(10).addCString("!");
  178. subject.addHeader(0x50);
  179. subject.addCString("!!");
  180. subject.addHeader(0x40);
  181. subject.addCString("!");
  182. var result = subject.flush(0x10);
  183. assert.equalBuffers(result, [0x50, 0, 0, 0, 6, 33, 0, 0x40, 0, 0, 0, 7, 33, 33, 0, 0x10, 0, 0, 0, 6, 33, 0]);
  184. });
  185. });