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