[libvirt] [PATCH 1/2] Test if JSON parser fails on invalid input

Martin Kletzander mkletzan at redhat.com
Fri Nov 15 07:47:34 UTC 2013


On Tue, Nov 05, 2013 at 03:59:55PM +0100, Ján Tomko wrote:
> ---
>  tests/jsontest.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/tests/jsontest.c b/tests/jsontest.c
> index 6add816..808a2ea 100644
> --- a/tests/jsontest.c
> +++ b/tests/jsontest.c
> @@ -145,6 +145,10 @@ mymain(void)
>  #define DO_TEST_PARSE(name, doc)                \
>      DO_TEST_FULL(name, FromString, doc, NULL, true)
>  
> +#define DO_TEST_PARSE_FAIL(name, doc)           \
> +    DO_TEST_FULL(name, FromString, doc, NULL, false)
> +
> +

You're testing virJSONValueFromString() here ...

>      DO_TEST_PARSE("Simple", "{\"return\": {}, \"id\": \"libvirt-1\"}");
>      DO_TEST_PARSE("NotSoSimple", "{\"QMP\": {\"version\": {\"qemu\":"
>              "{\"micro\": 91, \"minor\": 13, \"major\": 0},"
> @@ -188,6 +192,15 @@ mymain(void)
>      DO_TEST_FULL("add and remove", AddRemove,
>                   "[ 1 ]", NULL, false);
>  
> +
> +    DO_TEST_PARSE_FAIL("nothing", "");
> +    DO_TEST_PARSE_FAIL("number with garbage", "2345b45");
> +    DO_TEST_PARSE_FAIL("float with garbage", "0.0314159ee+100");
> +    DO_TEST_PARSE_FAIL("unterminated string", "\"The meaning of lif");

... that not only cannot parse these, but it can't even parse proper
numbers (e.g. "12345", "1.2e+34"), because it expects object (array or
map).  You can do two things with this.  Either create new
testJSON...() which will test numbers only (requires v2) or squash
this in (which makes it work properly and shows that also):

diff --git a/tests/jsontest.c b/tests/jsontest.c
index e806b6f..a7ee2ef 100644
--- a/tests/jsontest.c
+++ b/tests/jsontest.c
@@ -193,10 +193,18 @@ mymain(void)
                  "[ 1 ]", NULL, false);


+    DO_TEST_PARSE("almost nothing", "[]");
     DO_TEST_PARSE_FAIL("nothing", "");
-    DO_TEST_PARSE_FAIL("number with garbage", "2345b45");
-    DO_TEST_PARSE_FAIL("float with garbage", "0.0314159ee+100");
-    DO_TEST_PARSE_FAIL("unterminated string", "\"The meaning of lif");
+
+    DO_TEST_PARSE("number without garbage", "[ 234545 ]");
+    DO_TEST_PARSE_FAIL("number with garbage", "[ 2345b45 ]");
+
+    DO_TEST_PARSE("float without garbage", "[ 0.0314159e+100 ]");
+    DO_TEST_PARSE_FAIL("float with garbage", "[ 0.0314159ee+100 ]");
+
+    DO_TEST_PARSE("string", "[ \"The meaning of life\" ]");
+    DO_TEST_PARSE_FAIL("unterminated string", "[ \"The meaning of lif ]");
+
     DO_TEST_PARSE_FAIL("unterminated array", "[ 1, 2, 3");
     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");
--

ACK if you want to use the second option.

Martin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20131115/3da68d00/attachment-0001.sig>


More information about the libvir-list mailing list