49#define CATEGORY "/main/json/"
90 "JSON test leaked %zu allocations!\n",
alloc_count);
104 info->name =
"type_false";
106 info->summary =
"Testing fundamental JSON false value.";
107 info->description =
"Test JSON abstraction library.";
114 ast_test_validate(
test,
NULL != uut);
129 info->name =
"type_true";
131 info->summary =
"Testing JSON true value.";
132 info->description =
"Test JSON abstraction library.";
139 ast_test_validate(
test,
NULL != uut);
154 info->name =
"type_bool0";
156 info->summary =
"Testing JSON boolean function (false).";
157 info->description =
"Test JSON abstraction library.";
164 ast_test_validate(
test,
NULL != uut);
181 info->name =
"type_bool1";
183 info->summary =
"Testing JSON boolean function (true).";
184 info->description =
"Test JSON abstraction library.";
191 ast_test_validate(
test,
NULL != uut);
208 info->name =
"type_null";
210 info->summary =
"Testing JSON null value.";
211 info->description =
"Test JSON abstraction library.";
218 ast_test_validate(
test,
NULL != uut);
231 info->name =
"null_val";
233 info->summary =
"Testing JSON handling of NULL.";
234 info->description =
"Test JSON abstraction library.";
260 info->name =
"type_string";
262 info->summary =
"Basic string tests.";
263 info->description =
"Test JSON abstraction library.";
270 ast_test_validate(
test,
NULL != uut);
275 ast_test_validate(
test, -1 == uut_res);
279 ast_test_validate(
test, -1 == uut_res);
283 ast_test_validate(
test, 0 == uut_res);
287 ast_test_validate(
test, 0 == uut_res);
299 info->name =
"string_null";
301 info->summary =
"JSON string NULL tests.";
302 info->description =
"Test JSON abstraction library.";
310 ast_test_validate(
test,
NULL == uut);
332 info->name =
"stringf";
334 info->summary =
"Basic string formatting tests.";
335 info->description =
"Test JSON abstraction library.";
343 ast_test_validate(
test,
NULL == uut);
347 ast_test_validate(
test,
NULL == uut);
352 ast_test_validate(
test,
NULL != uut);
365 info->name =
"type_int";
367 info->summary =
"Basic JSON integer tests.";
368 info->description =
"Test JSON abstraction library.";
376 ast_test_validate(
test,
NULL != uut);
381 ast_test_validate(
test, 0 == uut_res);
385 ast_test_validate(
test, 0 == uut_res);
389 ast_test_validate(
test, 0 == uut_res);
393 ast_test_validate(
test, 0 == uut_res);
405 info->name =
"non_int";
407 info->summary =
"Testing integer functions with non-integer types.";
408 info->description =
"Test JSON abstraction library.";
426 ast_test_validate(
test,
NULL != uut);
443 info->name =
"array_create";
445 info->summary =
"Testing creating JSON arrays.";
446 info->description =
"Test JSON abstraction library.";
454 ast_test_validate(
test,
NULL != uut);
468 info->name =
"array_append";
470 info->summary =
"Testing appending to JSON arrays.";
471 info->description =
"Test JSON abstraction library.";
480 ast_test_validate(
test, 0 == uut_res);
497 info->name =
"array_insert";
499 info->summary =
"Testing inserting into JSON arrays.";
500 info->description =
"Test JSON abstraction library.";
509 ast_test_validate(
test, 0 == uut_res);
524 info->name =
"array_set";
526 info->summary =
"Testing setting a value in JSON arrays.";
527 info->description =
"Test JSON abstraction library.";
536 ast_test_validate(
test, 0 == uut_res);
552 info->name =
"array_remove";
554 info->summary =
"Testing removing a value from JSON arrays.";
555 info->description =
"Test JSON abstraction library.";
565 ast_test_validate(
test, 0 == uut_res);
578 info->name =
"array_clear";
580 info->summary =
"Testing clearing JSON arrays.";
581 info->description =
"Test JSON abstraction library.";
590 ast_test_validate(
test, 0 == uut_res);
605 info->name =
"array_extend";
607 info->summary =
"Testing extending JSON arrays.";
608 info->description =
"Test JSON abstraction library.";
634 ast_test_validate(
test, 0 == uut_res);
648 info->name =
"array_null";
650 info->summary =
"Testing NULL conditions for JSON arrays.";
651 info->description =
"Test JSON abstraction library.";
679 info->name =
"object_alloc";
681 info->summary =
"Testing creating JSON objects.";
682 info->description =
"Test JSON abstraction library.";
690 ast_test_validate(
test,
NULL != uut);
705 info->name =
"object_set";
707 info->summary =
"Testing setting values in JSON objects.";
708 info->description =
"Test JSON abstraction library.";
715 expected =
ast_json_pack(
"{s: i, s: i, s: i}",
"one", 1,
"two", 2,
"three", 3);
718 ast_test_validate(
test, 0 == uut_res);
720 ast_test_validate(
test, 0 == uut_res);
722 ast_test_validate(
test, 0 == uut_res);
736 info->name =
"object_set_overwriting";
738 info->summary =
"Testing changing values in JSON objects.";
739 info->description =
"Test JSON abstraction library.";
746 uut =
ast_json_pack(
"{s: i, s: i, s: i}",
"one", 1,
"two", 2,
"three", 3);
748 ast_test_validate(
test, 0 == uut_res);
760 info->name =
"object_get";
762 info->summary =
"Testing getting values from JSON objects.";
763 info->description =
"Test JSON abstraction library.";
770 uut =
ast_json_pack(
"{s: i, s: i, s: i}",
"one", 1,
"two", 2,
"three", 3);
786 info->name =
"object_del";
788 info->summary =
"Testing deleting values from JSON objects.";
789 info->description =
"Test JSON abstraction library.";
799 ast_test_validate(
test, 0 == uut_res);
802 ast_test_validate(
test, -1 == uut_res);
814 info->name =
"object_clear";
816 info->summary =
"Testing clearing values from JSON objects.";
817 info->description =
"Test JSON abstraction library.";
829 ast_test_validate(
test, 0 == uut_res);
844 info->name =
"object_alloc";
846 info->summary =
"Testing merging JSON objects.";
847 info->description =
"Test JSON abstraction library.";
872 ast_test_validate(
test, 0 == uut_res);
889 info->name =
"object_alloc";
891 info->summary =
"Testing merging JSON objects, updating only existing fields.";
892 info->description =
"Test JSON abstraction library.";
915 ast_test_validate(
test, 0 == uut_res);
932 info->name =
"object_merge_missing";
934 info->summary =
"Testing merging JSON objects, adding only missing fields.";
935 info->description =
"Test JSON abstraction library.";
960 ast_test_validate(
test, 0 == uut_res);
974 info->name =
"object_null";
976 info->summary =
"Testing JSON object NULL behavior.";
977 info->description =
"Test JSON abstraction library.";
1012 info->name =
"object_iter";
1014 info->summary =
"Testing iterating through JSON objects.";
1015 info->description =
"Test JSON abstraction library.";
1022 uut =
ast_json_pack(
"{s: i, s: i, s: i, s: i, s: i}",
"one", 1,
"two", 2,
"three", 3,
"four", 4,
"five", 5);
1026 ast_test_validate(
test,
NULL != iter);
1028 while (
NULL != iter) {
1041 ast_test_validate(
test, 0);
1046 ast_test_validate(
test, 5 == count);
1050 ast_test_validate(
test,
NULL == iter);
1054 ast_test_validate(
test,
NULL != iter);
1060 ast_test_validate(
test, 0 == uut_res);
1072 info->name =
"object_iter_null";
1074 info->summary =
"Testing JSON object iterator NULL testings.";
1075 info->description =
"Test JSON abstraction library.";
1104 info->name =
"object_create_vars";
1106 info->summary =
"Testing JSON object creation initialized using Asterisk variables.";
1107 info->description =
"Test JSON abstraction library.";
1126 ast_test_validate(
test, !strcmp(
"bar",
value));
1128 ast_test_validate(
test, !strcmp(
"baz",
value));
1135 ast_test_validate(
test, !strcmp(
"baz",
value));
1141 ast_test_validate(
test, !strcmp(
"baz",
value));
1147 ast_test_validate(
test, !strcmp(
"baz",
value));
1160 info->name =
"dump_load_string";
1162 info->summary =
"Testing dumping strings from JSON.";
1163 info->description =
"Test JSON abstraction library.";
1172 ast_test_validate(
test,
NULL != uut);
1190 info->name =
"dump_load_str";
1192 info->summary =
"Testing dumping ast_str from JSON.";
1193 info->description =
"Test JSON abstraction library.";
1203 ast_test_validate(
test, 0 == uut_res);
1205 ast_test_validate(
test,
NULL != uut);
1220 info->name =
"dump_str_fail";
1222 info->summary =
"Testing dumping to ast_str when it can't grow.";
1223 info->description =
"Test JSON abstraction library.";
1233 ast_test_validate(
test, 0 != uut_res);
1245 info->name =
"load_buffer";
1247 info->summary =
"Testing loading JSON from buffer.";
1248 info->description =
"Test JSON abstraction library.";
1255 str =
"{ \"one\": 1 } trailing garbage";
1257 ast_test_validate(
test,
NULL == uut);
1259 ast_test_validate(
test,
NULL != uut);
1277 char filename[] =
"/tmp/ast_json.XXXXXX";
1278 RAII_VAR(
char *, rm_on_exit, filename, unlink);
1284 info->name =
"dump_load_file";
1286 info->summary =
"Testing dumping/loading JSON to/from file by FILE *.";
1287 info->description =
"Test JSON abstraction library.";
1298 ast_test_validate(
test, 0 == uut_res);
1300 file = fopen(filename,
"r");
1312 char filename[] =
"/tmp/ast_json.XXXXXX";
1313 RAII_VAR(
char *, rm_on_exit, filename, unlink);
1319 info->name =
"dump_load_new_file";
1321 info->summary =
"Testing dumping/load JSON to/from file by filename.";
1322 info->description =
"Test JSON abstraction library.";
1333 ast_test_validate(
test, 0 == uut_res);
1343 char filename[] =
"/tmp/ast_json.XXXXXX";
1344 RAII_VAR(
char *, rm_on_exit, filename, unlink);
1349 info->name =
"dump_load_null";
1351 info->summary =
"Testing NULL handling of dump/load functions.";
1352 info->description =
"Test JSON abstraction library.";
1360 ast_test_validate(
test,
NULL != uut);
1384 info->name =
"parse_errors";
1386 info->summary =
"Testing various parse errors.";
1387 info->description =
"Test JSON abstraction library.";
1413 info->name =
"pack";
1415 info->summary =
"Testing json_pack function.";
1416 info->description =
"Test JSON abstraction library.";
1430 ast_test_validate(
test,
NULL != uut);
1442 info->name =
"pack_ownership";
1444 info->summary =
"Testing json_pack failure conditions.";
1445 info->description =
"Test JSON abstraction library.";
1462 info->name =
"object_alloc";
1464 info->summary =
"Testing json_pack failure conditions.";
1465 info->description =
"Test JSON abstraction library.";
1486 info->name =
"copy";
1488 info->summary =
"Testing copying JSON.";
1489 info->description =
"Test JSON abstraction library.";
1496 expected =
ast_json_pack(
"{s: {s: i}}",
"outer",
"inner", 8675309);
1498 ast_test_validate(
test,
NULL != uut);
1512 info->name =
"deep_copy";
1514 info->summary =
"Testing deep copying of JSON.";
1515 info->description =
"Test JSON abstraction library.";
1522 expected =
ast_json_pack(
"{s: {s: i}}",
"outer",
"inner", 8675309);
1524 ast_test_validate(
test,
NULL != uut);
1538 info->name =
"copy_null";
1540 info->summary =
"Testing NULL handling of copy functions.";
1541 info->description =
"Test JSON abstraction library.";
1561 info->name =
"circular_object";
1563 info->summary =
"Object cannot be added to itself.";
1564 info->description =
"Test JSON abstraction library.";
1574 ast_test_validate(
test, -1 == uut_res);
1587 info->name =
"circular_array";
1589 info->summary =
"Array cannot be added to itself.";
1590 info->description =
"Test JSON abstraction library.";
1599 ast_test_validate(
test, -1 == uut_res);
1614 info->name =
"clever_circle";
1616 info->summary =
"JSON with circular references cannot be encoded.";
1617 info->description =
"Test JSON abstraction library.";
1627 ast_test_validate(
test, 0 == uut_res);
1629 ast_test_validate(
test, 0 == uut_res);
1660 info->name =
"name_number";
1662 info->summary =
"JSON encoding of name/number pair.";
1663 info->description =
"Test JSON abstraction library.";
1681 struct timeval tv = {};
1685 info->name =
"type_timeval";
1687 info->summary =
"JSON encoding of timevals.";
1688 info->description =
"Test JSON abstraction library.";
1696 tv.tv_sec = 1360251154;
1697 tv.tv_usec = 314159;
1714 info->summary =
"JSON with circular references cannot be encoded.";
1715 info->description =
"Test JSON abstraction library.";
Asterisk main include file. File version handling, generic pbx functions.
Generic File Format Support. Should be included by clients of the file handling routines....
FILE * ast_file_mkftemp(char *template_name, mode_t mode)
same as mkstemp, but return a FILE
Configuration File Parser.
#define ast_variable_new(name, value, filename)
#define ast_variable_list_append(head, new_var)
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Asterisk JSON abstraction layer.
int ast_json_array_remove(struct ast_json *array, size_t index)
Remove an element from an array.
int ast_json_object_clear(struct ast_json *object)
Delete all elements from a JSON object.
#define ast_json_object_string_get(object, key)
Get a string field from a JSON object.
struct ast_json * ast_json_false(void)
Get the JSON false value.
struct ast_json * ast_json_object_iter_value(struct ast_json_iter *iter)
Get the value from an iterator.
struct ast_json * ast_json_deep_copy(const struct ast_json *value)
Copy a JSON value, and its children.
int ast_json_array_clear(struct ast_json *array)
Remove all elements from an array.
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from 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.
enum ast_json_type ast_json_typeof(const struct ast_json *value)
Get the type of value.
struct ast_json * ast_json_null(void)
Get the JSON null value.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
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_next(struct ast_json *object, struct ast_json_iter *iter)
Get the next iterator.
int ast_json_array_set(struct ast_json *array, size_t index, struct ast_json *value)
Change an element in an array.
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
#define ast_json_dump_str(root, dst)
int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
Compare two JSON objects.
int ast_json_is_false(const struct ast_json *value)
Check if value is JSON false.
size_t ast_json_object_size(struct ast_json *object)
Get size of JSON object.
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
struct ast_json * ast_json_array_get(const struct ast_json *array, size_t index)
Get an element from an array.
struct ast_json * ast_json_name_number(const char *name, const char *number)
Common JSON rendering functions for common 'objects'.
#define ast_json_dump_string(root)
Encode a JSON value to a compact string.
int ast_json_integer_set(struct ast_json *integer, intmax_t value)
Set the value of a JSON integer.
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
int ast_json_array_insert(struct ast_json *array, size_t index, struct ast_json *value)
Insert into an array.
struct ast_json * ast_json_timeval(const struct timeval tv, const char *zone)
Construct a timeval as JSON.
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.
struct ast_json * ast_json_boolean(int value)
Get the JSON boolean corresponding to value.
struct ast_json * ast_json_integer_create(intmax_t value)
Create a JSON integer.
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_array_create(void)
Create a empty JSON array.
#define ast_json_dump_file(root, output)
void * ast_json_malloc(size_t size)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
void ast_json_reset_alloc_funcs(void)
Change alloc funcs back to the resource module defaults.
struct ast_json * ast_json_object_create_vars(const struct ast_variable *variables, const char *excludes)
Create a new JSON object using the given variables.
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_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.
struct ast_json * ast_json_true(void)
Get the JSON true value.
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_object_del(struct ast_json *object, const char *key)
Delete a field from a JSON object.
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_array_extend(struct ast_json *array, struct ast_json *tail)
Append all elements from tail to array.
struct ast_json * ast_json_load_file(FILE *input, struct ast_json_error *error)
Parse a FILE into JSON object or array.
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_ref(struct ast_json *value)
Increase refcount on value.
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_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
Parse buffer with known length into a JSON object or array.
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.
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
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().
intmax_t ast_json_integer_get(const struct ast_json *integer)
Get the value from a JSON integer.
struct ast_json * ast_json_copy(const struct ast_json *value)
Copy a JSON value, but not its children.
int ast_json_is_true(const struct ast_json *value)
Check if value is JSON true.
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.
#define ast_json_dump_new_file(root, path)
int ast_json_object_update(struct ast_json *object, struct ast_json *other)
Update object with all of the fields of other.
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
int ast_json_is_null(const struct ast_json *value)
Check if value is JSON null.
int ast_json_string_set(struct ast_json *string, const char *value)
Change the value of a JSON string.
Asterisk module definitions.
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
@ AST_MODULE_SUPPORT_CORE
#define ASTERISK_GPL_KEY
The text the key() function should return.
@ AST_MODULE_LOAD_SUCCESS
#define ast_str_alloca(init_len)
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Iterator for JSON object key/values.
Abstract JSON element (object, array, string, int, ...).
Support for dynamic strings.
Contains all the initialization information required to store a new test definition.
Structure for variables, used for configurations and for channel variables.
#define AST_TEST_REGISTER(cb)
#define ast_test_status_update(a, b, c...)
#define AST_TEST_UNREGISTER(cb)
static size_t alloc_count
AST_TEST_DEFINE(json_test_false)
static int json_test_init(struct ast_test_info *info, struct ast_test *test)
static int json_test_cleanup(struct ast_test_info *info, struct ast_test *test)
static void * json_debug_malloc(size_t size)
static int safe_fclose(FILE *f)
fclose isn't NULL safe.
static int test_name_number(const char *name, const char *number)
static int load_module(void)
static int unload_module(void)
static void json_debug_free(void *p)
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.