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