]> xenbits.xensource.com Git - libvirt.git/commitdiff
tests: virjson: Test nesting of JSON string into a JSON (string) property
authorPeter Krempa <pkrempa@redhat.com>
Mon, 3 Jul 2017 14:39:21 +0000 (16:39 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 19 Jul 2017 15:59:09 +0000 (17:59 +0200)
Test that we are able to create a JSON object and nest it into a string
property of a JSON object and then correctly extract and parse it back.

tests/virjsontest.c

index d5a823431b6291b397951f79778043530e4e105b..30457d1187e70ac00c87ef3e13c9336bb1ca594f 100644 (file)
@@ -365,7 +365,67 @@ testJSONDeflatten(const void *data)
     VIR_FREE(actual);
 
     return ret;
+}
+
+
+static int
+testJSONEscapeObj(const void *data ATTRIBUTE_UNUSED)
+{
+    virJSONValuePtr json = NULL;
+    virJSONValuePtr nestjson = NULL;
+    virJSONValuePtr parsejson = NULL;
+    char *neststr = NULL;
+    char *result = NULL;
+    const char *parsednestedstr;
+    int ret = -1;
+
+    if (virJSONValueObjectCreate(&nestjson,
+                                 "s:stringkey", "stringvalue",
+                                 "i:numberkey", 1234,
+                                 "b:booleankey", false, NULL) < 0) {
+        VIR_TEST_VERBOSE("failed to create nested json object");
+        goto cleanup;
+    }
+
+    if (!(neststr = virJSONValueToString(nestjson, false))) {
+        VIR_TEST_VERBOSE("failed to format nested json object");
+        goto cleanup;
+    }
+
+    if (virJSONValueObjectCreate(&json, "s:test", neststr, NULL) < 0) {
+        VIR_TEST_VERBOSE("Failed to create json object");
+        goto cleanup;
+    }
+
+    if (!(result = virJSONValueToString(json, false))) {
+        VIR_TEST_VERBOSE("Failed to format json object");
+        goto cleanup;
+    }
+
+    if (!(parsejson = virJSONValueFromString(result))) {
+        VIR_TEST_VERBOSE("Failed to parse JSON with nested JSON in string");
+        goto cleanup;
+    }
+
+    if (!(parsednestedstr = virJSONValueObjectGetString(parsejson, "test"))) {
+        VIR_TEST_VERBOSE("Failed to retrieve string containing nested json");
+        goto cleanup;
+    }
 
+    if (STRNEQ(parsednestedstr, neststr)) {
+        virTestDifference(stderr, neststr, parsednestedstr);
+        goto cleanup;
+    }
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(neststr);
+    VIR_FREE(result);
+    virJSONValueFree(json);
+    virJSONValueFree(nestjson);
+    virJSONValueFree(parsejson);
+    return ret;
 }
 
 
@@ -526,6 +586,8 @@ mymain(void)
     DO_TEST_FULL("lookup with correct type", Lookup,
                  "{ \"a\": {}, \"b\": 1, \"c\": \"str\", \"d\": [] }",
                  NULL, true);
+    DO_TEST_FULL("create object with nested json in attribute", EscapeObj,
+                 NULL, NULL, true);
 
 #define DO_TEST_DEFLATTEN(name, pass) \
     DO_TEST_FULL(name, Deflatten, name, NULL, pass)