47 #define CATEGORY "/main/json/" 88 "JSON test leaked %zu allocations!\n",
alloc_count);
102 info->name =
"type_false";
104 info->summary =
"Testing fundamental JSON false value.";
105 info->description =
"Test JSON abstraction library.";
112 ast_test_validate(
test,
NULL != uut);
127 info->name =
"type_true";
129 info->summary =
"Testing JSON true value.";
130 info->description =
"Test JSON abstraction library.";
137 ast_test_validate(
test,
NULL != uut);
152 info->name =
"type_bool0";
154 info->summary =
"Testing JSON boolean function (false).";
155 info->description =
"Test JSON abstraction library.";
162 ast_test_validate(
test,
NULL != uut);
179 info->name =
"type_bool1";
181 info->summary =
"Testing JSON boolean function (true).";
182 info->description =
"Test JSON abstraction library.";
189 ast_test_validate(
test,
NULL != uut);
206 info->name =
"type_null";
208 info->summary =
"Testing JSON null value.";
209 info->description =
"Test JSON abstraction library.";
216 ast_test_validate(
test,
NULL != uut);
229 info->name =
"null_val";
231 info->summary =
"Testing JSON handling of NULL.";
232 info->description =
"Test JSON abstraction library.";
258 info->name =
"type_string";
260 info->summary =
"Basic string tests.";
261 info->description =
"Test JSON abstraction library.";
268 ast_test_validate(
test,
NULL != uut);
273 ast_test_validate(
test, -1 == uut_res);
277 ast_test_validate(
test, -1 == uut_res);
281 ast_test_validate(
test, 0 == uut_res);
285 ast_test_validate(
test, 0 == uut_res);
297 info->name =
"string_null";
299 info->summary =
"JSON string NULL tests.";
300 info->description =
"Test JSON abstraction library.";
308 ast_test_validate(
test,
NULL == uut);
330 info->name =
"stringf";
332 info->summary =
"Basic string formatting tests.";
333 info->description =
"Test JSON abstraction library.";
341 ast_test_validate(
test,
NULL == uut);
345 ast_test_validate(
test,
NULL == uut);
350 ast_test_validate(
test,
NULL != uut);
363 info->name =
"type_int";
365 info->summary =
"Basic JSON integer tests.";
366 info->description =
"Test JSON abstraction library.";
374 ast_test_validate(
test,
NULL != uut);
379 ast_test_validate(
test, 0 == uut_res);
383 ast_test_validate(
test, 0 == uut_res);
387 ast_test_validate(
test, 0 == uut_res);
391 ast_test_validate(
test, 0 == uut_res);
403 info->name =
"non_int";
405 info->summary =
"Testing integer functions with non-integer types.";
406 info->description =
"Test JSON abstraction library.";
424 ast_test_validate(
test,
NULL != uut);
441 info->name =
"array_create";
443 info->summary =
"Testing creating JSON arrays.";
444 info->description =
"Test JSON abstraction library.";
452 ast_test_validate(
test,
NULL != uut);
466 info->name =
"array_append";
468 info->summary =
"Testing appending to JSON arrays.";
469 info->description =
"Test JSON abstraction library.";
478 ast_test_validate(
test, 0 == uut_res);
495 info->name =
"array_insert";
497 info->summary =
"Testing inserting into JSON arrays.";
498 info->description =
"Test JSON abstraction library.";
507 ast_test_validate(
test, 0 == uut_res);
522 info->name =
"array_set";
524 info->summary =
"Testing setting a value in JSON arrays.";
525 info->description =
"Test JSON abstraction library.";
534 ast_test_validate(
test, 0 == uut_res);
550 info->name =
"array_remove";
552 info->summary =
"Testing removing a value from JSON arrays.";
553 info->description =
"Test JSON abstraction library.";
563 ast_test_validate(
test, 0 == uut_res);
576 info->name =
"array_clear";
578 info->summary =
"Testing clearing JSON arrays.";
579 info->description =
"Test JSON abstraction library.";
588 ast_test_validate(
test, 0 == uut_res);
603 info->name =
"array_extend";
605 info->summary =
"Testing extending JSON arrays.";
606 info->description =
"Test JSON abstraction library.";
632 ast_test_validate(
test, 0 == uut_res);
646 info->name =
"array_null";
648 info->summary =
"Testing NULL conditions for JSON arrays.";
649 info->description =
"Test JSON abstraction library.";
677 info->name =
"object_alloc";
679 info->summary =
"Testing creating JSON objects.";
680 info->description =
"Test JSON abstraction library.";
688 ast_test_validate(
test,
NULL != uut);
703 info->name =
"object_set";
705 info->summary =
"Testing setting values in JSON objects.";
706 info->description =
"Test JSON abstraction library.";
713 expected =
ast_json_pack(
"{s: i, s: i, s: i}",
"one", 1,
"two", 2,
"three", 3);
716 ast_test_validate(
test, 0 == uut_res);
718 ast_test_validate(
test, 0 == uut_res);
720 ast_test_validate(
test, 0 == uut_res);
734 info->name =
"object_set_overwriting";
736 info->summary =
"Testing changing values in JSON objects.";
737 info->description =
"Test JSON abstraction library.";
744 uut =
ast_json_pack(
"{s: i, s: i, s: i}",
"one", 1,
"two", 2,
"three", 3);
746 ast_test_validate(
test, 0 == uut_res);
758 info->name =
"object_get";
760 info->summary =
"Testing getting values from JSON objects.";
761 info->description =
"Test JSON abstraction library.";
768 uut =
ast_json_pack(
"{s: i, s: i, s: i}",
"one", 1,
"two", 2,
"three", 3);
784 info->name =
"object_del";
786 info->summary =
"Testing deleting values from JSON objects.";
787 info->description =
"Test JSON abstraction library.";
797 ast_test_validate(
test, 0 == uut_res);
800 ast_test_validate(
test, -1 == uut_res);
812 info->name =
"object_clear";
814 info->summary =
"Testing clearing values from JSON objects.";
815 info->description =
"Test JSON abstraction library.";
827 ast_test_validate(
test, 0 == uut_res);
842 info->name =
"object_alloc";
844 info->summary =
"Testing merging JSON objects.";
845 info->description =
"Test JSON abstraction library.";
870 ast_test_validate(
test, 0 == uut_res);
887 info->name =
"object_alloc";
889 info->summary =
"Testing merging JSON objects, updating only existing fields.";
890 info->description =
"Test JSON abstraction library.";
913 ast_test_validate(
test, 0 == uut_res);
930 info->name =
"object_merge_missing";
932 info->summary =
"Testing merging JSON objects, adding only missing fields.";
933 info->description =
"Test JSON abstraction library.";
958 ast_test_validate(
test, 0 == uut_res);
972 info->name =
"object_null";
974 info->summary =
"Testing JSON object NULL behavior.";
975 info->description =
"Test JSON abstraction library.";
1010 info->name =
"object_iter";
1012 info->summary =
"Testing iterating through JSON objects.";
1013 info->description =
"Test JSON abstraction library.";
1020 uut =
ast_json_pack(
"{s: i, s: i, s: i, s: i, s: i}",
"one", 1,
"two", 2,
"three", 3,
"four", 4,
"five", 5);
1024 ast_test_validate(
test,
NULL != iter);
1026 while (
NULL != iter) {
1039 ast_test_validate(
test, 0);
1044 ast_test_validate(
test, 5 == count);
1048 ast_test_validate(
test,
NULL == iter);
1052 ast_test_validate(
test,
NULL != iter);
1058 ast_test_validate(
test, 0 == uut_res);
1070 info->name =
"object_iter_null";
1072 info->summary =
"Testing JSON object iterator NULL testings.";
1073 info->description =
"Test JSON abstraction library.";
1101 info->name =
"dump_load_string";
1103 info->summary =
"Testing dumping strings from JSON.";
1104 info->description =
"Test JSON abstraction library.";
1113 ast_test_validate(
test,
NULL != uut);
1131 info->name =
"dump_load_str";
1133 info->summary =
"Testing dumping ast_str from JSON.";
1134 info->description =
"Test JSON abstraction library.";
1144 ast_test_validate(
test, 0 == uut_res);
1146 ast_test_validate(
test,
NULL != uut);
1161 info->name =
"dump_str_fail";
1163 info->summary =
"Testing dumping to ast_str when it can't grow.";
1164 info->description =
"Test JSON abstraction library.";
1174 ast_test_validate(
test, 0 != uut_res);
1186 info->name =
"load_buffer";
1188 info->summary =
"Testing loading JSON from buffer.";
1189 info->description =
"Test JSON abstraction library.";
1196 str =
"{ \"one\": 1 } trailing garbage";
1198 ast_test_validate(
test,
NULL == uut);
1200 ast_test_validate(
test,
NULL != uut);
1216 int fd = mkstemp(
template);
1225 file = fdopen(fd, mode);
1239 char filename[] =
"/tmp/ast_json.XXXXXX";
1240 RAII_VAR(
char *, rm_on_exit, filename, unlink);
1246 info->name =
"dump_load_file";
1248 info->summary =
"Testing dumping/loading JSON to/from file by FILE *.";
1249 info->description =
"Test JSON abstraction library.";
1260 ast_test_validate(
test, 0 == uut_res);
1262 file = fopen(filename,
"r");
1274 char filename[] =
"/tmp/ast_json.XXXXXX";
1275 RAII_VAR(
char *, rm_on_exit, filename, unlink);
1281 info->name =
"dump_load_new_file";
1283 info->summary =
"Testing dumping/load JSON to/from file by filename.";
1284 info->description =
"Test JSON abstraction library.";
1295 ast_test_validate(
test, 0 == uut_res);
1305 char filename[] =
"/tmp/ast_json.XXXXXX";
1306 RAII_VAR(
char *, rm_on_exit, filename, unlink);
1311 info->name =
"dump_load_null";
1313 info->summary =
"Testing NULL handling of dump/load functions.";
1314 info->description =
"Test JSON abstraction library.";
1322 ast_test_validate(
test,
NULL != uut);
1346 info->name =
"parse_errors";
1348 info->summary =
"Testing various parse errors.";
1349 info->description =
"Test JSON abstraction library.";
1375 info->name =
"pack";
1377 info->summary =
"Testing json_pack function.";
1378 info->description =
"Test JSON abstraction library.";
1392 ast_test_validate(
test,
NULL != uut);
1404 info->name =
"pack_ownership";
1406 info->summary =
"Testing json_pack failure conditions.";
1407 info->description =
"Test JSON abstraction library.";
1424 info->name =
"object_alloc";
1426 info->summary =
"Testing json_pack failure conditions.";
1427 info->description =
"Test JSON abstraction library.";
1448 info->name =
"copy";
1450 info->summary =
"Testing copying JSON.";
1451 info->description =
"Test JSON abstraction library.";
1458 expected =
ast_json_pack(
"{s: {s: i}}",
"outer",
"inner", 8675309);
1460 ast_test_validate(
test,
NULL != uut);
1474 info->name =
"deep_copy";
1476 info->summary =
"Testing deep copying of JSON.";
1477 info->description =
"Test JSON abstraction library.";
1484 expected =
ast_json_pack(
"{s: {s: i}}",
"outer",
"inner", 8675309);
1486 ast_test_validate(
test,
NULL != uut);
1500 info->name =
"copy_null";
1502 info->summary =
"Testing NULL handling of copy functions.";
1503 info->description =
"Test JSON abstraction library.";
1523 info->name =
"circular_object";
1525 info->summary =
"Object cannot be added to itself.";
1526 info->description =
"Test JSON abstraction library.";
1536 ast_test_validate(
test, -1 == uut_res);
1549 info->name =
"circular_array";
1551 info->summary =
"Array cannot be added to itself.";
1552 info->description =
"Test JSON abstraction library.";
1561 ast_test_validate(
test, -1 == uut_res);
1576 info->name =
"clever_circle";
1578 info->summary =
"JSON with circular references cannot be encoded.";
1579 info->description =
"Test JSON abstraction library.";
1589 ast_test_validate(
test, 0 == uut_res);
1591 ast_test_validate(
test, 0 == uut_res);
1608 "number", number ?:
"");
1622 info->name =
"name_number";
1624 info->summary =
"JSON encoding of name/number pair.";
1625 info->description =
"Test JSON abstraction library.";
1643 struct timeval tv = {};
1647 info->name =
"type_timeval";
1649 info->summary =
"JSON encoding of timevals.";
1650 info->description =
"Test JSON abstraction library.";
1658 tv.tv_sec = 1360251154;
1659 tv.tv_usec = 314159;
1676 info->summary =
"JSON with circular references cannot be encoded.";
1677 info->description =
"Test JSON abstraction library.";
int ast_json_array_extend(struct ast_json *array, struct ast_json *tail)
Append all elements from tail to array.
#define ast_json_dump_new_file(root, path)
Contains all the initialization information required to store a new test definition.
static int json_test_init(struct ast_test_info *info, struct ast_test *test)
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Asterisk main include file. File version handling, generic pbx functions.
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
struct ast_json * ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
Parse buffer with known length into a JSON object or array.
size_t ast_json_object_size(struct ast_json *object)
Get size of JSON object.
int ast_json_array_set(struct ast_json *array, size_t index, struct ast_json *value)
Change an element in an array.
int ast_test_register_init(const char *category, ast_test_init_cb_t *cb)
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
int ast_json_object_clear(struct ast_json *object)
Delete all elements from a JSON object.
int ast_json_array_clear(struct ast_json *array)
Remove all elements from an array.
Iterator for JSON object key/values.
int ast_json_object_update(struct ast_json *object, struct ast_json *other)
Update object with all of the fields of other.
int ast_json_object_update_existing(struct ast_json *object, struct ast_json *other)
Update existing fields in object with the fields of other.
int ast_json_object_del(struct ast_json *object, const char *key)
Delete a field from a JSON object.
void ast_json_free(void *p)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
struct ast_json_iter * ast_json_object_iter(struct ast_json *object)
Get an iterator pointing to the first field in a JSON object.
int ast_json_is_true(const struct ast_json *value)
Check if value is JSON true.
#define ast_json_dump_string(root)
Encode a JSON value to a compact string.
static int json_test_cleanup(struct ast_test_info *info, struct ast_test *test)
#define AST_TEST_REGISTER(cb)
struct ast_json * ast_json_stringf(const char *format,...)
Create a JSON string, printf style.
struct ast_json * ast_json_load_string(const char *input, struct ast_json_error *error)
Parse null terminated string into a JSON object or array.
struct ast_json * ast_json_load_str(const struct ast_str *input, struct ast_json_error *error)
Parse ast_str into a JSON object or array.
#define ast_str_alloca(init_len)
int ast_json_is_null(const struct ast_json *value)
Check if value is JSON null.
int ast_json_array_insert(struct ast_json *array, size_t index, struct ast_json *value)
Insert into an array.
#define ast_json_dump_file(root, output)
static int unload_module(void)
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
struct ast_json * ast_json_dialplan_cep_app(const char *context, const char *exten, int priority, const char *app_name, const char *app_data)
Construct a context/exten/priority/application/application_data as JSON.
struct ast_json * ast_json_null(void)
Get the JSON null value.
static int load_module(void)
struct ast_json * ast_json_name_number(const char *name, const char *number)
Common JSON rendering functions for common 'objects'.
Asterisk JSON abstraction layer.
struct ast_json * ast_json_object_iter_value(struct ast_json_iter *iter)
Get the value from an iterator.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
#define ast_test_status_update(a, b, c...)
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
int ast_json_object_iter_set(struct ast_json *object, struct ast_json_iter *iter, struct ast_json *value)
Set the value of the field pointed to by an iterator.
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
const char * ast_json_object_iter_key(struct ast_json_iter *iter)
Get the key from an iterator.
int ast_json_is_false(const struct ast_json *value)
Check if value is JSON false.
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
int ast_test_register_cleanup(const char *category, ast_test_cleanup_cb_t *cb)
struct ast_json * ast_json_timeval(const struct timeval tv, const char *zone)
Construct a timeval as JSON.
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
static size_t alloc_count
struct ast_json * ast_json_load_new_file(const char *path, struct ast_json_error *error)
Parse file at path into JSON object or array.
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
int ast_json_integer_set(struct ast_json *integer, intmax_t value)
Set the value of a JSON integer.
#define AST_TEST_UNREGISTER(cb)
int ast_json_array_remove(struct ast_json *array, size_t index)
Remove an element from an array.
void ast_json_set_alloc_funcs(void *(*malloc_fn)(size_t), void(*free_fn)(void *))
Set custom allocators instead of the standard ast_malloc() and ast_free().
struct ast_json * ast_json_load_file(FILE *input, struct ast_json_error *error)
Parse a FILE into JSON object or array.
static int test_name_number(const char *name, const char *number)
enum ast_json_type ast_json_typeof(const struct ast_json *value)
Get the type of value.
int ast_json_object_update_missing(struct ast_json *object, struct ast_json *other)
Add new fields to object with the fields of other.
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
AST_TEST_DEFINE(json_test_false)
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
struct ast_json * ast_json_false(void)
Get the JSON false value.
static FILE * mkstemp_file(char *template, const char *mode)
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
Compare two JSON objects.
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
Abstract JSON element (object, array, string, int, ...).
struct ast_json * ast_json_boolean(int value)
Get the JSON boolean corresponding to value.
static void * json_debug_malloc(size_t size)
void * ast_json_malloc(size_t size)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
static void json_debug_free(void *p)
struct ast_json_iter * ast_json_object_iter_at(struct ast_json *object, const char *key)
Get an iterator pointing to a specified key in object.
int ast_json_string_set(struct ast_json *string, const char *value)
Change the value of a JSON string.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
intmax_t ast_json_integer_get(const struct ast_json *integer)
Get the value from a JSON integer.
struct ast_json * ast_json_array_get(const struct ast_json *array, size_t index)
Get an element from an array.
static int safe_fclose(FILE *f)
fclose isn't NULL safe.
struct ast_json * ast_json_copy(const struct ast_json *value)
Copy a JSON value, but not its children.
void ast_json_reset_alloc_funcs(void)
Change alloc funcs back to the resource module defaults.
#define ast_json_dump_str(root, dst)
struct ast_json * ast_json_true(void)
Get the JSON true value.
struct ast_json * ast_json_deep_copy(const struct ast_json *value)
Copy a JSON value, and its children.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
struct ast_json_iter * ast_json_object_iter_next(struct ast_json *object, struct ast_json_iter *iter)
Get the next iterator.
struct ast_json * ast_json_integer_create(intmax_t value)
Create a JSON integer.