Newer
Older
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2012 - 2013, Digium, Inc.
*
* David M. Lee, II <dlee@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
*
* While some of these tests are actually testing our JSON library wrapper, the bulk of
* them are exploratory tests to determine what the behavior of the underlying JSON
* library is. This also gives us a good indicator if that behavior changes between
* Jansson revisions.
*
* \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
*
* \ingroup tests
*/
/*** MODULEINFO
<depend>TEST_FRAMEWORK</depend>
<support_level>core</support_level>
***/
#include "asterisk.h"
#include "asterisk/json.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
#include <stdio.h>
#include <unistd.h>
#define CATEGORY "/main/json/"
/*!
* Number of allocations from JSON library that have not yet been freed.
*/
static size_t alloc_count;
/*!@{*/
/*!
* JSON library has its own reference counting, so we'll provide our own allocators to
* test that everything gets freed as expected.
*/
static void *json_debug_malloc(size_t size)
{
if (p) {
++alloc_count;
}
return p;
}
static void json_debug_free(void *p)
{
if (p) {
--alloc_count;
}
static int json_test_init(struct ast_test_info *info, struct ast_test *test)
{
ast_json_set_alloc_funcs(json_debug_malloc, json_debug_free);
alloc_count = 0;
static int json_test_cleanup(struct ast_test_info *info, struct ast_test *test)
{
ast_json_reset_alloc_funcs();
if (0 != alloc_count) {
ast_test_status_update(test,
"JSON test leaked %zu allocations!\n", alloc_count);
}
/*!@}*/
AST_TEST_DEFINE(json_test_false)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "type_false";
info->category = CATEGORY;
info->summary = "Testing fundamental JSON false value.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
uut = ast_json_false();
ast_test_validate(test, NULL != uut);
ast_test_validate(test, AST_JSON_FALSE == ast_json_typeof(uut));
ast_test_validate(test, !ast_json_is_null(uut));
ast_test_validate(test, !ast_json_is_true(uut));
ast_test_validate(test, ast_json_is_false(uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_true)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->category = CATEGORY;
info->summary = "Testing JSON true value.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
uut = ast_json_true();
ast_test_validate(test, NULL != uut);
ast_test_validate(test, AST_JSON_TRUE == ast_json_typeof(uut));
ast_test_validate(test, !ast_json_is_null(uut));
ast_test_validate(test, ast_json_is_true(uut));
ast_test_validate(test, !ast_json_is_false(uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_bool0)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "type_bool0";
info->category = CATEGORY;
info->summary = "Testing JSON boolean function (false).";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
uut = ast_json_boolean(0);
ast_test_validate(test, NULL != uut);
ast_test_validate(test, AST_JSON_FALSE == ast_json_typeof(uut));
ast_test_validate(test, !ast_json_is_null(uut));
ast_test_validate(test, !ast_json_is_true(uut));
ast_test_validate(test, ast_json_is_false(uut));
ast_test_validate(test, ast_json_equal(uut, ast_json_false()));
ast_test_validate(test, !ast_json_equal(uut, ast_json_true()));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_bool1)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "type_bool1";
info->category = CATEGORY;
info->summary = "Testing JSON boolean function (true).";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
uut = ast_json_boolean(1);
ast_test_validate(test, NULL != uut);
ast_test_validate(test, AST_JSON_TRUE == ast_json_typeof(uut));
ast_test_validate(test, !ast_json_is_null(uut));
ast_test_validate(test, ast_json_is_true(uut));
ast_test_validate(test, !ast_json_is_false(uut));
ast_test_validate(test, !ast_json_equal(uut, ast_json_false()));
ast_test_validate(test, ast_json_equal(uut, ast_json_true()));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_null)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->category = CATEGORY;
info->summary = "Testing JSON null value.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
uut = ast_json_null();
ast_test_validate(test, NULL != uut);
ast_test_validate(test, AST_JSON_NULL == ast_json_typeof(uut));
ast_test_validate(test, ast_json_is_null(uut));
ast_test_validate(test, !ast_json_is_true(uut));
ast_test_validate(test, !ast_json_is_false(uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_null_val)
{
switch (cmd) {
case TEST_INIT:
info->name = "null_val";
info->category = CATEGORY;
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
info->summary = "Testing JSON handling of NULL.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* NULL isn't null, true or false */
ast_test_validate(test, !ast_json_is_null(NULL));
ast_test_validate(test, !ast_json_is_false(NULL));
ast_test_validate(test, !ast_json_is_true(NULL));
/* ref and unref should be NULL safe */
ast_json_ref(NULL);
ast_json_unref(NULL);
/* no segfault; we're good. le sigh. */
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_string)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "type_string";
info->category = CATEGORY;
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
info->summary = "Basic string tests.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
uut = ast_json_string_create("Hello, json");
ast_test_validate(test, NULL != uut);
ast_test_validate(test, AST_JSON_STRING == ast_json_typeof(uut));
ast_test_validate(test, 0 == strcmp("Hello, json", ast_json_string_get(uut)));
uut_res = ast_json_string_set(uut, NULL);
ast_test_validate(test, -1 == uut_res);
ast_test_validate(test, 0 == strcmp("Hello, json", ast_json_string_get(uut)));
uut_res = ast_json_string_set(uut, "Not UTF-8 - \xff");
ast_test_validate(test, -1 == uut_res);
ast_test_validate(test, 0 == strcmp("Hello, json", ast_json_string_get(uut)));
uut_res = ast_json_string_set(uut, "Is UTF-8 - \xE2\x98\xBA");
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, 0 == strcmp("Is UTF-8 - \xE2\x98\xBA", ast_json_string_get(uut)));
uut_res = ast_json_string_set(uut, "Goodbye, json");
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, 0 == strcmp("Goodbye, json", ast_json_string_get(uut)));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_string_null)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "string_null";
info->category = CATEGORY;
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
info->summary = "JSON string NULL tests.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* NULL string */
uut = ast_json_string_create(NULL);
ast_test_validate(test, NULL == uut);
/* NULL JSON strings */
ast_test_validate(test, NULL == ast_json_string_create(NULL));
ast_test_validate(test, NULL == ast_json_string_get(NULL));
ast_test_validate(test, -1 == ast_json_string_set(NULL, "not null"));
/* string_value from non-string elements should return NULL */
ast_test_validate(test, NULL == ast_json_string_get(ast_json_null()));
ast_test_validate(test, NULL == ast_json_string_get(ast_json_false()));
ast_test_validate(test, NULL == ast_json_string_get(ast_json_true()));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_stringf)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "stringf";
info->category = CATEGORY;
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
info->summary = "Basic string formatting tests.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* NULL format string */
uut = ast_json_stringf(NULL);
ast_test_validate(test, NULL == uut);
/* Non-UTF-8 strings are invalid */
uut = ast_json_stringf("Not UTF-8 - %s", "\xff");
ast_test_validate(test, NULL == uut);
/* formatted string */
uut = ast_json_stringf("Hello, %s", "json");
expected = ast_json_string_create("Hello, json");
ast_test_validate(test, NULL != uut);
ast_test_validate(test, ast_json_equal(expected, uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_int)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->category = CATEGORY;
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
info->summary = "Basic JSON integer tests.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* Integer tests */
uut = ast_json_integer_create(0);
ast_test_validate(test, NULL != uut);
ast_test_validate(test, AST_JSON_INTEGER == ast_json_typeof(uut));
ast_test_validate(test, 0 == ast_json_integer_get(uut));
uut_res = ast_json_integer_set(uut, 1);
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, 1 == ast_json_integer_get(uut));
uut_res = ast_json_integer_set(uut, -1);
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, -1 == ast_json_integer_get(uut));
uut_res = ast_json_integer_set(uut, LLONG_MAX);
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, LLONG_MAX == ast_json_integer_get(uut));
uut_res = ast_json_integer_set(uut, LLONG_MIN);
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, LLONG_MIN == ast_json_integer_get(uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_non_int)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "non_int";
info->category = CATEGORY;
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
info->summary = "Testing integer functions with non-integer types.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* Non-ints return 0 integer value */
ast_test_validate(test, 0 == ast_json_integer_get(ast_json_null()));
ast_test_validate(test, 0 == ast_json_integer_get(ast_json_true()));
ast_test_validate(test, 0 == ast_json_integer_get(ast_json_false()));
/* JSON NULL integers */
ast_test_validate(test, 0 == ast_json_integer_get(NULL));
ast_test_validate(test, -1 == ast_json_integer_set(NULL, 911));
ast_test_validate(test, 0 == ast_json_array_size(NULL));
/* No magical parsing of strings into ints */
uut = ast_json_string_create("314");
ast_test_validate(test, NULL != uut);
ast_test_validate(test, 0 == ast_json_integer_get(uut));
/* Or vice-versa */
ast_json_unref(uut);
uut = ast_json_integer_create(314);
ast_test_validate(test, NULL == ast_json_string_get(uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_array_create)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "array_create";
info->category = CATEGORY;
info->summary = "Testing creating JSON arrays.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* array creation */
uut = ast_json_array_create();
ast_test_validate(test, NULL != uut);
ast_test_validate(test, AST_JSON_ARRAY == ast_json_typeof(uut));
ast_test_validate(test, 0 == ast_json_array_size(uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_array_append)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "array_append";
info->category = CATEGORY;
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
info->summary = "Testing appending to JSON arrays.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* array append */
uut = ast_json_array_create();
uut_res = ast_json_array_append(uut, ast_json_string_create("one"));
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, 1 == ast_json_array_size(uut));
ast_test_validate(test, 0 == strcmp("one", ast_json_string_get(ast_json_array_get(uut, 0))));
/* index out of range */
ast_test_validate(test, NULL == ast_json_array_get(uut, 1));
ast_test_validate(test, NULL == ast_json_array_get(uut, -1));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_array_inset)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "array_insert";
info->category = CATEGORY;
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
info->summary = "Testing inserting into JSON arrays.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* array insert */
uut = ast_json_pack("[s]", "one");
uut_res = ast_json_array_insert(uut, 0, ast_json_string_create("zero"));
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, 2 == ast_json_array_size(uut));
ast_test_validate(test, 0 == strcmp("zero", ast_json_string_get(ast_json_array_get(uut, 0))));
ast_test_validate(test, 0 == strcmp("one", ast_json_string_get(ast_json_array_get(uut, 1))));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_array_set)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "array_set";
info->category = CATEGORY;
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
info->summary = "Testing setting a value in JSON arrays.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* array set */
uut = ast_json_pack("[s, s]", "zero", "one");
uut_res = ast_json_array_set(uut, 1, ast_json_integer_create(1));
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, 2 == ast_json_array_size(uut));
ast_test_validate(test, 0 == strcmp("zero", ast_json_string_get(ast_json_array_get(uut, 0))));
ast_test_validate(test, 1 == ast_json_integer_get(ast_json_array_get(uut, 1)));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_array_remove)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "array_remove";
info->category = CATEGORY;
info->summary = "Testing removing a value from JSON arrays.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* array remove */
uut = ast_json_pack("[s, i]", "zero", 1);
expected = ast_json_pack("[i]", 1);
uut_res = ast_json_array_remove(uut, 0);
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, ast_json_equal(expected, uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_array_clear)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "array_clear";
info->category = CATEGORY;
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
info->summary = "Testing clearing JSON arrays.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* array clear */
uut = ast_json_pack("[s, s]", "zero", "one");
uut_res = ast_json_array_clear(uut);
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, 0 == ast_json_array_size(uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_array_extend)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, tail, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "array_extend";
info->category = CATEGORY;
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
info->summary = "Testing extending JSON arrays.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* array extending */
expected = ast_json_array_create();
ast_json_array_append(expected, ast_json_string_create("a"));
ast_json_array_append(expected, ast_json_string_create("b"));
ast_json_array_append(expected, ast_json_string_create("c"));
ast_json_array_append(expected, ast_json_integer_create(1));
ast_json_array_append(expected, ast_json_integer_create(2));
ast_json_array_append(expected, ast_json_integer_create(3));
uut = ast_json_array_create();
ast_json_array_append(uut, ast_json_string_create("a"));
ast_json_array_append(uut, ast_json_string_create("b"));
ast_json_array_append(uut, ast_json_string_create("c"));
tail = ast_json_array_create();
ast_json_array_append(tail, ast_json_integer_create(1));
ast_json_array_append(tail, ast_json_integer_create(2));
ast_json_array_append(tail, ast_json_integer_create(3));
uut_res = ast_json_array_extend(uut, tail);
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, ast_json_equal(expected, uut));
/* tail is preserved */
ast_test_validate(test, 3 == ast_json_array_size(tail));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_array_null)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "array_null";
info->category = CATEGORY;
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
info->summary = "Testing NULL conditions for JSON arrays.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* array NULL checks */
ast_test_validate(test, 0 == ast_json_array_size(NULL));
ast_test_validate(test, NULL == ast_json_array_get(NULL, 0));
ast_test_validate(test, -1 == ast_json_array_set(NULL, 0, ast_json_null()));
ast_test_validate(test, -1 == ast_json_array_append(NULL, ast_json_null()));
ast_test_validate(test, -1 == ast_json_array_insert(NULL, 0, ast_json_null()));
ast_test_validate(test, -1 == ast_json_array_remove(NULL, 0));
ast_test_validate(test, -1 == ast_json_array_clear(NULL));
uut = ast_json_array_create();
ast_test_validate(test, -1 == ast_json_array_extend(uut, NULL));
ast_test_validate(test, -1 == ast_json_array_extend(NULL, uut));
ast_test_validate(test, -1 == ast_json_array_extend(NULL, NULL));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_object_alloc)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "object_alloc";
info->category = CATEGORY;
info->summary = "Testing creating JSON objects.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* object allocation */
uut = ast_json_object_create();
ast_test_validate(test, NULL != uut);
ast_test_validate(test, AST_JSON_OBJECT == ast_json_typeof(uut));
ast_test_validate(test, 0 == ast_json_object_size(uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_object_set)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "object_set";
info->category = CATEGORY;
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
info->summary = "Testing setting values in JSON objects.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* object set */
expected = ast_json_pack("{s: i, s: i, s: i}", "one", 1, "two", 2, "three", 3);
uut = ast_json_object_create();
uut_res = ast_json_object_set(uut, "one", ast_json_integer_create(1));
ast_test_validate(test, 0 == uut_res);
uut_res = ast_json_object_set(uut, "two", ast_json_integer_create(2));
ast_test_validate(test, 0 == uut_res);
uut_res = ast_json_object_set(uut, "three", ast_json_integer_create(3));
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, ast_json_equal(expected, uut));
ast_test_validate(test, NULL == ast_json_object_get(uut, "dne"));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_object_set_overwrite)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "object_set_overwriting";
info->category = CATEGORY;
info->summary = "Testing changing values in JSON objects.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* object set existing */
uut = ast_json_pack("{s: i, s: i, s: i}", "one", 1, "two", 2, "three", 3);
uut_res = ast_json_object_set(uut, "two", ast_json_integer_create(-2));
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, -2 == ast_json_integer_get(ast_json_object_get(uut, "two")));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_object_get)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "object_get";
info->category = CATEGORY;
info->summary = "Testing getting values from JSON objects.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* object get */
uut = ast_json_pack("{s: i, s: i, s: i}", "one", 1, "two", 2, "three", 3);
ast_test_validate(test, 2 == ast_json_integer_get(ast_json_object_get(uut, "two")));
ast_test_validate(test, NULL == ast_json_object_get(uut, "dne"));
ast_test_validate(test, NULL == ast_json_object_get(uut, NULL));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_object_del)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "object_del";
info->category = CATEGORY;
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
info->summary = "Testing deleting values from JSON objects.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* object del */
expected = ast_json_object_create();
uut = ast_json_pack("{s: i}", "one", 1);
uut_res = ast_json_object_del(uut, "one");
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, ast_json_equal(expected, uut));
uut_res = ast_json_object_del(uut, "dne");
ast_test_validate(test, -1 == uut_res);
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_object_clear)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "object_clear";
info->category = CATEGORY;
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
info->summary = "Testing clearing values from JSON objects.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* object clear */
uut = ast_json_object_create();
ast_json_object_set(uut, "one", ast_json_integer_create(1));
ast_json_object_set(uut, "two", ast_json_integer_create(2));
ast_json_object_set(uut, "three", ast_json_integer_create(3));
uut_res = ast_json_object_clear(uut);
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, 0 == ast_json_object_size(uut));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_object_merge_all)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, merge, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "object_alloc";
info->category = CATEGORY;
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
info->summary = "Testing merging JSON objects.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* object merging - all */
uut = ast_json_object_create();
ast_json_object_set(uut, "one", ast_json_integer_create(1));
ast_json_object_set(uut, "two", ast_json_integer_create(2));
ast_json_object_set(uut, "three", ast_json_integer_create(3));
merge = ast_json_object_create();
ast_json_object_set(merge, "three", ast_json_integer_create(-3));
ast_json_object_set(merge, "four", ast_json_integer_create(-4));
ast_json_object_set(merge, "five", ast_json_integer_create(-5));
expected = ast_json_object_create();
ast_json_object_set(expected, "one", ast_json_integer_create(1));
ast_json_object_set(expected, "two", ast_json_integer_create(2));
ast_json_object_set(expected, "three", ast_json_integer_create(-3));
ast_json_object_set(expected, "four", ast_json_integer_create(-4));
ast_json_object_set(expected, "five", ast_json_integer_create(-5));
uut_res = ast_json_object_update(uut, merge);
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, ast_json_equal(expected, uut));
/* merge object is untouched */
ast_test_validate(test, 3 == ast_json_object_size(merge));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_object_merge_existing)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, merge, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "object_alloc";
info->category = CATEGORY;
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
info->summary = "Testing merging JSON objects, updating only existing fields.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* object merging - existing */
uut = ast_json_object_create();
ast_json_object_set(uut, "one", ast_json_integer_create(1));
ast_json_object_set(uut, "two", ast_json_integer_create(2));
ast_json_object_set(uut, "three", ast_json_integer_create(3));
merge = ast_json_object_create();
ast_json_object_set(merge, "three", ast_json_integer_create(-3));
ast_json_object_set(merge, "four", ast_json_integer_create(-4));
ast_json_object_set(merge, "five", ast_json_integer_create(-5));
expected = ast_json_object_create();
ast_json_object_set(expected, "one", ast_json_integer_create(1));
ast_json_object_set(expected, "two", ast_json_integer_create(2));
ast_json_object_set(expected, "three", ast_json_integer_create(-3));
uut_res = ast_json_object_update_existing(uut, merge);
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, ast_json_equal(expected, uut));
/* merge object is untouched */
ast_test_validate(test, 3 == ast_json_object_size(merge));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_object_merge_missing)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, merge, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
int uut_res;
switch (cmd) {
case TEST_INIT:
info->name = "object_merge_missing";
info->category = CATEGORY;
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
info->summary = "Testing merging JSON objects, adding only missing fields.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* object merging - missing */
uut = ast_json_object_create();
ast_json_object_set(uut, "one", ast_json_integer_create(1));
ast_json_object_set(uut, "two", ast_json_integer_create(2));
ast_json_object_set(uut, "three", ast_json_integer_create(3));
merge = ast_json_object_create();
ast_json_object_set(merge, "three", ast_json_integer_create(-3));
ast_json_object_set(merge, "four", ast_json_integer_create(-4));
ast_json_object_set(merge, "five", ast_json_integer_create(-5));
expected = ast_json_object_create();
ast_json_object_set(expected, "one", ast_json_integer_create(1));
ast_json_object_set(expected, "two", ast_json_integer_create(2));
ast_json_object_set(expected, "three", ast_json_integer_create(3));
ast_json_object_set(expected, "four", ast_json_integer_create(-4));
ast_json_object_set(expected, "five", ast_json_integer_create(-5));
uut_res = ast_json_object_update_missing(uut, merge);
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, ast_json_equal(expected, uut));
/* merge object is untouched */
ast_test_validate(test, 3 == ast_json_object_size(merge));
return AST_TEST_PASS;
}
AST_TEST_DEFINE(json_test_object_null)
{
RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
switch (cmd) {
case TEST_INIT:
info->name = "object_null";
info->category = CATEGORY;
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
info->summary = "Testing JSON object NULL behavior.";
info->description = "Test JSON abstraction library.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* Object NULL testing */
ast_test_validate(test, 0 == ast_json_object_size(NULL));
ast_test_validate(test, NULL == ast_json_object_get(NULL, "not null"));
ast_test_validate(test, -1 == ast_json_object_set(NULL, "not null", ast_json_null()));
ast_test_validate(test, -1 == ast_json_object_del(NULL, "not null"));
ast_test_validate(test, -1 == ast_json_object_clear(NULL));
uut = ast_json_object_create();
ast_test_validate(test, -1 == ast_json_object_update(NULL, uut));
ast_test_validate(test, -1 == ast_json_object_update(uut, NULL));
ast_test_validate(test, -1 == ast_json_object_update(NULL, NULL));
ast_test_validate(test, -1 == ast_json_object_update_existing(NULL, uut));
ast_test_validate(test, -1 == ast_json_object_update_existing(uut, NULL));
ast_test_validate(test, -1 == ast_json_object_update_existing(NULL, NULL));
ast_test_validate(test, -1 == ast_json_object_update_missing(NULL, uut));
ast_test_validate(test, -1 == ast_json_object_update_missing(uut, NULL));
ast_test_validate(test, -1 == ast_json_object_update_missing(NULL, NULL));
return AST_TEST_PASS;
}