writer-tests.js 6.5 KB
Newer Older
Muddsair Sharif's avatar
Muddsair Sharif committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
var Writer = require(__dirname + "/../");

var assert = require('assert');
var util = require('util');

assert.equalBuffers = function(actual, expected) {
  var spit = function(actual, expected) {
    console.log("");
    console.log("actual " + util.inspect(actual));
    console.log("expect " + util.inspect(expected));
    console.log("");
  };
  if(actual.length != expected.length) {
    spit(actual, expected);
    assert.equal(actual.length, expected.length);
  }
  for(var i = 0; i < actual.length; i++) {
    if(actual[i] != expected[i]) {
      spit(actual, expected);
    }
    assert.equal(actual[i],expected[i]);
  }
};

suite('adding int32', function() {
  var testAddingInt32 = function(int, expectedBuffer) {
    test('writes ' + int, function() {
      var subject = new Writer();
      var result = subject.addInt32(int).join();
      assert.equalBuffers(result, expectedBuffer);
    });
  };

  testAddingInt32(0, [0, 0, 0, 0]);
  testAddingInt32(1, [0, 0, 0, 1]);
  testAddingInt32(256, [0, 0, 1, 0]);
  test('writes largest int32', function() {
    //todo need to find largest int32 when I have internet access
    return false;
  });

  test('writing multiple int32s', function() {
    var subject = new Writer();
    var result = subject.addInt32(1).addInt32(10).addInt32(0).join();
    assert.equalBuffers(result, [0, 0, 0, 1, 0, 0, 0, 0x0a, 0, 0, 0, 0]);
  });

  suite('having to resize the buffer', function() {
    test('after resize correct result returned', function() {
      var subject = new Writer(10);
      subject.addInt32(1).addInt32(1).addInt32(1);
      assert.equalBuffers(subject.join(), [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1]);
    });
  });
});

suite('int16', function() {
  test('writes 0', function() {
    var subject = new Writer();
    var result = subject.addInt16(0).join();
    assert.equalBuffers(result, [0,0]);
  });

  test('writes 400', function() {
    var subject = new Writer();
    var result = subject.addInt16(400).join();
    assert.equalBuffers(result, [1, 0x90]);
  });

  test('writes many', function() {
    var subject = new Writer();
    var result = subject.addInt16(0).addInt16(1).addInt16(2).join();
    assert.equalBuffers(result, [0, 0, 0, 1, 0, 2]);
  });

  test('resizes if internal buffer fills up', function() {
    var subject = new Writer(3);
    var result = subject.addInt16(2).addInt16(3).join();
    assert.equalBuffers(result, [0, 2, 0, 3]);
  });

});

suite('cString', function() {
  test('writes empty cstring', function() {
    var subject = new Writer();
    var result = subject.addCString().join();
    assert.equalBuffers(result, [0]);
  });
  
  test('writes two empty cstrings', function() {
    var subject = new Writer();
    var result = subject.addCString("").addCString("").join();
    assert.equalBuffers(result, [0, 0]);
  });


  test('writes non-empty cstring', function() {
    var subject = new Writer();
    var result = subject.addCString("!!!").join();
    assert.equalBuffers(result, [33, 33, 33, 0]);
  });

  test('resizes if reached end', function() {
    var subject = new Writer(3);
    var result = subject.addCString("!!!").join();
    assert.equalBuffers(result, [33, 33, 33, 0]);
  });

  test('writes multiple cstrings', function() {
    var subject = new Writer();
    var result = subject.addCString("!").addCString("!").join();
    assert.equalBuffers(result, [33, 0, 33, 0]);
  });

});

test('writes char', function() {
  var subject = new Writer(2);
  var result = subject.addChar('a').addChar('b').addChar('c').join();
  assert.equalBuffers(result, [0x61, 0x62, 0x63]);
});

test('gets correct byte length', function() {
  var subject = new Writer(5);
  assert.equal(subject.getByteLength(), 0);
  subject.addInt32(0);
  assert.equal(subject.getByteLength(), 4);
  subject.addCString("!");
  assert.equal(subject.getByteLength(), 6);
});

test('can add arbitrary buffer to the end', function() {
  var subject = new Writer(4);
  subject.addCString("!!!")
  var result = subject.add(Buffer("@@@")).join();
  assert.equalBuffers(result, [33, 33, 33, 0, 0x40, 0x40, 0x40]);
});

suite('can write normal string', function() {
  var subject = new Writer(4);
  var result = subject.addString("!").join();
  assert.equalBuffers(result, [33]);
  test('can write cString too', function() {
    var result = subject.addCString("!").join();
    assert.equalBuffers(result, [33, 33, 0]);
  });
  test('can resize', function() {
    var result = subject.addString("!!").join();
    assert.equalBuffers(result, [33, 33, 0, 33, 33]);
  });
});


suite('clearing', function() {
  var subject = new Writer();
  subject.addCString("@!!#!#");
  subject.addInt32(10401);
  test('clears', function() {
    subject.clear();
    assert.equalBuffers(subject.join(), []);
  });
  test('writing more', function() {
    var joinedResult = subject.addCString("!").addInt32(9).addInt16(2).join();
    assert.equalBuffers(joinedResult, [33, 0, 0, 0, 0, 9, 0, 2]);
  });
  test('returns result', function() {
    var flushedResult = subject.flush();
    assert.equalBuffers(flushedResult, [33, 0, 0, 0, 0, 9, 0, 2])
  });
  test('clears the writer', function() {
    assert.equalBuffers(subject.join(), [])
    assert.equalBuffers(subject.flush(), [])
  });
});

test("resizing to much larger", function() {
  var subject = new Writer(2);
  var string = "!!!!!!!!";
  var result = subject.addCString(string).flush();
  assert.equalBuffers(result, [33, 33, 33, 33, 33, 33, 33, 33, 0]);
});

suite("flush", function() {
  test('added as a hex code to a full writer', function() {
    var subject = new Writer(2);
    var result = subject.addCString("!").flush(0x50);
    assert.equalBuffers(result, [0x50, 0, 0, 0, 6, 33, 0]);
  });

  test('added as a hex code to a non-full writer', function() {
    var subject = new Writer(10).addCString("!");
    var joinedResult = subject.join(0x50);
    var result = subject.flush(0x50);
    assert.equalBuffers(result, [0x50, 0, 0, 0, 6, 33, 0]);
  });

  test('added as a hex code to a buffer which requires resizing', function() {
    var result = new Writer(2).addCString("!!!!!!!!").flush(0x50);
    assert.equalBuffers(result, [0x50, 0, 0, 0, 0x0D, 33, 33, 33, 33, 33, 33, 33, 33, 0]);
  });
});

suite("header", function() {
  test('adding two packets with headers', function() {
    var subject = new Writer(10).addCString("!");
    subject.addHeader(0x50);
    subject.addCString("!!");
    subject.addHeader(0x40);
    subject.addCString("!");
    var result = subject.flush(0x10);
    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 ]);
  });
});