json_test.c 19 KB

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