-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.bash
More file actions
executable file
·151 lines (134 loc) · 4 KB
/
Copy pathtest.bash
File metadata and controls
executable file
·151 lines (134 loc) · 4 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
#!/bin/bash
echo "=== Testing timeout command ==="
echo "Building timeout..."
make
echo "✓ Build successful"
echo "Testing help and version options"
./timeout --help | head -1
./timeout --version
echo "✓ Help and version options work"
echo "Testing normal command execution"
./timeout 1s echo "Hello world"
echo "✓ Normal command execution works"
echo "Testing zero duration disables timeout"
./timeout 0 sh -c 'sleep 0.1; exit 7'
exit_code=$?
if [ $exit_code -eq 7 ]; then
echo "✓ Zero duration disables timeout (exit: 7)"
else
echo "✗ Zero duration failed (exit: $exit_code, expected 7)"
exit 1
fi
echo "Testing timeout with sleep"
./timeout 1s sleep 3
exit_code=$?
if [ $exit_code -eq 124 ]; then
echo "✓ Timeout worked (exit: 124)"
else
echo "✗ Timeout failed (exit: $exit_code)"
exit 1
fi
echo "Testing timeout terminates descendant processes"
marker="/tmp/timeout_process_group_$$"
rm -f "$marker"
./timeout 0.1s sh -c "(sleep 0.3; touch '$marker') & wait"
exit_code=$?
sleep 0.4
if [ $exit_code -eq 124 ] && [ ! -e "$marker" ]; then
echo "✓ Timeout terminated descendant processes"
else
echo "✗ Descendant process survived timeout"
rm -f "$marker"
exit 1
fi
echo "Testing timeout resumes stopped processes after signaling"
marker="/tmp/timeout_sigcont_$$"
rm -f "$marker"
./timeout --kill-after=0.2s 0.1s sh -c "trap 'touch $marker; exit 0' TERM; kill -STOP \$\$; sleep 1"
exit_code=$?
if [ $exit_code -eq 124 ] && [ -e "$marker" ]; then
echo "✓ Timeout resumed stopped process to handle signal"
else
echo "✗ Stopped process did not handle timeout signal"
rm -f "$marker"
exit 1
fi
rm -f "$marker"
echo "Testing preserve-status option"
./timeout -p 1s echo "test"
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "✓ Preserve-status worked (exit: 0)"
else
echo "✗ Preserve-status failed (exit: $exit_code)"
exit 1
fi
echo "Testing signal option"
./timeout -s TERM 1s sleep 3
exit_code=$?
if [ $exit_code -eq 124 ]; then
echo "✓ Signal option worked (exit: 124)"
else
echo "✗ Signal option failed (exit: $exit_code)"
exit 1
fi
echo "Testing additional and SIG-prefixed signal names"
./timeout -s QUIT 0.1s sleep 1
quit_exit_code=$?
./timeout --preserve-status -s SIGTERM 0.1s sleep 1
term_exit_code=$?
if [ $quit_exit_code -eq 124 ] && [ $term_exit_code -eq 143 ]; then
echo "✓ Additional and SIG-prefixed signal names work"
else
echo "✗ Signal name support failed (QUIT: $quit_exit_code, SIGTERM: $term_exit_code)"
exit 1
fi
echo "Testing kill-after exit status"
./timeout --kill-after=0.1s 0.1s sh -c "trap '' TERM; sleep 1"
exit_code=$?
if [ $exit_code -eq 137 ]; then
echo "✓ Kill-after returned SIGKILL status (exit: 137)"
else
echo "✗ Kill-after failed (exit: $exit_code, expected 137)"
exit 1
fi
echo "Testing foreground option"
echo "test input" | ./timeout -f 1s cat > /tmp/foreground_test.txt
if [ "$(cat /tmp/foreground_test.txt)" = "test input" ]; then
echo "✓ Foreground option works with input/output"
else
echo "✗ Foreground option failed"
exit 1
fi
# Test that foreground mode still respects timeout
./timeout -f 1s sleep 3
exit_code=$?
if [ $exit_code -eq 124 ]; then
echo "✓ Foreground mode still respects timeout (exit: 124)"
else
echo "✗ Foreground mode timeout failed (exit: $exit_code)"
exit 1
fi
echo "Testing non-existent command"
./timeout 1s nonexistent_command_xyz_abc 2>/dev/null
exit_code=$?
if [ $exit_code -eq 127 ]; then
echo "✓ Non-existent command returns 127"
else
echo "✗ Non-existent command failed (exit: $exit_code, expected 127)"
exit 1
fi
echo "Testing non-executable command"
echo "#!/bin/sh" > test_not_exec
chmod a-x test_not_exec
./timeout 1s ./test_not_exec 2>/dev/null
exit_code=$?
if [ $exit_code -eq 126 ]; then
echo "✓ Non-executable command returns 126"
else
echo "✗ Non-executable command failed (exit: $exit_code, expected 126)"
rm -f test_not_exec
exit 1
fi
rm -f test_not_exec
echo "=== All tests completed ==="