4 * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
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.
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.
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
29 #include "../../libmsgpack/msgpack.h"
31 #define BUFFER_SIZE 4096
36 * echo 'null' | json2msgpack | xxd -i
38 static const uint8_t NIL_EXPECTED
[] = { 0xc0 };
41 * echo '"bye"' | json2msgpack | xxd -i
43 static const uint8_t STRING_BYE_EXPECTED
[] = { 0xa3, 0x62, 0x79, 0x65 };
46 * echo '1337' | json2msgpack | xxd -i
48 static const uint8_t UINT_1337_EXPECTED
[] = { 0xcd, 0x05, 0x39 };
51 * echo '127' | json2msgpack | xxd -i
53 static const uint8_t UINT_127_EXPECTED
[] = { 0x7f };
56 * echo '128' | json2msgpack | xxd -i
58 static const uint8_t UINT_128_EXPECTED
[] = { 0xcc, 0x80 };
61 * echo '256' | json2msgpack | xxd -i
63 static const uint8_t UINT_256_EXPECTED
[] = { 0xcd, 0x01, 0x00 };
66 * echo '65535' | json2msgpack | xxd -i
68 static const uint8_t UINT_65535_EXPECTED
[] = { 0xcd, 0xff, 0xff };
71 * echo '65536' | json2msgpack | xxd -i
73 static const uint8_t UINT_65536_EXPECTED
[] = { 0xce, 0x00, 0x01, 0x00, 0x00 };
76 * echo '4294967295' | json2msgpack | xxd -i
78 static const uint8_t UINT_4294967295_EXPECTED
[] = { 0xce, 0xff, 0xff, 0xff, 0xff };
81 * echo '4294967296' | json2msgpack | xxd -i
83 static const uint8_t UINT_4294967296_EXPECTED
[] = { 0xcf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 };
86 * echo '-32' | json2msgpack | xxd -i
88 static const uint8_t INT_NEG_32_EXPECTED
[] = { 0xe0 };
91 * echo '-33' | json2msgpack | xxd -i
93 static const uint8_t INT_NEG_33_EXPECTED
[] = { 0xd0, 0xdf };
96 * echo '-129' | json2msgpack | xxd -i
98 static const uint8_t INT_NEG_129_EXPECTED
[] = { 0xd1, 0xff, 0x7f};
101 * echo '-32768' | json2msgpack | xxd -i
103 static const uint8_t INT_NEG_32768_EXPECTED
[] = { 0xd1, 0x80, 0x00 };
106 * echo '-32769' | json2msgpack | xxd -i
108 static const uint8_t INT_NEG_32769_EXPECTED
[] = { 0xd2, 0xff, 0xff, 0x7f,
112 * echo '-2147483648' | json2msgpack | xxd -i
114 static const uint8_t INT_NEG_2147483648_EXPECTED
[] = { 0xd2, 0x80, 0x00, 0x00,
118 * echo '-2147483649' | json2msgpack | xxd -i
120 static const uint8_t INT_NEG_2147483649_EXPECTED
[] = { 0xd3, 0xff, 0xff, 0xff,
121 0xff, 0x7f, 0xff, 0xff, 0xff };
123 * echo '0.0' | json2msgpack | xxd -i
125 static const uint8_t DOUBLE_ZERO_EXPECTED
[] = { 0xcb, 0x00, 0x00, 0x00, 0x00, 0x00,
129 * echo '3.14159265' | json2msgpack | xxd -i
131 static const uint8_t DOUBLE_PI_EXPECTED
[] = { 0xcb, 0x40, 0x09, 0x21, 0xfb, 0x53,
135 * echo '3.14159265' | json2msgpack | xxd -i
137 static const uint8_t DOUBLE_NEG_PI_EXPECTED
[] = { 0xcb, 0xc0, 0x09, 0x21, 0xfb,
138 0x53, 0xc8, 0xd4, 0xf1 };
141 * echo [1.1, 2.3, -12345.2] | json2msgpack | xxd -i
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,
149 * echo '{"type":"enum","value":117}' | json2msgpack | xxd -i
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 };
156 * echo '["meow mix", 18, null, 14.197, [1980, 1995]]' | json2msgpack | xxd -i
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 };
163 static void string_test(uint8_t *buf
, const char *value
)
165 struct lttng_msgpack_writer writer
;
167 lttng_msgpack_writer_init(&writer
, buf
, BUFFER_SIZE
);
168 lttng_msgpack_write_str(&writer
, value
);
169 lttng_msgpack_writer_fini(&writer
);
172 static void int_test(uint8_t *buf
, int64_t value
)
174 struct lttng_msgpack_writer writer
;
176 lttng_msgpack_writer_init(&writer
, buf
, BUFFER_SIZE
);
177 lttng_msgpack_write_signed_integer(&writer
, value
);
179 lttng_msgpack_writer_fini(&writer
);
182 static void uint_test(uint8_t *buf
, uint64_t value
)
184 struct lttng_msgpack_writer writer
;
186 lttng_msgpack_writer_init(&writer
, buf
, BUFFER_SIZE
);
187 lttng_msgpack_write_unsigned_integer(&writer
, value
);
188 lttng_msgpack_writer_fini(&writer
);
191 static void double_test(uint8_t *buf
, double value
)
193 struct lttng_msgpack_writer writer
;
195 lttng_msgpack_writer_init(&writer
, buf
, BUFFER_SIZE
);
196 lttng_msgpack_write_double(&writer
, value
);
197 lttng_msgpack_writer_fini(&writer
);
200 static void array_double_test(uint8_t *buf
, double *values
, size_t nb_values
)
203 struct lttng_msgpack_writer writer
;
205 lttng_msgpack_writer_init(&writer
, buf
, BUFFER_SIZE
);
206 lttng_msgpack_begin_array(&writer
, nb_values
);
208 for (i
= 0; i
< nb_values
; i
++) {
209 lttng_msgpack_write_double(&writer
, values
[i
]);
212 lttng_msgpack_end_array(&writer
);
213 lttng_msgpack_writer_fini(&writer
);
216 static void map_test(uint8_t *buf
)
218 struct lttng_msgpack_writer writer
;
220 lttng_msgpack_writer_init(&writer
, buf
, BUFFER_SIZE
);
222 lttng_msgpack_begin_map(&writer
, 2);
224 lttng_msgpack_write_str(&writer
, "type");
225 lttng_msgpack_write_str(&writer
, "enum");
227 lttng_msgpack_write_str(&writer
, "value");
228 lttng_msgpack_write_unsigned_integer(&writer
, 117);
230 lttng_msgpack_end_map(&writer
);
231 lttng_msgpack_writer_fini(&writer
);
234 static void complete_capture_test(uint8_t *buf
)
237 * This testcase tests the following json representation:
238 * "meow mix",18, null, 14.197,[1980, 1995]]
240 struct lttng_msgpack_writer writer
;
242 lttng_msgpack_writer_init(&writer
, buf
, BUFFER_SIZE
);
244 lttng_msgpack_begin_array(&writer
, 5);
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);
251 lttng_msgpack_begin_array(&writer
, 2);
253 lttng_msgpack_write_unsigned_integer(&writer
, 1980);
254 lttng_msgpack_write_unsigned_integer(&writer
, 1995);
256 lttng_msgpack_end_array(&writer
);
258 lttng_msgpack_end_array(&writer
);
260 lttng_msgpack_writer_fini(&writer
);
263 static void nil_test(uint8_t *buf
)
265 struct lttng_msgpack_writer writer
;
267 lttng_msgpack_writer_init(&writer
, buf
, BUFFER_SIZE
);
268 lttng_msgpack_write_nil(&writer
);
269 lttng_msgpack_writer_fini(&writer
);
272 int main(int argc
, char *argv
[])
274 uint8_t buf
[BUFFER_SIZE
] = {0};
275 double arr_double
[] = {1.1, 2.3, -12345.2};
277 plan_tests(NUM_TESTS
);
279 diag("Testing msgpack implementation");
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
286 * echo 'null' | json2msgpack | hexdump -v -e '"\\\x" 1/1 "%02x"'
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.
294 ok(memcmp(buf
, NIL_EXPECTED
, sizeof(NIL_EXPECTED
)) == 0,
297 string_test(buf
, "bye");
298 ok(memcmp(buf
, STRING_BYE_EXPECTED
, sizeof(STRING_BYE_EXPECTED
)) == 0,
299 "String \"bye\" object");
301 uint_test(buf
, 1337);
302 ok(memcmp(buf
, UINT_1337_EXPECTED
, sizeof(UINT_1337_EXPECTED
)) == 0,
303 "Unsigned integer \"1337\" object");
306 ok(memcmp(buf
, UINT_127_EXPECTED
, sizeof(UINT_127_EXPECTED
)) == 0,
307 "Unsigned integer \"127\" object");
310 ok(memcmp(buf
, UINT_128_EXPECTED
, sizeof(UINT_128_EXPECTED
)) == 0,
311 "Unsigned integer \"128\" object");
314 ok(memcmp(buf
, UINT_256_EXPECTED
, sizeof(UINT_256_EXPECTED
)) == 0,
315 "Unsigned integer \"256\" object");
317 uint_test(buf
, 65536);
318 ok(memcmp(buf
, UINT_65536_EXPECTED
, sizeof(UINT_65536_EXPECTED
)) == 0,
319 "Unsigned integer \"65536\" object");
321 uint_test(buf
, 65535);
322 ok(memcmp(buf
, UINT_65535_EXPECTED
, sizeof(UINT_65535_EXPECTED
)) == 0,
323 "Unsigned integer \"65535\" object");
325 uint_test(buf
, 4294967295);
326 ok(memcmp(buf
, UINT_4294967295_EXPECTED
, sizeof(UINT_4294967295_EXPECTED
)) == 0,
327 "Unsigned integer \"4294967295\" object");
329 uint_test(buf
, 4294967296);
330 ok(memcmp(buf
, UINT_4294967296_EXPECTED
, sizeof(UINT_4294967296_EXPECTED
)) == 0,
331 "Unsigned integer \"4294967296\" object");
334 ok(memcmp(buf
, INT_NEG_32_EXPECTED
, sizeof(INT_NEG_32_EXPECTED
)) == 0,
335 "Signed integer \"-32\" object");
338 ok(memcmp(buf
, INT_NEG_33_EXPECTED
, sizeof(INT_NEG_33_EXPECTED
)) == 0,
339 "Signed integer \"-33\" object");
342 ok(memcmp(buf
, INT_NEG_129_EXPECTED
, sizeof(INT_NEG_129_EXPECTED
)) == 0,
343 "Signed integer \"-129\" object");
345 int_test(buf
, -32768);
346 ok(memcmp(buf
, INT_NEG_32768_EXPECTED
, sizeof(INT_NEG_32768_EXPECTED
)) == 0,
347 "Signed integer \"-32768\" object");
349 int_test(buf
, -32769);
350 ok(memcmp(buf
, INT_NEG_32769_EXPECTED
, sizeof(INT_NEG_32769_EXPECTED
)) == 0,
351 "Signed integer \"-32769\" object");
353 int_test(buf
, -2147483648);
354 ok(memcmp(buf
, INT_NEG_2147483648_EXPECTED
, sizeof(INT_NEG_2147483648_EXPECTED
)) == 0,
355 "Signed integer \"-2147483648\" object");
357 int_test(buf
, -2147483649);
358 ok(memcmp(buf
, INT_NEG_2147483649_EXPECTED
, sizeof(INT_NEG_2147483649_EXPECTED
)) == 0,
359 "Signed integer \"-2147483649\" object");
361 double_test(buf
, 0.0);
362 ok(memcmp(buf
, DOUBLE_ZERO_EXPECTED
, sizeof(DOUBLE_ZERO_EXPECTED
)) == 0,
363 "double \"0.0\" object");
365 double_test(buf
, 3.14159265);
366 ok(memcmp(buf
, DOUBLE_PI_EXPECTED
, sizeof(DOUBLE_PI_EXPECTED
)) == 0,
367 "double \"PI\" object");
369 double_test(buf
, -3.14159265);
370 ok(memcmp(buf
, DOUBLE_NEG_PI_EXPECTED
, sizeof(DOUBLE_NEG_PI_EXPECTED
)) == 0,
371 "double \"-PI\" object");
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");
378 ok(memcmp(buf
, MAP_EXPECTED
, sizeof(MAP_EXPECTED
)) == 0,
381 complete_capture_test(buf
);
382 ok(memcmp(buf
, COMPLETE_CAPTURE_EXPECTED
, sizeof(COMPLETE_CAPTURE_EXPECTED
)) == 0,
383 "Complete capture object");