-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·447 lines (386 loc) · 12.6 KB
/
test.sh
File metadata and controls
executable file
·447 lines (386 loc) · 12.6 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/bin/bash
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
RESET="\033[0m"
OK=$GREEN"OK"$RESET
NG=$RED"NG"$RESET
cat <<EOF | gcc -xc -o a.out -
#include <stdio.h>
int main() { printf("hello from a.out\n"); }
EOF
cat <<EOF | gcc -xc -o print_args -
#include <stdio.h>
int main(int argc, char *argv[]) {
for (int i = 0; argv[i]; i++)
printf("argv[%d] = [%s]\n", i, argv[i]);
}
EOF
cat <<EOF | gcc -xc -o exit42 -
int main() { return 42; }
EOF
print_desc() {
echo -e $YELLOW"$1"$RESET
}
cleanup() {
rm -f cmp out a.out print_args exit42 infinite_loop no_exec_perm no_read_perm
}
assert() {
COMMAND="$1"
shift
printf '%-70s:' "[$COMMAND]"
# exit status
echo -n -e "$COMMAND" | bash >cmp 2>&-
expected=$?
for arg in "$@"
do
mv "$arg" "$arg"".cmp"
done
echo -n -e "$COMMAND" | ./minishell >out 2>&-
actual=$?
for arg in "$@"
do
mv "$arg" "$arg"".out"
done
if diff out cmp > /dev/null; then
echo -e -n " diff $OK"
else
echo -e -n " diff $NG"
printf '%-70s:\n' "[$COMMAND]" >> error.log
diff -U 1 out cmp >>error.log
fi
if [ "$actual" = "$expected" ]; then
echo -e -n " status $OK"
else
echo -e -n " status $NG, expected $expected but got $actual"
printf '%-70s:' "[$COMMAND]" >>error.log
echo "status NG, expected $expected but got $actual" >>error.log
fi
for arg in "$@"
do
echo -n " [$arg] "
diff "$arg"".cmp" "$arg"".out" >/dev/null && echo -e -n "$OK" || echo -e -n "$NG"
rm -f "$arg"".cmp" "$arg"".out"
done
echo
}
rm -f error.log
# Empty line (EOF)
assert ''
# Absolute path commands without args
assert '/bin/pwd'
assert '/bin/echo'
assert '/bin/ls'
# Search command path without args
assert 'pwd'
assert 'echo'
assert 'ls'
assert './a.out'
## no such command
assert 'a.out'
assert 'nosuchfile'
## command not found
assert '""'
# assert '.' # . is a builtin command in bash
assert '..'
## is a directory
assert './'
assert '/'
assert '/etc'
assert '/etc/'
assert '////'
## Permission denied
echo "int main() { }" | gcc -xc -o no_exec_perm -
chmod -x no_exec_perm
assert 'no_exec_perm'
assert './no_exec_perm'
echo "int main() { }" | gcc -xc -o no_read_perm -
chmod -r no_read_perm
assert 'no_read_perm'
assert './no_read_perm'
mkdir -p /tmp/a /tmp/b
echo "int main() { return 1; }" | gcc -xc -o /tmp/a/simple_test -
echo "int main() { return 2; }" | gcc -xc -o /tmp/b/simple_test -
print_desc "/tmp/a /tmp/b both with permission"
assert 'unset PATH\nexport PATH="/tmp/a:/tmp/b"\nsimple_test'
assert 'unset PATH\nexport PATH="/tmp/b:/tmp/a"\nsimple_test'
print_desc "/tmp/a /tmp/b both without permission"
chmod -x /tmp/a/simple_test; chmod -x /tmp/b/simple_test;
assert 'unset PATH\nexport PATH="/tmp/a:/tmp/b"\nsimple_test'
assert 'unset PATH\nexport PATH="/tmp/b:/tmp/a"\nsimple_test'
print_desc "a with permission, b without permission"
chmod +x /tmp/a/simple_test; chmod -x /tmp/b/simple_test;
assert 'unset PATH\nexport PATH="/tmp/a:/tmp/b"\nsimple_test'
assert 'unset PATH\nexport PATH="/tmp/b:/tmp/a"\nsimple_test'
print_desc "a without permission, b with permission"
chmod -x /tmp/a/simple_test; chmod +x /tmp/b/simple_test;
assert 'unset PATH\nexport PATH="/tmp/a:/tmp/b"\nsimple_test'
assert 'unset PATH\nexport PATH="/tmp/b:/tmp/a"\nsimple_test'
# Tokenize
## unquoted word
assert 'ls /'
assert 'echo hello world '
assert 'nosuchfile\n\n'
## single quote
assert "./print_args 'hello world' '42Tokyo'"
assert "echo 'hello world' '42Tokyo'"
assert "echo '\"hello world\"' '42Tokyo'"
## double quote
assert './print_args "hello world" "42Tokyo"'
assert 'echo "hello world" "42Tokyo"'
assert "echo \"'hello world'\" \"42Tokyo\""
## combination
assert "echo hello' world'"
assert "echo hello' world '\" 42Tokyo \""
# Redirect
## Redirecting output
assert 'echo hello >hello.txt' 'hello.txt'
assert 'echo hello >f1>f2>f3' 'f1' 'f2' 'f3'
## Redirecting input
assert 'cat <Makefile'
echo hello >f1
echo world >f2
echo 42Tokyo >f3
assert 'cat <f1<f2<f3'
rm -f f1 f2 f3
assert 'cat <hoge'
## Appending Redirected output
assert 'pwd >>pwd.txt' 'pwd.txt'
assert 'pwd >>pwd.txt \n pwd >>pwd.txt' 'pwd.txt'
## Here Document
assert 'cat <<EOF\nhello\nworld\nEOF\nNOPRINT'
assert 'cat <<EOF<<eof\nhello\nworld\nEOF\neof\nNOPRINT'
assert 'cat <<EOF\nhello\nworld'
assert 'cat <<E"O"F\nhello\nworld\nEOF\nNOPRINT'
assert 'cat <<EOF \n$USER\n$NO_SUCH_VAR\n$FOO$BAR\nEOF'
assert 'cat <<"EOF" \n$USER\n$NO_SUCH_VAR\n$FOO$BAR\nEOF'
assert 'cat <<EO"F" \n$USER\n$NO_SUCH_VAR\n$FOO$BAR\nEOF'
(
print_desc 'export EOF="eof"'
export EOF="eof"
assert 'cat <<$EOF \neof\n$EOF\nEOF'
assert 'cat <<"$EOF" \neof\n$EOF\nEOF'
)
# Pipe
assert 'cat Makefile | grep minishell'
assert 'cat | cat | ls\n\n'
# Expand Variable
assert 'echo $USER'
assert 'echo $USER$PATH$TERM'
assert 'echo "$USER $PATH $TERM"'
# Special Parameter $?
assert 'echo $?'
assert 'invalid\necho $?\necho $?'
assert 'exit42\necho $?\necho $?'
assert 'exit42\n\necho $?\necho $?'
# Word Splitting
assert 'export FOO="echo hello"\n$FOO'
assert 'export TEST="cho -n"\ne$TEST'
assert 'export FOO="a b"\necho $FOO'
assert 'export FOO="a b"\necho hello$FOO'
assert 'export FOO="a b"\necho $FOO"world"'
assert 'export FOO="a b"\necho hello$FOO"world"'
assert 'export FOO="echo a b"\n$FOO'
assert 'export IFS=""\nexport FOO="echo hello"\n$FOO'
assert 'export IFS=""\nexport TEST="cho -n"\ne$TEST'
assert 'export IFS=""\nexport FOO="a b"\n./print_args $FOO'
assert 'export IFS=""\nexport FOO="a b"\n./print_args hello$FOO'
assert 'export IFS=""\nexport FOO="a b"\n./print_args $FOO"world"'
assert 'export IFS=""\nexport FOO="a b"\n./print_args hello$FOO"world"'
assert 'export IFS=""\nexport FOO="./print_args a b"\n$FOO'
assert 'export IFS="abc"\nexport FOO="./print_argsahellobbbbbbworldccc"\n$FOO'
assert 'export IFS="abc"\nexport TEST="choa-n"\ne$TEST'
assert 'export IFS="abc"\nexport FOO="xabcabcy"\n./print_args $FOO'
assert 'export IFS="abc"\nexport FOO="xabcabcy"\n./print_args hello$FOO'
assert 'export IFS="abc"\nexport FOO="xabcabcy"\n./print_args $FOO"world"'
assert 'export IFS="abc"\nexport FOO="xabcabcy"\n./print_args hello$FOO"world"'
assert 'export IFS="abc"\nexport FOO="./print_argsaaaaaxabcabcy"\n$FOO'
assert 'export IFS="a"\nexport FOO="aaahelloaaaworldaaa"\n./print_args $FOO'
assert 'export IFS="a "\nexport FOO=" hello world "\n./print_args $FOO'
assert 'export IFS="a "\nexport FOO=" a a hello a a world a a "\n./print_args $FOO'
assert 'export IFS="a "\nexport FOO="aaa"\n./print_args $FOO'
assert 'export IFS="a "\nexport FOO="helloaaa"\n./print_args $FOO'
assert 'export IFS="a "\nexport FOO="helloaaaworld"\n./print_args $FOO'
assert 'export IFS="a "\nexport FOO="aaahelloaaaworldaaa"\n./print_args $FOO'
assert 'export IFS=" :"\nexport FOO="hello: : :"\n./print_args $FOO'
assert 'export IFS=" :"\nexport FOO="hello: : : "\n./print_args $FOO'
assert 'export IFS=" :"\nexport FOO="hello : : :"\n./print_args $FOO'
assert 'export IFS=" :"\nexport FOO="hello : : : "\n./print_args $FOO'
assert 'export IFS=" :"\nexport FOO=": : :hello: : :"\n./print_args $FOO'
assert 'export IFS=" :"\nexport FOO=": : : hello: : : "\n./print_args $FOO'
assert 'export IFS=" :"\nexport FOO=" : : :hello : : :"\n./print_args $FOO'
assert 'export IFS=" :"\nexport FOO=" : : : hello : : : "\n./print_args $FOO'
assert 'export IFS=" :"\nexport FOO="hello: : :world"\n./print_args $FOO'
assert 'export IFS=" :"\nexport FOO="hello : : :world"\n./print_args $FOO'
assert 'export IFS=" :"\nexport FOO="hello: : : world"\n./print_args $FOO'
assert 'export IFS=" :"\nexport FOO="hello : : : world"\n./print_args $FOO'
assert 'echo "$IFS"'
print_desc 'export IFS=":"'
(
export IFS=":"
assert 'echo "$IFS"'
assert 'export FOO="hello:world:42Tokyo"\n./print_args $FOO'
)
# Signal handling
echo "int main() { while (1) ; }" | gcc -xc -o infinite_loop -
## Signal to shell processes
print_desc "SIGTERM to SHELL"
(sleep 0.01; pkill -SIGTERM bash;
sleep 0.01; pkill -SIGTERM minishell) &
assert './infinite_loop' 2>/dev/null # Redirect stderr to suppress signal terminated message
print_desc "SIGQUIT to SHELL"
(sleep 0.01; pkill -SIGQUIT bash; # SIGQUIT should not kill the shell
sleep 0.01; pkill -SIGTERM bash;
sleep 0.01; pkill -SIGQUIT minishell; # SIGQUIT should not kill the shell
sleep 0.01; pkill -SIGTERM minishell) &
assert './infinite_loop' 2>/dev/null # Redirect stderr to suppress signal terminated message
print_desc "SIGINT to SHELL"
(sleep 0.01; pkill -SIGINT bash; # SIGINT should not kill the shell
sleep 0.01; pkill -SIGTERM bash;
sleep 0.01; pkill -SIGINT minishell; # SIGINT should not kill the shell
sleep 0.01; pkill -SIGTERM minishell) &
assert './infinite_loop' 2>/dev/null # Redirect stderr to suppress signal terminated message
## Signal to child processes
print_desc "SIGTERM to child process"
(sleep 0.01; pkill -SIGTERM infinite_loop;
sleep 0.01; pkill -SIGTERM infinite_loop) &
assert './infinite_loop'
print_desc "SIGINT to child process"
(sleep 0.01; pkill -SIGINT infinite_loop;
sleep 0.01; pkill -SIGINT infinite_loop) &
assert './infinite_loop'
print_desc "SIGQUIT to child process"
(sleep 0.01; pkill -SIGQUIT infinite_loop;
sleep 0.01; pkill -SIGQUIT infinite_loop) &
assert './infinite_loop'
print_desc "SIGUSR1 to child process"
(sleep 0.01; pkill -SIGUSR1 infinite_loop;
sleep 0.01; pkill -SIGUSR1 infinite_loop) &
assert './infinite_loop'
# Manual Debug
# $ ./minishell
# $
# 1. Ctrl-\
# 2. Ctrl-C
# 3. Ctrl-D
#
# $ ./minishell
# $ hogehoge
# 1. Ctrl-\
# 2. Ctrl-C
# 3. Ctrl-D
#
# $ ./minishell
# $ cat <<EOF
# >
# 1. Ctrl-\
# 2. Ctrl-C
# 3. Ctrl-D
#
# $ ./minishell
# $ cat <<EOF
# > hoge
# > fuga
# 1. Ctrl-\
# 2. Ctrl-C
# 3. Ctrl-D
# Builtin
## exit
assert 'exit'
assert 'exit 42'
assert 'exit -42'
assert 'exit +42'
assert 'exit ""'
assert 'exit hello'
assert 'exit 42Tokyo'
assert 'exit 1 2'
assert 'exit 9223372036854775806'
assert 'exit 9223372036854775807'
assert 'exit 9223372036854775808'
assert 'exit -9223372036854775807'
assert 'exit -9223372036854775808'
assert 'exit -9223372036854775809'
## export
print_desc "Output of 'export' differs, but it's ok."
assert 'export' # order of variables, default variables differs...
assert 'export | grep nosuch | sort'
assert 'export nosuch\n export | grep nosuch | sort'
assert 'export nosuch=fuga\n export | grep nosuch | sort'
assert 'export nosuch=fuga hoge=nosuch\n export | grep nosuch | sort'
assert 'export [invalid]'
assert 'export [invalid_nosuch]\n export | grep nosuch | sort'
assert 'export [invalid]=nosuch\n export | grep nosuch | sort'
assert 'export [invalid] nosuch hoge=nosuch\n export | grep nosuch | sort'
assert 'export nosuch [invalid] hoge=nosuch\n export | grep nosuch | sort'
assert 'export nosuch hoge=nosuch [invalid]\n export | grep nosuch | sort'
assert 'export nosuch="nosuch2=hoge"\nexport $nosuch\n export | grep nosuch | sort'
## unset
(
print_desc 'export hoge fuga=fuga'
export hoge fuga=fuga
assert 'unset'
assert 'unset hoge'
assert 'unset fuga'
assert 'unset nosuch'
assert 'unset [invalid]'
assert 'unset hoge fuga'
assert 'unset hoge nosuch fuga'
assert 'unset fuga \n export | echo $fuga'
assert 'unset [invalid] fuga \n echo $fuga'
)
## env
print_desc "Output of 'env' differs, but it's ok."
assert 'env' # order of variables, default variables differs...
assert 'env | grep hoge | sort'
## cd
assert 'cd'
assert 'cd .'
assert 'cd ..'
assert 'cd ///'
assert 'cd /tmp'
assert 'cd /tmp/'
assert 'cd /tmp///'
assert 'cd /../../../././.././'
assert 'cd src'
assert 'unset HOME\ncd'
assert 'cd \n echo $PWD'
assert 'cd \n echo $PWD'
assert 'cd .\n echo $PWD'
assert 'cd ..\n echo $PWD'
assert 'cd ///\n echo $PWD'
assert 'cd /tmp\n echo $PWD'
assert 'cd /tmp/\n echo $PWD'
assert 'cd /tmp///\n echo $PWD'
assert 'cd /../../../././.././\n echo $PWD'
assert 'cd src\n echo $PWD'
assert 'unset HOME\ncd \n echo $PWD'
## echo
assert 'echo'
assert 'echo hello'
assert 'echo hello " " world'
assert 'echo -n'
assert 'echo -n hello'
assert 'echo -n hello world'
assert 'echo hello -n'
assert 'echo -nn'
assert 'echo -n-n-n'
assert 'echo ";|()"'
## pwd
assert 'pwd'
assert 'cd\npwd'
assert 'cd src\npwd'
assert 'cd /etc\npwd'
assert 'cd . \n pwd \n echo $PWD $OLDPWD'
assert 'cd .. \n pwd \n echo $PWD $OLDPWD'
assert 'cd /// \n pwd \n echo $PWD $OLDPWD'
assert 'cd /tmp/// \n pwd \n echo $PWD $OLDPWD'
assert 'unset PWD\npwd\ncd /etc\npwd'
## export attribute
assert 'unset PWD \n cd \n echo $PWD \ncd /tmp\necho $PWD'
assert 'unset PWD\ncd\necho $OLDPWD\ncd /tmp\necho $OLDPWD'
assert 'unset PWD\ncd\nexport|grep PWD\ncd /tmp\nexport|grep PWD'
assert 'unset PWD\ncd\nenv|grep PWD\ncd /tmp\nenv|grep PWD'
cleanup
if [ -f error.log ]; then
echo -e $RED"Some tests have failed. Please review the error.log for more information."$RESET
fi