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