forked from ithinuel/mqttsn-packet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
352 lines (314 loc) · 7.03 KB
/
test.js
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
'use strict';
var test = require('tape'),
mqttsn = require('./');
function testParseGenerate(name, object, buffer, opts, expect) {
test(name + ' parse', function(t) {
t.plan(2);
var parser = mqttsn.parser(opts),
expected = expect || object,
fixture = buffer;
parser.on('packet', function(packet) {
t.deepEqual(packet, expected, 'expected packet');
});
parser.on('error', function (error) {
t.error(error);
});
try {
t.equal(parser.parse(fixture), 0, 'remaining bytes');
} catch(e) {
t.error(e);
}
t.timeoutAfter(20);
});
test(name + ' generate', function(t) {
t.plan(1);
try {
t.equal(mqttsn.generate(object).toString('hex'), buffer.toString('hex'));
} catch (e) {
t.error(e);
}
});
test(name + ' mirror', function(t) {
t.plan(2);
var parser = mqttsn.parser(opts),
expected = expect || object,
fixture;
parser.on('packet', function(packet) {
t.deepEqual(packet, expected, 'expected packet');
});
parser.on('error', function (error) {
t.error(error);
});
try {
fixture = mqttsn.generate(object);
t.equal(parser.parse(fixture), 0, 'remaining bytes');
} catch (e) {
t.error(e);
}
t.timeoutAfter(20);
});
}
function testParseError(expected, fixture, message) {
test(expected, function(t) {
t.plan(2);
var parser = mqttsn.parser();
parser.on('error', function(err) {
t.equal(err.message, message, 'expected error message');
});
try {
t.equal(parser.parse(fixture), 0, 'remaining bytes');
} catch (e) {
t.error(e);
}
t.timeoutAfter(20);
});
}
function testGenerateError(expected, object, fixture) {
test(expected, function (t) {
t.plan(1);
try {
var buffer = mqttsn.generate(object);
t.fail('generate was expected to fail but get ' + buffer.toString('hex'));
} catch (e) {
t.equal(e.message, fixture, 'expected error message');
}
});
}
testParseError('parse command not supported', new Buffer([
2, 248 // header
]), 'command not supported');
testGenerateError('generate command not supported', {
cmd: 'bibapbeloola'
}, 'command not supported');
testParseGenerate('advertise', {
cmd: 'advertise',
gwId: 34,
duration: 3600
}, new Buffer([
5, 0, // Header
34, // Gateway Id
14, 16 // Duration
]));
testParseGenerate('searchgw', {
cmd: 'searchgw',
radius: 85
}, new Buffer([
3, 1, // Header
85, // radius
]));
testParseGenerate('gwinfo as server', {
cmd: 'gwinfo',
gwId: 34,
gwAdd: new Buffer([48, 24])
}, new Buffer([
3, 2, // Header
34, // Gateway Id
]), {}, {
cmd: 'gwinfo',
gwId: 34
});
testParseGenerate('gwinfo as client', {
cmd: 'gwinfo',
gwId: 34,
gwAdd: new Buffer([48, 24]),
isClient: true
}, new Buffer([
6, 2, // Header
34, // Gateway Id
2, 48, 24 // Gateway address
]), {
isClient: true
}, {
cmd: 'gwinfo',
gwId: 34,
gwAdd: new Buffer([48, 24])
});
testParseGenerate('connect', {
cmd: 'connect',
will: true,
cleanSession: true,
duration: 3600,
clientId: 'testClientId'
}, new Buffer([
18, 4, // header
12, 1, // flags & protocolId
14, 16, // duration
116, 101, 115, 116, 67, 108, 105, 101, 110, 116, 73, 100
]));
testParseGenerate('connack', {
cmd: 'connack',
returnCode: 'Accepted'
}, new Buffer([
3, 5, // header
0 // return code
]));
testParseGenerate('willtopicreq', {
cmd: 'willtopicreq'
}, new Buffer([
2, 6, // header
]));
testParseGenerate('empty willtopic', {
cmd: 'willtopic',
}, new Buffer([
2, 7, // header
]));
testParseGenerate('willtopic', {
cmd: 'willtopic',
qos: 1,
retain: true,
willTopic: 'hello/world'
}, new Buffer([
14, 7, // header
48, // flags
104, 101, 108, 108, 111, 47, 119, 111, 114, 108, 100
]));
testParseGenerate('empty willmsg', {
cmd: 'willmsg',
willMsg: ''
}, new Buffer([
2, 9, // header
]));
testParseGenerate('willmsg', {
cmd: 'willmsg',
willMsg: 'helloworld'
}, new Buffer([
12, 9, // header
104, 101, 108, 108, 111, 119, 111, 114, 108, 100
]));
testParseGenerate('register', {
cmd: 'register',
topicId: 294,
msgId: 24,
topicName: 'hello/world'
}, new Buffer([
17, 10, // header
1, 38,
0, 24,
104, 101, 108, 108, 111, 47, 119, 111, 114, 108, 100
]));
testParseGenerate('regack', {
cmd: 'regack',
topicId: 294,
msgId: 24,
returnCode: 'Rejected: congestion'
}, new Buffer([
7, 11, // header
1, 38,
0, 24,
1
]));
testParseGenerate('publish on normal topicId', {
cmd: 'publish',
dup: true,
qos: 1,
retain: true,
topicIdType: 'normal',
topicId: 294,
msgId: 24,
payload: new Buffer('{"test":"bonjour"}')
}, new Buffer([
25, 12, // header
176, // flags
1, 38, // topicId
0, 24, // msgId
0x7b, 0x22, 0x74, 0x65, 0x73, 0x74, 0x22, 0x3a, 0x22, 0x62, 0x6f, 0x6e, 0x6a, 0x6f, 0x75, 0x72, 0x22, 0x7d
]));
testParseGenerate('publish on pre-defined topicId', {
cmd: 'publish',
dup: true,
qos: 1,
retain: true,
topicIdType: 'pre-defined',
topicId: 294,
msgId: 24,
payload: new Buffer('{"test":"bonjour"}')
}, new Buffer([
25, 12, // header
177, // flags
1, 38, // topicId
0, 24, // msgId
0x7b, 0x22, 0x74, 0x65, 0x73, 0x74, 0x22, 0x3a, 0x22, 0x62, 0x6f, 0x6e, 0x6a, 0x6f, 0x75, 0x72, 0x22, 0x7d
]));
testParseGenerate('publish on short topic', {
cmd: 'publish',
dup: true,
qos: 1,
retain: true,
topicIdType: 'short topic',
topicId: 'ab',
msgId: 24,
payload: new Buffer('{"test":"bonjour"}')
}, new Buffer([
25, 12, // header
178, // flags
97, 98, // topicId
0, 24, // msgId
0x7b, 0x22, 0x74, 0x65, 0x73, 0x74, 0x22, 0x3a, 0x22, 0x62, 0x6f, 0x6e, 0x6a, 0x6f, 0x75, 0x72, 0x22, 0x7d
]));
testGenerateError('short topic is too long', {
cmd: 'publish',
dup: true,
qos: 1,
retain: true,
topicIdType: 'short topic',
topicId: 'àé',
msgId: 24
}, 'short topic must be exactly 2 bytes long');
testParseGenerate('puback', {
cmd: 'puback',
topicId: 240,
msgId: 523,
returnCode: 'Rejected: congestion'
}, new Buffer([
7, 13, // header
0, 240, // topicId
2, 11, // msgId
1 // return code
]));
testParseGenerate('pubcomp', {
cmd: 'pubcomp',
msgId: 523
}, new Buffer([
4, 14, // header
2, 11, // msgId
]));
testParseGenerate('subcribe', {
cmd: 'subscribe',
dup: true,
qos: 1,
msgId: 523,
topicIdType: 'normal',
topicName: 'hello/world'
}, new Buffer([
16, 18, // header
160, // flags
2, 11, // msgId
104, 101, 108, 108, 111, 47, 119, 111, 114, 108, 100
]));
testParseGenerate('suback', {
cmd: 'suback',
qos: 1,
topicId: 523,
msgId: 302,
returnCode: 'Accepted'
}, new Buffer([
8, 19, // header
32, // flags
2, 11, // topicId
1, 46, // msgId
0 // returnCode
]));
testParseGenerate('pingreq', {
cmd: 'pingreq',
clientId: 'jean-michel'
}, new Buffer([
13, 22, // header
106, 101, 97, 110, 45, 109, 105, 99, 104, 101, 108
]));
testParseGenerate('disconnect', {
cmd: 'disconnect',
duration: 3600
}, new Buffer([
4, 24, // header
14, 16
]));