-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmsgpack_test.dart
More file actions
295 lines (265 loc) · 7.16 KB
/
msgpack_test.dart
File metadata and controls
295 lines (265 loc) · 7.16 KB
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
import 'dart:typed_data';
import 'package:test/test.dart';
import 'package:msgpack/msgpack.dart';
var isString = predicate((e) => e is String, 'is a String');
var isInt = predicate((e) => e is int, 'is an int');
var isMap = predicate((e) => e is Map, 'is a Map');
var isList = predicate((e) => e is List, 'is a List');
class TestMessage extends Message {
int a;
String b;
Map c;
TestMessage(this.a, this.b, this.c);
static TestMessage fromList(List f) => new TestMessage(f[0], f[1], f[2]);
List toList() => [a, b, c];
}
class OuterMessage extends Message {
String a;
bool b;
List list;
TestMessage inner;
OuterMessage(this.a, this.b, this.list, this.inner);
static OuterMessage fromList(List f) =>
new OuterMessage(f[0], f[1], f[2], TestMessage.fromList(f[3]));
List toList() => [a, b, list, inner];
}
void main() {
test("Pack 5-character string", packString5);
test("Pack 22-character string", packString22);
test("Pack 256-character string", packString256);
test("Pack .NET SDK Test", packDSA);
test("Pack negative number -24577", packNegative1);
test("Pack negative number -245778641", packNegative2);
test("Pack string array", packStringArray);
test("Pack int-to-string map", packIntToStringMap);
//test("Pack 3-field message", packMessage);
//test("Pack nested message", packNestedMessage);
test("Unpack 5-character string", unpackString5);
test("Unpack 22-character string", unpackString22);
test("Unpack 256-character string", unpackString256);
test("Unpack string array", unpackStringArray);
test("Unpack int-to-string map", unpackIntToStringMap);
test("Unpack 3-field message", unpackMessage);
test("Unpack nested message", unpackNestedMessage);
}
// Test packing
void packString5() {
List<int> encoded = pack("hello");
expect(encoded, orderedEquals([165, 104, 101, 108, 108, 111]));
}
void packDSA() {
// Use http://kawanet.github.io/msgpack-lite/ to test decode
// 81 A3 6D 73 67 D1 00 EB
List<int> testObjData = [0x81, 0xA3, 0x6D, 0x73, 0x67, 0xD1, 0x00, 0xEB];
Object obj=unpack(testObjData);
expect(unpack(testObjData)["msg"], 235);
}
void packNegative1() {
List<int> encoded = pack(-24577);
expect(encoded, orderedEquals([0xd1,0x9f,0xff]));
Object decoded = unpack(encoded);
expect(decoded, -24577);
}
void packNegative2() {
List<int> encoded = pack(-245778641);
expect(encoded, orderedEquals([0xd2,0xf1,0x59,0xb7,0x2f]));
Object decoded = unpack(encoded);
expect(decoded, -245778641);
}
void packString22() {
List<int> encoded = pack("hello there, everyone!");
expect(encoded, orderedEquals([
182,
104,
101,
108,
108,
111,
32,
116,
104,
101,
114,
101,
44,
32,
101,
118,
101,
114,
121,
111,
110,
101,
33
]));
}
void packString256() {
List<int> encoded = pack(
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
expect(encoded, hasLength(259));
expect(encoded.sublist(0, 3), orderedEquals([218, 1, 0]));
expect(encoded.sublist(3, 259), everyElement(65));
}
void packStringArray() {
List<int> encoded = pack(["one", "two", "three"]);
expect(encoded, orderedEquals(
[147, 163, 111, 110, 101, 163, 116, 119, 111, 165, 116, 104, 114, 101, 101
]));
}
void packIntToStringMap() {
List<int> encoded = pack({1: "one", 2: "two"});
expect(encoded,
orderedEquals([130, 1, 163, 111, 110, 101, 2, 163, 116, 119, 111]));
}
void packMessage() {
Message message = new TestMessage(1, "one", {2: "two"});
List<int> encoded = pack(message);
expect(encoded,
orderedEquals([147, 1, 163, 111, 110, 101, 129, 2, 163, 116, 119, 111]));
}
void packNestedMessage() {
Message inner = new TestMessage(1, "one", {2: "two"});
Message outer = new OuterMessage("three", true, [4, 5, 6], inner);
List<int> encoded = pack(outer);
expect(encoded, orderedEquals([
148,
165,
116,
104,
114,
101,
101,
195,
147,
4,
5,
6,
147,
1,
163,
111,
110,
101,
129,
2,
163,
116,
119,
111
]));
}
// Test unpacking
void unpackString5() {
Uint8List data = new Uint8List.fromList([165, 104, 101, 108, 108, 111]);
Unpacker unpacker = new Unpacker(data.buffer);
var value = unpacker.unpack();
expect(value, isString);
expect(value, equals("hello"));
}
void unpackString22() {
Uint8List data = new Uint8List.fromList([
182,
104,
101,
108,
108,
111,
32,
116,
104,
101,
114,
101,
44,
32,
101,
118,
101,
114,
121,
111,
110,
101,
33
]);
Unpacker unpacker = new Unpacker(data.buffer);
var value = unpacker.unpack();
expect(value, isString);
expect(value, equals("hello there, everyone!"));
}
void unpackString256() {
Uint8List data = new Uint8List.fromList(
[218, 1, 0]..addAll(new List.filled(256, 65)));
Unpacker unpacker = new Unpacker(data.buffer);
var value = unpacker.unpack();
expect(value, isString);
expect(value, equals(
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
}
void unpackStringArray() {
Uint8List data = new Uint8List.fromList(
[147, 163, 111, 110, 101, 163, 116, 119, 111, 165, 116, 104, 114, 101, 101
]);
Unpacker unpacker = new Unpacker(data.buffer);
var value = unpacker.unpack();
expect(value, isList);
expect(value, orderedEquals(["one", "two", "three"]));
}
void unpackIntToStringMap() {
Uint8List data = new Uint8List.fromList(
[130, 1, 163, 111, 110, 101, 2, 163, 116, 119, 111]);
Unpacker unpacker = new Unpacker(data.buffer);
var value = unpacker.unpack();
expect(value, isMap);
expect(value[1], equals("one"));
expect(value[2], equals("two"));
}
void unpackMessage() {
Uint8List data = new Uint8List.fromList(
[147, 1, 163, 111, 110, 101, 129, 2, 163, 116, 119, 111]);
Unpacker unpacker = new Unpacker(data.buffer);
var value = unpacker.unpackMessage(TestMessage.fromList);
expect(value, predicate((x) => x is TestMessage));
expect(value.a, equals(1));
expect(value.b, equals("one"));
expect(value.c[2], equals("two"));
}
void unpackNestedMessage() {
Uint8List data = new Uint8List.fromList([
148,
165,
116,
104,
114,
101,
101,
195,
147,
4,
5,
6,
147,
1,
163,
111,
110,
101,
129,
2,
163,
116,
119,
111
]);
Unpacker unpacker = new Unpacker(data.buffer);
var value = unpacker.unpackMessage(OuterMessage.fromList);
expect(value, predicate((x) => x is OuterMessage));
expect(value.a, equals("three"));
expect(value.b, equals(true));
expect(value.list, orderedEquals([4, 5, 6]));
expect(value.inner, predicate((x) => x is TestMessage));
expect(value.inner.a, equals(1));
expect(value.inner.b, equals("one"));
expect(value.inner.c[2], equals("two"));
}