summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid M. Lee <dlee@digium.com>2013-02-11 20:39:11 +0000
committerDavid M. Lee <dlee@digium.com>2013-02-11 20:39:11 +0000
commitf2b9c12704136fdc5d4e71c63d104e6debd77320 (patch)
treeedba96f734f7893b12fec8ea7295b0d40effbf31
parent98ef0d321529a7c7420e8c8c755b6b8a5bc55c09 (diff)
Minor fixes to res_json and test_json.
* Made input checking more consistent with other Asterisk code * Added validation to ast_json_dump_new_file * Fixed tests for ownereship semantics (issue ASTERISK-20887) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@381214 65c4cc65-6c06-0410-ace0-fbb531ad65f3
-rw-r--r--res/res_json.c15
-rw-r--r--tests/test_json.c37
2 files changed, 39 insertions, 13 deletions
diff --git a/res/res_json.c b/res/res_json.c
index 218c09be2..f1ebaef60 100644
--- a/res/res_json.c
+++ b/res/res_json.c
@@ -242,10 +242,10 @@ size_t ast_json_object_size(struct ast_json *object)
}
struct ast_json *ast_json_object_get(struct ast_json *object, const char *key)
{
- if (key) {
- return (struct ast_json *)json_object_get((json_t *)object, key);
+ if (!key) {
+ return NULL;
}
- return NULL;
+ return (struct ast_json *)json_object_get((json_t *)object, key);
}
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
{
@@ -391,13 +391,16 @@ int ast_json_dump_str(struct ast_json *root, struct ast_str **dst)
int ast_json_dump_file(struct ast_json *root, FILE *output)
{
- if (root && output) {
- return json_dumpf((json_t *)root, output, dump_flags());
+ if (!root || !output) {
+ return -1;
}
- return -1;
+ return json_dumpf((json_t *)root, output, dump_flags());
}
int ast_json_dump_new_file(struct ast_json *root, const char *path)
{
+ if (!root || !path) {
+ return -1;
+ }
return json_dump_file((json_t *)root, path, dump_flags());
}
diff --git a/tests/test_json.c b/tests/test_json.c
index f67e35284..c4e5cc6fb 100644
--- a/tests/test_json.c
+++ b/tests/test_json.c
@@ -395,8 +395,6 @@ AST_TEST_DEFINE(json_test_int)
ast_test_validate(test, 0 == uut_res);
ast_test_validate(test, LLONG_MIN == ast_json_integer_get(uut));
- ast_json_unref(uut);
-
return AST_TEST_PASS;
}
@@ -461,8 +459,6 @@ AST_TEST_DEFINE(json_test_array_create)
ast_test_validate(test, AST_JSON_ARRAY == ast_json_typeof(uut));
ast_test_validate(test, 0 == ast_json_array_size(uut));
- ast_json_unref(uut);
-
return AST_TEST_PASS;
}
@@ -1406,6 +1402,27 @@ AST_TEST_DEFINE(json_test_pack)
return AST_TEST_PASS;
}
+AST_TEST_DEFINE(json_test_pack_ownership)
+{
+ RAII_VAR(void *, alloc_debug, json_test_init(test), json_test_finish);
+ RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "pack_ownership";
+ info->category = "/main/json/";
+ info->summary = "Testing json_pack failure conditions.";
+ info->description = "Test JSON abstraction library.";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ uut = ast_json_pack("[o]", ast_json_string_create("Am I freed?"));
+
+ return AST_TEST_PASS;
+}
+
AST_TEST_DEFINE(json_test_pack_errors)
{
RAII_VAR(void *, alloc_debug, json_test_init(test), json_test_finish);
@@ -1528,7 +1545,7 @@ AST_TEST_DEFINE(json_test_circular_object)
/* circular reference testing */
/* Cannot add self */
uut = ast_json_object_create();
- uut_res = ast_json_object_set(uut, "myself", uut);
+ uut_res = ast_json_object_set(uut, "myself", ast_json_ref(uut));
ast_test_validate(test, -1 == uut_res);
ast_test_validate(test, 0 == ast_json_object_size(uut));
@@ -1553,7 +1570,8 @@ AST_TEST_DEFINE(json_test_circular_array)
}
uut = ast_json_array_create();
- uut_res = ast_json_object_set(uut, "myself", uut);
+ ast_test_validate(test, 0 == ast_json_array_size(uut));
+ uut_res = ast_json_array_append(uut, ast_json_ref(uut));
ast_test_validate(test, -1 == uut_res);
ast_test_validate(test, 0 == ast_json_array_size(uut));
@@ -1636,6 +1654,7 @@ static int unload_module(void)
AST_TEST_UNREGISTER(json_test_dump_load_null);
AST_TEST_UNREGISTER(json_test_parse_errors);
AST_TEST_UNREGISTER(json_test_pack);
+ AST_TEST_UNREGISTER(json_test_pack_ownership);
AST_TEST_UNREGISTER(json_test_pack_errors);
AST_TEST_UNREGISTER(json_test_copy);
AST_TEST_UNREGISTER(json_test_deep_copy);
@@ -1688,6 +1707,7 @@ static int load_module(void)
AST_TEST_REGISTER(json_test_dump_load_null);
AST_TEST_REGISTER(json_test_parse_errors);
AST_TEST_REGISTER(json_test_pack);
+ AST_TEST_REGISTER(json_test_pack_ownership);
AST_TEST_REGISTER(json_test_pack_errors);
AST_TEST_REGISTER(json_test_copy);
AST_TEST_REGISTER(json_test_deep_copy);
@@ -1698,4 +1718,7 @@ static int load_module(void)
return AST_MODULE_LOAD_SUCCESS;
}
-AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "JSON testing.");
+AST_MODULE_INFO(ASTERISK_GPL_KEY, 0, "JSON testing",
+ .load = load_module,
+ .unload = unload_module,
+ .nonoptreq = "res_json");