summaryrefslogtreecommitdiffstats
path: root/tests/shtest
blob: 80f7d1c9ebad10f5fad01fa4eaa3c734ec66cd03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/bin/sh

. "${0%/*}/setup" "$@"

PATH=$JQBASEDIR:$PATH $JQBASEDIR/tests/jq-f-test.sh > /dev/null

if [ -f "$JQBASEDIR/.libs/libinject_errors.so" ]; then
  # Do some simple error injection tests to check that we're handling
  # I/O errors correctly.
  (
  libinject=$JQBASEDIR/.libs/libinject_errors.so
  cd $d
  LD_PRELOAD=$libinject $JQ . /dev/null
  touch fail_read
  LD_PRELOAD=$libinject $JQ . fail_read && exit 2
  touch fail_close
  LD_PRELOAD=$libinject $JQ . fail_close && exit 2
  true
  )
fi

printf 'a\0b\nc\0d\ne' > $d/input
$VALGRIND $Q $JQ -Rse '. == "a\u0000b\nc\u0000d\ne"' $d/input
$VALGRIND $Q $JQ -Rne '[inputs] == ["a\u0000b", "c\u0000d", "e"]' $d/input

## Test constant folding

## XXX If we add a builtin to list the program's disassembly then we can
## move all of these into tests/all.test

# String constant folding (addition only)
nref=`$VALGRIND $Q $JQ -n --debug-dump-disasm '"foo"' | wc -l`

# Numeric constant folding (not all ops yet)
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '1+1' | wc -l`
if [ $n -ne $nref ]; then
    echo "Constant expression folding for strings didn't work"
    exit 1
fi
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '1-1' | wc -l`
if [ $n -ne $nref ]; then
    echo "Constant expression folding for strings didn't work"
    exit 1
fi
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '2*3' | wc -l`
if [ $n -ne $nref ]; then
    echo "Constant expression folding for strings didn't work"
    exit 1
fi
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '9/3' | wc -l`
if [ $n -ne $nref ]; then
    echo "Constant expression folding for strings didn't work"
    exit 1
fi
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '9==3' | wc -l`
if [ $n -ne $nref ]; then
    echo "Constant expression folding for strings didn't work"
    exit 1
fi
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '9!=3' | wc -l`
if [ $n -ne $nref ]; then
    echo "Constant expression folding for strings didn't work"
    exit 1
fi
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '9<=3' | wc -l`
if [ $n -ne $nref ]; then
    echo "Constant expression folding for strings didn't work"
    exit 1
fi
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '9>=3' | wc -l`
if [ $n -ne $nref ]; then
    echo "Constant expression folding for strings didn't work"
    exit 1
fi

## Test JSON sequence support

cat > $d/expected <<EOF
ignoring parse error: Truncated value at line 2, column 5
ignoring parse error: Truncated value at line 2, column 25
ignoring parse error: Truncated value at line 2, column 41
EOF
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
cmp $d/out $d/expected

cat > $d/expected <<EOF
ignoring parse error: Truncated value at line 2, column 5
ignoring parse error: Truncated value at line 2, column 25
ignoring parse error: Truncated value at line 3, column 1
EOF
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
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
ignoring parse error: Unfinished abandoned text at EOF at line 1, column 4
EOF
printf '"foo' | $JQ -ce --seq . > $d/out 2>&1
cmp $d/out $d/expected

# Numeric values truncated by EOF are ignored
cat > $d/expected <<EOF
ignoring parse error: Unfinished abandoned text at EOF at line 1, column 1
EOF
printf '1' | $JQ -ce --seq . > $d/out 2>&1
cmp $d/out $d/expected

cat > $d/expected <<EOF
jq: error (at <stdin>:1): Unfinished abandoned text at EOF at line 2, column 0
EOF
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
cmp $d/out $d/expected

# 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

# 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
printf "[1,2][3,4]\n" | $JQ -cs add > $d/out 2>&1
cmp $d/out $d/expected


## Test streaming parser

## If we add an option to stream to the `import ... as $symbol;` directive
## then we can move these tests into tests/all.test.
$VALGRIND $Q $JQ -c '. as $d|path(..) as $p|$d|getpath($p)|scalars_or_empty|[$p,.]' < "$JQTESTDIR/torture/input0.json" > $d/out0
$VALGRIND $Q $JQ --stream -c '.|select(length==2)' < "$JQTESTDIR/torture/input0.json" > $d/out1
diff $d/out0 $d/out1

## XXX This test can be moved to tests/all.test _now_
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.
    #
    #len=`wc -c < "$JQTESTDIR/torture/input0.json"`
    if [ -z "$VALGRIND" ]; then
        start=1
        end=`wc -c < "$JQTESTDIR/torture/input0.json"`
    else
        start=120
        end=151
    fi
    for i in `seq $start $end`; do
        dd "if=tests/torture/input0.json" bs=$i count=1 2>/dev/null |
            $VALGRIND $JQ -c . > $d/out0 2>$d/err || true
        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 |
            $VALGRIND $JQ -cn --stream 'fromstream(inputs)' > $d/out1 2>$d/err || true
        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

## Fuzz parser

## XXX With a `urandom` builtin we could move this test into tests/all.test
clean=false
if dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null; then
    # Have a /dev/urandom, good
    $VALGRIND $Q $JQ --seq . $d/rand >/dev/null 2>&1
    $VALGRIND $Q $JQ --seq --stream . $d/rand >/dev/null 2>&1
    dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null
    $VALGRIND $Q $JQ --seq . $d/rand >/dev/null 2>&1
    $VALGRIND $Q $JQ --seq --stream . $d/rand >/dev/null 2>&1
    dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null
    $VALGRIND $Q $JQ --seq . $d/rand >/dev/null 2>&1
    $VALGRIND $Q $JQ --seq --stream . $d/rand >/dev/null 2>&1
fi
clean=true

## Test library/module system

# Check handling of ~/.jq; these can't move into jq_test.c yet because
# they depend on $HOME
if [ "`HOME="$mods" $VALGRIND $Q $JQ -nr fg`" != foobar ]; then
    echo "Bug #479 appears to be back" 1>&2
    exit 1
fi

if [ `HOME="$mods" $VALGRIND $Q $JQ --debug-dump-disasm -n fg | grep '^[a-z]' | wc -l` -gt 3 ]; then
    echo "Binding too many defs into program" 1>&2
    exit 1
fi

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

## 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
if [ -n "`$VALGRIND $Q $JQ -n 'halt_error(1)' 2>&1`" ]; then
    echo "jq halt_error(1) had unexpected output" 1>&2
    exit 1
fi
if [ -n "`$VALGRIND $Q $JQ -n '"xyz\n"|halt_error(1)' 2>/dev/null`" ]; then
    echo "jq halt_error(1) had unexpected output on stdout" 1>&2
    exit 1
fi
if [ "`$VALGRIND $Q $JQ -n '"xyz\n"|halt_error(1)' 2>&1`" != xyz ]; then