]> xenbits.xensource.com Git - libvirt.git/commitdiff
tests: json: Validate that attribute values are properly stolen
authorPeter Krempa <pkrempa@redhat.com>
Fri, 30 Mar 2018 10:18:50 +0000 (12:18 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 3 Apr 2018 11:34:33 +0000 (13:34 +0200)
Make sure that the 'a' and 'A' modifiers for virJSONValueObjectAddVArgs
behave correctly.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
tests/virjsontest.c

index 685df7276eafcd2caea4e82ae4ce4d903cec3091..fe72b8434095b90fdccb54554ced93eef01ad36b 100644 (file)
@@ -429,6 +429,51 @@ testJSONEscapeObj(const void *data ATTRIBUTE_UNUSED)
 }
 
 
+static int
+testJSONObjectFormatSteal(const void *opaque ATTRIBUTE_UNUSED)
+{
+    virJSONValuePtr a1 = NULL;
+    virJSONValuePtr a2 = NULL;
+    virJSONValuePtr t1 = NULL;
+    virJSONValuePtr t2 = NULL;
+    int ret = -1;
+
+    if (!(a1 = virJSONValueNewString("test")) ||
+        !(a2 = virJSONValueNewString("test"))) {
+        VIR_TEST_VERBOSE("Failed to create json object");
+    }
+
+    if (virJSONValueObjectCreate(&t1, "a:t", &a1, "s:f", NULL, NULL) != -1) {
+        VIR_TEST_VERBOSE("virJSONValueObjectCreate(t1) should have failed\n");
+        goto cleanup;
+    }
+
+    if (a1) {
+        VIR_TEST_VERBOSE("appended object a1 was not consumed\n");
+        goto cleanup;
+    }
+
+    if (virJSONValueObjectCreate(&t2, "s:f", NULL, "a:t", &a1, NULL) != -1) {
+        VIR_TEST_VERBOSE("virJSONValueObjectCreate(t2) should have failed\n");
+        goto cleanup;
+    }
+
+    if (!a2) {
+        VIR_TEST_VERBOSE("appended object a2 was consumed\n");
+        goto cleanup;
+    }
+
+    ret = 0;
+
+ cleanup:
+    virJSONValueFree(a1);
+    virJSONValueFree(a2);
+    virJSONValueFree(t1);
+    virJSONValueFree(t2);
+    return ret;
+}
+
+
 static int
 mymain(void)
 {
@@ -588,6 +633,8 @@ mymain(void)
                  NULL, true);
     DO_TEST_FULL("create object with nested json in attribute", EscapeObj,
                  NULL, NULL, true);
+    DO_TEST_FULL("stealing of attributes while creating objects",
+                 ObjectFormatSteal, NULL, NULL, true);
 
 #define DO_TEST_DEFLATTEN(name, pass) \
     DO_TEST_FULL(name, Deflatten, name, NULL, pass)