aboutsummaryrefslogtreecommitdiff
path: root/test/testprogs/test_object.c
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2009-10-15 21:02:27 +0300
committerPetri Lehtinen <petri@digip.org>2009-10-15 21:02:27 +0300
commit4cd777712b1e4ec17dc9efcced80a90f83ec1915 (patch)
treecca20833e8b55956be210f8cd285c387266817a2 /test/testprogs/test_object.c
parent79009e62c10bab0869d61edfaa6c813f03f027bc (diff)
downloadjansson-4cd777712b1e4ec17dc9efcced80a90f83ec1915.zip
jansson-4cd777712b1e4ec17dc9efcced80a90f83ec1915.tar.gz
jansson-4cd777712b1e4ec17dc9efcced80a90f83ec1915.tar.bz2
Enhance handling of circular references
It's now an error to try to add an object or array to itself. The encoder checks for circular references and fails with an error status if one is detected.
Diffstat (limited to 'test/testprogs/test_object.c')
-rw-r--r--test/testprogs/test_object.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/test/testprogs/test_object.c b/test/testprogs/test_object.c
index 3be49e5..657a9c6 100644
--- a/test/testprogs/test_object.c
+++ b/test/testprogs/test_object.c
@@ -139,6 +139,34 @@ static void test_update()
json_decref(object);
}
+static void test_circular()
+{
+ json_t *object1, *object2;
+
+ object1 = json_object();
+ object2 = json_object();
+ if(!object1 || !object2)
+ fail("unable to create object");
+
+ /* the simple case is checked */
+ if(json_object_set(object1, "a", object1) == 0)
+ fail("able to set self");
+
+ /* create circular references */
+ if(json_object_set(object1, "a", object2) ||
+ json_object_set(object2, "a", object1))
+ fail("unable to set value");
+
+ /* circularity is detected when dumping */
+ if(json_dumps(object1, 0) != NULL)
+ fail("able to dump circulars");
+
+ /* decref twice to deal with the circular references */
+ json_decref(object1);
+ json_decref(object2);
+ json_decref(object1);
+}
+
static void test_misc()
{
json_t *object, *string, *other_string, *value;
@@ -264,6 +292,7 @@ int main()
test_misc();
test_clear();
test_update();
+ test_circular();
return 0;
}