[asterisk-commits] dlee: trunk r381214 - in /trunk: res/res_json.c tests/test_json.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Feb 11 14:39:14 CST 2013


Author: dlee
Date: Mon Feb 11 14:39:11 2013
New Revision: 381214

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=381214
Log:
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)

Modified:
    trunk/res/res_json.c
    trunk/tests/test_json.c

Modified: trunk/res/res_json.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_json.c?view=diff&rev=381214&r1=381213&r2=381214
==============================================================================
--- trunk/res/res_json.c (original)
+++ trunk/res/res_json.c Mon Feb 11 14:39:11 2013
@@ -242,10 +242,10 @@
 }
 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);
-	}
-	return NULL;
+	if (!key) {
+		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_file(struct ast_json *root, FILE *output)
 {
-	if (root && output) {
-		return json_dumpf((json_t *)root, output, dump_flags());
-	}
-	return -1;
+	if (!root || !output) {
+		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());
 }
 

Modified: trunk/tests/test_json.c
URL: http://svnview.digium.com/svn/asterisk/trunk/tests/test_json.c?view=diff&rev=381214&r1=381213&r2=381214
==============================================================================
--- trunk/tests/test_json.c (original)
+++ trunk/tests/test_json.c Mon Feb 11 14:39:11 2013
@@ -395,8 +395,6 @@
 	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;
 }
 
@@ -460,8 +458,6 @@
 	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));
-
-	ast_json_unref(uut);
 
 	return AST_TEST_PASS;
 }
@@ -1406,6 +1402,27 @@
 	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 @@
 	/* 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 @@
 	}
 
 	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 @@
 	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 @@
 	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 @@
 	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");




More information about the asterisk-commits mailing list