From 197e3724641f26a54546c66ecdf6bf950735e05b Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Sun, 21 Jun 2020 17:36:38 +0000 Subject: [PATCH] In test_parse, fix lengths passed during a couple of incremental tests. 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 | 53 ++++++++++++++++++++++++++++++++++++--- tests/test_parse.expected | 4 +-- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/tests/test_parse.c b/tests/test_parse.c index 42b2fdf..57d584c 100644 --- a/tests/test_parse.c +++ b/tests/test_parse.c @@ -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 */ diff --git a/tests/test_parse.expected b/tests/test_parse.expected index df74fbd..a4b3393 100644 --- a/tests/test_parse.expected +++ b/tests/test_parse.expected @@ -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 }