]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/json_test.c
QLOG: JSON Encoder: Rename JSON_ENC
[thirdparty/openssl.git] / test / json_test.c
1 /*
2 * Copyright 2022 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_F64(x) OP_CALL_D(ossl_json_f64, (x))
123 #define OPJ_KEY(x) OP_CALL_P(ossl_json_key, (x))
124 #define OPJ_STR(x) OP_CALL_P(ossl_json_str, (x))
125 #define OPJ_STR_LEN(x, xl) OP_CALL_PZ(ossl_json_str_len, (x), (xl))
126 #define OPJ_STR_HEX(x, xl) OP_CALL_PZ(ossl_json_str_hex, (x), (xl))
127
128 #define BEGIN_SCRIPT(name, title, flags) \
129 static const struct script_info *get_script_##name(void) \
130 { \
131 const char *const script_name = #name; \
132 const char *const script_title = #title; \
133 \
134 static const struct script_word script_words[] = { \
135 OP_INIT_FLAGS(flags)
136
137 #define END_SCRIPT_EXPECTING(s, slen) \
138 OP_END() \
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 #define END_SCRIPT_EXPECTING_S(s) END_SCRIPT_EXPECTING(s, SIZE_MAX)
148 #define END_SCRIPT_EXPECTING_Q(s) END_SCRIPT_EXPECTING(#s, sizeof(#s) - 1)
149
150 #define SCRIPT(name) get_script_##name,
151
152 BEGIN_SCRIPT(null, "serialize a single null", 0)
153 OPJ_NULL()
154 END_SCRIPT_EXPECTING_Q(null)
155
156 BEGIN_SCRIPT(obj_empty, "serialize an empty object", 0)
157 OPJ_BEGIN_O()
158 OPJ_END_O()
159 END_SCRIPT_EXPECTING_Q({})
160
161 BEGIN_SCRIPT(array_empty, "serialize an empty array", 0)
162 OPJ_BEGIN_A()
163 OPJ_END_A()
164 END_SCRIPT_EXPECTING_Q([])
165
166 BEGIN_SCRIPT(bool_false, "serialize false", 0)
167 OPJ_BOOL(0)
168 END_SCRIPT_EXPECTING_Q(false)
169
170 BEGIN_SCRIPT(bool_true, "serialize true", 0)
171 OPJ_BOOL(1)
172 END_SCRIPT_EXPECTING_Q(true)
173
174 BEGIN_SCRIPT(u64_0, "serialize u64(0)", 0)
175 OPJ_U64(0)
176 END_SCRIPT_EXPECTING_Q(0)
177
178 BEGIN_SCRIPT(u64_1, "serialize u64(1)", 0)
179 OPJ_U64(1)
180 END_SCRIPT_EXPECTING_Q(1)
181
182 BEGIN_SCRIPT(u64_10, "serialize u64(10)", 0)
183 OPJ_U64(10)
184 END_SCRIPT_EXPECTING_Q(10)
185
186 BEGIN_SCRIPT(u64_12345, "serialize u64(12345)", 0)
187 OPJ_U64(12345)
188 END_SCRIPT_EXPECTING_Q(12345)
189
190 BEGIN_SCRIPT(u64_18446744073709551615, "serialize u64(18446744073709551615)", 0)
191 OPJ_U64(18446744073709551615ULL)
192 END_SCRIPT_EXPECTING_Q(18446744073709551615)
193
194 BEGIN_SCRIPT(i64_0, "serialize i64(0)", 0)
195 OPJ_I64(0)
196 END_SCRIPT_EXPECTING_Q(0)
197
198 BEGIN_SCRIPT(i64_1, "serialize i64(1)", 0)
199 OPJ_I64(1)
200 END_SCRIPT_EXPECTING_Q(1)
201
202 BEGIN_SCRIPT(i64_2, "serialize i64(2)", 0)
203 OPJ_I64(2)
204 END_SCRIPT_EXPECTING_Q(2)
205
206 BEGIN_SCRIPT(i64_10, "serialize i64(10)", 0)
207 OPJ_I64(10)
208 END_SCRIPT_EXPECTING_Q(10)
209
210 BEGIN_SCRIPT(i64_12345, "serialize i64(12345)", 0)
211 OPJ_I64(12345)
212 END_SCRIPT_EXPECTING_Q(12345)
213
214 BEGIN_SCRIPT(i64_9223372036854775807, "serialize i64(9223372036854775807)", 0)
215 OPJ_I64(9223372036854775807LL)
216 END_SCRIPT_EXPECTING_Q(9223372036854775807)
217
218 BEGIN_SCRIPT(i64_m1, "serialize i64(-1)", 0)
219 OPJ_I64(-1)
220 END_SCRIPT_EXPECTING_Q(-1)
221
222 BEGIN_SCRIPT(i64_m2, "serialize i64(-2)", 0)
223 OPJ_I64(-2)
224 END_SCRIPT_EXPECTING_Q(-2)
225
226 BEGIN_SCRIPT(i64_m10, "serialize i64(-10)", 0)
227 OPJ_I64(-10)
228 END_SCRIPT_EXPECTING_Q(-10)
229
230 BEGIN_SCRIPT(i64_m12345, "serialize i64(-12345)", 0)
231 OPJ_I64(-12345)
232 END_SCRIPT_EXPECTING_Q(-12345)
233
234 BEGIN_SCRIPT(i64_m9223372036854775807, "serialize i64(-9223372036854775807)", 0)
235 OPJ_I64(-9223372036854775807LL)
236 END_SCRIPT_EXPECTING_Q(-9223372036854775807)
237
238 BEGIN_SCRIPT(i64_m9223372036854775808, "serialize i64(-9223372036854775808)", 0)
239 OPJ_I64(-9223372036854775807LL - 1LL)
240 END_SCRIPT_EXPECTING_Q(-9223372036854775808)
241
242 BEGIN_SCRIPT(str_empty, "serialize \"\"", 0)
243 OPJ_STR("")
244 END_SCRIPT_EXPECTING_Q("")
245
246 BEGIN_SCRIPT(str_a, "serialize \"a\"", 0)
247 OPJ_STR("a")
248 END_SCRIPT_EXPECTING_Q("a")
249
250 BEGIN_SCRIPT(str_abc, "serialize \"abc\"", 0)
251 OPJ_STR("abc")
252 END_SCRIPT_EXPECTING_Q("abc")
253
254 BEGIN_SCRIPT(str_quote, "serialize with quote", 0)
255 OPJ_STR("abc\"def")
256 END_SCRIPT_EXPECTING_Q("abc\"def")
257
258 BEGIN_SCRIPT(str_quote2, "serialize with quote", 0)
259 OPJ_STR("abc\"\"def")
260 END_SCRIPT_EXPECTING_Q("abc\"\"def")
261
262 BEGIN_SCRIPT(str_escape, "serialize with various escapes", 0)
263 OPJ_STR("abc\"\"de'f\r\n\t\b\f\\\x01\v\x7f\\")
264 END_SCRIPT_EXPECTING_Q("abc\"\"de'f\r\n\t\b\f\\\u0001\u000b\u007f\\")
265
266 BEGIN_SCRIPT(str_len, "length-signalled string", 0)
267 OPJ_STR_LEN("abcdef", 6)
268 END_SCRIPT_EXPECTING_Q("abcdef")
269
270 BEGIN_SCRIPT(str_len0, "0-length-signalled string", 0)
271 OPJ_STR_LEN("", 0)
272 END_SCRIPT_EXPECTING_Q("")
273
274 BEGIN_SCRIPT(str_len_nul, "string with NUL", 0)
275 OPJ_STR_LEN("x\0y", 3)
276 END_SCRIPT_EXPECTING_Q("x\u0000y")
277
278 BEGIN_SCRIPT(hex_data0, "zero-length hex data", 0)
279 OPJ_STR_HEX("", 0)
280 END_SCRIPT_EXPECTING_Q("")
281
282 BEGIN_SCRIPT(hex_data, "hex data", 0)
283 OPJ_STR_HEX("\x00\x01\x5a\xfb\xff", 5)
284 END_SCRIPT_EXPECTING_Q("00015afbff")
285
286 BEGIN_SCRIPT(array_nest1, "serialize nested empty arrays", 0)
287 OPJ_BEGIN_A()
288 OPJ_BEGIN_A()
289 OPJ_END_A()
290 OPJ_END_A()
291 END_SCRIPT_EXPECTING_Q([[]])
292
293 BEGIN_SCRIPT(array_nest2, "serialize nested empty arrays", 0)
294 OPJ_BEGIN_A()
295 OPJ_BEGIN_A()
296 OPJ_BEGIN_A()
297 OPJ_END_A()
298 OPJ_END_A()
299 OPJ_END_A()
300 END_SCRIPT_EXPECTING_Q([[[]]])
301
302 BEGIN_SCRIPT(array_nest3, "serialize nested empty arrays", 0)
303 OPJ_BEGIN_A()
304 OPJ_BEGIN_A()
305 OPJ_BEGIN_A()
306 OPJ_END_A()
307 OPJ_BEGIN_A()
308 OPJ_END_A()
309 OPJ_BEGIN_A()
310 OPJ_END_A()
311 OPJ_END_A()
312 OPJ_BEGIN_A()
313 OPJ_END_A()
314 OPJ_END_A()
315 END_SCRIPT_EXPECTING_S("[[[],[],[]],[]]")
316
317 BEGIN_SCRIPT(array_nest4, "deep nested arrays", 0)
318 OPJ_BEGIN_A()
319 OPJ_BEGIN_A()
320 OPJ_BEGIN_A()
321 OPJ_BEGIN_A()
322 OPJ_BEGIN_A()
323 OPJ_BEGIN_A()
324 OPJ_BEGIN_A()
325 OPJ_BEGIN_A()
326 OPJ_BEGIN_A()
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_END_A()
339 OPJ_END_A()
340 OPJ_END_A()
341 OPJ_END_A()
342 OPJ_END_A()
343 OPJ_END_A()
344 OPJ_END_A()
345 OPJ_END_A()
346 OPJ_END_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_NULL()
358 OPJ_END_A()
359 END_SCRIPT_EXPECTING_S("[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]],null]")
360
361 BEGIN_SCRIPT(obj_nontrivial1, "serialize nontrivial object", 0)
362 OPJ_BEGIN_O()
363 OPJ_KEY("")
364 OPJ_NULL()
365 OPJ_END_O()
366 END_SCRIPT_EXPECTING_S("{\"\":null}")
367
368 BEGIN_SCRIPT(obj_nontrivial2, "serialize nontrivial object", 0)
369 OPJ_BEGIN_O()
370 OPJ_KEY("")
371 OPJ_NULL()
372 OPJ_KEY("x")
373 OPJ_NULL()
374 OPJ_END_O()
375 END_SCRIPT_EXPECTING_S("{\"\":null,\"x\":null}")
376
377 BEGIN_SCRIPT(obj_nest1, "serialize nested objects", 0)
378 OPJ_BEGIN_O()
379 OPJ_KEY("")
380 OPJ_BEGIN_O()
381 OPJ_KEY("x")
382 OPJ_U64(42)
383 OPJ_END_O()
384 OPJ_KEY("x")
385 OPJ_BEGIN_A()
386 OPJ_U64(42)
387 OPJ_U64(101)
388 OPJ_END_A()
389 OPJ_KEY("y")
390 OPJ_NULL()
391 OPJ_KEY("z")
392 OPJ_BEGIN_O()
393 OPJ_KEY("z0")
394 OPJ_I64(-1)
395 OPJ_KEY("z1")
396 OPJ_I64(-2)
397 OPJ_END_O()
398 OPJ_END_O()
399 END_SCRIPT_EXPECTING_S("{\"\":{\"x\":42},\"x\":[42,101],\"y\":null,\"z\":{\"z0\":-1,\"z1\":-2}}")
400
401 BEGIN_SCRIPT(err_obj_no_key, "error test: object item without key", 0)
402 OPJ_BEGIN_O()
403 OP_ASSERT_ERROR(0)
404 OPJ_NULL()
405 OP_ASSERT_ERROR(1)
406 OPJ_END_O()
407 OP_ASSERT_ERROR(1)
408 END_SCRIPT_EXPECTING_S("{")
409
410 BEGIN_SCRIPT(err_obj_multi_key, "error test: object item with repeated key", 0)
411 OPJ_BEGIN_O()
412 OPJ_KEY("x")
413 OP_ASSERT_ERROR(0)
414 OPJ_KEY("y")
415 OP_ASSERT_ERROR(1)
416 OPJ_NULL()
417 OP_ASSERT_ERROR(1)
418 END_SCRIPT_EXPECTING_S("{\"x\":")
419
420 BEGIN_SCRIPT(err_obj_no_value, "error test: object item with no value", 0)
421 OPJ_BEGIN_O()
422 OPJ_KEY("x")
423 OP_ASSERT_ERROR(0)
424 OPJ_END_O()
425 OP_ASSERT_ERROR(1)
426 END_SCRIPT_EXPECTING_S("{\"x\":")
427
428 BEGIN_SCRIPT(err_utf8, "error test: only basic ASCII supported", 0)
429 OPJ_STR("\x80")
430 OP_ASSERT_ERROR(1)
431 END_SCRIPT_EXPECTING_S("\"")
432
433 BEGIN_SCRIPT(ijson_int, "I-JSON: large integer", OSSL_JSON_FLAG_IJSON)
434 OPJ_BEGIN_A()
435 OPJ_U64(1)
436 OPJ_I64(-1)
437 OPJ_U64(9007199254740991)
438 OPJ_U64(9007199254740992)
439 OPJ_I64(-9007199254740991)
440 OPJ_I64(-9007199254740992)
441 OPJ_END_A()
442 END_SCRIPT_EXPECTING_S("[1,-1,9007199254740991,\"9007199254740992\",-9007199254740991,\"-9007199254740992\"]")
443
444 BEGIN_SCRIPT(multi_item, "multiple top level items", 0)
445 OPJ_NULL()
446 OPJ_NULL()
447 OPJ_BEGIN_A()
448 OPJ_END_A()
449 OPJ_BEGIN_A()
450 OPJ_END_A()
451 END_SCRIPT_EXPECTING_S("nullnull[][]")
452
453 BEGIN_SCRIPT(seq, "JSON-SEQ", OSSL_JSON_FLAG_SEQ)
454 OPJ_NULL()
455 OPJ_NULL()
456 OPJ_NULL()
457 OPJ_BEGIN_O()
458 OPJ_KEY("x")
459 OPJ_U64(1)
460 OPJ_KEY("y")
461 OPJ_BEGIN_O()
462 OPJ_END_O()
463 OPJ_END_O()
464 END_SCRIPT_EXPECTING_S("\x1Enull\n" "\x1Enull\n" "\x1Enull\n" "\x1E{\"x\":1,\"y\":{}}\n")
465
466 static const info_func scripts[] = {
467 SCRIPT(null)
468 SCRIPT(obj_empty)
469 SCRIPT(array_empty)
470 SCRIPT(bool_false)
471 SCRIPT(bool_true)
472 SCRIPT(u64_0)
473 SCRIPT(u64_1)
474 SCRIPT(u64_10)
475 SCRIPT(u64_12345)
476 SCRIPT(u64_18446744073709551615)
477 SCRIPT(i64_0)
478 SCRIPT(i64_1)
479 SCRIPT(i64_2)
480 SCRIPT(i64_10)
481 SCRIPT(i64_12345)
482 SCRIPT(i64_9223372036854775807)
483 SCRIPT(i64_m1)
484 SCRIPT(i64_m2)
485 SCRIPT(i64_m10)
486 SCRIPT(i64_m12345)
487 SCRIPT(i64_m9223372036854775807)
488 SCRIPT(i64_m9223372036854775808)
489 SCRIPT(str_empty)
490 SCRIPT(str_a)
491 SCRIPT(str_abc)
492 SCRIPT(str_quote)
493 SCRIPT(str_quote2)
494 SCRIPT(str_escape)
495 SCRIPT(str_len)
496 SCRIPT(str_len0)
497 SCRIPT(str_len_nul)
498 SCRIPT(hex_data0)
499 SCRIPT(hex_data)
500 SCRIPT(array_nest1)
501 SCRIPT(array_nest2)
502 SCRIPT(array_nest3)
503 SCRIPT(array_nest4)
504 SCRIPT(obj_nontrivial1)
505 SCRIPT(obj_nontrivial2)
506 SCRIPT(obj_nest1)
507 SCRIPT(err_obj_no_key)
508 SCRIPT(err_obj_multi_key)
509 SCRIPT(err_obj_no_value)
510 SCRIPT(err_utf8)
511 SCRIPT(ijson_int)
512 SCRIPT(multi_item)
513 SCRIPT(seq)
514 };
515
516 /* Test runner. */
517 static int run_script(const struct script_info *info)
518 {
519 int ok = 0, asserted = -1;
520 const struct script_word *words = info->words;
521 size_t wp = 0;
522 struct script_word w;
523 struct helper h = {0};
524 BUF_MEM *bufp = NULL;
525
526 TEST_info("running script '%s' (%s)", info->name, info->title);
527
528 #define GET_WORD() (w = words[wp++])
529 #define GET_U64() (GET_WORD().u64)
530 #define GET_I64() (GET_WORD().i64)
531 #define GET_FP() (GET_WORD().fp)
532 #define GET_P() (GET_WORD().p)
533
534 for (;;)
535 switch (GET_U64()) {
536 case OPK_END:
537 goto stop;
538 case OPK_INIT_FLAGS:
539 helper_set_flags(&h, (uint32_t)GET_U64());
540 break;
541 case OPK_CALL:
542 {
543 fp_type f = (fp_type)GET_FP();
544
545 if (!TEST_true(helper_ensure(&h)))
546 goto err;
547
548 f(&h.j);
549 break;
550 }
551 case OPK_CALL_I:
552 {
553 fp_i_type f = (fp_i_type)GET_FP();
554
555 if (!TEST_true(helper_ensure(&h)))
556 goto err;
557
558 f(&h.j, (int)GET_I64());
559 break;
560 }
561 case OPK_CALL_U64:
562 {
563 fp_u64_type f = (fp_u64_type)GET_FP();
564
565 if (!TEST_true(helper_ensure(&h)))
566 goto err;
567
568 f(&h.j, GET_U64());
569 break;
570 }
571 case OPK_CALL_I64:
572 {
573 fp_i64_type f = (fp_i64_type)GET_FP();
574
575 if (!TEST_true(helper_ensure(&h)))
576 goto err;
577
578 f(&h.j, GET_I64());
579 break;
580 }
581 case OPK_CALL_P:
582 {
583 fp_p_type f = (fp_p_type)GET_FP();
584
585 if (!TEST_true(helper_ensure(&h)))
586 goto err;
587
588 f(&h.j, GET_P());
589 break;
590 }
591 case OPK_CALL_PZ:
592 {
593 fp_pz_type f = (fp_pz_type)GET_FP();
594 void *p;
595 uint64_t u64;
596
597 if (!TEST_true(helper_ensure(&h)))
598 goto err;
599
600 p = GET_P();
601 u64 = GET_U64();
602 f(&h.j, p, (size_t)u64);
603 break;
604 }
605 case OPK_ASSERT_ERROR:
606 {
607 if (!TEST_true(helper_ensure(&h)))
608 goto err;
609
610 asserted = (int)GET_U64();
611 if (!TEST_int_eq(ossl_json_in_error(&h.j), asserted))
612 goto err;
613
614 break;
615 }
616 #define OP_ASSERT_ERROR(err) OP_U64(OPK_ASSERT_ERROR) OP_U64(err)
617
618 default:
619 TEST_error("unknown opcode");
620 goto err;
621 }
622 stop:
623
624 if (!TEST_true(helper_ensure(&h)))
625 goto err;
626
627 if (!TEST_true(ossl_json_flush(&h.j)))
628 goto err;
629
630 /* Implicit error check if not done explicitly. */
631 if (asserted < 0 && !TEST_false(ossl_json_in_error(&h.j)))
632 goto err;
633
634 if (!TEST_true(BIO_get_mem_ptr(h.mem_bio, &bufp)))
635 goto err;
636
637 if (!TEST_mem_eq(bufp->data, bufp->length,
638 info->expected_output,
639 info->expected_output_len == SIZE_MAX
640 ? strlen(info->expected_output)
641 : info->expected_output_len))
642 goto err;
643
644 ok = 1;
645 err:
646 if (!ok)
647 TEST_error("script '%s' failed", info->name);
648
649 helper_cleanup(&h);
650 return ok;
651 }
652
653 static int test_json_enc(void)
654 {
655 int ok = 1;
656 size_t i;
657
658 for (i = 0; i < OSSL_NELEM(scripts); ++i)
659 if (!TEST_true(run_script(scripts[i]())))
660 ok = 0;
661
662 return ok;
663 }
664
665 int setup_tests(void)
666 {
667 ADD_TEST(test_json_enc);
668 return 1;
669 }