1 /* 2 * Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #include <stdio.h> 11 #include <string.h> 12 13 #include "testutil.h" 14 #include "internal/json_enc.h" 15 16 struct helper { 17 OSSL_JSON_ENC j; 18 int init; 19 uint32_t flags; 20 BIO *mem_bio; 21 }; 22 23 static int helper_ensure(struct helper *h) 24 { 25 if (h->init) 26 return 1; 27 28 if (!TEST_ptr(h->mem_bio = BIO_new(BIO_s_mem()))) 29 return 0; 30 31 if (!ossl_json_init(&h->j, h->mem_bio, h->flags)) { 32 BIO_free_all(h->mem_bio); 33 h->mem_bio = NULL; 34 return 0; 35 } 36 37 h->init = 1; 38 return 1; 39 } 40 41 static void helper_cleanup(struct helper *h) 42 { 43 BIO_free_all(h->mem_bio); 44 h->mem_bio = NULL; 45 46 if (h->init) { 47 ossl_json_cleanup(&h->j); 48 h->init = 0; 49 } 50 } 51 52 static void helper_set_flags(struct helper *h, uint32_t flags) 53 { 54 helper_cleanup(h); 55 h->flags = flags; 56 } 57 58 struct script_word { 59 void *p; 60 uint64_t u64; 61 int64_t i64; 62 double d; 63 void (*fp)(void); 64 }; 65 66 #define OP_P(x) { (x) }, 67 #define OP_U64(x) { NULL, (x) }, 68 #define OP_I64(x) { NULL, 0, (x) }, 69 #define OP_D(x) { NULL, 0, 0, (x) }, 70 #define OP_FP(x) { NULL, 0, 0, 0, (void (*)(void))(x) }, 71 72 struct script_info { 73 const char *name, *title; 74 const struct script_word *words; 75 size_t num_words; 76 const char *expected_output; 77 size_t expected_output_len; 78 }; 79 80 typedef const struct script_info *(*info_func)(void); 81 82 enum { 83 OPK_END, 84 OPK_CALL, /* (OSSL_JSON_ENC *) */ 85 OPK_CALL_P, /* (OSSL_JSON_ENC *, const void *) */ 86 OPK_CALL_I, /* (OSSL_JSON_ENC *, int) */ 87 OPK_CALL_U64, /* (OSSL_JSON_ENC *, uint64_t) */ 88 OPK_CALL_I64, /* (OSSL_JSON_ENC *, int64_t) */ 89 OPK_CALL_D, /* (OSSL_JSON_ENC *, double) */ 90 OPK_CALL_PZ, /* (OSSL_JSON_ENC *, const void *, size_t) */ 91 OPK_ASSERT_ERROR, /* (OSSL_JSON_ENC *, int expect_error) */ 92 OPK_INIT_FLAGS /* (uint32_t flags) */ 93 }; 94 95 typedef void (*fp_type)(OSSL_JSON_ENC *); 96 typedef void (*fp_p_type)(OSSL_JSON_ENC *, const void *); 97 typedef void (*fp_i_type)(OSSL_JSON_ENC *, int); 98 typedef void (*fp_u64_type)(OSSL_JSON_ENC *, uint64_t); 99 typedef void (*fp_i64_type)(OSSL_JSON_ENC *, int64_t); 100 typedef void (*fp_d_type)(OSSL_JSON_ENC *, double); 101 typedef void (*fp_pz_type)(OSSL_JSON_ENC *, const void *, size_t); 102 103 #define OP_END() OP_U64(OPK_END) 104 #define OP_CALL(f) OP_U64(OPK_CALL) OP_FP(f) 105 #define OP_CALL_P(f, x) OP_U64(OPK_CALL_P) OP_FP(f) OP_P(x) 106 #define OP_CALL_I(f, x) OP_U64(OPK_CALL_I) OP_FP(f) OP_I64(x) 107 #define OP_CALL_U64(f, x) OP_U64(OPK_CALL_U64) OP_FP(f) OP_U64(x) 108 #define OP_CALL_I64(f, x) OP_U64(OPK_CALL_I64) OP_FP(f) OP_I64(x) 109 #define OP_CALL_D(f, x) OP_U64(OPK_CALL_D) OP_FP(f) OP_D(x) 110 #define OP_CALL_PZ(f, x, xl) OP_U64(OPK_CALL_PZ) OP_FP(f) OP_P(x) OP_U64(xl) 111 #define OP_ASSERT_ERROR(err) OP_U64(OPK_ASSERT_ERROR) OP_U64(err) 112 #define OP_INIT_FLAGS(flags) OP_U64(OPK_INIT_FLAGS) OP_U64(flags) 113 114 #define OPJ_BEGIN_O() OP_CALL(ossl_json_object_begin) 115 #define OPJ_END_O() OP_CALL(ossl_json_object_end) 116 #define OPJ_BEGIN_A() OP_CALL(ossl_json_array_begin) 117 #define OPJ_END_A() OP_CALL(ossl_json_array_end) 118 #define OPJ_NULL() OP_CALL(ossl_json_null) 119 #define OPJ_BOOL(x) OP_CALL_I(ossl_json_bool, (x)) 120 #define OPJ_U64(x) OP_CALL_U64(ossl_json_u64, (x)) 121 #define OPJ_I64(x) OP_CALL_I64(ossl_json_i64, (x)) 122 #define OPJ_KEY(x) OP_CALL_P(ossl_json_key, (x)) 123 #define OPJ_STR(x) OP_CALL_P(ossl_json_str, (x)) 124 #define OPJ_STR_LEN(x, xl) OP_CALL_PZ(ossl_json_str_len, (x), (xl)) 125 #define OPJ_STR_HEX(x, xl) OP_CALL_PZ(ossl_json_str_hex, (x), (xl)) 126 127 #define BEGIN_SCRIPT(name, title, flags) \ 128 static const struct script_info *get_script_##name(void) \ 129 { \ 130 static const char script_name[] = #name; \ 131 static const char script_title[] = #title; \ 132 \ 133 static const struct script_word script_words[] = { \ 134 OP_INIT_FLAGS(flags) 135 136 #define END_SCRIPT_EXPECTING(s, slen) \ 137 OP_END() \ 138 } \ 139 ; \ 140 static const struct script_info script_info = { \ 141 script_name, script_title, script_words, OSSL_NELEM(script_words), \ 142 (s), (slen) \ 143 }; \ 144 return &script_info; \ 145 } 146 147 #ifdef OPENSSL_SYS_VMS 148 /* 149 * The VMS C compiler recognises \u in strings, and emits a warning, which 150 * stops the build. Because we think we know what we're doing, we change that 151 * particular message to be merely informational. 152 */ 153 #pragma message informational UCNNOMAP 154 #endif 155 156 #define END_SCRIPT_EXPECTING_S(s) END_SCRIPT_EXPECTING(s, SIZE_MAX) 157 #define END_SCRIPT_EXPECTING_Q(s) END_SCRIPT_EXPECTING(#s, sizeof(#s) - 1) 158 159 #define SCRIPT(name) get_script_##name, 160 161 BEGIN_SCRIPT(null, "serialize a single null", 0) 162 OPJ_NULL() 163 END_SCRIPT_EXPECTING_Q(null) 164 165 BEGIN_SCRIPT(obj_empty, "serialize an empty object", 0) 166 OPJ_BEGIN_O() 167 OPJ_END_O() 168 END_SCRIPT_EXPECTING_Q({}) 169 170 BEGIN_SCRIPT(array_empty, "serialize an empty array", 0) 171 OPJ_BEGIN_A() 172 OPJ_END_A() 173 END_SCRIPT_EXPECTING_Q([]) 174 175 BEGIN_SCRIPT(bool_false, "serialize false", 0) 176 OPJ_BOOL(0) 177 END_SCRIPT_EXPECTING_Q(false) 178 179 BEGIN_SCRIPT(bool_true, "serialize true", 0) 180 OPJ_BOOL(1) 181 END_SCRIPT_EXPECTING_Q(true) 182 183 BEGIN_SCRIPT(u64_0, "serialize u64(0)", 0) 184 OPJ_U64(0) 185 END_SCRIPT_EXPECTING_Q(0) 186 187 BEGIN_SCRIPT(u64_1, "serialize u64(1)", 0) 188 OPJ_U64(1) 189 END_SCRIPT_EXPECTING_Q(1) 190 191 BEGIN_SCRIPT(u64_10, "serialize u64(10)", 0) 192 OPJ_U64(10) 193 END_SCRIPT_EXPECTING_Q(10) 194 195 BEGIN_SCRIPT(u64_12345, "serialize u64(12345)", 0) 196 OPJ_U64(12345) 197 END_SCRIPT_EXPECTING_Q(12345) 198 199 BEGIN_SCRIPT(u64_18446744073709551615, "serialize u64(18446744073709551615)", 0) 200 OPJ_U64(18446744073709551615ULL) 201 END_SCRIPT_EXPECTING_Q(18446744073709551615) 202 203 BEGIN_SCRIPT(i64_0, "serialize i64(0)", 0) 204 OPJ_I64(0) 205 END_SCRIPT_EXPECTING_Q(0) 206 207 BEGIN_SCRIPT(i64_1, "serialize i64(1)", 0) 208 OPJ_I64(1) 209 END_SCRIPT_EXPECTING_Q(1) 210 211 BEGIN_SCRIPT(i64_2, "serialize i64(2)", 0) 212 OPJ_I64(2) 213 END_SCRIPT_EXPECTING_Q(2) 214 215 BEGIN_SCRIPT(i64_10, "serialize i64(10)", 0) 216 OPJ_I64(10) 217 END_SCRIPT_EXPECTING_Q(10) 218 219 BEGIN_SCRIPT(i64_12345, "serialize i64(12345)", 0) 220 OPJ_I64(12345) 221 END_SCRIPT_EXPECTING_Q(12345) 222 223 BEGIN_SCRIPT(i64_9223372036854775807, "serialize i64(9223372036854775807)", 0) 224 OPJ_I64(9223372036854775807LL) 225 END_SCRIPT_EXPECTING_Q(9223372036854775807) 226 227 BEGIN_SCRIPT(i64_m1, "serialize i64(-1)", 0) 228 OPJ_I64(-1) 229 END_SCRIPT_EXPECTING_Q(-1) 230 231 BEGIN_SCRIPT(i64_m2, "serialize i64(-2)", 0) 232 OPJ_I64(-2) 233 END_SCRIPT_EXPECTING_Q(-2) 234 235 BEGIN_SCRIPT(i64_m10, "serialize i64(-10)", 0) 236 OPJ_I64(-10) 237 END_SCRIPT_EXPECTING_Q(-10) 238 239 BEGIN_SCRIPT(i64_m12345, "serialize i64(-12345)", 0) 240 OPJ_I64(-12345) 241 END_SCRIPT_EXPECTING_Q(-12345) 242 243 BEGIN_SCRIPT(i64_m9223372036854775807, "serialize i64(-9223372036854775807)", 0) 244 OPJ_I64(-9223372036854775807LL) 245 END_SCRIPT_EXPECTING_Q(-9223372036854775807) 246 247 BEGIN_SCRIPT(i64_m9223372036854775808, "serialize i64(-9223372036854775808)", 0) 248 OPJ_I64(-9223372036854775807LL - 1LL) 249 END_SCRIPT_EXPECTING_Q(-9223372036854775808) 250 251 BEGIN_SCRIPT(str_empty, "serialize \"\"", 0) 252 OPJ_STR("") 253 END_SCRIPT_EXPECTING_Q("") 254 255 BEGIN_SCRIPT(str_a, "serialize \"a\"", 0) 256 OPJ_STR("a") 257 END_SCRIPT_EXPECTING_Q("a") 258 259 BEGIN_SCRIPT(str_abc, "serialize \"abc\"", 0) 260 OPJ_STR("abc") 261 END_SCRIPT_EXPECTING_Q("abc") 262 263 BEGIN_SCRIPT(str_quote, "serialize with quote", 0) 264 OPJ_STR("abc\"def") 265 END_SCRIPT_EXPECTING_Q("abc\"def") 266 267 BEGIN_SCRIPT(str_quote2, "serialize with quote", 0) 268 OPJ_STR("abc\"\"def") 269 END_SCRIPT_EXPECTING_Q("abc\"\"def") 270 271 BEGIN_SCRIPT(str_escape, "serialize with various escapes", 0) 272 OPJ_STR("abc\"\"de'f\r\n\t\b\f\\\x01\v\x7f\\") 273 END_SCRIPT_EXPECTING_Q("abc\"\"de'f\r\n\t\b\f\\\u0001\u000b\u007f\\") 274 275 BEGIN_SCRIPT(str_len, "length-signalled string", 0) 276 OPJ_STR_LEN("abcdef", 6) 277 END_SCRIPT_EXPECTING_Q("abcdef") 278 279 BEGIN_SCRIPT(str_len0, "0-length-signalled string", 0) 280 OPJ_STR_LEN("", 0) 281 END_SCRIPT_EXPECTING_Q("") 282 283 BEGIN_SCRIPT(str_len_nul, "string with NUL", 0) 284 OPJ_STR_LEN("x\0y", 3) 285 END_SCRIPT_EXPECTING_Q("x\u0000y") 286 287 BEGIN_SCRIPT(hex_data0, "zero-length hex data", 0) 288 OPJ_STR_HEX("", 0) 289 END_SCRIPT_EXPECTING_Q("") 290 291 BEGIN_SCRIPT(hex_data, "hex data", 0) 292 OPJ_STR_HEX("\x00\x01\x5a\xfb\xff", 5) 293 END_SCRIPT_EXPECTING_Q("00015afbff") 294 295 BEGIN_SCRIPT(array_nest1, "serialize nested empty arrays", 0) 296 OPJ_BEGIN_A() 297 OPJ_BEGIN_A() 298 OPJ_END_A() 299 OPJ_END_A() 300 END_SCRIPT_EXPECTING_Q([[]]) 301 302 BEGIN_SCRIPT(array_nest2, "serialize nested empty arrays", 0) 303 OPJ_BEGIN_A() 304 OPJ_BEGIN_A() 305 OPJ_BEGIN_A() 306 OPJ_END_A() 307 OPJ_END_A() 308 OPJ_END_A() 309 END_SCRIPT_EXPECTING_Q([[[]]]) 310 311 BEGIN_SCRIPT(array_nest3, "serialize nested empty arrays", 0) 312 OPJ_BEGIN_A() 313 OPJ_BEGIN_A() 314 OPJ_BEGIN_A() 315 OPJ_END_A() 316 OPJ_BEGIN_A() 317 OPJ_END_A() 318 OPJ_BEGIN_A() 319 OPJ_END_A() 320 OPJ_END_A() 321 OPJ_BEGIN_A() 322 OPJ_END_A() 323 OPJ_END_A() 324 END_SCRIPT_EXPECTING_S("[[[],[],[]],[]]") 325 326 BEGIN_SCRIPT(array_nest4, "deep nested arrays", 0) 327 OPJ_BEGIN_A() 328 OPJ_BEGIN_A() 329 OPJ_BEGIN_A() 330 OPJ_BEGIN_A() 331 OPJ_BEGIN_A() 332 OPJ_BEGIN_A() 333 OPJ_BEGIN_A() 334 OPJ_BEGIN_A() 335 OPJ_BEGIN_A() 336 OPJ_BEGIN_A() 337 OPJ_BEGIN_A() 338 OPJ_BEGIN_A() 339 OPJ_BEGIN_A() 340 OPJ_BEGIN_A() 341 OPJ_BEGIN_A() 342 OPJ_BEGIN_A() 343 OPJ_BEGIN_A() 344 OPJ_BEGIN_A() 345 OPJ_BEGIN_A() 346 OPJ_BEGIN_A() 347 OPJ_END_A() 348 OPJ_END_A() 349 OPJ_END_A() 350 OPJ_END_A() 351 OPJ_END_A() 352 OPJ_END_A() 353 OPJ_END_A() 354 OPJ_END_A() 355 OPJ_END_A() 356 OPJ_END_A() 357 OPJ_END_A() 358 OPJ_END_A() 359 OPJ_END_A() 360 OPJ_END_A() 361 OPJ_END_A() 362 OPJ_END_A() 363 OPJ_END_A() 364 OPJ_END_A() 365 OPJ_END_A() 366 OPJ_NULL() 367 OPJ_END_A() 368 END_SCRIPT_EXPECTING_S("[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]],null]") 369 370 BEGIN_SCRIPT(obj_nontrivial1, "serialize nontrivial object", 0) 371 OPJ_BEGIN_O() 372 OPJ_KEY("") 373 OPJ_NULL() 374 OPJ_END_O() 375 END_SCRIPT_EXPECTING_S("{\"\":null}") 376 377 BEGIN_SCRIPT(obj_nontrivial2, "serialize nontrivial object", 0) 378 OPJ_BEGIN_O() 379 OPJ_KEY("") 380 OPJ_NULL() 381 OPJ_KEY("x") 382 OPJ_NULL() 383 OPJ_END_O() 384 END_SCRIPT_EXPECTING_S("{\"\":null,\"x\":null}") 385 386 BEGIN_SCRIPT(obj_nest1, "serialize nested objects", 0) 387 OPJ_BEGIN_O() 388 OPJ_KEY("") 389 OPJ_BEGIN_O() 390 OPJ_KEY("x") 391 OPJ_U64(42) 392 OPJ_END_O() 393 OPJ_KEY("x") 394 OPJ_BEGIN_A() 395 OPJ_U64(42) 396 OPJ_U64(101) 397 OPJ_END_A() 398 OPJ_KEY("y") 399 OPJ_NULL() 400 OPJ_KEY("z") 401 OPJ_BEGIN_O() 402 OPJ_KEY("z0") 403 OPJ_I64(-1) 404 OPJ_KEY("z1") 405 OPJ_I64(-2) 406 OPJ_END_O() 407 OPJ_END_O() 408 END_SCRIPT_EXPECTING_S("{\"\":{\"x\":42},\"x\":[42,101],\"y\":null,\"z\":{\"z0\":-1,\"z1\":-2}}") 409 410 BEGIN_SCRIPT(err_obj_no_key, "error test: object item without key", 0) 411 OPJ_BEGIN_O() 412 OP_ASSERT_ERROR(0) 413 OPJ_NULL() 414 OP_ASSERT_ERROR(1) 415 OPJ_END_O() 416 OP_ASSERT_ERROR(1) 417 END_SCRIPT_EXPECTING_S("{") 418 419 BEGIN_SCRIPT(err_obj_multi_key, "error test: object item with repeated key", 0) 420 OPJ_BEGIN_O() 421 OPJ_KEY("x") 422 OP_ASSERT_ERROR(0) 423 OPJ_KEY("y") 424 OP_ASSERT_ERROR(1) 425 OPJ_NULL() 426 OP_ASSERT_ERROR(1) 427 END_SCRIPT_EXPECTING_S("{\"x\":") 428 429 BEGIN_SCRIPT(err_obj_no_value, "error test: object item with no value", 0) 430 OPJ_BEGIN_O() 431 OPJ_KEY("x") 432 OP_ASSERT_ERROR(0) 433 OPJ_END_O() 434 OP_ASSERT_ERROR(1) 435 END_SCRIPT_EXPECTING_S("{\"x\":") 436 437 BEGIN_SCRIPT(err_utf8, "error test: only basic ASCII supported", 0) 438 OPJ_STR("\x80") 439 OP_ASSERT_ERROR(0) 440 END_SCRIPT_EXPECTING_S("\"\\u0080\"") 441 442 BEGIN_SCRIPT(utf8_2, "test: valid UTF-8 2byte supported", 0) 443 OPJ_STR("low=\xc2\x80, high=\xdf\xbf") 444 OP_ASSERT_ERROR(0) 445 END_SCRIPT_EXPECTING_S("\"low=\xc2\x80, high=\xdf\xbf\"") 446 447 BEGIN_SCRIPT(utf8_3, "test: valid UTF-8 3byte supported", 0) 448 OPJ_STR("low=\xe0\xa0\x80, high=\xef\xbf\xbf") 449 OP_ASSERT_ERROR(0) 450 END_SCRIPT_EXPECTING_S("\"low=\xe0\xa0\x80, high=\xef\xbf\xbf\"") 451 452 BEGIN_SCRIPT(utf8_4, "test: valid UTF-8 4byte supported", 0) 453 OPJ_STR("low=\xf0\x90\xbf\xbf, high=\xf4\x8f\xbf\xbf") 454 OP_ASSERT_ERROR(0) 455 END_SCRIPT_EXPECTING_S("\"low=\xf0\x90\xbf\xbf, high=\xf4\x8f\xbf\xbf\"") 456 457 BEGIN_SCRIPT(ijson_int, "I-JSON: large integer", OSSL_JSON_FLAG_IJSON) 458 OPJ_BEGIN_A() 459 OPJ_U64(1) 460 OPJ_I64(-1) 461 OPJ_U64(9007199254740991) 462 OPJ_U64(9007199254740992) 463 OPJ_I64(-9007199254740991) 464 OPJ_I64(-9007199254740992) 465 OPJ_END_A() 466 END_SCRIPT_EXPECTING_S("[1,-1,9007199254740991,\"9007199254740992\",-9007199254740991,\"-9007199254740992\"]") 467 468 BEGIN_SCRIPT(multi_item, "multiple top level items", 0) 469 OPJ_NULL() 470 OPJ_NULL() 471 OPJ_BEGIN_A() 472 OPJ_END_A() 473 OPJ_BEGIN_A() 474 OPJ_END_A() 475 END_SCRIPT_EXPECTING_S("nullnull[][]") 476 477 BEGIN_SCRIPT(seq, "JSON-SEQ", OSSL_JSON_FLAG_SEQ) 478 OPJ_NULL() 479 OPJ_NULL() 480 OPJ_NULL() 481 OPJ_BEGIN_O() 482 OPJ_KEY("x") 483 OPJ_U64(1) 484 OPJ_KEY("y") 485 OPJ_BEGIN_O() 486 OPJ_END_O() 487 OPJ_END_O() 488 END_SCRIPT_EXPECTING_S("\x1Enull\n" 489 "\x1Enull\n" 490 "\x1Enull\n" 491 "\x1E{\"x\":1,\"y\":{}}\n") 492 493 static const info_func scripts[] = { 494 SCRIPT(null) 495 SCRIPT(obj_empty) 496 SCRIPT(array_empty) 497 SCRIPT(bool_false) 498 SCRIPT(bool_true) 499 SCRIPT(u64_0) 500 SCRIPT(u64_1) 501 SCRIPT(u64_10) 502 SCRIPT(u64_12345) 503 SCRIPT(u64_18446744073709551615) 504 SCRIPT(i64_0) 505 SCRIPT(i64_1) 506 SCRIPT(i64_2) 507 SCRIPT(i64_10) 508 SCRIPT(i64_12345) 509 SCRIPT(i64_9223372036854775807) 510 SCRIPT(i64_m1) 511 SCRIPT(i64_m2) 512 SCRIPT(i64_m10) 513 SCRIPT(i64_m12345) 514 SCRIPT(i64_m9223372036854775807) 515 SCRIPT(i64_m9223372036854775808) 516 SCRIPT(str_empty) 517 SCRIPT(str_a) 518 SCRIPT(str_abc) 519 SCRIPT(str_quote) 520 SCRIPT(str_quote2) 521 SCRIPT(str_escape) 522 SCRIPT(str_len) 523 SCRIPT(str_len0) 524 SCRIPT(str_len_nul) 525 SCRIPT(hex_data0) 526 SCRIPT(hex_data) 527 SCRIPT(array_nest1) 528 SCRIPT(array_nest2) 529 SCRIPT(array_nest3) 530 SCRIPT(array_nest4) 531 SCRIPT(obj_nontrivial1) 532 SCRIPT(obj_nontrivial2) 533 SCRIPT(obj_nest1) 534 SCRIPT(err_obj_no_key) 535 SCRIPT(err_obj_multi_key) 536 SCRIPT(err_obj_no_value) 537 SCRIPT(err_utf8) 538 SCRIPT(utf8_2) 539 SCRIPT(utf8_3) 540 SCRIPT(utf8_4) 541 SCRIPT(ijson_int) 542 SCRIPT(multi_item) 543 SCRIPT(seq) 544 }; 545 546 /* Test runner. */ 547 static int run_script(const struct script_info *info) 548 { 549 int ok = 0, asserted = -1; 550 const struct script_word *words = info->words; 551 size_t wp = 0; 552 struct script_word w; 553 struct helper h = { 0 }; 554 BUF_MEM *bufp = NULL; 555 556 TEST_info("running script '%s' (%s)", info->name, info->title); 557 558 #define GET_WORD() (w = words[wp++]) 559 #define GET_U64() (GET_WORD().u64) 560 #define GET_I64() (GET_WORD().i64) 561 #define GET_FP() (GET_WORD().fp) 562 #define GET_P() (GET_WORD().p) 563 564 for (;;) 565 switch (GET_U64()) { 566 case OPK_END: 567 goto stop; 568 case OPK_INIT_FLAGS: 569 helper_set_flags(&h, (uint32_t)GET_U64()); 570 break; 571 case OPK_CALL: { 572 fp_type f = (fp_type)GET_FP(); 573 574 if (!TEST_true(helper_ensure(&h))) 575 goto err; 576 577 f(&h.j); 578 break; 579 } 580 case OPK_CALL_I: { 581 fp_i_type f = (fp_i_type)GET_FP(); 582 583 if (!TEST_true(helper_ensure(&h))) 584 goto err; 585 586 f(&h.j, (int)GET_I64()); 587 break; 588 } 589 case OPK_CALL_U64: { 590 fp_u64_type f = (fp_u64_type)GET_FP(); 591 592 if (!TEST_true(helper_ensure(&h))) 593 goto err; 594 595 f(&h.j, GET_U64()); 596 break; 597 } 598 case OPK_CALL_I64: { 599 fp_i64_type f = (fp_i64_type)GET_FP(); 600 601 if (!TEST_true(helper_ensure(&h))) 602 goto err; 603 604 f(&h.j, GET_I64()); 605 break; 606 } 607 case OPK_CALL_P: { 608 fp_p_type f = (fp_p_type)GET_FP(); 609 610 if (!TEST_true(helper_ensure(&h))) 611 goto err; 612 613 f(&h.j, GET_P()); 614 break; 615 } 616 case OPK_CALL_PZ: { 617 fp_pz_type f = (fp_pz_type)GET_FP(); 618 void *p; 619 uint64_t u64; 620 621 if (!TEST_true(helper_ensure(&h))) 622 goto err; 623 624 p = GET_P(); 625 u64 = GET_U64(); 626 f(&h.j, p, (size_t)u64); 627 break; 628 } 629 case OPK_ASSERT_ERROR: { 630 if (!TEST_true(helper_ensure(&h))) 631 goto err; 632 633 asserted = (int)GET_U64(); 634 if (!TEST_int_eq(ossl_json_in_error(&h.j), asserted)) 635 goto err; 636 637 break; 638 } 639 #define OP_ASSERT_ERROR(err) OP_U64(OPK_ASSERT_ERROR) OP_U64(err) 640 641 default: 642 TEST_error("unknown opcode"); 643 goto err; 644 } 645 stop: 646 647 if (!TEST_true(helper_ensure(&h))) 648 goto err; 649 650 if (!TEST_true(ossl_json_flush(&h.j))) 651 goto err; 652 653 /* Implicit error check if not done explicitly. */ 654 if (asserted < 0 && !TEST_false(ossl_json_in_error(&h.j))) 655 goto err; 656 657 if (!TEST_true(BIO_get_mem_ptr(h.mem_bio, &bufp))) 658 goto err; 659 660 if (!TEST_mem_eq(bufp->data, bufp->length, 661 info->expected_output, 662 info->expected_output_len == SIZE_MAX 663 ? strlen(info->expected_output) 664 : info->expected_output_len)) 665 goto err; 666 667 ok = 1; 668 err: 669 if (!ok) 670 TEST_error("script '%s' failed", info->name); 671 672 helper_cleanup(&h); 673 return ok; 674 } 675 676 static int test_json_enc(void) 677 { 678 int ok = 1; 679 size_t i; 680 681 for (i = 0; i < OSSL_NELEM(scripts); ++i) 682 if (!TEST_true(run_script(scripts[i]()))) 683 ok = 0; 684 685 return ok; 686 } 687 688 int setup_tests(void) 689 { 690 ADD_TEST(test_json_enc); 691 return 1; 692 } 693