[libvirt] [PATCH 2/9] tests: json: Validate that attribute values are properly stolen

Peter Krempa pkrempa at redhat.com
Fri Mar 30 10:59:09 UTC 2018


Make sure that the 'a' and 'A' modifiers for virJSONValueObjectAddVArgs
behave correctly.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 tests/virjsontest.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/tests/virjsontest.c b/tests/virjsontest.c
index 685df7276e..fe72b84340 100644
--- a/tests/virjsontest.c
+++ b/tests/virjsontest.c
@@ -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)
-- 
2.16.2




More information about the libvir-list mailing list