]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
In test_parse, fix lengths passed during a couple of incremental tests.
authorEric Haszlakiewicz <erh+git@nimenees.com>
Sun, 21 Jun 2020 17:36:38 +0000 (17:36 +0000)
committerEric Haszlakiewicz <erh+git@nimenees.com>
Sun, 21 Jun 2020 17:36:38 +0000 (17:36 +0000)
Add an optional way to use an incremental chunk size ($TEST_PARSE_CHUNKSIZE)
for all of the single_basic_parse tests, in additional to the regular way.

tests/test_parse.c
tests/test_parse.expected

index 42b2fdfd1e47f88ca83a920ae0cef1a5827d8096..57d584cdfbaf8722c5fb0e255fc711881d6996b7 100644 (file)
@@ -33,6 +33,47 @@ int main(void)
 static json_c_visit_userfunc clear_serializer;
 static void do_clear_serializer(json_object *jso);
 
+static void single_incremental_parse(const char *test_string, int clear_serializer)
+{
+       int ii;
+       int chunksize = atoi(getenv("TEST_PARSE_CHUNKSIZE"));
+       struct json_tokener *tok;
+       enum json_tokener_error jerr;
+       json_object *all_at_once_obj, *new_obj;
+       const char *all_at_once_str, *new_str;
+
+       all_at_once_obj = json_tokener_parse(test_string);
+       if (clear_serializer)
+               do_clear_serializer(all_at_once_obj);
+       all_at_once_str = json_object_to_json_string(all_at_once_obj);
+
+       tok = json_tokener_new();
+       int test_string_len = strlen(test_string) + 1; // Including '\0' !
+       for (ii = 0; ii < test_string_len; ii += chunksize)
+       {
+               int len_to_parse = chunksize;
+               if (ii + chunksize > test_string_len)
+                       len_to_parse = test_string_len - ii;
+
+               if (getenv("TEST_PARSE_DEBUG") != NULL)
+                       printf(" chunk: %.*s\n", len_to_parse, &test_string[ii]);
+               new_obj = json_tokener_parse_ex(tok, &test_string[ii], len_to_parse);
+               jerr = json_tokener_get_error(tok);
+               if (jerr != json_tokener_continue || new_obj)
+                       break;
+       }
+       if (clear_serializer && new_obj)
+               do_clear_serializer(new_obj);
+       new_str = json_object_to_json_string(new_obj);
+
+       if (strcmp(all_at_once_str, new_str) != 0)
+       {
+               printf("ERROR: failed to parse (%s) in %d byte chunks: %s != %s\n",
+                   test_string, chunksize, all_at_once_str, new_str);
+       }
+       json_tokener_free(tok);
+}
+
 static void single_basic_parse(const char *test_string, int clear_serializer)
 {
        json_object *new_obj;
@@ -42,6 +83,9 @@ static void single_basic_parse(const char *test_string, int clear_serializer)
                do_clear_serializer(new_obj);
        printf("new_obj.to_string(%s)=%s\n", test_string, json_object_to_json_string(new_obj));
        json_object_put(new_obj);
+
+       if (getenv("TEST_PARSE_CHUNKSIZE") != NULL)
+               single_incremental_parse(test_string, clear_serializer);
 }
 static void test_basic_parse()
 {
@@ -353,14 +397,17 @@ struct incremental_step
         * the next few tests check that parsing multiple sequential
         * json objects in the input works as expected
         */
-    {"null123", 9, 4, json_tokener_success, 0},
+    {"null123", 8, 4, json_tokener_success, 0},
     {&"null123"[4], 4, 3, json_tokener_success, 1},
-    {"nullx", 5, 4, json_tokener_success, 0},
+    {"nullx", 6, 4, json_tokener_success, 0},
     {&"nullx"[4], 2, 0, json_tokener_error_parse_unexpected, 1},
     {"{\"a\":1}{\"b\":2}", 15, 7, json_tokener_success, 0},
     {&"{\"a\":1}{\"b\":2}"[7], 8, 7, json_tokener_success, 1},
 
-    /* Some bad formatting. Check we get the correct error status */
+    /* Some bad formatting. Check we get the correct error status
+     * XXX this means we can't have two numbers in the incremental parse
+     * XXX stream with the second one being a negative number!
+     */
     {"2015-01-15", 10, 4, json_tokener_error_parse_number, 1},
 
     /* Strings have a well defined end point, so we can stop at the quote */
index df74fbd8b4b66e0be59881eaa38f9e745959c6ab..a4b3393b6ff213576527ce761cf0eb80931c0fe9 100644 (file)
@@ -184,9 +184,9 @@ json_tokener_parse_ex(tok, noodle      ,   7) ... OK: got correct error: null ex
 json_tokener_parse_ex(tok, naodle      ,   7) ... OK: got correct error: null expected
 json_tokener_parse_ex(tok, track       ,   6) ... OK: got correct error: boolean expected
 json_tokener_parse_ex(tok, fail        ,   5) ... OK: got correct error: boolean expected
-json_tokener_parse_ex(tok, null123     ,   9) ... OK: got object of type [null]: null
+json_tokener_parse_ex(tok, null123     ,   8) ... OK: got object of type [null]: null
 json_tokener_parse_ex(tok, 123         ,   4) ... OK: got object of type [int]: 123
-json_tokener_parse_ex(tok, nullx       ,   5) ... OK: got object of type [null]: null
+json_tokener_parse_ex(tok, nullx       ,   6) ... OK: got object of type [null]: null
 json_tokener_parse_ex(tok, x           ,   2) ... OK: got correct error: unexpected character
 json_tokener_parse_ex(tok, {"a":1}{"b":2},  15) ... OK: got object of type [object]: { "a": 1 }
 json_tokener_parse_ex(tok, {"b":2}     ,   8) ... OK: got object of type [object]: { "b": 2 }