"Fossies" - the Fresh Open Source Software Archive 
Member "jq-1.6/tests/shtest" (2 Nov 2018, 10720 Bytes) of package /linux/misc/jq-1.6.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 #!/bin/sh
2
3 . "${0%/*}/setup" "$@"
4
5 PATH=$JQBASEDIR:$PATH $JQBASEDIR/tests/jq-f-test.sh > /dev/null
6
7 if [ -f "$JQBASEDIR/.libs/libinject_errors.so" ]; then
8 # Do some simple error injection tests to check that we're handling
9 # I/O errors correctly.
10 (
11 libinject=$JQBASEDIR/.libs/libinject_errors.so
12 cd $d
13 LD_PRELOAD=$libinject $JQ . /dev/null
14 touch fail_read
15 LD_PRELOAD=$libinject $JQ . fail_read && exit 2
16 touch fail_close
17 LD_PRELOAD=$libinject $JQ . fail_close && exit 2
18 true
19 )
20 fi
21
22 printf 'a\0b\nc\0d\ne' > $d/input
23 $VALGRIND $Q $JQ -Rse '. == "a\u0000b\nc\u0000d\ne"' $d/input
24 $VALGRIND $Q $JQ -Rne '[inputs] == ["a\u0000b", "c\u0000d", "e"]' $d/input
25
26 ## Test constant folding
27
28 ## XXX If we add a builtin to list the program's disassembly then we can
29 ## move all of these into tests/all.test
30
31 # String constant folding (addition only)
32 nref=$($VALGRIND $Q $JQ -n --debug-dump-disasm '"foo"' | wc -l)
33
34 # Numeric constant folding (not all ops yet)
35 n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '1+1' | wc -l)
36 if [ $n -ne $nref ]; then
37 echo "Constant expression folding for strings didn't work"
38 exit 1
39 fi
40 n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '1-1' | wc -l)
41 if [ $n -ne $nref ]; then
42 echo "Constant expression folding for strings didn't work"
43 exit 1
44 fi
45 n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '2*3' | wc -l)
46 if [ $n -ne $nref ]; then
47 echo "Constant expression folding for strings didn't work"
48 exit 1
49 fi
50 n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9/3' | wc -l)
51 if [ $n -ne $nref ]; then
52 echo "Constant expression folding for strings didn't work"
53 exit 1
54 fi
55 n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9==3' | wc -l)
56 if [ $n -ne $nref ]; then
57 echo "Constant expression folding for strings didn't work"
58 exit 1
59 fi
60 n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9!=3' | wc -l)
61 if [ $n -ne $nref ]; then
62 echo "Constant expression folding for strings didn't work"
63 exit 1
64 fi
65 n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9<=3' | wc -l)
66 if [ $n -ne $nref ]; then
67 echo "Constant expression folding for strings didn't work"
68 exit 1
69 fi
70 n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9>=3' | wc -l)
71 if [ $n -ne $nref ]; then
72 echo "Constant expression folding for strings didn't work"
73 exit 1
74 fi
75
76 ## Test JSON sequence support
77
78 cat > $d/expected <<EOF
79 ignoring parse error: Truncated value at line 2, column 5
80 ignoring parse error: Truncated value at line 2, column 25
81 ignoring parse error: Truncated value at line 2, column 41
82 EOF
83 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
84 cmp $d/out $d/expected
85
86 cat > $d/expected <<EOF
87 ignoring parse error: Truncated value at line 2, column 5
88 ignoring parse error: Truncated value at line 2, column 25
89 ignoring parse error: Truncated value at line 3, column 1
90 EOF
91 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
92 cmp $d/out $d/expected
93
94 # Note that here jq sees no inputs at all but it still succeeds because
95 # --seq ignores parse errors
96 cat > $d/expected <<EOF
97 ignoring parse error: Unfinished abandoned text at EOF at line 1, column 4
98 EOF
99 printf '"foo' | $JQ -ce --seq . > $d/out 2>&1
100 cmp $d/out $d/expected
101
102 # Numeric values truncated by EOF are ignored
103 cat > $d/expected <<EOF
104 ignoring parse error: Unfinished abandoned text at EOF at line 1, column 1
105 EOF
106 printf '1' | $JQ -ce --seq . > $d/out 2>&1
107 cmp $d/out $d/expected
108
109 cat > $d/expected <<EOF
110 jq: error (at <stdin>:1): Unfinished abandoned text at EOF at line 2, column 0
111 EOF
112 if printf '1\n' | $JQ -cen --seq '[inputs] == []' >/dev/null 2> $d/out; then
113 printf 'Error expected but jq exited successfully\n' 1>&2
114 exit 2
115 fi
116 cmp $d/out $d/expected
117
118 # Regression test for #951
119 printf '"a\n' > $d/input
120 if $VALGRIND $Q $JQ -e . $d/input; then
121 printf 'Issue #951 is back?\n' 1>&2
122 exit 2
123 fi
124
125 # Regression test for #1534
126 echo "[1,2,3,4]" > $d/expected
127 printf "[1,2][3,4]" | $JQ -cs add > $d/out 2>&1
128 cmp $d/out $d/expected
129 printf "[1,2][3,4]\n" | $JQ -cs add > $d/out 2>&1
130 cmp $d/out $d/expected
131
132
133 ## Test streaming parser
134
135 ## If we add an option to stream to the `import ... as $symbol;` directive
136 ## then we can move these tests into tests/all.test.
137 $VALGRIND $Q $JQ -c '. as $d|path(..) as $p|$d|getpath($p)|scalars_or_empty|[$p,.]' < "$JQTESTDIR/torture/input0.json" > $d/out0
138 $VALGRIND $Q $JQ --stream -c '.|select(length==2)' < "$JQTESTDIR/torture/input0.json" > $d/out1
139 diff $d/out0 $d/out1
140
141 ## XXX This test can be moved to tests/all.test _now_
142 clean=false
143 if which seq > /dev/null 2>&1; then
144 # XXX We should try every prefix of input0.json, but that makes this
145 # test very, very slow when run with valgrind, and the whole point
146 # is to run it with valgrind.
147 #
148 #len=$(wc -c < "$JQTESTDIR/torture/input0.json")
149 if [ -z "$VALGRIND" ]; then
150 start=1
151 end=$(wc -c < "$JQTESTDIR/torture/input0.json")
152 else
153 start=120
154 end=151
155 fi
156 for i in $(seq $start $end); do
157 dd "if=tests/torture/input0.json" bs=$i count=1 2>/dev/null |
158 $VALGRIND $JQ -c . > $d/out0 2>$d/err || true
159 if [ -n "$VALGRIND" ]; then
160 grep '^==[0-9][0-9]*== ERROR SUMMARY: 0 errors' $d/err > /dev/null
161 else
162 tail -1 $d/err | egrep -i 'assert|abort|core' && false
163 fi
164
165 dd "if=tests/torture/input0.json" bs=$i count=1 2>/dev/null |
166 $VALGRIND $JQ -cn --stream 'fromstream(inputs)' > $d/out1 2>$d/err || true
167 if [ -n "$VALGRIND" ]; then
168 grep '^==[0-9][0-9]*== ERROR SUMMARY: 0 errors' $d/err > /dev/null
169 else
170 tail -1 $d/err | egrep -i 'assert|abort|core' && false
171 fi
172
173 diff $d/out0 $d/out1
174 done
175 else
176 echo "Not doing torture tests"
177 fi
178
179 ## Fuzz parser
180
181 ## XXX With a $(urandom) builtin we could move this test into tests/all.test
182 clean=false
183 if dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null; then
184 # Have a /dev/urandom, good
185 $VALGRIND $Q $JQ --seq . $d/rand >/dev/null 2>&1
186 $VALGRIND $Q $JQ --seq --stream . $d/rand >/dev/null 2>&1
187 dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null
188 $VALGRIND $Q $JQ --seq . $d/rand >/dev/null 2>&1
189 $VALGRIND $Q $JQ --seq --stream . $d/rand >/dev/null 2>&1
190 dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null
191 $VALGRIND $Q $JQ --seq . $d/rand >/dev/null 2>&1
192 $VALGRIND $Q $JQ --seq --stream . $d/rand >/dev/null 2>&1
193 fi
194 clean=true
195
196 ## Test library/module system
197
198 # Check handling of ~/.jq; these can't move into jq_test.c yet because
199 # they depend on $HOME
200 if [ "$(HOME="$mods" $VALGRIND $Q $JQ -nr fg)" != foobar ]; then
201 echo "Bug #479 appears to be back" 1>&2
202 exit 1
203 fi
204
205 if [ $(HOME="$mods" $VALGRIND $Q $JQ --debug-dump-disasm -n fg | grep '^[a-z]' | wc -l) -gt 3 ]; then
206 echo "Binding too many defs into program" 1>&2
207 exit 1
208 fi
209
210 cd "$JQBASEDIR" # so that relative library paths are guaranteed correct
211 if ! $VALGRIND $Q $JQ -L ./tests/modules -ne 'import "test_bind_order" as check; check::check==true'; then
212 echo "Issue #817 regression?" 1>&2
213 exit 1
214 fi
215
216 cd "$JQBASEDIR"
217 if ! $VALGRIND $Q $JQ -L tests/modules -ne 'import "test_bind_order" as check; check::check==true'; then
218 echo "Issue #817 regression?" 1>&2
219 exit 1
220 fi
221
222 ## Halt
223
224 if ! $VALGRIND $Q $JQ -n halt; then
225 echo "jq halt didn't work as expected" 1>&2
226 exit 1
227 fi
228 if $VALGRIND $Q $VG_EXIT0 $JQ -n 'halt_error(1)'; then
229 echo "jq halt_error(1) didn't work as expected" 1>&2
230 exit 1
231 elif [ $? -ne 1 ]; then
232 echo "jq halt_error(1) had wrong error code" 1>&2
233 exit 1
234 fi
235 if $VALGRIND $Q $VG_EXIT0 $JQ -n 'halt_error(11)'; then
236 echo "jq halt_error(11) didn't work as expected" 1>&2
237 exit 1
238 elif [ $? -ne 11 ]; then
239 echo "jq halt_error(11) had wrong error code" 1>&2
240 exit 1
241 fi
242 if [ -n "$($VALGRIND $Q $JQ -n 'halt_error(1)' 2>&1)" ]; then
243 echo "jq halt_error(1) had unexpected output" 1>&2
244 exit 1
245 fi
246 if [ -n "$($VALGRIND $Q $JQ -n '"xyz\n"|halt_error(1)' 2>/dev/null)" ]; then
247 echo "jq halt_error(1) had unexpected output on stdout" 1>&2
248 exit 1
249 fi
250 if [ "$($VALGRIND $Q $JQ -n '"xyz\n"|halt_error(1)' 2>&1)" != xyz ]; then
251 echo "jq halt_error(1) had unexpected output" 1>&2
252 exit 1
253 fi
254
255 # Check $JQ_COLORS
256 $JQ -Ccn . > $d/color
257 printf '\033[1;30mnull\033[0m\n' > $d/expect
258 cmp $d/color $d/expect
259 JQ_COLORS='4;31' $JQ -Ccn . > $d/color
260 printf '\033[4;31mnull\033[0m\n' > $d/expect
261 cmp $d/color $d/expect
262 JQ_COLORS='1;30:0;31:0;32:0;33:0;34:1;35:1;36' \
263 $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color
264 (
265 printf '\033[1;35m[\033[1;36m{'
266 printf '\033[0m\033[34;1m"a"\033['
267 printf '0m\033[1;36m:\033[0m\033['
268 printf '0;32mtrue\033[0m\033[1'
269 printf ';36m,\033[0m\033[34;1m'
270 printf '"b"\033[0m\033[1;36m:\033'
271 printf '[0m\033[0;31mfalse\033'
272 printf '[0m\033[1;36m\033[1;36'
273 printf 'm}\033[0m\033[1;35m,\033['
274 printf '0;33m123\033[0m\033[1;'
275 printf '35m,\033[1;30mnull\033'
276 printf '[0m\033[1;35m\033[1;35'
277 printf 'm]\033[0m\n'
278 ) > $d/expect
279 cmp $d/color $d/expect
280
281 # Check garbage in JQ_COLORS. We write each color sequence into a 16
282 # char buffer that needs to hold ESC [ <color> m NUL, so each color
283 # sequence can be no more than 12 chars (bytes). These emit a warning
284 # on stderr.
285 set -vx
286 echo 'Failed to set $JQ_COLORS' > $d/expect_warning
287 $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/expect
288 JQ_COLORS='garbage;30:*;31:,;3^:0;$%:0;34:1;35:1;36' \
289 $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning
290 cmp $d/color $d/expect
291 cmp $d/warning $d/expect_warning
292 JQ_COLORS='1234567890123456789;30:0;31:0;32:0;33:0;34:1;35:1;36' \
293 $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning
294 cmp $d/color $d/expect
295 cmp $d/warning $d/expect_warning
296 JQ_COLORS='1;31234567890123456789:0;31:0;32:0;33:0;34:1;35:1;36' \
297 $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning
298 cmp $d/color $d/expect
299 cmp $d/warning $d/expect_warning
300 JQ_COLORS='1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456' \
301 $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning
302 cmp $d/color $d/expect
303 cmp $d/warning $d/expect_warning
304 JQ_COLORS="0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:" \
305 $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning
306 cmp $d/color $d/expect
307 cmp $d/warning $d/expect_warning
308
309 exit 0