2019-03-25 09:52:36 -05:00
#!/bin/sh -x
2013-05-08 02:30:08 +01:00
2016-01-17 12:23:05 -06:00
. " ${ 0 %/* } /setup" " $@ "
2014-07-13 19:00:37 -05:00
2015-12-14 23:43:22 -06:00
PATH = $JQBASEDIR :$PATH $JQBASEDIR /tests/jq-f-test.sh > /dev/null
2015-06-18 19:38:25 -07:00
if [ -f " $JQBASEDIR /.libs/libinject_errors.so" ] ; then
2015-05-03 19:23:37 -05:00
# Do some simple error injection tests to check that we're handling
# I/O errors correctly.
(
2015-06-18 19:38:25 -07:00
libinject = $JQBASEDIR /.libs/libinject_errors.so
2015-05-03 19:23:37 -05:00
cd $d
2015-06-18 19:38:25 -07:00
LD_PRELOAD = $libinject $JQ . /dev/null
2015-05-03 19:23:37 -05:00
touch fail_read
2015-06-18 19:38:25 -07:00
LD_PRELOAD = $libinject $JQ . fail_read && exit 2
2015-05-03 19:23:37 -05:00
touch fail_close
2015-06-18 19:38:25 -07:00
LD_PRELOAD = $libinject $JQ . fail_close && exit 2
2015-05-03 19:23:37 -05:00
true
)
fi
2015-04-23 18:27:53 -05:00
printf 'a\0b\nc\0d\ne' > $d /input
2015-06-18 19:38:25 -07:00
$VALGRIND $Q $JQ -Rse '. == "a\u0000b\nc\u0000d\ne"' $d /input
$VALGRIND $Q $JQ -Rne '[inputs] == ["a\u0000b", "c\u0000d", "e"]' $d /input
2015-04-23 18:27:53 -05:00
2014-07-27 17:33:22 -05:00
## Test constant folding
2015-01-02 19:15:57 -06:00
## XXX If we add a builtin to list the program's disassembly then we can
## move all of these into tests/all.test
2014-07-27 17:33:22 -05:00
# String constant folding (addition only)
2017-10-28 10:51:59 -04:00
nref = $( $VALGRIND $Q $JQ -n --debug-dump-disasm '"foo"' | wc -l)
2014-07-27 17:33:22 -05:00
# Numeric constant folding (not all ops yet)
2017-10-28 10:51:59 -04:00
n = $( $VALGRIND $Q $JQ -n --debug-dump-disasm '1+1' | wc -l)
2017-02-19 18:23:36 -06:00
if [ $n -ne $nref ] ; then
2014-07-27 17:33:22 -05:00
echo "Constant expression folding for strings didn't work"
exit 1
fi
2017-10-28 10:51:59 -04:00
n = $( $VALGRIND $Q $JQ -n --debug-dump-disasm '1-1' | wc -l)
2017-02-19 18:23:36 -06:00
if [ $n -ne $nref ] ; then
2014-07-27 17:33:22 -05:00
echo "Constant expression folding for strings didn't work"
exit 1
fi
2017-10-28 10:51:59 -04:00
n = $( $VALGRIND $Q $JQ -n --debug-dump-disasm '2*3' | wc -l)
2017-02-19 18:23:36 -06:00
if [ $n -ne $nref ] ; then
2014-07-27 17:33:22 -05:00
echo "Constant expression folding for strings didn't work"
exit 1
fi
2017-10-28 10:51:59 -04:00
n = $( $VALGRIND $Q $JQ -n --debug-dump-disasm '9/3' | wc -l)
2017-02-19 18:23:36 -06:00
if [ $n -ne $nref ] ; then
2014-07-27 17:33:22 -05:00
echo "Constant expression folding for strings didn't work"
exit 1
fi
2017-10-28 10:51:59 -04:00
n = $( $VALGRIND $Q $JQ -n --debug-dump-disasm '9==3' | wc -l)
2017-02-19 18:23:36 -06:00
if [ $n -ne $nref ] ; then
2014-07-27 17:33:22 -05:00
echo "Constant expression folding for strings didn't work"
exit 1
fi
2017-10-28 10:51:59 -04:00
n = $( $VALGRIND $Q $JQ -n --debug-dump-disasm '9!=3' | wc -l)
2017-02-19 18:23:36 -06:00
if [ $n -ne $nref ] ; then
2014-07-27 17:33:22 -05:00
echo "Constant expression folding for strings didn't work"
exit 1
fi
2017-10-28 10:51:59 -04:00
n = $( $VALGRIND $Q $JQ -n --debug-dump-disasm '9<=3' | wc -l)
2017-02-19 18:23:36 -06:00
if [ $n -ne $nref ] ; then
2014-07-27 17:33:22 -05:00
echo "Constant expression folding for strings didn't work"
exit 1
fi
2017-10-28 10:51:59 -04:00
n = $( $VALGRIND $Q $JQ -n --debug-dump-disasm '9>=3' | wc -l)
2017-02-19 18:23:36 -06:00
if [ $n -ne $nref ] ; then
2014-07-27 17:33:22 -05:00
echo "Constant expression folding for strings didn't work"
exit 1
fi
2014-10-10 22:19:38 -05:00
## Test JSON sequence support
cat > $d /expected <<EOF
2019-03-25 09:52:36 -05:00
jq: ignoring parse error: Truncated value at line 2, column 5
jq: ignoring parse error: Truncated value at line 2, column 25
jq: ignoring parse error: Truncated value at line 2, column 41
2014-10-10 22:19:38 -05:00
EOF
2015-06-18 19:38:25 -07:00
printf '1\0362 3\n[0,1\036[4,5]true"ab"{"c":4\036{}{"d":5,"e":6"\036false\n' | $VALGRIND $Q $JQ -ces --seq '. == [2,3,[4,5],true,"ab",{},false]' > /dev/null 2> $d /out
2014-10-10 22:19:38 -05:00
cmp $d /out $d /expected
cat > $d /expected <<EOF
2019-03-25 09:52:36 -05:00
jq: ignoring parse error: Truncated value at line 2, column 5
jq: ignoring parse error: Truncated value at line 2, column 25
jq: ignoring parse error: Truncated value at line 3, column 1
2014-10-10 22:19:38 -05:00
EOF
2015-06-18 19:38:25 -07:00
printf '1\0362 3\n[0,1\036[4,5]true"ab"{"c":4\036{}{"d":5,"e":6"false\n\036null' | $VALGRIND $Q $JQ -ces --seq '. == [2,3,[4,5],true,"ab",{},null]' > /dev/null 2> $d /out
2014-10-10 22:19:38 -05:00
cmp $d /out $d /expected
# Note that here jq sees no inputs at all but it still succeeds because
# --seq ignores parse errors
cat > $d /expected <<EOF
2019-03-25 09:52:36 -05:00
jq: ignoring parse error: Unfinished abandoned text at EOF at line 1, column 4
2014-10-10 22:19:38 -05:00
EOF
2018-07-29 12:50:28 -07:00
printf '"foo' | $JQ -c --seq . > $d /out 2>& 1
cmp $d /out $d /expected
# with -e option should give 4 here as there's no valid output after
# ignoring parse errors with --seq.
printf '"foo' | $JQ -ce --seq . > $d /out 2>& 1 || ret = $?
[ $ret -eq 4 ]
2014-10-10 22:19:38 -05:00
cmp $d /out $d /expected
# Numeric values truncated by EOF are ignored
cat > $d /expected <<EOF
2019-03-25 09:52:36 -05:00
jq: ignoring parse error: Unfinished abandoned text at EOF at line 1, column 1
2014-10-10 22:19:38 -05:00
EOF
2018-07-29 12:50:28 -07:00
printf '1' | $JQ -c --seq . > $d /out 2>& 1
2014-10-10 22:19:38 -05:00
cmp $d /out $d /expected
cat > $d /expected <<EOF
2015-09-21 23:24:08 -05:00
jq: error (at <stdin>:1): Unfinished abandoned text at EOF at line 2, column 0
2014-10-10 22:19:38 -05:00
EOF
2015-09-21 23:24:08 -05:00
if printf '1\n' | $JQ -cen --seq '[inputs] == []' >/dev/null 2> $d /out; then
printf 'Error expected but jq exited successfully\n' 1>& 2
exit 2
fi
2014-10-10 22:19:38 -05:00
cmp $d /out $d /expected
2018-07-29 12:50:28 -07:00
## Test --exit-status
data = '{"i": 1}\n{"i": 2}\n{"i": 3}\n'
2019-02-26 18:45:32 -05:00
printf " $data " | $JQ --exit-status 'select(.i==1)' > /dev/null 2>& 1
printf " $data " | $JQ --exit-status 'select(.i==2)' > /dev/null 2>& 1
printf " $data " | $JQ --exit-status 'select(.i==3)' > /dev/null 2>& 1
2018-07-29 12:50:28 -07:00
ret = 0
2019-02-26 18:45:32 -05:00
printf " $data " | $JQ --exit-status 'select(.i==4)' > /dev/null 2>& 1 || ret = $?
2018-07-29 12:50:28 -07:00
[ $ret -eq 4 ]
ret = 0
2019-02-26 18:45:32 -05:00
printf " $data " | $JQ --exit-status 'select(.i==2) | false' > /dev/null 2>& 1 || ret = $?
2018-07-29 12:50:28 -07:00
[ $ret -eq 1 ]
2019-02-26 18:45:32 -05:00
printf " $data " | $JQ --exit-status 'select(.i==2) | true' > /dev/null 2>& 1
2018-07-29 12:50:28 -07:00
2015-09-21 23:24:08 -05:00
# Regression test for #951
printf '"a\n' > $d /input
if $VALGRIND $Q $JQ -e . $d /input; then
printf 'Issue #951 is back?\n' 1>& 2
exit 2
fi
2017-12-04 19:20:58 -05:00
# Regression test for #1534
echo "[1,2,3,4]" > $d /expected
printf "[1,2][3,4]" | $JQ -cs add > $d /out 2>& 1
cmp $d /out $d /expected
2017-12-04 20:10:56 -05:00
printf "[1,2][3,4]\n" | $JQ -cs add > $d /out 2>& 1
cmp $d /out $d /expected
2017-12-04 19:20:58 -05:00
2023-06-06 07:19:43 -04:00
# Regression test for -0 / --nul-output
printf "a\0b\0" > $d /expected
printf '["a", "b"]' | $JQ -0 .[] > $d /out 2>& 1
cmp $d /out $d /expected
printf '["a", "b"]' | $JQ --nul-output .[] > $d /out 2>& 1
cmp $d /out $d /expected
2017-12-04 19:20:58 -05:00
2014-12-22 23:06:27 -06:00
## Test streaming parser
2015-01-02 19:15:57 -06:00
## If we add an option to stream to the `import ... as $symbol;` directive
## then we can move these tests into tests/all.test.
2019-02-25 23:53:21 -05:00
$VALGRIND $Q $JQ -c '. as $d|path(..) as $p|$d|getpath($p)|select((type|. != "array" and . != "object") or length==0)|[$p,.]' < " $JQTESTDIR /torture/input0.json" > $d /out0
2015-06-18 19:38:25 -07:00
$VALGRIND $Q $JQ --stream -c '.|select(length==2)' < " $JQTESTDIR /torture/input0.json" > $d /out1
2014-12-22 23:06:27 -06:00
diff $d /out0 $d /out1
2015-01-02 19:15:57 -06:00
## XXX This test can be moved to tests/all.test _now_
2014-12-26 21:28:31 -06:00
clean = false
if which seq > /dev/null 2>& 1; then
# XXX We should try every prefix of input0.json, but that makes this
# test very, very slow when run with valgrind, and the whole point
# is to run it with valgrind.
#
2017-10-28 10:51:59 -04:00
#len=$(wc -c < "$JQTESTDIR/torture/input0.json")
2014-12-26 21:28:31 -06:00
if [ -z " $VALGRIND " ] ; then
start = 1
2017-10-28 10:51:59 -04:00
end = $( wc -c < " $JQTESTDIR /torture/input0.json" )
2014-12-26 21:28:31 -06:00
else
start = 120
end = 151
fi
2017-10-28 10:51:59 -04:00
for i in $( seq $start $end ) ; do
2014-12-26 21:28:31 -06:00
dd "if=tests/torture/input0.json" bs = $i count = 1 2>/dev/null |
2015-06-18 19:38:25 -07:00
$VALGRIND $JQ -c . > $d /out0 2>$d /err || true
2014-12-26 21:28:31 -06:00
if [ -n " $VALGRIND " ] ; then
grep '^==[0-9][0-9]*== ERROR SUMMARY: 0 errors' $d /err > /dev/null
else
tail -1 $d /err | egrep -i 'assert|abort|core' && false
fi
dd "if=tests/torture/input0.json" bs = $i count = 1 2>/dev/null |
2015-06-26 20:16:23 -05:00
$VALGRIND $JQ -cn --stream 'fromstream(inputs)' > $d /out1 2>$d /err || true
2014-12-26 21:28:31 -06:00
if [ -n " $VALGRIND " ] ; then
grep '^==[0-9][0-9]*== ERROR SUMMARY: 0 errors' $d /err > /dev/null
else
tail -1 $d /err | egrep -i 'assert|abort|core' && false
fi
diff $d /out0 $d /out1
done
else
echo "Not doing torture tests"
fi
2023-07-04 21:23:52 +02:00
## Regression test for issue #2378 assert when stream parse broken object pair
echo '{"a":1,"b",' | $JQ --stream > /dev/null 2> $d /err
grep 'Objects must consist of key:value pairs' $d /err > /dev/null
2014-12-26 19:31:47 -06:00
## Fuzz parser
2017-10-28 10:51:59 -04:00
## XXX With a $(urandom) builtin we could move this test into tests/all.test
2014-12-26 21:28:31 -06:00
clean = false
2014-12-26 19:31:47 -06:00
if dd if = /dev/urandom bs = 16 count = 1024 > $d /rand 2>/dev/null; then
# Have a /dev/urandom, good
2015-06-18 19:38:25 -07:00
$VALGRIND $Q $JQ --seq . $d /rand >/dev/null 2>& 1
$VALGRIND $Q $JQ --seq --stream . $d /rand >/dev/null 2>& 1
2014-12-26 19:31:47 -06:00
dd if = /dev/urandom bs = 16 count = 1024 > $d /rand 2>/dev/null
2015-06-18 19:38:25 -07:00
$VALGRIND $Q $JQ --seq . $d /rand >/dev/null 2>& 1
$VALGRIND $Q $JQ --seq --stream . $d /rand >/dev/null 2>& 1
2014-12-26 19:31:47 -06:00
dd if = /dev/urandom bs = 16 count = 1024 > $d /rand 2>/dev/null
2015-06-18 19:38:25 -07:00
$VALGRIND $Q $JQ --seq . $d /rand >/dev/null 2>& 1
$VALGRIND $Q $JQ --seq --stream . $d /rand >/dev/null 2>& 1
2014-12-26 19:31:47 -06:00
fi
2014-12-26 21:28:31 -06:00
clean = true
2014-12-26 19:31:47 -06:00
2014-07-27 17:33:22 -05:00
## Test library/module system
2014-12-31 19:21:45 -06:00
# Check handling of ~/.jq; these can't move into jq_test.c yet because
# they depend on $HOME
2019-02-20 22:53:37 -05:00
if [ " $( HOME = " $mods /home1" $VALGRIND $Q $JQ -nr fg ) " != foobar ] ; then
2014-07-13 19:00:37 -05:00
echo "Bug #479 appears to be back" 1>& 2
exit 1
fi
2019-02-20 22:53:37 -05:00
if [ $( HOME = " $mods /home1" $VALGRIND $Q $JQ --debug-dump-disasm -n fg | grep '^[a-z]' | wc -l) -ne 3 ] ; then
2014-07-13 19:00:37 -05:00
echo "Binding too many defs into program" 1>& 2
exit 1
fi
2019-02-20 22:53:37 -05:00
if ! HOME = " $mods /home2" $VALGRIND $Q $JQ -n 'include "g"; empty' ; then
2023-06-13 07:01:20 -04:00
echo "Mishandling directory ~/.jq" 1>& 2
2019-02-20 22:53:37 -05:00
exit 1
fi
2015-08-11 17:40:39 -05:00
cd " $JQBASEDIR " # so that relative library paths are guaranteed correct
if ! $VALGRIND $Q $JQ -L ./tests/modules -ne 'import "test_bind_order" as check; check::check==true' ; then
echo "Issue #817 regression?" 1>& 2
exit 1
fi
cd " $JQBASEDIR "
if ! $VALGRIND $Q $JQ -L tests/modules -ne 'import "test_bind_order" as check; check::check==true' ; then
echo "Issue #817 regression?" 1>& 2
exit 1
fi
2017-02-22 23:01:56 -06:00
## Halt
if ! $VALGRIND $Q $JQ -n halt; then
echo "jq halt didn't work as expected" 1>& 2
exit 1
fi
if $VALGRIND $Q $VG_EXIT0 $JQ -n 'halt_error(1)' ; then
echo "jq halt_error(1) didn't work as expected" 1>& 2
exit 1
elif [ $? -ne 1 ] ; then
echo "jq halt_error(1) had wrong error code" 1>& 2
exit 1
fi
if $VALGRIND $Q $VG_EXIT0 $JQ -n 'halt_error(11)' ; then
echo "jq halt_error(11) didn't work as expected" 1>& 2
exit 1
elif [ $? -ne 11 ] ; then
echo "jq halt_error(11) had wrong error code" 1>& 2
exit 1
fi
2017-10-28 10:51:59 -04:00
if [ -n " $( $VALGRIND $Q $JQ -n 'halt_error(1)' 2>& 1) " ] ; then
2017-02-22 23:01:56 -06:00
echo "jq halt_error(1) had unexpected output" 1>& 2
exit 1
fi
2023-06-27 07:33:21 +09:00
if [ -n " $( $VALGRIND $Q $JQ -n '"xyz\n" | halt_error(1)' 2>/dev/null) " ] ; then
2017-02-22 23:01:56 -06:00
echo "jq halt_error(1) had unexpected output on stdout" 1>& 2
exit 1
fi
2023-06-27 07:33:21 +09:00
if [ " $( $VALGRIND $Q $JQ -n '"xy" | halt_error(1)' 2>& 1 || echo "z" ) " != "xyz" ] ; then
echo "jq halt_error(1) had unexpected output" 1>& 2
exit 1
fi
if [ " $( $VALGRIND $Q $JQ -n '"x\u0000y\u0000z" | halt_error(1)' 2>& 1 | tr '\0' '.' ) " != "x.y.z" ] ; then
echo "jq halt_error(1) had unexpected output" 1>& 2
exit 1
fi
if [ " $( $VALGRIND $Q $JQ -n '{"a":"xyz"} | halt_error(1)' 2>& 1) " != '{"a":"xyz"}' ] ; then
2017-02-22 23:01:56 -06:00
echo "jq halt_error(1) had unexpected output" 1>& 2
exit 1
fi
2017-04-29 13:00:52 -05:00
# Check $JQ_COLORS
$JQ -Ccn . > $d /color
printf '\033[1;30mnull\033[0m\n' > $d /expect
cmp $d /color $d /expect
JQ_COLORS = '4;31' $JQ -Ccn . > $d /color
printf '\033[4;31mnull\033[0m\n' > $d /expect
cmp $d /color $d /expect
JQ_COLORS = '1;30:0;31:0;32:0;33:0;34:1;35:1;36' \
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d /color
(
printf '\033[1;35m[\033[1;36m{'
printf '\033[0m\033[34;1m"a"\033['
printf '0m\033[1;36m:\033[0m\033['
printf '0;32mtrue\033[0m\033[1'
printf ';36m,\033[0m\033[34;1m'
printf '"b"\033[0m\033[1;36m:\033'
printf '[0m\033[0;31mfalse\033'
printf '[0m\033[1;36m\033[1;36'
printf 'm}\033[0m\033[1;35m,\033['
printf '0;33m123\033[0m\033[1;'
printf '35m,\033[1;30mnull\033'
printf '[0m\033[1;35m\033[1;35'
printf 'm]\033[0m\n'
) > $d /expect
cmp $d /color $d /expect
# Check garbage in JQ_COLORS. We write each color sequence into a 16
# char buffer that needs to hold ESC [ <color> m NUL, so each color
# sequence can be no more than 12 chars (bytes). These emit a warning
# on stderr.
set -vx
echo 'Failed to set $JQ_COLORS' > $d /expect_warning
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d /expect
JQ_COLORS = 'garbage;30:*;31:,;3^:0;$%:0;34:1;35:1;36' \
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d /color 2>$d /warning
cmp $d /color $d /expect
cmp $d /warning $d /expect_warning
JQ_COLORS = '1234567890123456789;30:0;31:0;32:0;33:0;34:1;35:1;36' \
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d /color 2>$d /warning
cmp $d /color $d /expect
cmp $d /warning $d /expect_warning
JQ_COLORS = '1;31234567890123456789:0;31:0;32:0;33:0;34:1;35:1;36' \
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d /color 2>$d /warning
cmp $d /color $d /expect
cmp $d /warning $d /expect_warning
JQ_COLORS = '1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456' \
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d /color 2>$d /warning
cmp $d /color $d /expect
cmp $d /warning $d /expect_warning
JQ_COLORS = "0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:" \
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d /color 2>$d /warning
cmp $d /color $d /expect
cmp $d /warning $d /expect_warning
2014-12-26 21:28:31 -06:00
exit 0