Commit | Line | Data |
---|---|---|
49705576 FD |
1 | /* |
2 | * test_msgpack.c | |
3 | * | |
4 | * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com> | |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; only | |
9 | * version 2.1 of the License. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <assert.h> | |
22 | #include <stdint.h> | |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | ||
27 | #include "tap.h" | |
28 | ||
29 | #include "../../libmsgpack/msgpack.h" | |
30 | ||
31 | #define BUFFER_SIZE 4096 | |
32 | #define NUM_TESTS 23 | |
33 | ||
34 | ||
35 | /* | |
36 | * echo 'null' | json2msgpack | xxd -i | |
37 | */ | |
38 | static const uint8_t NIL_EXPECTED[] = { 0xc0 }; | |
39 | ||
40 | /* | |
41 | * echo '"bye"' | json2msgpack | xxd -i | |
42 | */ | |
43 | static const uint8_t STRING_BYE_EXPECTED[] = { 0xa3, 0x62, 0x79, 0x65 }; | |
44 | ||
45 | /* | |
46 | * echo '1337' | json2msgpack | xxd -i | |
47 | */ | |
48 | static const uint8_t UINT_1337_EXPECTED[] = { 0xcd, 0x05, 0x39 }; | |
49 | ||
50 | /* | |
51 | * echo '127' | json2msgpack | xxd -i | |
52 | */ | |
53 | static const uint8_t UINT_127_EXPECTED[] = { 0x7f }; | |
54 | ||
55 | /* | |
56 | * echo '128' | json2msgpack | xxd -i | |
57 | */ | |
58 | static const uint8_t UINT_128_EXPECTED[] = { 0xcc, 0x80 }; | |
59 | ||
60 | /* | |
61 | * echo '256' | json2msgpack | xxd -i | |
62 | */ | |
63 | static const uint8_t UINT_256_EXPECTED[] = { 0xcd, 0x01, 0x00 }; | |
64 | ||
65 | /* | |
66 | * echo '65535' | json2msgpack | xxd -i | |
67 | */ | |
68 | static const uint8_t UINT_65535_EXPECTED[] = { 0xcd, 0xff, 0xff }; | |
69 | ||
70 | /* | |
71 | * echo '65536' | json2msgpack | xxd -i | |
72 | */ | |
73 | static const uint8_t UINT_65536_EXPECTED[] = { 0xce, 0x00, 0x01, 0x00, 0x00 }; | |
74 | ||
75 | /* | |
76 | * echo '4294967295' | json2msgpack | xxd -i | |
77 | */ | |
78 | static const uint8_t UINT_4294967295_EXPECTED[] = { 0xce, 0xff, 0xff, 0xff, 0xff }; | |
79 | ||
80 | /* | |
81 | * echo '4294967296' | json2msgpack | xxd -i | |
82 | */ | |
83 | static const uint8_t UINT_4294967296_EXPECTED[] = { 0xcf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 }; | |
84 | ||
85 | /* | |
86 | * echo '-32' | json2msgpack | xxd -i | |
87 | */ | |
88 | static const uint8_t INT_NEG_32_EXPECTED[] = { 0xe0 }; | |
89 | ||
90 | /* | |
91 | * echo '-33' | json2msgpack | xxd -i | |
92 | */ | |
93 | static const uint8_t INT_NEG_33_EXPECTED[] = { 0xd0, 0xdf }; | |
94 | ||
95 | /* | |
96 | * echo '-129' | json2msgpack | xxd -i | |
97 | */ | |
98 | static const uint8_t INT_NEG_129_EXPECTED[] = { 0xd1, 0xff, 0x7f}; | |
99 | ||
100 | /* | |
101 | * echo '-32768' | json2msgpack | xxd -i | |
102 | */ | |
103 | static const uint8_t INT_NEG_32768_EXPECTED[] = { 0xd1, 0x80, 0x00 }; | |
104 | ||
105 | /* | |
106 | * echo '-32769' | json2msgpack | xxd -i | |
107 | */ | |
108 | static const uint8_t INT_NEG_32769_EXPECTED[] = { 0xd2, 0xff, 0xff, 0x7f, | |
109 | 0xff }; | |
110 | ||
111 | /* | |
112 | * echo '-2147483648' | json2msgpack | xxd -i | |
113 | */ | |
114 | static const uint8_t INT_NEG_2147483648_EXPECTED[] = { 0xd2, 0x80, 0x00, 0x00, | |
115 | 0x00 }; | |
116 | ||
117 | /* | |
118 | * echo '-2147483649' | json2msgpack | xxd -i | |
119 | */ | |
120 | static const uint8_t INT_NEG_2147483649_EXPECTED[] = { 0xd3, 0xff, 0xff, 0xff, | |
121 | 0xff, 0x7f, 0xff, 0xff, 0xff }; | |
122 | /* | |
123 | * echo '0.0' | json2msgpack | xxd -i | |
124 | */ | |
125 | static const uint8_t DOUBLE_ZERO_EXPECTED[] = { 0xcb, 0x00, 0x00, 0x00, 0x00, 0x00, | |
126 | 0x00, 0x00, 0x00 }; | |
127 | ||
128 | /* | |
129 | * echo '3.14159265' | json2msgpack | xxd -i | |
130 | */ | |
131 | static const uint8_t DOUBLE_PI_EXPECTED[] = { 0xcb, 0x40, 0x09, 0x21, 0xfb, 0x53, | |
132 | 0xc8, 0xd4, 0xf1 }; | |
133 | ||
134 | /* | |
135 | * echo '3.14159265' | json2msgpack | xxd -i | |
136 | */ | |
137 | static const uint8_t DOUBLE_NEG_PI_EXPECTED[] = { 0xcb, 0xc0, 0x09, 0x21, 0xfb, | |
138 | 0x53, 0xc8, 0xd4, 0xf1 }; | |
139 | ||
140 | /* | |
141 | * echo [1.1, 2.3, -12345.2] | json2msgpack | xxd -i | |
142 | */ | |
143 | static const uint8_t ARRAY_DOUBLE_EXPECTED[] = { 0x93, 0xcb, 0x3f, 0xf1, 0x99, | |
144 | 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0x40, 0x02, 0x66, 0x66, | |
145 | 0x66, 0x66, 0x66, 0x66, 0xcb, 0xc0, 0xc8, 0x1c, 0x99, 0x99, | |
146 | 0x99, 0x99, 0x9a }; | |
147 | ||
148 | /* | |
149 | * echo '{"type":"enum","value":117}' | json2msgpack | xxd -i | |
150 | */ | |
151 | static const uint8_t MAP_EXPECTED[] = { | |
152 | 0x82, 0xa4, 0x74, 0x79, 0x70, 0x65, 0xa4, 0x65, 0x6e, 0x75, 0x6d, 0xa5, | |
153 | 0x76, 0x61, 0x6c, 0x75, 0x65, 0x75 }; | |
154 | ||
155 | /* | |
156 | * echo '["meow mix", 18, null, 14.197, [1980, 1995]]' | json2msgpack | xxd -i | |
157 | */ | |
158 | static const uint8_t COMPLETE_CAPTURE_EXPECTED[] = { 0x95, 0xa8, 0x6d, 0x65, | |
159 | 0x6f, 0x77, 0x20, 0x6d, 0x69, 0x78, 0x12, 0xc0, 0xcb, 0x40, | |
160 | 0x2c, 0x64, 0xdd, 0x2f, 0x1a, 0x9f, 0xbe, 0x92, 0xcd, 0x07, | |
161 | 0xbc, 0xcd, 0x07, 0xcb }; | |
162 | ||
163 | static void string_test(uint8_t *buf, const char *value) | |
164 | { | |
165 | struct lttng_msgpack_writer writer; | |
166 | ||
167 | lttng_msgpack_writer_init(&writer, buf, BUFFER_SIZE); | |
168 | lttng_msgpack_write_str(&writer, value); | |
169 | lttng_msgpack_writer_fini(&writer); | |
170 | } | |
171 | ||
172 | static void int_test(uint8_t *buf, int64_t value) | |
173 | { | |
174 | struct lttng_msgpack_writer writer; | |
175 | ||
176 | lttng_msgpack_writer_init(&writer, buf, BUFFER_SIZE); | |
177 | lttng_msgpack_write_signed_integer(&writer, value); | |
178 | ||
179 | lttng_msgpack_writer_fini(&writer); | |
180 | } | |
181 | ||
182 | static void uint_test(uint8_t *buf, uint64_t value) | |
183 | { | |
184 | struct lttng_msgpack_writer writer; | |
185 | ||
186 | lttng_msgpack_writer_init(&writer, buf, BUFFER_SIZE); | |
187 | lttng_msgpack_write_unsigned_integer(&writer, value); | |
188 | lttng_msgpack_writer_fini(&writer); | |
189 | } | |
190 | ||
191 | static void double_test(uint8_t *buf, double value) | |
192 | { | |
193 | struct lttng_msgpack_writer writer; | |
194 | ||
195 | lttng_msgpack_writer_init(&writer, buf, BUFFER_SIZE); | |
196 | lttng_msgpack_write_double(&writer, value); | |
197 | lttng_msgpack_writer_fini(&writer); | |
198 | } | |
199 | ||
200 | static void array_double_test(uint8_t *buf, double *values, size_t nb_values) | |
201 | { | |
202 | int i = 0; | |
203 | struct lttng_msgpack_writer writer; | |
204 | ||
205 | lttng_msgpack_writer_init(&writer, buf, BUFFER_SIZE); | |
206 | lttng_msgpack_begin_array(&writer, nb_values); | |
207 | ||
208 | for (i = 0; i < nb_values; i++) { | |
209 | lttng_msgpack_write_double(&writer, values[i]); | |
210 | } | |
211 | ||
212 | lttng_msgpack_end_array(&writer); | |
213 | lttng_msgpack_writer_fini(&writer); | |
214 | } | |
215 | ||
216 | static void map_test(uint8_t *buf) | |
217 | { | |
218 | struct lttng_msgpack_writer writer; | |
219 | ||
220 | lttng_msgpack_writer_init(&writer, buf, BUFFER_SIZE); | |
221 | ||
222 | lttng_msgpack_begin_map(&writer, 2); | |
223 | ||
224 | lttng_msgpack_write_str(&writer, "type"); | |
225 | lttng_msgpack_write_str(&writer, "enum"); | |
226 | ||
227 | lttng_msgpack_write_str(&writer, "value"); | |
228 | lttng_msgpack_write_unsigned_integer(&writer, 117); | |
229 | ||
230 | lttng_msgpack_end_map(&writer); | |
231 | lttng_msgpack_writer_fini(&writer); | |
232 | } | |
233 | ||
234 | static void complete_capture_test(uint8_t *buf) | |
235 | { | |
236 | /* | |
237 | * This testcase tests the following json representation: | |
238 | * "meow mix",18, null, 14.197,[1980, 1995]] | |
239 | */ | |
240 | struct lttng_msgpack_writer writer; | |
241 | ||
242 | lttng_msgpack_writer_init(&writer, buf, BUFFER_SIZE); | |
243 | ||
244 | lttng_msgpack_begin_array(&writer, 5); | |
245 | ||
246 | lttng_msgpack_write_str(&writer, "meow mix"); | |
247 | lttng_msgpack_write_signed_integer(&writer, 18); | |
248 | lttng_msgpack_write_nil(&writer); | |
249 | lttng_msgpack_write_double(&writer, 14.197); | |
250 | ||
251 | lttng_msgpack_begin_array(&writer, 2); | |
252 | ||
253 | lttng_msgpack_write_unsigned_integer(&writer, 1980); | |
254 | lttng_msgpack_write_unsigned_integer(&writer, 1995); | |
255 | ||
256 | lttng_msgpack_end_array(&writer); | |
257 | ||
258 | lttng_msgpack_end_array(&writer); | |
259 | ||
260 | lttng_msgpack_writer_fini(&writer); | |
261 | } | |
262 | ||
263 | static void nil_test(uint8_t *buf) | |
264 | { | |
265 | struct lttng_msgpack_writer writer; | |
266 | ||
267 | lttng_msgpack_writer_init(&writer, buf, BUFFER_SIZE); | |
268 | lttng_msgpack_write_nil(&writer); | |
269 | lttng_msgpack_writer_fini(&writer); | |
270 | } | |
271 | ||
272 | int main(int argc, char *argv[]) | |
273 | { | |
274 | uint8_t buf[BUFFER_SIZE] = {0}; | |
275 | double arr_double[] = {1.1, 2.3, -12345.2}; | |
276 | ||
277 | plan_tests(NUM_TESTS); | |
278 | ||
279 | diag("Testing msgpack implementation"); | |
280 | ||
281 | /* | |
282 | * Expected outputs were produced using the `json2msgpack` tool. | |
283 | * https://github.com/ludocode/msgpack-tools | |
284 | * For example, here is the command to produce the null test expected | |
285 | * output: | |
286 | * echo 'null' | json2msgpack | hexdump -v -e '"\\\x" 1/1 "%02x"' | |
287 | * | |
288 | * The only exception is that we always produce 64bits integer to | |
289 | * represent integers even if they would fit into smaller objects so | |
290 | * they need to be manually crafted in 64bits two's complement (if | |
291 | * signed) big endian. | |
292 | */ | |
293 | nil_test(buf); | |
294 | ok(memcmp(buf, NIL_EXPECTED, sizeof(NIL_EXPECTED)) == 0, | |
295 | "NIL object"); | |
296 | ||
297 | string_test(buf, "bye"); | |
298 | ok(memcmp(buf, STRING_BYE_EXPECTED, sizeof(STRING_BYE_EXPECTED)) == 0, | |
299 | "String \"bye\" object"); | |
300 | ||
301 | uint_test(buf, 1337); | |
302 | ok(memcmp(buf, UINT_1337_EXPECTED, sizeof(UINT_1337_EXPECTED)) == 0, | |
303 | "Unsigned integer \"1337\" object"); | |
304 | ||
305 | uint_test(buf, 127); | |
306 | ok(memcmp(buf, UINT_127_EXPECTED, sizeof(UINT_127_EXPECTED)) == 0, | |
307 | "Unsigned integer \"127\" object"); | |
308 | ||
309 | uint_test(buf, 128); | |
310 | ok(memcmp(buf, UINT_128_EXPECTED, sizeof(UINT_128_EXPECTED)) == 0, | |
311 | "Unsigned integer \"128\" object"); | |
312 | ||
313 | uint_test(buf, 256); | |
314 | ok(memcmp(buf, UINT_256_EXPECTED, sizeof(UINT_256_EXPECTED)) == 0, | |
315 | "Unsigned integer \"256\" object"); | |
316 | ||
317 | uint_test(buf, 65536); | |
318 | ok(memcmp(buf, UINT_65536_EXPECTED, sizeof(UINT_65536_EXPECTED)) == 0, | |
319 | "Unsigned integer \"65536\" object"); | |
320 | ||
321 | uint_test(buf, 65535); | |
322 | ok(memcmp(buf, UINT_65535_EXPECTED, sizeof(UINT_65535_EXPECTED)) == 0, | |
323 | "Unsigned integer \"65535\" object"); | |
324 | ||
325 | uint_test(buf, 4294967295); | |
326 | ok(memcmp(buf, UINT_4294967295_EXPECTED, sizeof(UINT_4294967295_EXPECTED)) == 0, | |
327 | "Unsigned integer \"4294967295\" object"); | |
328 | ||
329 | uint_test(buf, 4294967296); | |
330 | ok(memcmp(buf, UINT_4294967296_EXPECTED, sizeof(UINT_4294967296_EXPECTED)) == 0, | |
331 | "Unsigned integer \"4294967296\" object"); | |
332 | ||
333 | int_test(buf, -32); | |
334 | ok(memcmp(buf, INT_NEG_32_EXPECTED, sizeof(INT_NEG_32_EXPECTED)) == 0, | |
335 | "Signed integer \"-32\" object"); | |
336 | ||
337 | int_test(buf, -33); | |
338 | ok(memcmp(buf, INT_NEG_33_EXPECTED, sizeof(INT_NEG_33_EXPECTED)) == 0, | |
339 | "Signed integer \"-33\" object"); | |
340 | ||
341 | int_test(buf, -129); | |
342 | ok(memcmp(buf, INT_NEG_129_EXPECTED, sizeof(INT_NEG_129_EXPECTED)) == 0, | |
343 | "Signed integer \"-129\" object"); | |
344 | ||
345 | int_test(buf, -32768); | |
346 | ok(memcmp(buf, INT_NEG_32768_EXPECTED, sizeof(INT_NEG_32768_EXPECTED)) == 0, | |
347 | "Signed integer \"-32768\" object"); | |
348 | ||
349 | int_test(buf, -32769); | |
350 | ok(memcmp(buf, INT_NEG_32769_EXPECTED, sizeof(INT_NEG_32769_EXPECTED)) == 0, | |
351 | "Signed integer \"-32769\" object"); | |
352 | ||
353 | int_test(buf, -2147483648); | |
354 | ok(memcmp(buf, INT_NEG_2147483648_EXPECTED, sizeof(INT_NEG_2147483648_EXPECTED)) == 0, | |
355 | "Signed integer \"-2147483648\" object"); | |
356 | ||
357 | int_test(buf, -2147483649); | |
358 | ok(memcmp(buf, INT_NEG_2147483649_EXPECTED, sizeof(INT_NEG_2147483649_EXPECTED)) == 0, | |
359 | "Signed integer \"-2147483649\" object"); | |
360 | ||
361 | double_test(buf, 0.0); | |
362 | ok(memcmp(buf, DOUBLE_ZERO_EXPECTED, sizeof(DOUBLE_ZERO_EXPECTED)) == 0, | |
363 | "double \"0.0\" object"); | |
364 | ||
365 | double_test(buf, 3.14159265); | |
366 | ok(memcmp(buf, DOUBLE_PI_EXPECTED, sizeof(DOUBLE_PI_EXPECTED)) == 0, | |
367 | "double \"PI\" object"); | |
368 | ||
369 | double_test(buf, -3.14159265); | |
370 | ok(memcmp(buf, DOUBLE_NEG_PI_EXPECTED, sizeof(DOUBLE_NEG_PI_EXPECTED)) == 0, | |
371 | "double \"-PI\" object"); | |
372 | ||
373 | array_double_test(buf, arr_double, 3); | |
374 | ok(memcmp(buf, ARRAY_DOUBLE_EXPECTED, sizeof(ARRAY_DOUBLE_EXPECTED)) == 0, | |
375 | "Array of double object"); | |
376 | ||
377 | map_test(buf); | |
378 | ok(memcmp(buf, MAP_EXPECTED, sizeof(MAP_EXPECTED)) == 0, | |
379 | "Map object"); | |
380 | ||
381 | complete_capture_test(buf); | |
382 | ok(memcmp(buf, COMPLETE_CAPTURE_EXPECTED, sizeof(COMPLETE_CAPTURE_EXPECTED)) == 0, | |
383 | "Complete capture object"); | |
384 | ||
385 | return EXIT_SUCCESS; | |
386 | } |