aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorallen <leloucharcher@163.com>2019-10-12 15:36:05 +0800
committerallen <leloucharcher@163.com>2019-10-12 15:36:05 +0800
commit00d2d274bc4260d9197d5003cfb4531f7eca494b (patch)
treeda59c8ff9048da225330ba5a316af619d89a00fe /test
parentcb4727c4a931a471f7d2f1594b162a71349b4dfe (diff)
downloadjansson-00d2d274bc4260d9197d5003cfb4531f7eca494b.zip
jansson-00d2d274bc4260d9197d5003cfb4531f7eca494b.tar.gz
jansson-00d2d274bc4260d9197d5003cfb4531f7eca494b.tar.bz2
add loop check for json_object_update_recursive function
Diffstat (limited to 'test')
-rw-r--r--test/suites/api/test_object.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/test/suites/api/test_object.c b/test/suites/api/test_object.c
index ac10936..0493e98 100644
--- a/test/suites/api/test_object.c
+++ b/test/suites/api/test_object.c
@@ -209,7 +209,7 @@ static void test_conditional_updates()
static void test_recursive_updates()
{
- json_t *invalid, *object, *other;
+ json_t *invalid, *object, *other, *barBefore, *barAfter;
invalid = json_integer(42);
@@ -241,6 +241,10 @@ static void test_recursive_updates()
object = json_pack("{sis{si}}", "foo", 1, "bar", "baz", 2);
other = json_pack("{s{si}}", "bar", "baz", 3);
+ barBefore = json_object_get(object, "bar");
+
+ if(!barBefore)
+ fail("can't get bar object before json_object_update_recursive");
if(json_object_update_recursive(object, other))
fail("json_object_update_recursive failed");
@@ -254,6 +258,33 @@ static void test_recursive_updates()
if(json_integer_value(json_object_get(json_object_get(object, "bar"), "baz")) != 3)
fail("json_object_update_recursive failed to update nested value");
+ barAfter = json_object_get(object, "bar");
+ if(!barAfter)
+ fail("can't get bar object after json_object_update_recursive");
+
+ if(barBefore != barAfter)
+ fail("bar object reference changed after json_object_update_recursive");
+
+ json_decref(object);
+ json_decref(other);
+
+ /* check circular reference */
+ object = json_pack("{s{s{si}}}", "foo", "bar", "baz", 2);
+ other = json_pack("{s{s{si}}}", "foo", "bar", "baz", 2);
+ json_object_set(json_object_get(json_object_get(object, "foo"), "bar"), "baz",
+ json_object_get(other, "foo"));
+ json_object_set(json_object_get(json_object_get(other, "foo"), "bar"), "baz",
+ json_object_get(other, "foo"));
+
+ if(!json_object_update_recursive(object, other))
+ fail("json_object_update_recursive update a circular reference!");
+
+ json_object_set_new(json_object_get(json_object_get(other, "foo"), "bar"), "baz",
+ json_integer(1));
+
+ if(json_object_update_recursive(object, other))
+ fail("json_object_update_recursive failed!");
+
json_decref(object);
json_decref(other);
}