diff options
author | Lloyd Hilaiel <lloyd@hilaiel.com> | 2011-04-20 10:41:14 -0600 |
---|---|---|
committer | Lloyd Hilaiel <lloyd@hilaiel.com> | 2011-04-20 10:41:14 -0600 |
commit | b9e91a37f7132bf7f5b91de895b766db06ae3072 (patch) | |
tree | a2f006d686e40502ebc74be858992db52ba2204b /test | |
parent | dc206cf75b8489b4cdcc8ff0cdbe94cbcc61a551 (diff) | |
download | yajl-b9e91a37f7132bf7f5b91de895b766db06ae3072.tar.gz |
o rework yajl api
- remove yajl_status_parse_incomplete, replace with three
flag settings
- yajl_allow_multiple_values
- yajl_forbid_trailing_garbage
- yajl_forbid_partial_values
In the new model, callers must consistently call yajl_parse_complete
and check it's return. Two new parse errors have been introduced:
"premature EOF" and "trailing garbage".
yajl_test.c demonstrates the simplifying effect on calling code.
adds 3 flags to yajl_test
-g forbids trailing garbage
-p forbids partial values
-m allows multiple values to be parsed.
and complementary tests.
lth: Addresses the majority of issue #24. gno is awesomesauce.
Signed-off-by: Lloyd Hilaiel <lloyd@hilaiel.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/cases/ag_false_then_garbage.json | 1 | ||||
-rw-r--r-- | test/cases/ag_false_then_garbage.json.gold | 3 | ||||
-rw-r--r-- | test/cases/ag_null_then_garbage.json | 1 | ||||
-rw-r--r-- | test/cases/ag_null_then_garbage.json.gold | 3 | ||||
-rw-r--r-- | test/cases/ag_true_then_garbage.json | 1 | ||||
-rw-r--r-- | test/cases/ag_true_then_garbage.json.gold | 3 | ||||
-rw-r--r-- | test/cases/am_eof.json | 1 | ||||
-rw-r--r-- | test/cases/am_eof.json.gold | 4 | ||||
-rw-r--r-- | test/cases/am_integers.json.gold | 3 | ||||
-rw-r--r-- | test/cases/am_multiple.json | 3 | ||||
-rw-r--r-- | test/cases/am_multiple.json.gold | 5 | ||||
-rw-r--r-- | test/cases/am_stuff.json | 7 | ||||
-rw-r--r-- | test/cases/am_stuff.json.gold | 14 | ||||
-rw-r--r-- | test/cases/eof_str.json | 1 | ||||
-rw-r--r-- | test/cases/eof_str.json.gold | 1 | ||||
-rw-r--r-- | test/cases/multiple.json | 3 | ||||
-rw-r--r-- | test/cases/multiple.json.gold | 3 | ||||
-rwxr-xr-x | test/run_tests.sh | 14 | ||||
-rw-r--r-- | test/yajl_test.c | 47 |
19 files changed, 93 insertions, 25 deletions
diff --git a/test/cases/ag_false_then_garbage.json b/test/cases/ag_false_then_garbage.json new file mode 100644 index 0000000..78f4e96 --- /dev/null +++ b/test/cases/ag_false_then_garbage.json @@ -0,0 +1 @@ +falsex
\ No newline at end of file diff --git a/test/cases/ag_false_then_garbage.json.gold b/test/cases/ag_false_then_garbage.json.gold new file mode 100644 index 0000000..fe8fc1a --- /dev/null +++ b/test/cases/ag_false_then_garbage.json.gold @@ -0,0 +1,3 @@ +bool: false +parse error: trailing garbage +memory leaks: 0 diff --git a/test/cases/ag_null_then_garbage.json b/test/cases/ag_null_then_garbage.json new file mode 100644 index 0000000..7b65b35 --- /dev/null +++ b/test/cases/ag_null_then_garbage.json @@ -0,0 +1 @@ +nullx diff --git a/test/cases/ag_null_then_garbage.json.gold b/test/cases/ag_null_then_garbage.json.gold new file mode 100644 index 0000000..349235c --- /dev/null +++ b/test/cases/ag_null_then_garbage.json.gold @@ -0,0 +1,3 @@ +null +parse error: trailing garbage +memory leaks: 0 diff --git a/test/cases/ag_true_then_garbage.json b/test/cases/ag_true_then_garbage.json new file mode 100644 index 0000000..31f9deb --- /dev/null +++ b/test/cases/ag_true_then_garbage.json @@ -0,0 +1 @@ +truex diff --git a/test/cases/ag_true_then_garbage.json.gold b/test/cases/ag_true_then_garbage.json.gold new file mode 100644 index 0000000..73af6ce --- /dev/null +++ b/test/cases/ag_true_then_garbage.json.gold @@ -0,0 +1,3 @@ +bool: true +parse error: trailing garbage +memory leaks: 0 diff --git a/test/cases/am_eof.json b/test/cases/am_eof.json new file mode 100644 index 0000000..43a772f --- /dev/null +++ b/test/cases/am_eof.json @@ -0,0 +1 @@ +{ "123": diff --git a/test/cases/am_eof.json.gold b/test/cases/am_eof.json.gold new file mode 100644 index 0000000..5a9708b --- /dev/null +++ b/test/cases/am_eof.json.gold @@ -0,0 +1,4 @@ +map open '{' +key: '123' +parse error: premature EOF +memory leaks: 0 diff --git a/test/cases/am_integers.json.gold b/test/cases/am_integers.json.gold new file mode 100644 index 0000000..7b8a953 --- /dev/null +++ b/test/cases/am_integers.json.gold @@ -0,0 +1,3 @@ +integer: 1221 +integer: 21 +memory leaks: 0 diff --git a/test/cases/am_multiple.json b/test/cases/am_multiple.json new file mode 100644 index 0000000..b573db0 --- /dev/null +++ b/test/cases/am_multiple.json @@ -0,0 +1,3 @@ + +{} +{} diff --git a/test/cases/am_multiple.json.gold b/test/cases/am_multiple.json.gold new file mode 100644 index 0000000..bf705d4 --- /dev/null +++ b/test/cases/am_multiple.json.gold @@ -0,0 +1,5 @@ +map open '{' +map close '}' +map open '{' +map close '}' +memory leaks: 0 diff --git a/test/cases/am_stuff.json b/test/cases/am_stuff.json new file mode 100644 index 0000000..89c607d --- /dev/null +++ b/test/cases/am_stuff.json @@ -0,0 +1,7 @@ +{} +[] +[] +"sdfasd" +123 +{ "123" : 123 } +3.1e124 diff --git a/test/cases/am_stuff.json.gold b/test/cases/am_stuff.json.gold new file mode 100644 index 0000000..bf3a5bb --- /dev/null +++ b/test/cases/am_stuff.json.gold @@ -0,0 +1,14 @@ +map open '{' +map close '}' +array open '[' +array close ']' +array open '[' +array close ']' +string: 'sdfasd' +integer: 123 +map open '{' +key: '123' +integer: 123 +map close '}' +double: 3.1e+124 +memory leaks: 0 diff --git a/test/cases/eof_str.json b/test/cases/eof_str.json new file mode 100644 index 0000000..6a21793 --- /dev/null +++ b/test/cases/eof_str.json @@ -0,0 +1 @@ +"abc
\ No newline at end of file diff --git a/test/cases/eof_str.json.gold b/test/cases/eof_str.json.gold new file mode 100644 index 0000000..736730b --- /dev/null +++ b/test/cases/eof_str.json.gold @@ -0,0 +1 @@ +memory leaks: 0 diff --git a/test/cases/multiple.json b/test/cases/multiple.json new file mode 100644 index 0000000..b573db0 --- /dev/null +++ b/test/cases/multiple.json @@ -0,0 +1,3 @@ + +{} +{} diff --git a/test/cases/multiple.json.gold b/test/cases/multiple.json.gold new file mode 100644 index 0000000..c687717 --- /dev/null +++ b/test/cases/multiple.json.gold @@ -0,0 +1,3 @@ +map open '{' +map close '}' +memory leaks: 0 diff --git a/test/run_tests.sh b/test/run_tests.sh index d596daf..c4fa519 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/sh ECHO=`which echo` @@ -33,21 +33,29 @@ testsTotal=0 for file in cases/*.json ; do allowComments="-c" + allowGarbage="" + allowMultiple="" # if the filename starts with dc_, we disallow comments for this test case $(basename $file) in dc_*) allowComments="" ;; + ag_*) + allowGarbage="-g" + ;; + am_*) + allowMultiple="-m"; + ;; esac ${ECHO} -n " test case: '$file': " iter=1 success="success" - ${ECHO} "$testBin $allowComments -b $iter < $file > ${file}.test " + ${ECHO} "$testBin $allowComments $allowGarbage $allowMultiple -b $iter < $file > ${file}.test " # parse with a read buffer size ranging from 1-31 to stress stream parsing while [ $iter -lt 32 ] && [ $success = "success" ] ; do - $testBin $allowComments -b $iter < $file > ${file}.test 2>&1 + $testBin $allowComments $allowGarbage $allowMultiple -b $iter < $file > ${file}.test 2>&1 diff ${DIFF_FLAGS} ${file}.gold ${file}.test if [ $? -eq 0 ] ; then if [ $iter -eq 31 ] ; then : $(( testsSucceeded += 1)) ; fi diff --git a/test/yajl_test.c b/test/yajl_test.c index 9ac7168..a677da6 100644 --- a/test/yajl_test.c +++ b/test/yajl_test.c @@ -184,8 +184,8 @@ main(int argc, char ** argv) yajl_status stat; size_t rd; yajl_parser_config cfg = { 0, 1 }; - int i, j, done; - + int i, j; + int multiple = 0 ,trailing = 0, partial = 0; /* memory allocation debugging: allocate a structure which collects * statistics */ yajlTestMemoryContext memCtx = { 0,0 }; @@ -221,6 +221,12 @@ main(int argc, char ** argv) fprintf(stderr, "%zu is an invalid buffer size\n", bufSize); } + } else if (!strcmp("-m", argv[i])) { + multiple = 1; partial = 1; + } else if (!strcmp("-g", argv[i])) { + trailing = 1; + } else if (!strcmp("-p", argv[i])) { + partial = 1; } else { fprintf(stderr, "invalid command line option: '%s'\n", argv[i]); @@ -241,37 +247,34 @@ main(int argc, char ** argv) /* ok. open file. let's read and parse */ hand = yajl_alloc(&callbacks, &cfg, &allocFuncs, NULL); + if (trailing) yajl_forbid_trailing_garbage(hand); + if (multiple) yajl_allow_multiple_values(hand); + if (partial) yajl_forbid_partial_values(hand); - done = 0; - while (!done) { + for (;;) { rd = fread((void *) fileData, 1, bufSize, stdin); if (rd == 0) { if (!feof(stdin)) { fprintf(stderr, "error reading from '%s'\n", fileName); - break; } - done = 1; + break; } - - if (done) - /* parse any remaining buffered data */ - stat = yajl_parse_complete(hand); - else /* read file data, pass to parser */ - stat = yajl_parse(hand, fileData, rd); + stat = yajl_parse(hand, fileData, rd); - if (stat != yajl_status_insufficient_data && - stat != yajl_status_ok) - { - unsigned char * str = yajl_get_error(hand, 0, fileData, rd); - fflush(stdout); - fprintf(stderr, "%s", (char *) str); - yajl_free_error(hand, str); - break; - } - } + if (stat != yajl_status_ok) break; + } + stat = yajl_parse_complete(hand); + if (stat != yajl_status_ok) + { + unsigned char * str = yajl_get_error(hand, 0, fileData, rd); + fflush(stdout); + fprintf(stderr, "%s", (char *) str); + yajl_free_error(hand, str); + } + yajl_free(hand); free(fileData); |