Debian Codegraph @

tests/shtest

raw
1 #!/bin/sh -x
2
3 . "${0%/*}/setup" "$@"
4
5 msys=false
6 mingw=false
7 sunos=false
8 case "$(uname -s)" in
9 MSYS*) msys=true;;
10 MINGW*) mingw=true;;
11 SunOS*) sunos=true;;
12 esac
13
14 JQ_NO_B=$JQ
15 JQ="$JQ -b"
16
17 # On Solaris, GNU tools are available in /usr/gnu. This script should prefer
18 # them over the native tools, because the native versions may lack some
19 # command-line options the script relies on.
20 $sunos && PATH="/usr/gnu/bin:$PATH"
21
22 PATH=$JQBASEDIR:$PATH $JQBASEDIR/tests/jq-f-test.sh > /dev/null
23
24 SHELL=/bin/sh
25 export SHELL
26
27 unset JQ_COLORS
28 unset NO_COLOR
29
30 if [ -f "$JQBASEDIR/.libs/libinject_errors.so" ]; then
31 # Do some simple error injection tests to check that we're handling
32 # I/O errors correctly.
33 (
34 libinject=$JQBASEDIR/.libs/libinject_errors.so
35 cd $d
36 LD_PRELOAD=$libinject $JQ . /dev/null
37 touch fail_read
38 LD_PRELOAD=$libinject $JQ . fail_read && exit 2
39 touch fail_close
40 LD_PRELOAD=$libinject $JQ . fail_close && exit 2
41 true
42 )
43 fi
44
45 printf 'a\0b\nc\0d\ne' > $d/input
46 $VALGRIND $Q $JQ -Rse '. == "a\u0000b\nc\u0000d\ne"' $d/input
47 $VALGRIND $Q $JQ -Rne '[inputs] == ["a\u0000b", "c\u0000d", "e"]' $d/input
48
49 ## Test constant folding
50 nref=$($VALGRIND $Q $JQ -n --debug-dump-disasm '"foo"' | wc -l)
51 for exp in '1+1' '1-1' '2*3' '9/3' '9%3' '9==3' '9!=3' \
52 '9<3' '9>3' '9<=3' '9>=3' '1+2*3-4/5%6' '"foo" + "bar"'; do
53 n=$($VALGRIND $Q $JQ -n --debug-dump-disasm "$exp" | wc -l)
54 if [ $n -ne $nref ]; then
55 echo "Constant expression folding didn't work: $exp"
56 exit 1
57 fi
58 done
59
60 ## Test JSON sequence support
61 cat > $d/expected <<EOF
62 jq: ignoring parse error: Truncated value at line 2, column 5
63 jq: ignoring parse error: Truncated value at line 2, column 25
64 jq: ignoring parse error: Truncated value at line 2, column 41
65 EOF
66 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
67 cmp $d/out $d/expected
68
69 cat > $d/expected <<EOF
70 jq: ignoring parse error: Truncated value at line 2, column 5
71 jq: ignoring parse error: Truncated value at line 2, column 25
72 jq: ignoring parse error: Truncated value at line 3, column 1
73 EOF
74 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
75 cmp $d/out $d/expected
76
77 # Note that here jq sees no inputs at all but it still succeeds because
78 # --seq ignores parse errors
79 cat > $d/expected <<EOF
80 jq: ignoring parse error: Unfinished abandoned text at EOF at line 1, column 4
81 EOF
82 printf '"foo' | $JQ -c --seq . > $d/out 2>&1
83 cmp $d/out $d/expected
84
85 # with -e option should give 4 here as there's no valid output after
86 # ignoring parse errors with --seq.
87 printf '"foo' | $JQ -ce --seq . > $d/out 2>&1 || ret=$?
88 [ $ret -eq 4 ]
89 cmp $d/out $d/expected
90
91 # Numeric values truncated by EOF are ignored
92 cat > $d/expected <<EOF
93 jq: ignoring parse error: Unfinished abandoned text at EOF at line 1, column 1
94 EOF
95 printf '1' | $JQ -c --seq . > $d/out 2>&1
96 cmp $d/out $d/expected
97
98 cat > $d/expected <<EOF
99 jq: error (at <stdin>:1): Unfinished abandoned text at EOF at line 2, column 0
100 EOF
101 if printf '1\n' | $JQ -cen --seq '[inputs] == []' >/dev/null 2> $d/out; then
102 printf 'Error expected but jq exited successfully\n' 1>&2
103 exit 2
104 fi
105 cmp $d/out $d/expected
106
107 # Test control characters for #2909
108 cat > $d/expected <<EOF
109 jq: parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 1, column 3
110 EOF
111 for i in 1 2 30 31; do
112 if printf "\"$(printf '\\%03o' $i)\"" | $JQ '.' > $d/out 2>&1; then
113 printf 'Error expected but jq exited successfully\n' 1>&2
114 exit 2
115 fi
116 cmp $d/out $d/expected
117 done
118 printf '" ~\\u007f"\n' > $d/expected
119 printf "\"$(printf '\\%03o' 32 126 127)\"" | $JQ '.' > $d/out 2>&1
120 cmp $d/out $d/expected
121
122 ## Test --exit-status
123 data='{"i": 1}\n{"i": 2}\n{"i": 3}\n'
124 printf "$data" | $JQ --exit-status 'select(.i==1)' > /dev/null 2>&1
125 printf "$data" | $JQ --exit-status 'select(.i==2)' > /dev/null 2>&1
126 printf "$data" | $JQ --exit-status 'select(.i==3)' > /dev/null 2>&1
127 ret=0
128 printf "$data" | $JQ --exit-status 'select(.i==4)' > /dev/null 2>&1 || ret=$?
129 [ $ret -eq 4 ]
130 ret=0
131 printf "$data" | $JQ --exit-status 'select(.i==2) | false' > /dev/null 2>&1 || ret=$?
132 [ $ret -eq 1 ]
133 printf "$data" | $JQ --exit-status 'select(.i==2) | true' > /dev/null 2>&1
134
135
136 # Regression test for #951
137 printf '"a\n' > $d/input
138 if $VALGRIND $Q $JQ -e . $d/input; then
139 printf 'Issue #951 is back?\n' 1>&2
140 exit 2
141 fi
142
143 # Regression test for #2146
144 if echo "foobar" | $JQ .; then
145 printf 'Issue #2146 is back?\n' 1>&2
146 exit 1
147 elif [ $? -ne 5 ]; then
148 echo "Invalid input had wrong error code" 1>&2
149 exit 1
150 fi
151
152 # Regression test for #2367; make sure to call jq twice
153 if ! echo '{"a": 1E9999999999}' | $JQ . | $JQ -e .a; then
154 printf 'Issue #2367 is back?\n' 1>&2
155 exit 1
156 fi
157
158 # Regression test for #1534
159 echo "[1,2,3,4]" > $d/expected
160 printf "[1,2][3,4]" | $JQ -cs add > $d/out 2>&1
161 cmp $d/out $d/expected
162 printf "[1,2][3,4]\n" | $JQ -cs add > $d/out 2>&1
163 cmp $d/out $d/expected
164
165 # Regression test for #3273
166 echo "[[[0],1],[[0]],[[0],2],[[0]]]" > $d/expected
167 printf "[1][2]" | $JQ -c -s --stream . > $d/out 2>&1
168 cmp $d/out $d/expected
169 printf "[1][2]\n" | $JQ -c -s --stream . > $d/out 2>&1
170 cmp $d/out $d/expected
171
172 # Regression test for --raw-output0
173 printf "a\0b\0" > $d/expected
174 printf '["a", "b"]' | $VALGRIND $Q $JQ --raw-output0 '.[]' > $d/out
175 cmp $d/out $d/expected
176 printf "a\0" > $d/expected
177 if printf '["a", "c\\u0000d", "b"]' | $VALGRIND $Q $JQ --raw-output0 '.[]' > $d/out; then
178 echo "Should exit error on string containing NUL with --raw-output0" 1>&2
179 exit 1
180 elif [ $? -ne 5 ]; then
181 echo "Invalid error code" 1>&2
182 exit 1
183 else
184 cmp $d/out $d/expected
185 fi
186
187 # Regression tests for #3194
188 echo 42 > $d/expected
189 $JQ -nn 42 > $d/out 2>&1
190 cmp $d/out $d/expected
191
192 $JQ -nL. 42 > $d/out 2>&1
193 cmp $d/out $d/expected
194 $JQ -nL . 42 > $d/out 2>&1
195 cmp $d/out $d/expected
196
197 $JQ -h > $d/expected 2>&1
198 $JQ -hV > $d/out 2>&1
199 cmp $d/out $d/expected
200 $JQ -h -V > $d/out 2>&1
201 cmp $d/out $d/expected
202
203 $JQ -V > $d/expected 2>&1
204 $JQ -Vh > $d/out 2>&1
205 cmp $d/out $d/expected
206 $JQ -V -h > $d/out 2>&1
207 cmp $d/out $d/expected
208
209 # CRLF line break support #1219
210 if ! x=$($JQ -n "$(printf '1\r\n+\r\n2')") || [ "$x" != '3' ]; then
211 echo 'CRLF line break support failed?' 1>&2
212 exit 1
213 fi
214
215 ## Test streaming parser
216
217 ## If we add an option to stream to the `import ... as $symbol;` directive
218 ## then we can move these tests into tests/all.test.
219 $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
220 $VALGRIND $Q $JQ --stream -c '.|select(length==2)' < "$JQTESTDIR/torture/input0.json" > $d/out1
221 diff $d/out0 $d/out1
222
223 printf '["Unfinished JSON term at EOF at line 1, column 1",[0]]\n' > $d/expected
224 printf '[' | $VALGRIND $Q $JQ --stream-errors -c . > $d/out 2>&1
225 diff $d/out $d/expected
226
227
228 ## XXX This test can be moved to tests/all.test _now_
229 clean=false
230 if which seq > /dev/null 2>&1; then
231 # XXX We should try every prefix of input0.json, but that makes this
232 # test very, very slow when run with valgrind, and the whole point
233 # is to run it with valgrind.
234 #
235 #len=$(wc -c < "$JQTESTDIR/torture/input0.json")
236 if [ -z "$VALGRIND" ]; then
237 start=1
238 end=$(wc -c < "$JQTESTDIR/torture/input0.json")
239 else
240 start=120
241 end=151
242 fi
243 for i in $(seq $start $end); do
244 dd "if=tests/torture/input0.json" bs=$i count=1 2>/dev/null |
245 $VALGRIND $JQ -c . > $d/out0 2>$d/err || true
246 if [ -n "$VALGRIND" ]; then
247 grep '^==[0-9][0-9]*== ERROR SUMMARY: 0 errors' $d/err > /dev/null
248 else
249 tail -n1 -- "$d/err" | grep -Ei 'assert|abort|core' && false
250 fi
251
252 dd "if=tests/torture/input0.json" bs=$i count=1 2>/dev/null |
253 $VALGRIND $JQ -cn --stream 'fromstream(inputs)' > $d/out1 2>$d/err || true
254 if [ -n "$VALGRIND" ]; then
255 grep '^==[0-9][0-9]*== ERROR SUMMARY: 0 errors' $d/err > /dev/null
256 else
257 tail -n1 -- "$d/err" | grep -Ei 'assert|abort|core' && false
258 fi
259
260 diff $d/out0 $d/out1
261 done
262 else
263 echo "Not doing torture tests"
264 fi
265
266 ## Regression test for issue #2378 assert when stream parse broken object pair
267 echo '{"a":1,"b",' | $JQ --stream > /dev/null 2> $d/err || true
268 grep 'Objects must consist of key:value pairs' $d/err > /dev/null
269
270 ## Regression tests for issue #2463 assert when stream parse non-scalar object key
271 echo '{{"a":"b"}}' | $JQ --stream > /dev/null 2> $d/err || true
272 grep "Expected string key after '{', not '{'" $d/err > /dev/null
273 echo '{"x":"y",{"a":"b"}}' | $JQ --stream > /dev/null 2> $d/err || true
274 grep "Expected string key after ',' in object, not '{'" $d/err > /dev/null
275 echo '{["a","b"]}' | $JQ --stream > /dev/null 2> $d/err || true
276 grep "Expected string key after '{', not '\\['" $d/err > /dev/null
277 echo '{"x":"y",["a","b"]}' | $JQ --stream > /dev/null 2> $d/err || true
278 grep "Expected string key after ',' in object, not '\\['" $d/err > /dev/null
279
280 # debug, stderr
281 $VALGRIND $Q $JQ -n '"test", {} | debug, stderr' >/dev/null
282 $JQ -n -c -j '"hello\nworld", null, [false, 0], {"foo":["bar"]}, "\n" | stderr' >$d/out 2>$d/err
283 cat > $d/expected <<'EOF'
284 hello
285 worldnull[false,0]{"foo":["bar"]}
286 EOF
287 cmp $d/out $d/expected
288 cmp $d/err $d/expected
289
290 # --arg, --argjson, $ARGS.named
291 $VALGRIND $JQ -n -c --arg foo 1 --argjson bar 2 '{$foo, $bar} | ., . == $ARGS.named' > $d/out
292 printf '{"foo":"1","bar":2}\ntrue\n' > $d/expected
293 cmp $d/out $d/expected
294
295 # --slurpfile, --rawfile
296 $VALGRIND $JQ -n --slurpfile foo $JQBASEDIR/tests/modules/data.json \
297 --rawfile bar $JQBASEDIR/tests/modules/data.json '{$foo, $bar}' > $d/out
298 cat > $d/expected <<'EOF'
299 {
300 "foo": [
301 {
302 "this": "is a test",
303 "that": "is too"
304 }
305 ],
306 "bar": "{\n \"this\": \"is a test\",\n \"that\": \"is too\"\n}\n"
307 }
308 EOF
309 cmp $d/out $d/expected
310
311 # --args, --jsonargs, $ARGS.positional
312 $VALGRIND $JQ -n -c --args '$ARGS.positional' foo bar baz > $d/out
313 printf '["foo","bar","baz"]\n' > $d/expected
314 cmp $d/out $d/expected
315 $VALGRIND $JQ -n -c --jsonargs '$ARGS.positional' null true '[]' '{}' > $d/out
316 printf '[null,true,[],{}]\n' > $d/expected
317 cmp $d/out $d/expected
318 $VALGRIND $JQ -n -c '$ARGS.positional' --args foo 1 --jsonargs 2 '{}' --args 3 4 > $d/out
319 printf '["foo","1",2,{},"3","4"]\n' > $d/expected
320 cmp $d/out $d/expected
321 $VALGRIND $JQ -n -c '$ARGS.positional' --args --jsonargs > $d/out
322 printf '[]\n' > $d/expected
323 cmp $d/out $d/expected
324
325 ## Regression test for issue #2572 assert when using --jsonargs and invalid JSON
326 $VALGRIND $JQ -n --jsonargs null invalid && EC=$? || EC=$?
327 if [ "$EC" -ne 2 ]; then
328 echo "--jsonargs exited with wrong exit code, expected 2 got $EC" 1>&2
329 exit 1
330 fi
331 # this tests the args_done code path "--"
332 $VALGRIND $JQ -n --jsonargs null -- invalid && EC=$? || EC=$?
333 if [ "$EC" -ne 2 ]; then
334 echo "--jsonargs exited with wrong exit code, expected 2 got $EC" 1>&2
335 exit 1
336 fi
337
338 ## Fuzz parser
339
340 ## XXX With a $(urandom) builtin we could move this test into tests/all.test
341 clean=false
342 if dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null; then
343 # Have a /dev/urandom, good
344 $VALGRIND $Q $JQ --seq . $d/rand >/dev/null 2>&1
345 $VALGRIND $Q $JQ --seq --stream . $d/rand >/dev/null 2>&1
346 dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null
347 $VALGRIND $Q $JQ --seq . $d/rand >/dev/null 2>&1
348 $VALGRIND $Q $JQ --seq --stream . $d/rand >/dev/null 2>&1
349 dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null
350 $VALGRIND $Q $JQ --seq . $d/rand >/dev/null 2>&1
351 $VALGRIND $Q $JQ --seq --stream . $d/rand >/dev/null 2>&1
352 fi
353 clean=true
354
355 ## Test library/module system
356
357 # Check handling of ~/.jq; these can't move into jq_test.c yet because
358 # they depend on $HOME
359 if [ "$(HOME="$mods/home1" $VALGRIND $Q $JQ -nr fg)" != foobar ]; then
360 echo "Bug #479 appears to be back" 1>&2
361 exit 1
362 fi
363
364 if $msys || $mingw; then
365 HOME_BAK=$HOME
366 unset HOME
367 if [ "$(USERPROFILE="$mods/home1" $VALGRIND $Q $JQ -nr foo)" != baz ]; then
368 echo "Bug #3104 regressed (sourcing %USERPROFILE%/.jq on Windows)" 1>&2
369 exit 1
370 fi
371 export HOME=$HOME_BAK
372 unset HOME_BAK
373 fi
374
375 if [ $(HOME="$mods/home1" $VALGRIND $Q $JQ --debug-dump-disasm -n fg | grep '^[a-z]' | wc -l) -ne 3 ]; then
376 echo "Binding too many defs into program" 1>&2
377 exit 1
378 fi
379
380 if ! HOME="$mods/home2" $VALGRIND $Q $JQ -n 'include "g"; empty'; then
381 echo "Mishandling directory ~/.jq" 1>&2
382 exit 1
383 fi
384
385 (
386 cd "$JQBASEDIR" # so that relative library paths are guaranteed correct
387 if ! $VALGRIND $Q $JQ -L ./tests/modules -ne 'import "test_bind_order" as check; check::check==true'; then
388 echo "Issue #817 regression?" 1>&2
389 exit 1
390 fi
391 )
392
393 (
394 cd "$JQBASEDIR"
395 if ! $VALGRIND $Q $JQ -L tests/modules -ne 'import "test_bind_order" as check; check::check==true'; then
396 echo "Issue #817 regression?" 1>&2
397 exit 1
398 fi
399 )
400
401 # CVE-2026-44777: Circular imports should be detected
402 if $VALGRIND $JQ -L "$mods" -ne 'import "cycle_a" as a; null' 2> $d/out; then
403 echo "Mutual import should be rejected" 1>&2
404 exit 1
405 fi
406 if ! grep -q "circular import" $d/out; then
407 echo "Expected circular import error" 1>&2
408 exit 1
409 fi
410
411 if $VALGRIND $JQ -L "$mods" -ne 'import "cycle_self" as s; null' 2> $d/out; then
412 echo "Self import should be rejected" 1>&2
413 exit 1
414 fi
415 if ! grep -q "circular import" $d/out; then
416 echo "Expected circular import error" 1>&2
417 exit 1
418 fi
419
420 ## Halt
421
422 if ! $VALGRIND $Q $JQ -n halt; then
423 echo "jq halt didn't work as expected" 1>&2
424 exit 1
425 fi
426 if $VALGRIND $Q $VG_EXIT0 $JQ -n 'halt_error(1)'; then
427 echo "jq halt_error(1) didn't work as expected" 1>&2
428 exit 1
429 elif [ $? -ne 1 ]; then
430 echo "jq halt_error(1) had wrong error code" 1>&2
431 exit 1
432 fi
433 if $VALGRIND $Q $VG_EXIT0 $JQ -n 'halt_error(11)'; then
434 echo "jq halt_error(11) didn't work as expected" 1>&2
435 exit 1
436 elif [ $? -ne 11 ]; then
437 echo "jq halt_error(11) had wrong error code" 1>&2
438 exit 1
439 fi
440 if [ -n "$($VALGRIND $Q $JQ -n 'halt_error(1)' 2>&1)" ]; then
441 echo "jq halt_error(1) had unexpected output" 1>&2
442 exit 1
443 fi
444 if [ -n "$($VALGRIND $Q $JQ -n '"xyz\n" | halt_error(1)' 2>/dev/null)" ]; then
445 echo "jq halt_error(1) had unexpected output on stdout" 1>&2
446 exit 1
447 fi
448 if [ "$($VALGRIND $Q $JQ -n '"xy" | halt_error(1)' 2>&1 || echo "z")" != "xyz" ]; then
449 echo "jq halt_error(1) had unexpected output" 1>&2
450 exit 1
451 fi
452 if [ "$($VALGRIND $Q $JQ -n '"x\u0000y\u0000z" | halt_error(1)' 2>&1 | tr '\0' '.')" != "x.y.z" ]; then
453 echo "jq halt_error(1) had unexpected output" 1>&2
454 exit 1
455 fi
456 if [ "$($VALGRIND $Q $JQ -n '{"a":"xyz"} | halt_error(1)' 2>&1)" != '{"a":"xyz"}' ]; then
457 echo "jq halt_error(1) had unexpected output" 1>&2
458 exit 1
459 fi
460
461 # Check $JQ_COLORS
462
463 ## Default colors, null input
464 $JQ -Ccn . > $d/color
465 printf '\033[0;90mnull\033[0m\n' > $d/expect
466 cmp $d/color $d/expect
467
468 ## Set non-default color, null input
469 JQ_COLORS='4;31' $JQ -Ccn . > $d/color
470 printf '\033[4;31mnull\033[0m\n' > $d/expect
471 cmp $d/color $d/expect
472
473 ## Set implicit empty color, null input
474 $JQ -Ccn . > $d/color
475 printf '\033[0;90mnull\033[0m\n' > $d/expect
476 cmp $d/color $d/expect
477
478 ## Set explicit empty color, null input
479 JQ_COLORS=':' $JQ -Ccn . > $d/color
480 printf '\033[mnull\033[0m\n' > $d/expect
481 cmp $d/color $d/expect
482
483 ## Extra colors, null input
484 JQ_COLORS='::::::::' $JQ -Ccn . > $d/color
485 printf '\033[mnull\033[0m\n' > $d/expect
486 cmp $d/color $d/expect
487
488 ## Default colors, complex input
489 $JQ -Ccn '[{"a":true,"b":false},"abc",123,null]' > $d/color
490 {
491 printf '\033[1;39m[\033[0m'
492 printf '\033[1;39m{\033[0m'
493 printf '\033[1;34m"a"\033[0m'
494 printf '\033[1;39m:\033[0m'
495 printf '\033[0;39mtrue\033[0m'
496 printf '\033[1;39m,\033[0m'
497 printf '\033[1;34m"b"\033[0m'
498 printf '\033[1;39m:\033[0m'
499 printf '\033[0;39mfalse\033[0m'
500 printf '\033[1;39m}\033[0m'
501 printf '\033[1;39m,\033[0m'
502 printf '\033[0;32m"abc"\033[0m'
503 printf '\033[1;39m,\033[0m'
504 printf '\033[0;39m123\033[0m'
505 printf '\033[1;39m,\033[0m'
506 printf '\033[0;90mnull\033[0m'
507 printf '\033[1;39m]\033[0m\n'
508 } > $d/expect
509 cmp $d/color $d/expect
510
511 ## Set non-default colors, complex input
512 JQ_COLORS='0;30:0;31:0;32:0;33:0;34:1;35:1;36:1;37' \
513 $JQ -Ccn '[{"a":true,"b":false},"abc",123,null]' > $d/color
514 {
515 printf '\033[1;35m[\033[0m'
516 printf '\033[1;36m{\033[0m'
517 printf '\033[1;37m"a"\033[0m'
518 printf '\033[1;36m:\033[0m'
519 printf '\033[0;32mtrue\033[0m'
520 printf '\033[1;36m,\033[0m'
521 printf '\033[1;37m"b"\033[0m'
522 printf '\033[1;36m:\033[0m'
523 printf '\033[0;31mfalse\033[0m'
524 printf '\033[1;36m}\033[0m'
525 printf '\033[1;35m,\033[0m'
526 printf '\033[0;34m"abc"\033[0m'
527 printf '\033[1;35m,\033[0m'
528 printf '\033[0;33m123\033[0m'
529 printf '\033[1;35m,\033[0m'
530 printf '\033[0;30mnull\033[0m'
531 printf '\033[1;35m]\033[0m\n'
532 } > $d/expect
533 cmp $d/color $d/expect
534
535 ## Set non-default colors only for literals, complex input
536 JQ_COLORS='0;37:0;31:0;35:0;34:0;36' \
537 $JQ -Ccn '[{"a":true,"b":false},"abc",123,null]' > $d/color
538 {
539 printf '\033[1;39m[\033[0m'
540 printf '\033[1;39m{\033[0m'
541 printf '\033[1;34m"a"\033[0m'
542 printf '\033[1;39m:\033[0m'
543 printf '\033[0;35mtrue\033[0m'
544 printf '\033[1;39m,\033[0m'
545 printf '\033[1;34m"b"\033[0m'
546 printf '\033[1;39m:\033[0m'
547 printf '\033[0;31mfalse\033[0m'
548 printf '\033[1;39m}\033[0m'
549 printf '\033[1;39m,\033[0m'
550 printf '\033[0;36m"abc"\033[0m'
551 printf '\033[1;39m,\033[0m'
552 printf '\033[0;34m123\033[0m'
553 printf '\033[1;39m,\033[0m'
554 printf '\033[0;37mnull\033[0m'
555 printf '\033[1;39m]\033[0m\n'
556 } > $d/expect
557 cmp $d/color $d/expect
558
559 ## Default colors, complex input, indented
560 $JQ -Cn '[{"a":true,"b":false},"abc",123,null]' > $d/color
561 {
562 printf '\033[1;39m[\033[0m\n'
563 printf ' \033[1;39m{\033[0m\n'
564 printf ' \033[1;34m"a"\033[0m'
565 printf '\033[1;39m:\033[0m '
566 printf '\033[0;39mtrue\033[0m'
567 printf '\033[1;39m,\033[0m\n'
568 printf ' \033[1;34m"b"\033[0m'
569 printf '\033[1;39m:\033[0m '
570 printf '\033[0;39mfalse\033[0m\n'
571 printf ' \033[1;39m}\033[0m'
572 printf '\033[1;39m,\033[0m\n'
573 printf ' \033[0;32m"abc"\033[0m'
574 printf '\033[1;39m,\033[0m\n'
575 printf ' \033[0;39m123\033[0m'
576 printf '\033[1;39m,\033[0m\n'
577 printf ' \033[0;90mnull\033[0m\n'
578 printf '\033[1;39m]\033[0m\n'
579 } > $d/expect
580 cmp $d/color $d/expect
581
582 ## Set non-default colors, complex input, indented
583 JQ_COLORS='0;30:0;31:0;32:0;33:0;34:1;35:1;36:1;37' \
584 $JQ -Cn '[{"a":true,"b":false},"abc",123,null]' > $d/color
585 {
586 printf '\033[1;35m[\033[0m\n'
587 printf ' \033[1;36m{\033[0m\n'
588 printf ' \033[1;37m"a"\033[0m'
589 printf '\033[1;36m:\033[0m '
590 printf '\033[0;32mtrue\033[0m'
591 printf '\033[1;36m,\033[0m\n'
592 printf ' \033[1;37m"b"\033[0m'
593 printf '\033[1;36m:\033[0m '
594 printf '\033[0;31mfalse\033[0m\n'
595 printf ' \033[1;36m}\033[0m'
596 printf '\033[1;35m,\033[0m\n'
597 printf ' \033[0;34m"abc"\033[0m'
598 printf '\033[1;35m,\033[0m\n'
599 printf ' \033[0;33m123\033[0m'
600 printf '\033[1;35m,\033[0m\n'
601 printf ' \033[0;30mnull\033[0m\n'
602 printf '\033[1;35m]\033[0m\n'
603 } > $d/expect
604 cmp $d/color $d/expect
605
606 ## Set truecolor, complex input, indented
607 JQ_COLORS=$(printf '38;2;%s:' \
608 '255;173;173' '255;214;165' '253;255;182' '202;255;191' \
609 '155;246;255' '160;196;255' '189;178;255' '255;198;255') \
610 $JQ -Cn '[{"a":true,"b":false},"abc",123,null]' > $d/color
611 {
612 printf '\033[38;2;160;196;255m[\033[0m\n'
613 printf ' \033[38;2;189;178;255m{\033[0m\n'
614 printf ' \033[38;2;255;198;255m"a"\033[0m'
615 printf '\033[38;2;189;178;255m:\033[0m '
616 printf '\033[38;2;253;255;182mtrue\033[0m'
617 printf '\033[38;2;189;178;255m,\033[0m\n'
618 printf ' \033[38;2;255;198;255m"b"\033[0m'
619 printf '\033[38;2;189;178;255m:\033[0m '
620 printf '\033[38;2;255;214;165mfalse\033[0m\n'
621 printf ' \033[38;2;189;178;255m}\033[0m'
622 printf '\033[38;2;160;196;255m,\033[0m\n'
623 printf ' \033[38;2;155;246;255m"abc"\033[0m'
624 printf '\033[38;2;160;196;255m,\033[0m\n'
625 printf ' \033[38;2;202;255;191m123\033[0m'
626 printf '\033[38;2;160;196;255m,\033[0m\n'
627 printf ' \033[38;2;255;173;173mnull\033[0m\n'
628 printf '\033[38;2;160;196;255m]\033[0m\n'
629 } > $d/expect
630 cmp $d/color $d/expect
631
632 # Check invalid JQ_COLORS
633 echo 'Failed to set $JQ_COLORS' > $d/expect_warning
634 $JQ -Ccn '[{"a":true,"b":false},"abc",123,null]' > $d/expect
635 for colors in '/' '[30' '30m' '30:31m:32' '30:*:31' 'invalid'; do
636 export JQ_COLORS=$colors
637 $JQ -Ccn '[{"a":true,"b":false},"abc",123,null]' > $d/color 2>$d/warning
638 cmp $d/color $d/expect
639 cmp $d/warning $d/expect_warning
640 done
641 unset JQ_COLORS
642
643 # Check $NO_COLOR
644 test_no_color=true
645 $msys && test_no_color=false
646 $mingw && test_no_color=false
647 $sunos && test_no_color=false
648 if $test_no_color && command -v script >/dev/null 2>&1; then
649 if script -qc echo /dev/null >/dev/null 2>&1; then
650 faketty() { script -qec "$*" /dev/null; }
651 else # macOS
652 faketty() { script -q /dev/null "$@" /dev/null |
653 sed 's/^\x5E\x44\x08\x08//'; }
654 fi
655
656 faketty $JQ_NO_B -n . > $d/color
657 printf '\033[0;90mnull\033[0m\r\n' > $d/expect
658 od -tc $d/expect
659 od -tc $d/color
660 cmp $d/color $d/expect
661 faketty env NO_COLOR= $JQ_NO_B -n . > $d/color
662 printf '\033[0;90mnull\033[0m\r\n' > $d/expect
663 od -tc $d/expect
664 od -tc $d/color
665 cmp $d/color $d/expect
666 faketty env NO_COLOR=1 $JQ_NO_B -n . > $d/color
667 printf 'null\r\n' > $d/expect
668 od -tc $d/expect
669 od -tc $d/color
670 cmp $d/color $d/expect
671 faketty env NO_COLOR=1 $JQ_NO_B -Cn . > $d/color
672 printf '\033[0;90mnull\033[0m\r\n' > $d/expect
673 od -tc $d/expect
674 od -tc $d/color
675 cmp $d/color $d/expect
676 fi
677
678 # #2785
679 if $VALGRIND $Q $JQ -n -f "$JQTESTDIR/no-main-program.jq" > $d/out 2>&1; then
680 echo "jq -f $JQTESTDIR/no-main-program.jq succeeded"
681 exit 1
682 else
683 EC=$?
684 if [ $EC -eq 1 ]; then
685 echo "jq -f $JQTESTDIR/no-main-program.jq failed with memory errors"
686 exit 1
687 fi
688 if [ $EC -ne 3 ]; then
689 echo "jq -f $JQTESTDIR/no-main-program.jq failed with wrong exit code ($EC)"
690 exit 1
691 fi
692 fi
693 cat > $d/expected <<EOF
694 jq: error: Top-level program not given (try ".")
695 jq: 1 compile error
696 EOF
697 if ! diff $d/expected $d/out; then
698 echo "jq -f $JQTESTDIR/no-main-program.jq failed but its error message is not the expected one"
699 exit 1
700 fi
701
702 if ! $VALGRIND $Q $JQ -n -f "$JQTESTDIR/yes-main-program.jq" > $d/out 2>&1; then
703 echo "jq -f $JQTESTDIR/yes-main-program.jq failed"
704 exit 1
705 fi
706
707 if ! $msys && ! $mingw && locales=$(locale -a); then
708 { l=$(grep -Ev '^(C|LANG|POSIX|en)' | grep -Ei '\.utf-?8$' | head -n1) ;} \
709 <<EOF
710 $locales
711 EOF
712 if [ -z "$l" ]; then
713 echo "WARNING: Not testing localization"
714 else
715 dt1=$(LC_ALL=$l date +'%a %d %b %Y at %H:%M:%S')
716 dt2=$(LC_ALL=$l $JQ -nr 'now | strflocaltime("%a %d %b %Y at %H:%M:%S")')
717 dt3=$(LC_ALL=$l date +'%a %d %b %Y at %H:%M:%S')
718 if [ "$dt1" != "$dt2" ] && [ "$dt2" != "$dt3" ]; then
719 echo "jq does not honor LC_ALL environment variable ($dt1, $dt2, $dt3)"
720 exit 1
721 fi
722 fi
723 fi
724
725 # Test syntax error messages of multiline filter (we can't test in jq.test)
726 if $VALGRIND $Q $JQ -n '[
727 try if .
728 then 1
729 else 2
730 catch ]' > $d/out 2>&1; then
731 echo 'syntax error not detected'
732 exit 1
733 fi
734 cat > $d/expected <<EOF
735 jq: error: syntax error, unexpected catch, expecting end or '|' or ',' at <top-level>, line 5, column 3:
736 catch ]
737 ^^^^^
738 jq: error: Possibly unterminated 'if' statement at <top-level>, line 2, column 7:
739 try if .
740 ^^^^
741 jq: error: Possibly unterminated 'try' statement at <top-level>, line 2, column 3:
742 try if .
743 ^^^^^^^^
744 jq: 3 compile errors
745 EOF
746 diff $d/out $d/expected
747
748 # Test syntax error messages when error happens at the end.
749 if $VALGRIND $Q $JQ -n 'if
750 ' > $d/out 2>&1; then
751 echo 'syntax error not detected'
752 exit 1
753 fi
754 cat > $d/expected <<EOF
755 jq: error: syntax error, unexpected end of file at <top-level>, line 1, column 3:
756 if
757 ^
758 jq: 1 compile error
759 EOF
760 diff $d/out $d/expected
761
762 # Comments!
763 if ! x=$($JQ -n '123 # comment') || [ "$x" != 123 ]; then
764 echo 'comment did not work'
765 exit 1
766 fi
767
768 cr=$(printf \\r)
769 if ! x=$($JQ -n "1 # foo$cr + 2") || [ "$x" != 1 ]; then
770 echo 'regression: carriage return terminates comment'
771 exit 1
772 fi
773
774 if ! x=$($JQ -cn '[
775 1,
776 # foo \
777 2,
778 # bar \\
779 3,
780 4, # baz \\\
781 5, \
782 6,
783 7
784 # comment \
785 comment \
786 comment
787 ]') || [ "$x" != '[1,3,4,7]' ]; then
788 echo 'multiline comment was not handled correctly'
789 exit 1
790 fi
791
792 if ! x=$($JQ -cn "$(printf '[\r\n1,# comment\r\n2,# comment\\\r\ncomment\r\n3\r\n]')") \
793 || [ "$x" != '[1,2,3]' ]; then
794 echo 'multiline comment with CRLF line breaks was not handled correctly'
795 exit 1
796 fi
797
798 # Allow passing the inline jq script before -- #2919
799 if ! r=$($JQ --args -rn -- '$ARGS.positional[0]' bar) || [ "$r" != bar ]; then
800 echo "passing the inline script after -- didn't work"
801 exit 1
802 fi
803 if ! r=$($JQ --args -rn 1 -- '$ARGS.positional[0]' bar) || [ "$r" != 1 ]; then
804 echo "passing the inline script before -- didn't work"
805 exit 1
806 fi
807
808 # CVE-2023-50246: No heap overflow for '-10E-1000000001'
809 $VALGRIND $Q $JQ . <<\NUM
810 -10E-1000000001
811 NUM
812
813 # test for --indent and --compact-output -- #1465
814 printf '[1,2]\n' > $d/expected
815 $JQ --compact-output -n "[1,2]" > $d/out
816 cmp $d/out $d/expected
817
818 printf '[\n1,\n2\n]\n' > $d/expected
819 $JQ --indent 0 -n "[1,2]" > $d/out
820 cmp $d/out $d/expected
821
822 printf '[\n 1,\n 2\n]\n' > $d/expected
823 $JQ --indent 1 -n "[1,2]" > $d/out
824 cmp $d/out $d/expected
825
826 printf '[\n 1,\n 2\n]\n' > $d/expected
827 $JQ --indent 5 -n "[1,2]" > $d/out
828 cmp $d/out $d/expected
829
830 printf '[\n{\n"a": 1\n}\n]\n' > $d/expected
831 $JQ --indent 0 -n "[{a:1}]" > $d/out
832 cmp $d/out $d/expected
833
834 printf '[\n {\n "a": 1\n }\n]\n' > $d/expected
835 $JQ --indent 1 -n "[{a:1}]" > $d/out
836 cmp $d/out $d/expected
837
838 printf '[\n {\n "a": 1\n }\n]\n' > $d/expected
839 $JQ --indent 6 -n "[{a:1}]" > $d/out
840 cmp $d/out $d/expected
841
842 if ! $msys && ! $mingw; then
843 # Test handling of timezones -- #2429, #2475
844 if ! r=$(TZ=Asia/Tokyo $JQ -rn '1731627341 | strflocaltime("%F %T %z %Z")') \
845 || [ "$r" != "2024-11-15 08:35:41 +0900 JST" ]; then
846 echo "Incorrectly formatted local time"
847 exit 1
848 fi
849
850 if ! r=$(TZ=Europe/Paris $JQ -rn '1731627341 | strflocaltime("%F %T %z %Z")') \
851 || [ "$r" != "2024-11-15 00:35:41 +0100 CET" ]; then
852 echo "Incorrectly formatted local time"
853 exit 1
854 fi
855
856 # Test when DST is in effect: #1912
857 if ! r=$(TZ=Europe/Paris $JQ -rn '1750500000 | strflocaltime("%F %T %z %Z")') \
858 || [ "$r" != "2025-06-21 12:00:00 +0200 CEST" ]; then
859 echo "Incorrectly formatted local time"
860 exit 1
861 fi
862
863 if ! r=$(TZ=Europe/Paris $JQ -rn '1731627341 | strftime("%F %T %z %Z")') \
864 || ( [ "$r" != "2024-11-14 23:35:41 +0000 UTC" ] \
865 && [ "$r" != "2024-11-14 23:35:41 +0000 GMT" ] ); then
866 echo "Incorrectly formatted universal time"
867 exit 1
868 fi
869
870 if ! r=$(TZ=Etc/GMT+7 $JQ -nc '1731627341 | .,. | [strftime("%FT%T"),strflocaltime("%FT%T%z")]'
871 ) \
872 || [ "$r" != '["2024-11-14T23:35:41","2024-11-14T16:35:41-0700"]
873 ["2024-11-14T23:35:41","2024-11-14T16:35:41-0700"]' ]; then
874 echo "strftime or strftimelocal are not pure functions"
875 exit 1
876 fi
877 fi
878
879 # Test ARG_NEWCLOSURE overflow protection (issue #3458)
880 # Test 1: Too many function parameters (4097 params triggers the limit)
881 echo "Testing function parameter limit..."
882 cat > $d/expected <<EOF
883 jq: error: too many function parameters or local function definitions (max 4095)
884 jq: 1 compile error
885 EOF
886 $JQ -rn '"def f(\([range(4097) | "a\(.)"] | join(";"))): .; f(\([range(4097)] | join(";")))"' > $d/prog.jq
887 $JQ -nf $d/prog.jq 2> $d/out && {
888 echo "Expected compile error for too many parameters"
889 exit 1
890 }
891 diff $d/out $d/expected
892
893 # Test 2: Too many local functions (4097 functions triggers the limit)
894 echo "Testing local function limit..."
895 cat > $d/expected <<EOF
896 jq: error: too many function parameters or local function definitions (max 4095)
897 jq: 1 compile error
898 EOF
899 $JQ -rn '"\([range(4097) | "def f\(.): \(.)"] | join("; ")); \([range(4097) | "f\(.)"] | join(" + "))"' > $d/prog.jq
900 $JQ -nf $d/prog.jq 2> $d/out && {
901 echo "Expected compile error for too many local functions"
902 exit 1
903 }
904 diff $d/out $d/expected
905
906 # CVE-2026-33948: No NUL truncation in the JSON parser
907 if printf '{}\x00{}' | $JQ >/dev/null 2> /dev/null; then
908 printf 'Error expected but jq exited successfully\n' 1>&2
909 exit 1
910 fi
911
912 # CVE-2026-41256: No NUL truncation in program files loaded with -f
913 printf '.\x00invalid' > "$d/nul_prog.jq"
914 if echo '42' | $JQ -f "$d/nul_prog.jq" >/dev/null 2>/dev/null; then
915 printf 'Error expected for program file with NUL bytes\n' 1>&2
916 exit 1
917 fi
918
919 # CVE-2026-43895: No NUL bytes in module/data import paths
920 printf 'import "a\\u0000b" as $x; .' > "$d/nul_import.jq"
921 if $JQ -nf "$d/nul_import.jq" >/dev/null 2>/dev/null; then
922 printf 'Error expected for import path with NUL bytes\n' 1>&2
923 exit 1
924 fi
925 printf 'include "a\\u0000b"; .' > "$d/nul_include.jq"
926 if $JQ -nf "$d/nul_include.jq" >/dev/null 2>/dev/null; then
927 printf 'Error expected for include path with NUL bytes\n' 1>&2
928 exit 1
929 fi
930 printf '"a\\u0000b" | modulemeta' > "$d/nul_modulemeta.jq"
931 if $JQ -nf "$d/nul_modulemeta.jq" >/dev/null 2>/dev/null; then
932 printf 'Error expected for modulemeta with NUL bytes\n' 1>&2
933 exit 1
934 fi
935
936 exit 0