[libvirt] [PATCH v2 4/4] tests: virjson: Test string escaping

Peter Krempa posted 4 patches 7 years, 10 months ago
[libvirt] [PATCH v2 4/4] tests: virjson: Test string escaping
Posted by Peter Krempa 7 years, 10 months ago
Make sure that JSON strings can contain characters which need to be
escaped (double quotes, backslashes, tabs, etc.) and that JSON objects
formatted into strings can be nested into strings.
---
 tests/virjsontest.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/tests/virjsontest.c b/tests/virjsontest.c
index a6e158179..30457d118 100644
--- a/tests/virjsontest.c
+++ b/tests/virjsontest.c
@@ -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;
 }


@@ -490,6 +550,10 @@ mymain(void)
     DO_TEST_PARSE("integer", "1", NULL);
     DO_TEST_PARSE("boolean", "true", NULL);
     DO_TEST_PARSE("null", "null", NULL);
+
+    DO_TEST_PARSE("escaping symbols", "[\"\\\"\\t\\n\\\\\"]", NULL);
+    DO_TEST_PARSE("escaped strings", "[\"{\\\"blurb\\\":\\\"test\\\"}\"]", NULL);
+
     DO_TEST_PARSE_FAIL("incomplete keyword", "tr");
     DO_TEST_PARSE_FAIL("overdone keyword", "[ truest ]");
     DO_TEST_PARSE_FAIL("unknown keyword", "huh");
@@ -522,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)
-- 
2.12.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 4/4] tests: virjson: Test string escaping
Posted by John Ferlan 7 years, 10 months ago

On 07/11/2017 08:56 AM, Peter Krempa wrote:
> Make sure that JSON strings can contain characters which need to be
> escaped (double quotes, backslashes, tabs, etc.) and that JSON objects
> formatted into strings can be nested into strings.
> ---
>  tests/virjsontest.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 66 insertions(+)
> 

Seems there should be two separate patches here, one that ensures
escaped strings work for DO_TEST_PARSE and the other passing/using a
nested string obj. Is "EscapeObj" even a right name, seems like
"NestedObj" would be more appropriate. Is there some sort of escaping in
the strings that I'm missing?

Also, w/r/t escaping do we need handle double commas ",," that are
supposed to turn into "," ?

John

> diff --git a/tests/virjsontest.c b/tests/virjsontest.c
> index a6e158179..30457d118 100644
> --- a/tests/virjsontest.c
> +++ b/tests/virjsontest.c
> @@ -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;
>  }
> 
> 
> @@ -490,6 +550,10 @@ mymain(void)
>      DO_TEST_PARSE("integer", "1", NULL);
>      DO_TEST_PARSE("boolean", "true", NULL);
>      DO_TEST_PARSE("null", "null", NULL);
> +
> +    DO_TEST_PARSE("escaping symbols", "[\"\\\"\\t\\n\\\\\"]", NULL);
> +    DO_TEST_PARSE("escaped strings", "[\"{\\\"blurb\\\":\\\"test\\\"}\"]", NULL);
> +
>      DO_TEST_PARSE_FAIL("incomplete keyword", "tr");
>      DO_TEST_PARSE_FAIL("overdone keyword", "[ truest ]");
>      DO_TEST_PARSE_FAIL("unknown keyword", "huh");
> @@ -522,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)
> 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 4/4] tests: virjson: Test string escaping
Posted by Peter Krempa 7 years, 10 months ago
On Wed, Jul 19, 2017 at 09:08:16 -0400, John Ferlan wrote:
> 
> 
> On 07/11/2017 08:56 AM, Peter Krempa wrote:
> > Make sure that JSON strings can contain characters which need to be
> > escaped (double quotes, backslashes, tabs, etc.) and that JSON objects
> > formatted into strings can be nested into strings.
> > ---
> >  tests/virjsontest.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 66 insertions(+)
> > 
> 
> Seems there should be two separate patches here, one that ensures
> escaped strings work for DO_TEST_PARSE and the other passing/using a

Okay I'll split it and resend.

> nested string obj. Is "EscapeObj" even a right name, seems like
> "NestedObj" would be more appropriate. Is there some sort of escaping in
> the strings that I'm missing?

The naming issue is somewhere between those two, since you can nest JSON
naturally, but nesting a json string in a json value is what we are
tesing here.

> 
> Also, w/r/t escaping do we need handle double commas ",," that are
> supposed to turn into "," ?

Commas? Aren't you confusing this with escaping for qemu? JSON escaping
rules don't have anything special for commas since the escaping needs
to be done only if you want to store a JSON string into another JSON
string. Commas are not interpreted inside of strings.
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list