test_json.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file \brief Test JSON API.
  20. *
  21. * While some of these tests are actually testing our JSON library wrapper, the bulk of
  22. * them are exploratory tests to determine what the behavior of the underlying JSON
  23. * library is. This also gives us a good indicator if that behavior changes between
  24. * Jansson revisions.
  25. *
  26. * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
  27. *
  28. * \ingroup tests
  29. */
  30. /*** MODULEINFO
  31. <depend>TEST_FRAMEWORK</depend>
  32. <support_level>core</support_level>
  33. ***/
  34. #include "asterisk.h"
  35. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  36. #include "asterisk/json.h"
  37. #include "asterisk/module.h"
  38. #include "asterisk/test.h"
  39. #include <stdio.h>
  40. #include <unistd.h>
  41. #define CATEGORY "/main/json/"
  42. /*!
  43. * Number of allocations from JSON library that have not yet been freed.
  44. */
  45. static size_t alloc_count;
  46. /*!@{*/
  47. /*!
  48. * JSON library has its own reference counting, so we'll provide our own allocators to
  49. * test that everything gets freed as expected.
  50. */
  51. static void *json_debug_malloc(size_t size)
  52. {
  53. void *p = ast_json_malloc(size);
  54. if (p) {
  55. ++alloc_count;
  56. }
  57. return p;
  58. }
  59. static void json_debug_free(void *p)
  60. {
  61. if (p) {
  62. --alloc_count;
  63. }
  64. ast_json_free(p);
  65. }
  66. static int json_test_init(struct ast_test_info *info, struct ast_test *test)
  67. {
  68. ast_json_set_alloc_funcs(json_debug_malloc, json_debug_free);
  69. alloc_count = 0;
  70. return 0;
  71. }
  72. static int json_test_cleanup(struct ast_test_info *info, struct ast_test *test)
  73. {
  74. ast_json_reset_alloc_funcs();
  75. if (0 != alloc_count) {
  76. ast_test_status_update(test,
  77. "JSON test leaked %zu allocations!\n", alloc_count);
  78. return -1;
  79. }
  80. return 0;
  81. }
  82. /*!@}*/
  83. AST_TEST_DEFINE(json_test_false)
  84. {
  85. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  86. switch (cmd) {
  87. case TEST_INIT:
  88. info->name = "type_false";
  89. info->category = CATEGORY;
  90. info->summary = "Testing fundamental JSON false value.";
  91. info->description = "Test JSON abstraction library.";
  92. return AST_TEST_NOT_RUN;
  93. case TEST_EXECUTE:
  94. break;
  95. }
  96. uut = ast_json_false();
  97. ast_test_validate(test, NULL != uut);
  98. ast_test_validate(test, AST_JSON_FALSE == ast_json_typeof(uut));
  99. ast_test_validate(test, !ast_json_is_null(uut));
  100. ast_test_validate(test, !ast_json_is_true(uut));
  101. ast_test_validate(test, ast_json_is_false(uut));
  102. return AST_TEST_PASS;
  103. }
  104. AST_TEST_DEFINE(json_test_true)
  105. {
  106. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  107. switch (cmd) {
  108. case TEST_INIT:
  109. info->name = "type_true";
  110. info->category = CATEGORY;
  111. info->summary = "Testing JSON true value.";
  112. info->description = "Test JSON abstraction library.";
  113. return AST_TEST_NOT_RUN;
  114. case TEST_EXECUTE:
  115. break;
  116. }
  117. uut = ast_json_true();
  118. ast_test_validate(test, NULL != uut);
  119. ast_test_validate(test, AST_JSON_TRUE == ast_json_typeof(uut));
  120. ast_test_validate(test, !ast_json_is_null(uut));
  121. ast_test_validate(test, ast_json_is_true(uut));
  122. ast_test_validate(test, !ast_json_is_false(uut));
  123. return AST_TEST_PASS;
  124. }
  125. AST_TEST_DEFINE(json_test_bool0)
  126. {
  127. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  128. switch (cmd) {
  129. case TEST_INIT:
  130. info->name = "type_bool0";
  131. info->category = CATEGORY;
  132. info->summary = "Testing JSON boolean function (false).";
  133. info->description = "Test JSON abstraction library.";
  134. return AST_TEST_NOT_RUN;
  135. case TEST_EXECUTE:
  136. break;
  137. }
  138. uut = ast_json_boolean(0);
  139. ast_test_validate(test, NULL != uut);
  140. ast_test_validate(test, AST_JSON_FALSE == ast_json_typeof(uut));
  141. ast_test_validate(test, !ast_json_is_null(uut));
  142. ast_test_validate(test, !ast_json_is_true(uut));
  143. ast_test_validate(test, ast_json_is_false(uut));
  144. ast_test_validate(test, ast_json_equal(uut, ast_json_false()));
  145. ast_test_validate(test, !ast_json_equal(uut, ast_json_true()));
  146. return AST_TEST_PASS;
  147. }
  148. AST_TEST_DEFINE(json_test_bool1)
  149. {
  150. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  151. switch (cmd) {
  152. case TEST_INIT:
  153. info->name = "type_bool1";
  154. info->category = CATEGORY;
  155. info->summary = "Testing JSON boolean function (true).";
  156. info->description = "Test JSON abstraction library.";
  157. return AST_TEST_NOT_RUN;
  158. case TEST_EXECUTE:
  159. break;
  160. }
  161. uut = ast_json_boolean(1);
  162. ast_test_validate(test, NULL != uut);
  163. ast_test_validate(test, AST_JSON_TRUE == ast_json_typeof(uut));
  164. ast_test_validate(test, !ast_json_is_null(uut));
  165. ast_test_validate(test, ast_json_is_true(uut));
  166. ast_test_validate(test, !ast_json_is_false(uut));
  167. ast_test_validate(test, !ast_json_equal(uut, ast_json_false()));
  168. ast_test_validate(test, ast_json_equal(uut, ast_json_true()));
  169. return AST_TEST_PASS;
  170. }
  171. AST_TEST_DEFINE(json_test_null)
  172. {
  173. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  174. switch (cmd) {
  175. case TEST_INIT:
  176. info->name = "type_null";
  177. info->category = CATEGORY;
  178. info->summary = "Testing JSON null value.";
  179. info->description = "Test JSON abstraction library.";
  180. return AST_TEST_NOT_RUN;
  181. case TEST_EXECUTE:
  182. break;
  183. }
  184. uut = ast_json_null();
  185. ast_test_validate(test, NULL != uut);
  186. ast_test_validate(test, AST_JSON_NULL == ast_json_typeof(uut));
  187. ast_test_validate(test, ast_json_is_null(uut));
  188. ast_test_validate(test, !ast_json_is_true(uut));
  189. ast_test_validate(test, !ast_json_is_false(uut));
  190. return AST_TEST_PASS;
  191. }
  192. AST_TEST_DEFINE(json_test_null_val)
  193. {
  194. switch (cmd) {
  195. case TEST_INIT:
  196. info->name = "null_val";
  197. info->category = CATEGORY;
  198. info->summary = "Testing JSON handling of NULL.";
  199. info->description = "Test JSON abstraction library.";
  200. return AST_TEST_NOT_RUN;
  201. case TEST_EXECUTE:
  202. break;
  203. }
  204. /* NULL isn't null, true or false */
  205. ast_test_validate(test, !ast_json_is_null(NULL));
  206. ast_test_validate(test, !ast_json_is_false(NULL));
  207. ast_test_validate(test, !ast_json_is_true(NULL));
  208. /* ref and unref should be NULL safe */
  209. ast_json_ref(NULL);
  210. ast_json_unref(NULL);
  211. /* no segfault; we're good. le sigh. */
  212. return AST_TEST_PASS;
  213. }
  214. AST_TEST_DEFINE(json_test_string)
  215. {
  216. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  217. int uut_res;
  218. switch (cmd) {
  219. case TEST_INIT:
  220. info->name = "type_string";
  221. info->category = CATEGORY;
  222. info->summary = "Basic string tests.";
  223. info->description = "Test JSON abstraction library.";
  224. return AST_TEST_NOT_RUN;
  225. case TEST_EXECUTE:
  226. break;
  227. }
  228. uut = ast_json_string_create("Hello, json");
  229. ast_test_validate(test, NULL != uut);
  230. ast_test_validate(test, AST_JSON_STRING == ast_json_typeof(uut));
  231. ast_test_validate(test, 0 == strcmp("Hello, json", ast_json_string_get(uut)));
  232. uut_res = ast_json_string_set(uut, NULL);
  233. ast_test_validate(test, -1 == uut_res);
  234. ast_test_validate(test, 0 == strcmp("Hello, json", ast_json_string_get(uut)));
  235. uut_res = ast_json_string_set(uut, "Not UTF-8 - \xff");
  236. ast_test_validate(test, -1 == uut_res);
  237. ast_test_validate(test, 0 == strcmp("Hello, json", ast_json_string_get(uut)));
  238. uut_res = ast_json_string_set(uut, "Is UTF-8 - \xE2\x98\xBA");
  239. ast_test_validate(test, 0 == uut_res);
  240. ast_test_validate(test, 0 == strcmp("Is UTF-8 - \xE2\x98\xBA", ast_json_string_get(uut)));
  241. uut_res = ast_json_string_set(uut, "Goodbye, json");
  242. ast_test_validate(test, 0 == uut_res);
  243. ast_test_validate(test, 0 == strcmp("Goodbye, json", ast_json_string_get(uut)));
  244. return AST_TEST_PASS;
  245. }
  246. AST_TEST_DEFINE(json_test_string_null)
  247. {
  248. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  249. switch (cmd) {
  250. case TEST_INIT:
  251. info->name = "string_null";
  252. info->category = CATEGORY;
  253. info->summary = "JSON string NULL tests.";
  254. info->description = "Test JSON abstraction library.";
  255. return AST_TEST_NOT_RUN;
  256. case TEST_EXECUTE:
  257. break;
  258. }
  259. /* NULL string */
  260. uut = ast_json_string_create(NULL);
  261. ast_test_validate(test, NULL == uut);
  262. /* NULL JSON strings */
  263. ast_test_validate(test, NULL == ast_json_string_create(NULL));
  264. ast_test_validate(test, NULL == ast_json_string_get(NULL));
  265. ast_test_validate(test, -1 == ast_json_string_set(NULL, "not null"));
  266. /* string_value from non-string elements should return NULL */
  267. ast_test_validate(test, NULL == ast_json_string_get(ast_json_null()));
  268. ast_test_validate(test, NULL == ast_json_string_get(ast_json_false()));
  269. ast_test_validate(test, NULL == ast_json_string_get(ast_json_true()));
  270. return AST_TEST_PASS;
  271. }
  272. AST_TEST_DEFINE(json_test_stringf)
  273. {
  274. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  275. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  276. switch (cmd) {
  277. case TEST_INIT:
  278. info->name = "stringf";
  279. info->category = CATEGORY;
  280. info->summary = "Basic string formatting tests.";
  281. info->description = "Test JSON abstraction library.";
  282. return AST_TEST_NOT_RUN;
  283. case TEST_EXECUTE:
  284. break;
  285. }
  286. /* NULL format string */
  287. uut = ast_json_stringf(NULL);
  288. ast_test_validate(test, NULL == uut);
  289. /* Non-UTF-8 strings are invalid */
  290. uut = ast_json_stringf("Not UTF-8 - %s", "\xff");
  291. ast_test_validate(test, NULL == uut);
  292. /* formatted string */
  293. uut = ast_json_stringf("Hello, %s", "json");
  294. expected = ast_json_string_create("Hello, json");
  295. ast_test_validate(test, NULL != uut);
  296. ast_test_validate(test, ast_json_equal(expected, uut));
  297. return AST_TEST_PASS;
  298. }
  299. AST_TEST_DEFINE(json_test_int)
  300. {
  301. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  302. int uut_res;
  303. switch (cmd) {
  304. case TEST_INIT:
  305. info->name = "type_int";
  306. info->category = CATEGORY;
  307. info->summary = "Basic JSON integer tests.";
  308. info->description = "Test JSON abstraction library.";
  309. return AST_TEST_NOT_RUN;
  310. case TEST_EXECUTE:
  311. break;
  312. }
  313. /* Integer tests */
  314. uut = ast_json_integer_create(0);
  315. ast_test_validate(test, NULL != uut);
  316. ast_test_validate(test, AST_JSON_INTEGER == ast_json_typeof(uut));
  317. ast_test_validate(test, 0 == ast_json_integer_get(uut));
  318. uut_res = ast_json_integer_set(uut, 1);
  319. ast_test_validate(test, 0 == uut_res);
  320. ast_test_validate(test, 1 == ast_json_integer_get(uut));
  321. uut_res = ast_json_integer_set(uut, -1);
  322. ast_test_validate(test, 0 == uut_res);
  323. ast_test_validate(test, -1 == ast_json_integer_get(uut));
  324. uut_res = ast_json_integer_set(uut, LLONG_MAX);
  325. ast_test_validate(test, 0 == uut_res);
  326. ast_test_validate(test, LLONG_MAX == ast_json_integer_get(uut));
  327. uut_res = ast_json_integer_set(uut, LLONG_MIN);
  328. ast_test_validate(test, 0 == uut_res);
  329. ast_test_validate(test, LLONG_MIN == ast_json_integer_get(uut));
  330. return AST_TEST_PASS;
  331. }
  332. AST_TEST_DEFINE(json_test_non_int)
  333. {
  334. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  335. switch (cmd) {
  336. case TEST_INIT:
  337. info->name = "non_int";
  338. info->category = CATEGORY;
  339. info->summary = "Testing integer functions with non-integer types.";
  340. info->description = "Test JSON abstraction library.";
  341. return AST_TEST_NOT_RUN;
  342. case TEST_EXECUTE:
  343. break;
  344. }
  345. /* Non-ints return 0 integer value */
  346. ast_test_validate(test, 0 == ast_json_integer_get(ast_json_null()));
  347. ast_test_validate(test, 0 == ast_json_integer_get(ast_json_true()));
  348. ast_test_validate(test, 0 == ast_json_integer_get(ast_json_false()));
  349. /* JSON NULL integers */
  350. ast_test_validate(test, 0 == ast_json_integer_get(NULL));
  351. ast_test_validate(test, -1 == ast_json_integer_set(NULL, 911));
  352. ast_test_validate(test, 0 == ast_json_array_size(NULL));
  353. /* No magical parsing of strings into ints */
  354. uut = ast_json_string_create("314");
  355. ast_test_validate(test, NULL != uut);
  356. ast_test_validate(test, 0 == ast_json_integer_get(uut));
  357. /* Or vice-versa */
  358. ast_json_unref(uut);
  359. uut = ast_json_integer_create(314);
  360. ast_test_validate(test, NULL == ast_json_string_get(uut));
  361. return AST_TEST_PASS;
  362. }
  363. AST_TEST_DEFINE(json_test_array_create)
  364. {
  365. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  366. switch (cmd) {
  367. case TEST_INIT:
  368. info->name = "array_create";
  369. info->category = CATEGORY;
  370. info->summary = "Testing creating JSON arrays.";
  371. info->description = "Test JSON abstraction library.";
  372. return AST_TEST_NOT_RUN;
  373. case TEST_EXECUTE:
  374. break;
  375. }
  376. /* array creation */
  377. uut = ast_json_array_create();
  378. ast_test_validate(test, NULL != uut);
  379. ast_test_validate(test, AST_JSON_ARRAY == ast_json_typeof(uut));
  380. ast_test_validate(test, 0 == ast_json_array_size(uut));
  381. return AST_TEST_PASS;
  382. }
  383. AST_TEST_DEFINE(json_test_array_append)
  384. {
  385. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  386. int uut_res;
  387. switch (cmd) {
  388. case TEST_INIT:
  389. info->name = "array_append";
  390. info->category = CATEGORY;
  391. info->summary = "Testing appending to JSON arrays.";
  392. info->description = "Test JSON abstraction library.";
  393. return AST_TEST_NOT_RUN;
  394. case TEST_EXECUTE:
  395. break;
  396. }
  397. /* array append */
  398. uut = ast_json_array_create();
  399. uut_res = ast_json_array_append(uut, ast_json_string_create("one"));
  400. ast_test_validate(test, 0 == uut_res);
  401. ast_test_validate(test, 1 == ast_json_array_size(uut));
  402. ast_test_validate(test, 0 == strcmp("one", ast_json_string_get(ast_json_array_get(uut, 0))));
  403. /* index out of range */
  404. ast_test_validate(test, NULL == ast_json_array_get(uut, 1));
  405. ast_test_validate(test, NULL == ast_json_array_get(uut, -1));
  406. return AST_TEST_PASS;
  407. }
  408. AST_TEST_DEFINE(json_test_array_inset)
  409. {
  410. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  411. int uut_res;
  412. switch (cmd) {
  413. case TEST_INIT:
  414. info->name = "array_insert";
  415. info->category = CATEGORY;
  416. info->summary = "Testing inserting into JSON arrays.";
  417. info->description = "Test JSON abstraction library.";
  418. return AST_TEST_NOT_RUN;
  419. case TEST_EXECUTE:
  420. break;
  421. }
  422. /* array insert */
  423. uut = ast_json_pack("[s]", "one");
  424. uut_res = ast_json_array_insert(uut, 0, ast_json_string_create("zero"));
  425. ast_test_validate(test, 0 == uut_res);
  426. ast_test_validate(test, 2 == ast_json_array_size(uut));
  427. ast_test_validate(test, 0 == strcmp("zero", ast_json_string_get(ast_json_array_get(uut, 0))));
  428. ast_test_validate(test, 0 == strcmp("one", ast_json_string_get(ast_json_array_get(uut, 1))));
  429. return AST_TEST_PASS;
  430. }
  431. AST_TEST_DEFINE(json_test_array_set)
  432. {
  433. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  434. int uut_res;
  435. switch (cmd) {
  436. case TEST_INIT:
  437. info->name = "array_set";
  438. info->category = CATEGORY;
  439. info->summary = "Testing setting a value in JSON arrays.";
  440. info->description = "Test JSON abstraction library.";
  441. return AST_TEST_NOT_RUN;
  442. case TEST_EXECUTE:
  443. break;
  444. }
  445. /* array set */
  446. uut = ast_json_pack("[s, s]", "zero", "one");
  447. uut_res = ast_json_array_set(uut, 1, ast_json_integer_create(1));
  448. ast_test_validate(test, 0 == uut_res);
  449. ast_test_validate(test, 2 == ast_json_array_size(uut));
  450. ast_test_validate(test, 0 == strcmp("zero", ast_json_string_get(ast_json_array_get(uut, 0))));
  451. ast_test_validate(test, 1 == ast_json_integer_get(ast_json_array_get(uut, 1)));
  452. return AST_TEST_PASS;
  453. }
  454. AST_TEST_DEFINE(json_test_array_remove)
  455. {
  456. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  457. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  458. int uut_res;
  459. switch (cmd) {
  460. case TEST_INIT:
  461. info->name = "array_remove";
  462. info->category = CATEGORY;
  463. info->summary = "Testing removing a value from JSON arrays.";
  464. info->description = "Test JSON abstraction library.";
  465. return AST_TEST_NOT_RUN;
  466. case TEST_EXECUTE:
  467. break;
  468. }
  469. /* array remove */
  470. uut = ast_json_pack("[s, i]", "zero", 1);
  471. expected = ast_json_pack("[i]", 1);
  472. uut_res = ast_json_array_remove(uut, 0);
  473. ast_test_validate(test, 0 == uut_res);
  474. ast_test_validate(test, ast_json_equal(expected, uut));
  475. return AST_TEST_PASS;
  476. }
  477. AST_TEST_DEFINE(json_test_array_clear)
  478. {
  479. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  480. int uut_res;
  481. switch (cmd) {
  482. case TEST_INIT:
  483. info->name = "array_clear";
  484. info->category = CATEGORY;
  485. info->summary = "Testing clearing JSON arrays.";
  486. info->description = "Test JSON abstraction library.";
  487. return AST_TEST_NOT_RUN;
  488. case TEST_EXECUTE:
  489. break;
  490. }
  491. /* array clear */
  492. uut = ast_json_pack("[s, s]", "zero", "one");
  493. uut_res = ast_json_array_clear(uut);
  494. ast_test_validate(test, 0 == uut_res);
  495. ast_test_validate(test, 0 == ast_json_array_size(uut));
  496. return AST_TEST_PASS;
  497. }
  498. AST_TEST_DEFINE(json_test_array_extend)
  499. {
  500. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  501. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  502. RAII_VAR(struct ast_json *, tail, NULL, ast_json_unref);
  503. int uut_res;
  504. switch (cmd) {
  505. case TEST_INIT:
  506. info->name = "array_extend";
  507. info->category = CATEGORY;
  508. info->summary = "Testing extending JSON arrays.";
  509. info->description = "Test JSON abstraction library.";
  510. return AST_TEST_NOT_RUN;
  511. case TEST_EXECUTE:
  512. break;
  513. }
  514. /* array extending */
  515. expected = ast_json_array_create();
  516. ast_json_array_append(expected, ast_json_string_create("a"));
  517. ast_json_array_append(expected, ast_json_string_create("b"));
  518. ast_json_array_append(expected, ast_json_string_create("c"));
  519. ast_json_array_append(expected, ast_json_integer_create(1));
  520. ast_json_array_append(expected, ast_json_integer_create(2));
  521. ast_json_array_append(expected, ast_json_integer_create(3));
  522. uut = ast_json_array_create();
  523. ast_json_array_append(uut, ast_json_string_create("a"));
  524. ast_json_array_append(uut, ast_json_string_create("b"));
  525. ast_json_array_append(uut, ast_json_string_create("c"));
  526. tail = ast_json_array_create();
  527. ast_json_array_append(tail, ast_json_integer_create(1));
  528. ast_json_array_append(tail, ast_json_integer_create(2));
  529. ast_json_array_append(tail, ast_json_integer_create(3));
  530. uut_res = ast_json_array_extend(uut, tail);
  531. ast_test_validate(test, 0 == uut_res);
  532. ast_test_validate(test, ast_json_equal(expected, uut));
  533. /* tail is preserved */
  534. ast_test_validate(test, 3 == ast_json_array_size(tail));
  535. return AST_TEST_PASS;
  536. }
  537. AST_TEST_DEFINE(json_test_array_null)
  538. {
  539. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  540. switch (cmd) {
  541. case TEST_INIT:
  542. info->name = "array_null";
  543. info->category = CATEGORY;
  544. info->summary = "Testing NULL conditions for JSON arrays.";
  545. info->description = "Test JSON abstraction library.";
  546. return AST_TEST_NOT_RUN;
  547. case TEST_EXECUTE:
  548. break;
  549. }
  550. /* array NULL checks */
  551. ast_test_validate(test, 0 == ast_json_array_size(NULL));
  552. ast_test_validate(test, NULL == ast_json_array_get(NULL, 0));
  553. ast_test_validate(test, -1 == ast_json_array_set(NULL, 0, ast_json_null()));
  554. ast_test_validate(test, -1 == ast_json_array_append(NULL, ast_json_null()));
  555. ast_test_validate(test, -1 == ast_json_array_insert(NULL, 0, ast_json_null()));
  556. ast_test_validate(test, -1 == ast_json_array_remove(NULL, 0));
  557. ast_test_validate(test, -1 == ast_json_array_clear(NULL));
  558. uut = ast_json_array_create();
  559. ast_test_validate(test, -1 == ast_json_array_extend(uut, NULL));
  560. ast_test_validate(test, -1 == ast_json_array_extend(NULL, uut));
  561. ast_test_validate(test, -1 == ast_json_array_extend(NULL, NULL));
  562. return AST_TEST_PASS;
  563. }
  564. AST_TEST_DEFINE(json_test_object_alloc)
  565. {
  566. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  567. switch (cmd) {
  568. case TEST_INIT:
  569. info->name = "object_alloc";
  570. info->category = CATEGORY;
  571. info->summary = "Testing creating JSON objects.";
  572. info->description = "Test JSON abstraction library.";
  573. return AST_TEST_NOT_RUN;
  574. case TEST_EXECUTE:
  575. break;
  576. }
  577. /* object allocation */
  578. uut = ast_json_object_create();
  579. ast_test_validate(test, NULL != uut);
  580. ast_test_validate(test, AST_JSON_OBJECT == ast_json_typeof(uut));
  581. ast_test_validate(test, 0 == ast_json_object_size(uut));
  582. return AST_TEST_PASS;
  583. }
  584. AST_TEST_DEFINE(json_test_object_set)
  585. {
  586. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  587. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  588. int uut_res;
  589. switch (cmd) {
  590. case TEST_INIT:
  591. info->name = "object_set";
  592. info->category = CATEGORY;
  593. info->summary = "Testing setting values in JSON objects.";
  594. info->description = "Test JSON abstraction library.";
  595. return AST_TEST_NOT_RUN;
  596. case TEST_EXECUTE:
  597. break;
  598. }
  599. /* object set */
  600. expected = ast_json_pack("{s: i, s: i, s: i}", "one", 1, "two", 2, "three", 3);
  601. uut = ast_json_object_create();
  602. uut_res = ast_json_object_set(uut, "one", ast_json_integer_create(1));
  603. ast_test_validate(test, 0 == uut_res);
  604. uut_res = ast_json_object_set(uut, "two", ast_json_integer_create(2));
  605. ast_test_validate(test, 0 == uut_res);
  606. uut_res = ast_json_object_set(uut, "three", ast_json_integer_create(3));
  607. ast_test_validate(test, 0 == uut_res);
  608. ast_test_validate(test, ast_json_equal(expected, uut));
  609. ast_test_validate(test, NULL == ast_json_object_get(uut, "dne"));
  610. return AST_TEST_PASS;
  611. }
  612. AST_TEST_DEFINE(json_test_object_set_overwrite)
  613. {
  614. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  615. int uut_res;
  616. switch (cmd) {
  617. case TEST_INIT:
  618. info->name = "object_set_overwriting";
  619. info->category = CATEGORY;
  620. info->summary = "Testing changing values in JSON objects.";
  621. info->description = "Test JSON abstraction library.";
  622. return AST_TEST_NOT_RUN;
  623. case TEST_EXECUTE:
  624. break;
  625. }
  626. /* object set existing */
  627. uut = ast_json_pack("{s: i, s: i, s: i}", "one", 1, "two", 2, "three", 3);
  628. uut_res = ast_json_object_set(uut, "two", ast_json_integer_create(-2));
  629. ast_test_validate(test, 0 == uut_res);
  630. ast_test_validate(test, -2 == ast_json_integer_get(ast_json_object_get(uut, "two")));
  631. return AST_TEST_PASS;
  632. }
  633. AST_TEST_DEFINE(json_test_object_get)
  634. {
  635. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  636. switch (cmd) {
  637. case TEST_INIT:
  638. info->name = "object_get";
  639. info->category = CATEGORY;
  640. info->summary = "Testing getting values from JSON objects.";
  641. info->description = "Test JSON abstraction library.";
  642. return AST_TEST_NOT_RUN;
  643. case TEST_EXECUTE:
  644. break;
  645. }
  646. /* object get */
  647. uut = ast_json_pack("{s: i, s: i, s: i}", "one", 1, "two", 2, "three", 3);
  648. ast_test_validate(test, 2 == ast_json_integer_get(ast_json_object_get(uut, "two")));
  649. ast_test_validate(test, NULL == ast_json_object_get(uut, "dne"));
  650. ast_test_validate(test, NULL == ast_json_object_get(uut, NULL));
  651. return AST_TEST_PASS;
  652. }
  653. AST_TEST_DEFINE(json_test_object_del)
  654. {
  655. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  656. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  657. int uut_res;
  658. switch (cmd) {
  659. case TEST_INIT:
  660. info->name = "object_del";
  661. info->category = CATEGORY;
  662. info->summary = "Testing deleting values from JSON objects.";
  663. info->description = "Test JSON abstraction library.";
  664. return AST_TEST_NOT_RUN;
  665. case TEST_EXECUTE:
  666. break;
  667. }
  668. /* object del */
  669. expected = ast_json_object_create();
  670. uut = ast_json_pack("{s: i}", "one", 1);
  671. uut_res = ast_json_object_del(uut, "one");
  672. ast_test_validate(test, 0 == uut_res);
  673. ast_test_validate(test, ast_json_equal(expected, uut));
  674. uut_res = ast_json_object_del(uut, "dne");
  675. ast_test_validate(test, -1 == uut_res);
  676. return AST_TEST_PASS;
  677. }
  678. AST_TEST_DEFINE(json_test_object_clear)
  679. {
  680. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  681. int uut_res;
  682. switch (cmd) {
  683. case TEST_INIT:
  684. info->name = "object_clear";
  685. info->category = CATEGORY;
  686. info->summary = "Testing clearing values from JSON objects.";
  687. info->description = "Test JSON abstraction library.";
  688. return AST_TEST_NOT_RUN;
  689. case TEST_EXECUTE:
  690. break;
  691. }
  692. /* object clear */
  693. uut = ast_json_object_create();
  694. ast_json_object_set(uut, "one", ast_json_integer_create(1));
  695. ast_json_object_set(uut, "two", ast_json_integer_create(2));
  696. ast_json_object_set(uut, "three", ast_json_integer_create(3));
  697. uut_res = ast_json_object_clear(uut);
  698. ast_test_validate(test, 0 == uut_res);
  699. ast_test_validate(test, 0 == ast_json_object_size(uut));
  700. return AST_TEST_PASS;
  701. }
  702. AST_TEST_DEFINE(json_test_object_merge_all)
  703. {
  704. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  705. RAII_VAR(struct ast_json *, merge, NULL, ast_json_unref);
  706. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  707. int uut_res;
  708. switch (cmd) {
  709. case TEST_INIT:
  710. info->name = "object_alloc";
  711. info->category = CATEGORY;
  712. info->summary = "Testing merging JSON objects.";
  713. info->description = "Test JSON abstraction library.";
  714. return AST_TEST_NOT_RUN;
  715. case TEST_EXECUTE:
  716. break;
  717. }
  718. /* object merging - all */
  719. uut = ast_json_object_create();
  720. ast_json_object_set(uut, "one", ast_json_integer_create(1));
  721. ast_json_object_set(uut, "two", ast_json_integer_create(2));
  722. ast_json_object_set(uut, "three", ast_json_integer_create(3));
  723. merge = ast_json_object_create();
  724. ast_json_object_set(merge, "three", ast_json_integer_create(-3));
  725. ast_json_object_set(merge, "four", ast_json_integer_create(-4));
  726. ast_json_object_set(merge, "five", ast_json_integer_create(-5));
  727. expected = ast_json_object_create();
  728. ast_json_object_set(expected, "one", ast_json_integer_create(1));
  729. ast_json_object_set(expected, "two", ast_json_integer_create(2));
  730. ast_json_object_set(expected, "three", ast_json_integer_create(-3));
  731. ast_json_object_set(expected, "four", ast_json_integer_create(-4));
  732. ast_json_object_set(expected, "five", ast_json_integer_create(-5));
  733. uut_res = ast_json_object_update(uut, merge);
  734. ast_test_validate(test, 0 == uut_res);
  735. ast_test_validate(test, ast_json_equal(expected, uut));
  736. /* merge object is untouched */
  737. ast_test_validate(test, 3 == ast_json_object_size(merge));
  738. return AST_TEST_PASS;
  739. }
  740. AST_TEST_DEFINE(json_test_object_merge_existing)
  741. {
  742. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  743. RAII_VAR(struct ast_json *, merge, NULL, ast_json_unref);
  744. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  745. int uut_res;
  746. switch (cmd) {
  747. case TEST_INIT:
  748. info->name = "object_alloc";
  749. info->category = CATEGORY;
  750. info->summary = "Testing merging JSON objects, updating only existing fields.";
  751. info->description = "Test JSON abstraction library.";
  752. return AST_TEST_NOT_RUN;
  753. case TEST_EXECUTE:
  754. break;
  755. }
  756. /* object merging - existing */
  757. uut = ast_json_object_create();
  758. ast_json_object_set(uut, "one", ast_json_integer_create(1));
  759. ast_json_object_set(uut, "two", ast_json_integer_create(2));
  760. ast_json_object_set(uut, "three", ast_json_integer_create(3));
  761. merge = ast_json_object_create();
  762. ast_json_object_set(merge, "three", ast_json_integer_create(-3));
  763. ast_json_object_set(merge, "four", ast_json_integer_create(-4));
  764. ast_json_object_set(merge, "five", ast_json_integer_create(-5));
  765. expected = ast_json_object_create();
  766. ast_json_object_set(expected, "one", ast_json_integer_create(1));
  767. ast_json_object_set(expected, "two", ast_json_integer_create(2));
  768. ast_json_object_set(expected, "three", ast_json_integer_create(-3));
  769. uut_res = ast_json_object_update_existing(uut, merge);
  770. ast_test_validate(test, 0 == uut_res);
  771. ast_test_validate(test, ast_json_equal(expected, uut));
  772. /* merge object is untouched */
  773. ast_test_validate(test, 3 == ast_json_object_size(merge));
  774. return AST_TEST_PASS;
  775. }
  776. AST_TEST_DEFINE(json_test_object_merge_missing)
  777. {
  778. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  779. RAII_VAR(struct ast_json *, merge, NULL, ast_json_unref);
  780. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  781. int uut_res;
  782. switch (cmd) {
  783. case TEST_INIT:
  784. info->name = "object_merge_missing";
  785. info->category = CATEGORY;
  786. info->summary = "Testing merging JSON objects, adding only missing fields.";
  787. info->description = "Test JSON abstraction library.";
  788. return AST_TEST_NOT_RUN;
  789. case TEST_EXECUTE:
  790. break;
  791. }
  792. /* object merging - missing */
  793. uut = ast_json_object_create();
  794. ast_json_object_set(uut, "one", ast_json_integer_create(1));
  795. ast_json_object_set(uut, "two", ast_json_integer_create(2));
  796. ast_json_object_set(uut, "three", ast_json_integer_create(3));
  797. merge = ast_json_object_create();
  798. ast_json_object_set(merge, "three", ast_json_integer_create(-3));
  799. ast_json_object_set(merge, "four", ast_json_integer_create(-4));
  800. ast_json_object_set(merge, "five", ast_json_integer_create(-5));
  801. expected = ast_json_object_create();
  802. ast_json_object_set(expected, "one", ast_json_integer_create(1));
  803. ast_json_object_set(expected, "two", ast_json_integer_create(2));
  804. ast_json_object_set(expected, "three", ast_json_integer_create(3));
  805. ast_json_object_set(expected, "four", ast_json_integer_create(-4));
  806. ast_json_object_set(expected, "five", ast_json_integer_create(-5));
  807. uut_res = ast_json_object_update_missing(uut, merge);
  808. ast_test_validate(test, 0 == uut_res);
  809. ast_test_validate(test, ast_json_equal(expected, uut));
  810. /* merge object is untouched */
  811. ast_test_validate(test, 3 == ast_json_object_size(merge));
  812. return AST_TEST_PASS;
  813. }
  814. AST_TEST_DEFINE(json_test_object_null)
  815. {
  816. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  817. switch (cmd) {
  818. case TEST_INIT:
  819. info->name = "object_null";
  820. info->category = CATEGORY;
  821. info->summary = "Testing JSON object NULL behavior.";
  822. info->description = "Test JSON abstraction library.";
  823. return AST_TEST_NOT_RUN;
  824. case TEST_EXECUTE:
  825. break;
  826. }
  827. /* Object NULL testing */
  828. ast_test_validate(test, 0 == ast_json_object_size(NULL));
  829. ast_test_validate(test, NULL == ast_json_object_get(NULL, "not null"));
  830. ast_test_validate(test, -1 == ast_json_object_set(NULL, "not null", ast_json_null()));
  831. ast_test_validate(test, -1 == ast_json_object_del(NULL, "not null"));
  832. ast_test_validate(test, -1 == ast_json_object_clear(NULL));
  833. uut = ast_json_object_create();
  834. ast_test_validate(test, -1 == ast_json_object_update(NULL, uut));
  835. ast_test_validate(test, -1 == ast_json_object_update(uut, NULL));
  836. ast_test_validate(test, -1 == ast_json_object_update(NULL, NULL));
  837. ast_test_validate(test, -1 == ast_json_object_update_existing(NULL, uut));
  838. ast_test_validate(test, -1 == ast_json_object_update_existing(uut, NULL));
  839. ast_test_validate(test, -1 == ast_json_object_update_existing(NULL, NULL));
  840. ast_test_validate(test, -1 == ast_json_object_update_missing(NULL, uut));
  841. ast_test_validate(test, -1 == ast_json_object_update_missing(uut, NULL));
  842. ast_test_validate(test, -1 == ast_json_object_update_missing(NULL, NULL));
  843. return AST_TEST_PASS;
  844. }
  845. AST_TEST_DEFINE(json_test_object_iter)
  846. {
  847. struct ast_json_iter *iter;
  848. int count;
  849. int uut_res;
  850. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  851. switch (cmd) {
  852. case TEST_INIT:
  853. info->name = "object_iter";
  854. info->category = CATEGORY;
  855. info->summary = "Testing iterating through JSON objects.";
  856. info->description = "Test JSON abstraction library.";
  857. return AST_TEST_NOT_RUN;
  858. case TEST_EXECUTE:
  859. break;
  860. }
  861. /* Object iterator testing */
  862. uut = ast_json_pack("{s: i, s: i, s: i, s: i, s: i}", "one", 1, "two", 2, "three", 3, "four", 4, "five", 5);
  863. /* Iterate through the object; be aware that order isn't specified */
  864. iter = ast_json_object_iter(uut);
  865. ast_test_validate(test, NULL != iter);
  866. count = 0;
  867. while (NULL != iter) {
  868. if (0 == strcmp("one", ast_json_object_iter_key(iter))) {
  869. ast_test_validate(test, 1 == ast_json_integer_get(ast_json_object_iter_value(iter)));
  870. } else if (0 == strcmp("two", ast_json_object_iter_key(iter))) {
  871. ast_test_validate(test, 2 == ast_json_integer_get(ast_json_object_iter_value(iter)));
  872. } else if (0 == strcmp("three", ast_json_object_iter_key(iter))) {
  873. ast_test_validate(test, 3 == ast_json_integer_get(ast_json_object_iter_value(iter)));
  874. } else if (0 == strcmp("four", ast_json_object_iter_key(iter))) {
  875. ast_test_validate(test, 4 == ast_json_integer_get(ast_json_object_iter_value(iter)));
  876. } else if (0 == strcmp("five", ast_json_object_iter_key(iter))) {
  877. ast_test_validate(test, 5 == ast_json_integer_get(ast_json_object_iter_value(iter)));
  878. } else {
  879. /* Unexpected key */
  880. ast_test_validate(test, 0);
  881. }
  882. iter = ast_json_object_iter_next(uut, iter);
  883. ++count;
  884. }
  885. ast_test_validate(test, 5 == count);
  886. /* iterator non-existing key */
  887. iter = ast_json_object_iter_at(uut, "dne");
  888. ast_test_validate(test, NULL == iter);
  889. /* iterator specific key */
  890. iter = ast_json_object_iter_at(uut, "three");
  891. ast_test_validate(test, NULL != iter);
  892. ast_test_validate(test, 3 == ast_json_integer_get(ast_json_object_iter_value(iter)));
  893. /* set via iter */
  894. iter = ast_json_object_iter_at(uut, "three");
  895. uut_res = ast_json_object_iter_set(uut, iter, ast_json_integer_create(-3));
  896. ast_test_validate(test, 0 == uut_res);
  897. ast_test_validate(test, -3 == ast_json_integer_get(ast_json_object_get(uut, "three")));
  898. return AST_TEST_PASS;
  899. }
  900. AST_TEST_DEFINE(json_test_object_iter_null)
  901. {
  902. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  903. switch (cmd) {
  904. case TEST_INIT:
  905. info->name = "object_iter_null";
  906. info->category = CATEGORY;
  907. info->summary = "Testing JSON object iterator NULL testings.";
  908. info->description = "Test JSON abstraction library.";
  909. return AST_TEST_NOT_RUN;
  910. case TEST_EXECUTE:
  911. break;
  912. }
  913. /* iterator NULL tests */
  914. uut = ast_json_object_create();
  915. ast_test_validate(test, NULL == ast_json_object_iter(NULL));
  916. ast_test_validate(test, NULL == ast_json_object_iter_at(NULL, "not null"));
  917. ast_test_validate(test, NULL == ast_json_object_iter_next(NULL, NULL));
  918. ast_test_validate(test, NULL == ast_json_object_iter_next(uut, NULL));
  919. ast_test_validate(test, NULL == ast_json_object_iter_key(NULL));
  920. ast_test_validate(test, NULL == ast_json_object_iter_value(NULL));
  921. ast_test_validate(test, -1 == ast_json_object_iter_set(NULL, NULL, ast_json_null()));
  922. ast_test_validate(test, -1 == ast_json_object_iter_set(uut, NULL, ast_json_null()));
  923. return AST_TEST_PASS;
  924. }
  925. AST_TEST_DEFINE(json_test_dump_load_string)
  926. {
  927. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  928. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  929. RAII_VAR(char *, str, NULL, json_debug_free);
  930. switch (cmd) {
  931. case TEST_INIT:
  932. info->name = "dump_load_string";
  933. info->category = CATEGORY;
  934. info->summary = "Testing dumping strings from JSON.";
  935. info->description = "Test JSON abstraction library.";
  936. return AST_TEST_NOT_RUN;
  937. case TEST_EXECUTE:
  938. break;
  939. }
  940. expected = ast_json_pack("{ s: i }", "one", 1);
  941. str = ast_json_dump_string(expected);
  942. ast_test_validate(test, NULL != str);
  943. uut = ast_json_load_string(str, NULL);
  944. ast_test_validate(test, NULL != uut);
  945. ast_test_validate(test, ast_json_equal(expected, uut));
  946. /* dump_string NULL */
  947. ast_test_validate(test, NULL == ast_json_dump_string(NULL));
  948. return AST_TEST_PASS;
  949. }
  950. AST_TEST_DEFINE(json_test_dump_load_str)
  951. {
  952. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  953. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  954. RAII_VAR(struct ast_str *, astr, NULL, ast_free);
  955. int uut_res;
  956. switch (cmd) {
  957. case TEST_INIT:
  958. info->name = "dump_load_str";
  959. info->category = CATEGORY;
  960. info->summary = "Testing dumping ast_str from JSON.";
  961. info->description = "Test JSON abstraction library.";
  962. return AST_TEST_NOT_RUN;
  963. case TEST_EXECUTE:
  964. break;
  965. }
  966. /* dump/load ast_str */
  967. expected = ast_json_pack("{ s: i }", "one", 1);
  968. astr = ast_str_create(1); /* should expand to hold output */
  969. uut_res = ast_json_dump_str(expected, &astr);
  970. ast_test_validate(test, 0 == uut_res);
  971. uut = ast_json_load_str(astr, NULL);
  972. ast_test_validate(test, NULL != uut);
  973. ast_test_validate(test, ast_json_equal(expected, uut));
  974. return AST_TEST_PASS;
  975. }
  976. AST_TEST_DEFINE(json_test_dump_str_fail)
  977. {
  978. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  979. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  980. struct ast_str *astr;
  981. int uut_res;
  982. switch (cmd) {
  983. case TEST_INIT:
  984. info->name = "dump_str_fail";
  985. info->category = CATEGORY;
  986. info->summary = "Testing dumping to ast_str when it can't grow.";
  987. info->description = "Test JSON abstraction library.";
  988. return AST_TEST_NOT_RUN;
  989. case TEST_EXECUTE:
  990. break;
  991. }
  992. /* dump ast_str growth failure */
  993. expected = ast_json_pack("{ s: i }", "one", 1);
  994. astr = ast_str_alloca(1); /* cannot grow */
  995. uut_res = ast_json_dump_str(expected, &astr);
  996. ast_test_validate(test, 0 != uut_res);
  997. return AST_TEST_PASS;
  998. }
  999. AST_TEST_DEFINE(json_test_load_buffer)
  1000. {
  1001. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1002. const char *str;
  1003. switch (cmd) {
  1004. case TEST_INIT:
  1005. info->name = "load_buffer";
  1006. info->category = CATEGORY;
  1007. info->summary = "Testing loading JSON from buffer.";
  1008. info->description = "Test JSON abstraction library.";
  1009. return AST_TEST_NOT_RUN;
  1010. case TEST_EXECUTE:
  1011. break;
  1012. }
  1013. /* load buffer */
  1014. str = "{ \"one\": 1 } trailing garbage";
  1015. uut = ast_json_load_string(str, NULL);
  1016. ast_test_validate(test, NULL == uut);
  1017. uut = ast_json_load_buf(str, strlen("{ \"one\": 1 }"), NULL);
  1018. ast_test_validate(test, NULL != uut);
  1019. return AST_TEST_PASS;
  1020. }
  1021. /*! \brief \a fclose isn't NULL safe. */
  1022. static int safe_fclose(FILE *f)
  1023. {
  1024. if (f) {
  1025. return fclose(f);
  1026. }
  1027. return 0;
  1028. }
  1029. static FILE *mkstemp_file(char *template, const char *mode)
  1030. {
  1031. int fd = mkstemp(template);
  1032. FILE *file;
  1033. if (fd < 0) {
  1034. ast_log(LOG_ERROR, "Failed to create temp file: %s\n",
  1035. strerror(errno));
  1036. return NULL;
  1037. }
  1038. file = fdopen(fd, mode);
  1039. if (!file) {
  1040. ast_log(LOG_ERROR, "Failed to create temp file: %s\n",
  1041. strerror(errno));
  1042. return NULL;
  1043. }
  1044. return file;
  1045. }
  1046. AST_TEST_DEFINE(json_test_dump_load_file)
  1047. {
  1048. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1049. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  1050. char filename[] = "/tmp/ast_json.XXXXXX";
  1051. RAII_VAR(char *, rm_on_exit, filename, unlink);
  1052. RAII_VAR(FILE *, file, NULL, safe_fclose);
  1053. int uut_res;
  1054. switch (cmd) {
  1055. case TEST_INIT:
  1056. info->name = "dump_load_file";
  1057. info->category = CATEGORY;
  1058. info->summary = "Testing dumping/loading JSON to/from file by FILE *.";
  1059. info->description = "Test JSON abstraction library.";
  1060. return AST_TEST_NOT_RUN;
  1061. case TEST_EXECUTE:
  1062. break;
  1063. }
  1064. /* dump/load file */
  1065. expected = ast_json_pack("{ s: i }", "one", 1);
  1066. file = mkstemp_file(filename, "w");
  1067. ast_test_validate(test, NULL != file);
  1068. uut_res = ast_json_dump_file(expected, file);
  1069. ast_test_validate(test, 0 == uut_res);
  1070. fclose(file);
  1071. file = fopen(filename, "r");
  1072. ast_test_validate(test, NULL != file);
  1073. uut = ast_json_load_file(file, NULL);
  1074. ast_test_validate(test, ast_json_equal(expected, uut));
  1075. return AST_TEST_PASS;
  1076. }
  1077. AST_TEST_DEFINE(json_test_dump_load_new_file)
  1078. {
  1079. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1080. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  1081. char filename[] = "/tmp/ast_json.XXXXXX";
  1082. RAII_VAR(char *, rm_on_exit, filename, unlink);
  1083. RAII_VAR(FILE *, file, NULL, safe_fclose);
  1084. int uut_res;
  1085. switch (cmd) {
  1086. case TEST_INIT:
  1087. info->name = "dump_load_new_file";
  1088. info->category = CATEGORY;
  1089. info->summary = "Testing dumping/load JSON to/from file by filename.";
  1090. info->description = "Test JSON abstraction library.";
  1091. return AST_TEST_NOT_RUN;
  1092. case TEST_EXECUTE:
  1093. break;
  1094. }
  1095. /* dump/load filename */
  1096. expected = ast_json_pack("{ s: i }", "one", 1);
  1097. file = mkstemp_file(filename, "w");
  1098. ast_test_validate(test, NULL != file);
  1099. uut_res = ast_json_dump_new_file(expected, filename);
  1100. ast_test_validate(test, 0 == uut_res);
  1101. uut = ast_json_load_new_file(filename, NULL);
  1102. ast_test_validate(test, ast_json_equal(expected, uut));
  1103. return AST_TEST_PASS;
  1104. }
  1105. AST_TEST_DEFINE(json_test_dump_load_null)
  1106. {
  1107. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1108. char filename[] = "/tmp/ast_json.XXXXXX";
  1109. RAII_VAR(char *, rm_on_exit, filename, unlink);
  1110. RAII_VAR(FILE *, file, NULL, safe_fclose);
  1111. switch (cmd) {
  1112. case TEST_INIT:
  1113. info->name = "dump_load_null";
  1114. info->category = CATEGORY;
  1115. info->summary = "Testing NULL handling of dump/load functions.";
  1116. info->description = "Test JSON abstraction library.";
  1117. return AST_TEST_NOT_RUN;
  1118. case TEST_EXECUTE:
  1119. break;
  1120. }
  1121. /* dump/load NULL tests */
  1122. uut = ast_json_load_string("{ \"one\": 1 }", NULL);
  1123. ast_test_validate(test, NULL != uut);
  1124. file = mkstemp_file(filename, "w");
  1125. ast_test_validate(test, NULL != file);
  1126. ast_test_validate(test, NULL == ast_json_dump_string(NULL));
  1127. ast_test_validate(test, -1 == ast_json_dump_file(NULL, file));
  1128. ast_test_validate(test, -1 == ast_json_dump_file(uut, NULL));
  1129. ast_test_validate(test, -1 == ast_json_dump_file(NULL, NULL));
  1130. ast_test_validate(test, -1 == ast_json_dump_new_file(uut, NULL));
  1131. ast_test_validate(test, -1 == ast_json_dump_new_file(NULL, filename));
  1132. ast_test_validate(test, -1 == ast_json_dump_new_file(NULL, NULL));
  1133. ast_test_validate(test, NULL == ast_json_load_string(NULL, NULL));
  1134. ast_test_validate(test, NULL == ast_json_load_buf(NULL, 0, NULL));
  1135. ast_test_validate(test, NULL == ast_json_load_file(NULL, NULL));
  1136. ast_test_validate(test, NULL == ast_json_load_new_file(NULL, NULL));
  1137. return AST_TEST_PASS;
  1138. }
  1139. AST_TEST_DEFINE(json_test_parse_errors)
  1140. {
  1141. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1142. switch (cmd) {
  1143. case TEST_INIT:
  1144. info->name = "parse_errors";
  1145. info->category = CATEGORY;
  1146. info->summary = "Testing various parse errors.";
  1147. info->description = "Test JSON abstraction library.";
  1148. return AST_TEST_NOT_RUN;
  1149. case TEST_EXECUTE:
  1150. break;
  1151. }
  1152. /* parse errors */
  1153. ast_test_validate(test, NULL == ast_json_load_string("'singleton'", NULL));
  1154. ast_test_validate(test, NULL == ast_json_load_string("{ no value }", NULL));
  1155. ast_test_validate(test, NULL == ast_json_load_string("{ 'no': 'curly' ", NULL));
  1156. ast_test_validate(test, NULL == ast_json_load_string("[ 'no', 'square'", NULL));
  1157. ast_test_validate(test, NULL == ast_json_load_string("{ 1: 'int key' }", NULL));
  1158. ast_test_validate(test, NULL == ast_json_load_string("", NULL));
  1159. ast_test_validate(test, NULL == ast_json_load_string("{ 'missing' 'colon' }", NULL));
  1160. ast_test_validate(test, NULL == ast_json_load_string("[ 'missing' 'comma' ]", NULL));
  1161. return AST_TEST_PASS;
  1162. }
  1163. AST_TEST_DEFINE(json_test_pack)
  1164. {
  1165. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1166. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  1167. switch (cmd) {
  1168. case TEST_INIT:
  1169. info->name = "pack";
  1170. info->category = CATEGORY;
  1171. info->summary = "Testing json_pack function.";
  1172. info->description = "Test JSON abstraction library.";
  1173. return AST_TEST_NOT_RUN;
  1174. case TEST_EXECUTE:
  1175. break;
  1176. }
  1177. /* pack test */
  1178. expected = ast_json_array_create();
  1179. ast_json_array_append(expected, ast_json_array_create());
  1180. ast_json_array_append(expected, ast_json_object_create());
  1181. ast_json_array_append(ast_json_array_get(expected, 0), ast_json_integer_create(1));
  1182. ast_json_array_append(ast_json_array_get(expected, 0), ast_json_integer_create(2));
  1183. ast_json_object_set(ast_json_array_get(expected, 1), "cool", ast_json_true());
  1184. uut = ast_json_pack("[[i,i],{s:b}]", 1, 2, "cool", 1);
  1185. ast_test_validate(test, NULL != uut);
  1186. ast_test_validate(test, ast_json_equal(expected, uut));
  1187. return AST_TEST_PASS;
  1188. }
  1189. AST_TEST_DEFINE(json_test_pack_ownership)
  1190. {
  1191. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1192. switch (cmd) {
  1193. case TEST_INIT:
  1194. info->name = "pack_ownership";
  1195. info->category = CATEGORY;
  1196. info->summary = "Testing json_pack failure conditions.";
  1197. info->description = "Test JSON abstraction library.";
  1198. return AST_TEST_NOT_RUN;
  1199. case TEST_EXECUTE:
  1200. break;
  1201. }
  1202. uut = ast_json_pack("[o]", ast_json_string_create("Am I freed?"));
  1203. return AST_TEST_PASS;
  1204. }
  1205. AST_TEST_DEFINE(json_test_pack_errors)
  1206. {
  1207. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1208. switch (cmd) {
  1209. case TEST_INIT:
  1210. info->name = "object_alloc";
  1211. info->category = CATEGORY;
  1212. info->summary = "Testing json_pack failure conditions.";
  1213. info->description = "Test JSON abstraction library.";
  1214. return AST_TEST_NOT_RUN;
  1215. case TEST_EXECUTE:
  1216. break;
  1217. }
  1218. /* pack errors */
  1219. ast_test_validate(test, NULL == ast_json_pack(NULL));
  1220. ast_test_validate(test, NULL == ast_json_pack("{s:i", "no curly", 911));
  1221. ast_test_validate(test, NULL == ast_json_pack("[s, s", "no", "square"));
  1222. return AST_TEST_PASS;
  1223. }
  1224. AST_TEST_DEFINE(json_test_copy)
  1225. {
  1226. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1227. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  1228. switch (cmd) {
  1229. case TEST_INIT:
  1230. info->name = "copy";
  1231. info->category = CATEGORY;
  1232. info->summary = "Testing copying JSON.";
  1233. info->description = "Test JSON abstraction library.";
  1234. return AST_TEST_NOT_RUN;
  1235. case TEST_EXECUTE:
  1236. break;
  1237. }
  1238. /* copy test */
  1239. expected = ast_json_pack("{s: {s: i}}", "outer", "inner", 8675309);
  1240. uut = ast_json_copy(expected);
  1241. ast_test_validate(test, NULL != uut);
  1242. ast_test_validate(test, ast_json_equal(expected, uut));
  1243. ast_test_validate(test, ast_json_object_get(expected, "outer") == ast_json_object_get(uut, "outer"));
  1244. return AST_TEST_PASS;
  1245. }
  1246. AST_TEST_DEFINE(json_test_deep_copy)
  1247. {
  1248. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1249. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  1250. switch (cmd) {
  1251. case TEST_INIT:
  1252. info->name = "deep_copy";
  1253. info->category = CATEGORY;
  1254. info->summary = "Testing deep copying of JSON.";
  1255. info->description = "Test JSON abstraction library.";
  1256. return AST_TEST_NOT_RUN;
  1257. case TEST_EXECUTE:
  1258. break;
  1259. }
  1260. /* deep copy test */
  1261. expected = ast_json_pack("{s: {s: i}}", "outer", "inner", 8675309);
  1262. uut = ast_json_deep_copy(expected);
  1263. ast_test_validate(test, NULL != uut);
  1264. ast_test_validate(test, ast_json_equal(expected, uut));
  1265. ast_test_validate(test, ast_json_object_get(expected, "outer") != ast_json_object_get(uut, "outer"));
  1266. /* Changing the inner value of one should not change the other */
  1267. ast_json_integer_set(ast_json_object_get(ast_json_object_get(uut, "outer"), "inner"), 411);
  1268. ast_test_validate(test, !ast_json_equal(expected, uut));
  1269. return AST_TEST_PASS;
  1270. }
  1271. AST_TEST_DEFINE(json_test_copy_null)
  1272. {
  1273. switch (cmd) {
  1274. case TEST_INIT:
  1275. info->name = "copy_null";
  1276. info->category = CATEGORY;
  1277. info->summary = "Testing NULL handling of copy functions.";
  1278. info->description = "Test JSON abstraction library.";
  1279. return AST_TEST_NOT_RUN;
  1280. case TEST_EXECUTE:
  1281. break;
  1282. }
  1283. /* copy NULL */
  1284. ast_test_validate(test, NULL == ast_json_copy(NULL));
  1285. ast_test_validate(test, NULL == ast_json_deep_copy(NULL));
  1286. return AST_TEST_PASS;
  1287. }
  1288. AST_TEST_DEFINE(json_test_circular_object)
  1289. {
  1290. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1291. int uut_res;
  1292. switch (cmd) {
  1293. case TEST_INIT:
  1294. info->name = "circular_object";
  1295. info->category = CATEGORY;
  1296. info->summary = "Object cannot be added to itself.";
  1297. info->description = "Test JSON abstraction library.";
  1298. return AST_TEST_NOT_RUN;
  1299. case TEST_EXECUTE:
  1300. break;
  1301. }
  1302. /* circular reference testing */
  1303. /* Cannot add self */
  1304. uut = ast_json_object_create();
  1305. uut_res = ast_json_object_set(uut, "myself", ast_json_ref(uut));
  1306. ast_test_validate(test, -1 == uut_res);
  1307. ast_test_validate(test, 0 == ast_json_object_size(uut));
  1308. return AST_TEST_PASS;
  1309. }
  1310. AST_TEST_DEFINE(json_test_circular_array)
  1311. {
  1312. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1313. int uut_res;
  1314. switch (cmd) {
  1315. case TEST_INIT:
  1316. info->name = "circular_array";
  1317. info->category = CATEGORY;
  1318. info->summary = "Array cannot be added to itself.";
  1319. info->description = "Test JSON abstraction library.";
  1320. return AST_TEST_NOT_RUN;
  1321. case TEST_EXECUTE:
  1322. break;
  1323. }
  1324. uut = ast_json_array_create();
  1325. ast_test_validate(test, 0 == ast_json_array_size(uut));
  1326. uut_res = ast_json_array_append(uut, ast_json_ref(uut));
  1327. ast_test_validate(test, -1 == uut_res);
  1328. ast_test_validate(test, 0 == ast_json_array_size(uut));
  1329. return AST_TEST_PASS;
  1330. }
  1331. AST_TEST_DEFINE(json_test_clever_circle)
  1332. {
  1333. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1334. RAII_VAR(struct ast_json *, inner_child, NULL, ast_json_unref);
  1335. RAII_VAR(char *, str, NULL, json_debug_free);
  1336. int uut_res;
  1337. switch (cmd) {
  1338. case TEST_INIT:
  1339. info->name = "clever_circle";
  1340. info->category = CATEGORY;
  1341. info->summary = "JSON with circular references cannot be encoded.";
  1342. info->description = "Test JSON abstraction library.";
  1343. return AST_TEST_NOT_RUN;
  1344. case TEST_EXECUTE:
  1345. break;
  1346. }
  1347. /* can add to self if you're clever enough, but it should not encode */
  1348. uut = ast_json_object_create();
  1349. inner_child = ast_json_object_create();
  1350. uut_res = ast_json_object_set(uut, "inner_child", ast_json_ref(inner_child)); /* incref to keep a reference */
  1351. ast_test_validate(test, 0 == uut_res);
  1352. uut_res = ast_json_object_set(inner_child, "parent", ast_json_ref(uut)); /* incref to keep a reference */
  1353. ast_test_validate(test, 0 == uut_res);
  1354. str = ast_json_dump_string(uut);
  1355. ast_test_validate(test, NULL == str);
  1356. /* Circular refs screw up reference counting, so break the cycle */
  1357. ast_json_object_clear(inner_child);
  1358. return AST_TEST_PASS;
  1359. }
  1360. static int test_name_number(const char *name, const char *number)
  1361. {
  1362. int res;
  1363. struct ast_json *uut;
  1364. struct ast_json *expected;
  1365. expected = ast_json_pack("{s: s, s: s}",
  1366. "name", name ?: "",
  1367. "number", number ?: "");
  1368. uut = ast_json_name_number(name, number);
  1369. res = ast_json_equal(expected, uut);
  1370. ast_json_unref(expected);
  1371. ast_json_unref(uut);
  1372. return res;
  1373. }
  1374. AST_TEST_DEFINE(json_test_name_number)
  1375. {
  1376. switch (cmd) {
  1377. case TEST_INIT:
  1378. info->name = "name_number";
  1379. info->category = CATEGORY;
  1380. info->summary = "JSON encoding of name/number pair.";
  1381. info->description = "Test JSON abstraction library.";
  1382. return AST_TEST_NOT_RUN;
  1383. case TEST_EXECUTE:
  1384. break;
  1385. }
  1386. ast_test_validate(test, test_name_number("name", NULL));
  1387. ast_test_validate(test, test_name_number(NULL, "1234"));
  1388. ast_test_validate(test, test_name_number(NULL, NULL));
  1389. ast_test_validate(test, test_name_number("Jenny", "867-5309"));
  1390. return AST_TEST_PASS;
  1391. }
  1392. AST_TEST_DEFINE(json_test_timeval)
  1393. {
  1394. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1395. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  1396. struct timeval tv = {};
  1397. switch (cmd) {
  1398. case TEST_INIT:
  1399. info->name = "type_timeval";
  1400. info->category = CATEGORY;
  1401. info->summary = "JSON encoding of timevals.";
  1402. info->description = "Test JSON abstraction library.";
  1403. return AST_TEST_NOT_RUN;
  1404. case TEST_EXECUTE:
  1405. break;
  1406. }
  1407. expected = ast_json_string_create("2013-02-07T09:32:34.314-0600");
  1408. tv.tv_sec = 1360251154;
  1409. tv.tv_usec = 314159;
  1410. uut = ast_json_timeval(tv, "America/Chicago");
  1411. ast_test_validate(test, ast_json_equal(expected, uut));
  1412. return AST_TEST_PASS;
  1413. }
  1414. AST_TEST_DEFINE(json_test_cep)
  1415. {
  1416. RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
  1417. RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
  1418. switch (cmd) {
  1419. case TEST_INIT:
  1420. info->name = "cep";
  1421. info->category = CATEGORY;
  1422. info->summary = "JSON with circular references cannot be encoded.";
  1423. info->description = "Test JSON abstraction library.";
  1424. return AST_TEST_NOT_RUN;
  1425. case TEST_EXECUTE:
  1426. break;
  1427. }
  1428. expected = ast_json_pack("{s: o, s: o, s: o}",
  1429. "context", ast_json_null(),
  1430. "exten", ast_json_null(),
  1431. "priority", ast_json_null());
  1432. uut = ast_json_dialplan_cep(NULL, NULL, -1);
  1433. ast_test_validate(test, ast_json_equal(expected, uut));
  1434. ast_json_unref(expected);
  1435. ast_json_unref(uut);
  1436. expected = ast_json_pack("{s: s, s: s, s: i}",
  1437. "context", "main",
  1438. "exten", "4321",
  1439. "priority", 7);
  1440. uut = ast_json_dialplan_cep("main", "4321", 7);
  1441. ast_test_validate(test, ast_json_equal(expected, uut));
  1442. return AST_TEST_PASS;
  1443. }
  1444. static int unload_module(void)
  1445. {
  1446. AST_TEST_UNREGISTER(json_test_false);
  1447. AST_TEST_UNREGISTER(json_test_true);
  1448. AST_TEST_UNREGISTER(json_test_bool0);
  1449. AST_TEST_UNREGISTER(json_test_bool1);
  1450. AST_TEST_UNREGISTER(json_test_null);
  1451. AST_TEST_UNREGISTER(json_test_null_val);
  1452. AST_TEST_UNREGISTER(json_test_string);
  1453. AST_TEST_UNREGISTER(json_test_string_null);
  1454. AST_TEST_UNREGISTER(json_test_stringf);
  1455. AST_TEST_UNREGISTER(json_test_int);
  1456. AST_TEST_UNREGISTER(json_test_non_int);
  1457. AST_TEST_UNREGISTER(json_test_array_create);
  1458. AST_TEST_UNREGISTER(json_test_array_append);
  1459. AST_TEST_UNREGISTER(json_test_array_inset);
  1460. AST_TEST_UNREGISTER(json_test_array_set);
  1461. AST_TEST_UNREGISTER(json_test_array_remove);
  1462. AST_TEST_UNREGISTER(json_test_array_clear);
  1463. AST_TEST_UNREGISTER(json_test_array_extend);
  1464. AST_TEST_UNREGISTER(json_test_array_null);
  1465. AST_TEST_UNREGISTER(json_test_object_alloc);
  1466. AST_TEST_UNREGISTER(json_test_object_set);
  1467. AST_TEST_UNREGISTER(json_test_object_set_overwrite);
  1468. AST_TEST_UNREGISTER(json_test_object_get);
  1469. AST_TEST_UNREGISTER(json_test_object_del);
  1470. AST_TEST_UNREGISTER(json_test_object_clear);
  1471. AST_TEST_UNREGISTER(json_test_object_merge_all);
  1472. AST_TEST_UNREGISTER(json_test_object_merge_existing);
  1473. AST_TEST_UNREGISTER(json_test_object_merge_missing);
  1474. AST_TEST_UNREGISTER(json_test_object_null);
  1475. AST_TEST_UNREGISTER(json_test_object_iter);
  1476. AST_TEST_UNREGISTER(json_test_object_iter_null);
  1477. AST_TEST_UNREGISTER(json_test_dump_load_string);
  1478. AST_TEST_UNREGISTER(json_test_dump_load_str);
  1479. AST_TEST_UNREGISTER(json_test_dump_str_fail);
  1480. AST_TEST_UNREGISTER(json_test_load_buffer);
  1481. AST_TEST_UNREGISTER(json_test_dump_load_file);
  1482. AST_TEST_UNREGISTER(json_test_dump_load_new_file);
  1483. AST_TEST_UNREGISTER(json_test_dump_load_null);
  1484. AST_TEST_UNREGISTER(json_test_parse_errors);
  1485. AST_TEST_UNREGISTER(json_test_pack);
  1486. AST_TEST_UNREGISTER(json_test_pack_ownership);
  1487. AST_TEST_UNREGISTER(json_test_pack_errors);
  1488. AST_TEST_UNREGISTER(json_test_copy);
  1489. AST_TEST_UNREGISTER(json_test_deep_copy);
  1490. AST_TEST_UNREGISTER(json_test_copy_null);
  1491. AST_TEST_UNREGISTER(json_test_circular_object);
  1492. AST_TEST_UNREGISTER(json_test_circular_array);
  1493. AST_TEST_UNREGISTER(json_test_clever_circle);
  1494. AST_TEST_UNREGISTER(json_test_name_number);
  1495. AST_TEST_UNREGISTER(json_test_timeval);
  1496. AST_TEST_UNREGISTER(json_test_cep);
  1497. return 0;
  1498. }
  1499. static int load_module(void)
  1500. {
  1501. AST_TEST_REGISTER(json_test_false);
  1502. AST_TEST_REGISTER(json_test_true);
  1503. AST_TEST_REGISTER(json_test_bool0);
  1504. AST_TEST_REGISTER(json_test_bool1);
  1505. AST_TEST_REGISTER(json_test_null);
  1506. AST_TEST_REGISTER(json_test_null_val);
  1507. AST_TEST_REGISTER(json_test_string);
  1508. AST_TEST_REGISTER(json_test_string_null);
  1509. AST_TEST_REGISTER(json_test_stringf);
  1510. AST_TEST_REGISTER(json_test_int);
  1511. AST_TEST_REGISTER(json_test_non_int);
  1512. AST_TEST_REGISTER(json_test_array_create);
  1513. AST_TEST_REGISTER(json_test_array_append);
  1514. AST_TEST_REGISTER(json_test_array_inset);
  1515. AST_TEST_REGISTER(json_test_array_set);
  1516. AST_TEST_REGISTER(json_test_array_remove);
  1517. AST_TEST_REGISTER(json_test_array_clear);
  1518. AST_TEST_REGISTER(json_test_array_extend);
  1519. AST_TEST_REGISTER(json_test_array_null);
  1520. AST_TEST_REGISTER(json_test_object_alloc);
  1521. AST_TEST_REGISTER(json_test_object_set);
  1522. AST_TEST_REGISTER(json_test_object_set_overwrite);
  1523. AST_TEST_REGISTER(json_test_object_get);
  1524. AST_TEST_REGISTER(json_test_object_del);
  1525. AST_TEST_REGISTER(json_test_object_clear);
  1526. AST_TEST_REGISTER(json_test_object_merge_all);
  1527. AST_TEST_REGISTER(json_test_object_merge_existing);
  1528. AST_TEST_REGISTER(json_test_object_merge_missing);
  1529. AST_TEST_REGISTER(json_test_object_null);
  1530. AST_TEST_REGISTER(json_test_object_iter);
  1531. AST_TEST_REGISTER(json_test_object_iter_null);
  1532. AST_TEST_REGISTER(json_test_dump_load_string);
  1533. AST_TEST_REGISTER(json_test_dump_load_str);
  1534. AST_TEST_REGISTER(json_test_dump_str_fail);
  1535. AST_TEST_REGISTER(json_test_load_buffer);
  1536. AST_TEST_REGISTER(json_test_dump_load_file);
  1537. AST_TEST_REGISTER(json_test_dump_load_new_file);
  1538. AST_TEST_REGISTER(json_test_dump_load_null);
  1539. AST_TEST_REGISTER(json_test_parse_errors);
  1540. AST_TEST_REGISTER(json_test_pack);
  1541. AST_TEST_REGISTER(json_test_pack_ownership);
  1542. AST_TEST_REGISTER(json_test_pack_errors);
  1543. AST_TEST_REGISTER(json_test_copy);
  1544. AST_TEST_REGISTER(json_test_deep_copy);
  1545. AST_TEST_REGISTER(json_test_copy_null);
  1546. AST_TEST_REGISTER(json_test_circular_object);
  1547. AST_TEST_REGISTER(json_test_circular_array);
  1548. AST_TEST_REGISTER(json_test_clever_circle);
  1549. AST_TEST_REGISTER(json_test_name_number);
  1550. AST_TEST_REGISTER(json_test_timeval);
  1551. AST_TEST_REGISTER(json_test_cep);
  1552. ast_test_register_init(CATEGORY, json_test_init);
  1553. ast_test_register_cleanup(CATEGORY, json_test_cleanup);
  1554. return AST_MODULE_LOAD_SUCCESS;
  1555. }
  1556. AST_MODULE_INFO(ASTERISK_GPL_KEY, 0, "JSON testing",
  1557. .support_level = AST_MODULE_SUPPORT_CORE,
  1558. .load = load_module,
  1559. .unload = unload_module
  1560. );