]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
json: enhance parser test
authorEric Blake <eblake@redhat.com>
Mon, 22 Jun 2015 21:14:04 +0000 (15:14 -0600)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 25 Jun 2015 07:11:15 +0000 (09:11 +0200)
We already enable the parser option to detect invalid UTF-8, but
didn't test it.  Also, JSON states that behavior of an object
with a duplicated key is undefined; we chose to reject it, but
were not testing it.

With the enhanced tests in place, we can simplify yajl2
initialization by relying on parser defaults being sane.

* src/util/virjson.c (virJSONValueFromString): Simplify.
* tests/jsontest.c (mymain): Test more bad usage.

Signed-off-by: Eric Blake <eblake@redhat.com>
src/util/virjson.c
tests/jsontest.c

index 3c6ed3448a876e9798ec70688664c197b4d8824c..c123943e3317f60ed0112b556a34d55cd2f91ace 100644 (file)
@@ -1597,7 +1597,7 @@ virJSONValueFromString(const char *jsonstring)
     int rc;
     size_t len = strlen(jsonstring);
 # ifndef WITH_YAJL2
-    yajl_parser_config cfg = { 0, 1 };
+    yajl_parser_config cfg = { 0, 1 }; /* Match yajl 2 default behavior */
     virJSONValuePtr tmp;
 # endif
 
@@ -1605,10 +1605,6 @@ virJSONValueFromString(const char *jsonstring)
 
 # ifdef WITH_YAJL2
     hand = yajl_alloc(&parserCallbacks, NULL, &parser);
-    if (hand) {
-        yajl_config(hand, yajl_allow_comments, 0);
-        yajl_config(hand, yajl_dont_validate_strings, 0);
-    }
 # else
     hand = yajl_alloc(&parserCallbacks, &cfg, NULL, &parser);
 # endif
index 97b9c0a4a8e1810e6170ea2c93dc82552fc3f143..223f867195f1e4c4e6d071ff364ea607c2ecff9d 100644 (file)
@@ -422,6 +422,7 @@ mymain(void)
     DO_TEST_PARSE_FAIL("trailing garbage", "[] []");
     DO_TEST_PARSE_FAIL("list without array", "1, 1");
     DO_TEST_PARSE_FAIL("parser abuse", "1] [2");
+    DO_TEST_PARSE_FAIL("invalid UTF-8", "\"\x80\"");
 
     DO_TEST_PARSE_FAIL("object with numeric keys", "{ 1:1, 2:1, 3:2 }");
     DO_TEST_PARSE_FAIL("unterminated object", "{ \"1\":1, \"2\":1, \"3\":2");
@@ -430,6 +431,7 @@ mymain(void)
     DO_TEST_PARSE_FAIL("array of an object with an array as a key",
                        "[ {[\"key1\", \"key2\"]: \"value\"} ]");
     DO_TEST_PARSE_FAIL("object with unterminated key", "{ \"key:7 }");
+    DO_TEST_PARSE_FAIL("duplicate key", "{ \"a\": 1, \"a\": 1 }");
 
     DO_TEST_FULL("lookup on array", Lookup,
                  "[ 1 ]", NULL, false);