Streaming parser torture tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nicolas Williams · committed by Nicolas Williams · parent 845ad5e · Browse files · 2 files·+95 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+95 added−4 removed
- tests:+95 added−4 removed
Provenance
- Commit
559863c- Tree
d3e8e6e- Author
Nicolas Williams <nico@cryptonector.com>- Committer
Nicolas Williams <nico@cryptonector.com>- Parent
845ad5e
2 files changed+95 added−4 removed
added:tests/modules/streaming.jq+41 added
1+2+# Filter and adjust streamed values so that only values from the .th3+# level are output.4+def trunc(inputs):5+ . as $n | inputs | . as $input | if (.[0]|length) > 1 then setpath([0];$input[0][1:]) else empty end;6+7+# Reduce streamed values back to normal8+def tovalues(i):9+ foreach i as $item (10+ # [<current value being built>,11+ # <is current value valid?>,12+ # <previous, complete value>,13+ # <is previous value valid?>]14+ [null,false,null,false];15+16+ # Updator17+ # 18+ # If the new $item is a top-level value,19+ # then clear out the current value20+ if ($item[0]|length) == 0 then [null,false,.[2],.[3]]21+ # else if the new $item terminates the current value,22+ # then rotate the current value into the previous value slot.23+ elif ($item|length) == 1 and ($item[0]|length) < 2 then [null,false,.[0],.[1]]24+ else . end |25+ . as $state |26+ # If the new $item has a leaf, upate the current value27+ if ($item|length) > 1 and ($item[0]|length) > 0 then28+ [.[0]|setpath(($item|.[0]); ($item|.[1])), # update current value29+ true, # current value is now valid (if, perhaps, incomplete)30+ $state[2], # previous value is unchanged31+ $state[3]] # previous value is unchanged32+ else .33+ end;34+35+ # Extractor36+ #37+ # If previous value is valid, output it38+ if ($item[0]|length) == 1 and ($item|length == 1) and .[3] then .[2] else empty end,39+ # and/or if the new $item is a top-level scalar, output it40+ if ($item[0]|length) == 0 then $item[1] else empty end41+ );modified:tests/run+54 added-4 removed
10 unmodified lines11 fi12 13 cat $@ | $VALGRIND $Q ./jq --run-tests14 15+set -x16+17+mods=$PWD/tests/modules18+19+clean=true20 d=16-trap '[ -n "$d" ] && rm -rf "$d"' EXIT21+clean () {22+ if ! $clean; then23+ echo "See temp files in $d!"24+ elif [ -n "$d" ]; then25+ rm -rf "$d"26+ fi27+}28+trap clean EXIT29 d=`mktemp -d -t || true`30 if [ -z "$d" ]; then31 echo "Your OS does not support mktemp(1) -d" 1>&232 exit 0101 unmodified lines134 $VALGRIND $Q ./jq -c '. as $d|path(..) as $p|$d|getpath($p)|scalars_or_empty|[$p,.]' < "$PWD/tests/torture/input0.json" > $d/out0135 $VALGRIND $Q ./jq --stream -c '.|select(length==2)' < "$PWD/tests/torture/input0.json" > $d/out1136 diff $d/out0 $d/out1137 138+clean=false139+if which seq > /dev/null 2>&1; then140+ # XXX We should try every prefix of input0.json, but that makes this141+ # test very, very slow when run with valgrind, and the whole point142+ # is to run it with valgrind.143+ #144+ #len=`wc -c < "$PWD/tests/torture/input0.json"`145+ if [ -z "$VALGRIND" ]; then146+ start=1147+ end=`wc -c < "$PWD/tests/torture/input0.json"`148+ else149+ start=120150+ end=151151+ fi152+ for i in `seq $start $end`; do153+ dd "if=tests/torture/input0.json" bs=$i count=1 2>/dev/null |154+ $VALGRIND ./jq -c . > $d/out0 2>$d/err || true155+ if [ -n "$VALGRIND" ]; then156+ grep '^==[0-9][0-9]*== ERROR SUMMARY: 0 errors' $d/err > /dev/null157+ else158+ tail -1 $d/err | egrep -i 'assert|abort|core' && false159+ fi160+161+ dd "if=tests/torture/input0.json" bs=$i count=1 2>/dev/null |162+ $VALGRIND ./jq -cn --stream -L "$mods" 'import streaming; tovalues(inputs)' > $d/out1 2>$d/err || true163+ if [ -n "$VALGRIND" ]; then164+ grep '^==[0-9][0-9]*== ERROR SUMMARY: 0 errors' $d/err > /dev/null165+ else166+ tail -1 $d/err | egrep -i 'assert|abort|core' && false167+ fi168+169+ diff $d/out0 $d/out1170+ done171+else172+ echo "Not doing torture tests"173+fi174+175 ## Fuzz parser176 177+clean=false178 if dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null; then179 # Have a /dev/urandom, good130- set -x180 $VALGRIND $Q ./jq --seq . $d/rand >/dev/null 2>&1181 $VALGRIND $Q ./jq --seq --stream . $d/rand >/dev/null 2>&1182 dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null183 $VALGRIND $Q ./jq --seq . $d/rand >/dev/null 2>&11 unmodified line185 dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null186 $VALGRIND $Q ./jq --seq . $d/rand >/dev/null 2>&1187 $VALGRIND $Q ./jq --seq --stream . $d/rand >/dev/null 2>&1188 fi189+clean=true190 191 ## Test library/module system192 143-mods=$PWD/tests/modules144-193 if [ "`HOME="$mods" $VALGRIND $Q ./jq -nr fg`" != foobar ]; then194 echo "Bug #479 appears to be back" 1>&2195 exit 1196 fi52 unmodified lines249 echo "Memory errors when programs have syntax errors" 1>&2250 cat $d/out251 exit 1252 fi253+254+exit 0