-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest-lib.sh
338 lines (294 loc) · 6.71 KB
/
test-lib.sh
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
#!/bin/sh
rundir=$(mktemp -d secfs-test.XXXXXXXXXX)
mntat="$rundir/mnt"
mkdir "$mntat"
server=0 # PID of server, set by includer
uri="" # set by includer
# shellcheck disable=SC2034
uxsock="$rundir/sock" # read by includer
# colors from http://stackoverflow.com/questions/4332478/read-the-current-text-color-in-a-xterm/4332530#4332530
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
BLUE=$(tput setaf 4)
YELLOW=$(tput setaf 3)
NORMAL=$(tput sgr0)
INFO="[ ${BLUE}INFO${NORMAL} ]"
PASS="[ ${GREEN}PASS${NORMAL} ]"
FAIL="[ ${RED}FAIL${NORMAL} ]"
OHNO="[ ${RED}OHNO${NORMAL} ]"
DOTS="[ ${YELLOW}....${NORMAL} ]"
WARN="[ ${YELLOW}INFO${NORMAL} ]"
fuse=0
nxt_fname="primary-client"
client() {
rm -f "$nxt_fname.log" 2>/dev/null
if [ $# -eq 0 ]; then
# shellcheck disable=SC2024
sudo PYTHONUNBUFFERED=1 venv/bin/secfs-fuse "$uri" "$mntat" "root.pub" "user-0-key.pem" "user-$(id -u)-key.pem" "user-666-key.pem" > "$nxt_fname.log" 2> "$nxt_fname.err" &
fuse=$!
else
# shellcheck disable=SC2024
sudo PYTHONUNBUFFERED=1 venv/bin/secfs-fuse "$uri" "$mntat" "$@" > "$nxt_fname.log" 2> "$nxt_fname.err" &
fuse=$!
fi
info "client started; waiting for init"
sync
while ! grep -P "^ready$" "$nxt_fname.log" > /dev/null; do
sleep .2
done
info "client ready; continuing with tests"
}
client_cleanup() {
sudo umount "$mntat" 2>/dev/null
sleep 1 # give it time to unmount cleanly
sudo kill -9 $fuse 2>/dev/null
wait $fuse 2>/dev/null
if [ $? -ne 0 ]; then
info "$nxt_fname died"
else
info "$nxt_fname exited cleanly"
fi
sudo umount "$mntat" 2>/dev/null
# make server release global lock just in case
# this may be needed even if the client excited with exit=0!
info "making server release lock"
kill -USR1 "$server"
}
_mntat=""
_fuse=""
pushc() {
_mntat="$mntat"
_fuse=$fuse
mntat="$rundir/mnt-$1"
if [ ! -d "$mntat" ]; then
mkdir "$mntat"
fi
nxt_fname="$1"
}
popc() {
client_cleanup
# Restore old client parameters
nxt_fname="primary-client"
mntat="$_mntat"
fuse=$_fuse
_mntat=''
_fuse=''
}
cleanup() {
if [ -n "$1" ]; then
ohno "Server or client died!"
fi
if [ -n "$_fuse" ]; then
# clean nested instance
popc
fi
client_cleanup
kill "$server" 2>/dev/null
wait "$server" 2>/dev/null
rm -rf "$rundir"
if [ -n "$1" ]; then
warn "partial test completion (passed %d/%d -- %.1f%%); cleaning up\n" "$passed" "$tests" "$(echo "100*$passed/$tests" | bc -l)"
fi
}
tests=0
passed=0
msg() {
local type="$1"
shift
local fmt="$1"
shift
local msg
# shellcheck disable=SC2059
msg="$(printf "$fmt" "$@")"
printf "%s: %s\n" "$type" "$msg"
}
info() {
msg "$INFO" "$@"
}
warn() {
msg "$WARN" "$@"
}
fail() {
msg "$FAIL" "$@"
}
ohno() {
msg "$OHNO" "$@" > /dev/stderr
}
section() {
echo ""
info "Entering section %s." "$1"
}
try() {
if ! sudo kill -0 $fuse 2> /dev/null; then
if [ -z "$_fuse" ]; then
# Primary client died. Game over.
return 255
else
# Non-primary client died, but there may be other tests we can do
return 254
fi
fi
local shcmd="$1"
# shellcheck disable=SC2164
cd "$mntat" 2>/dev/null
local ex=$?
if [ $ex -ne 0 ]; then
echo "could not enter mountpoint; got no such file or directory"
return $ex
fi
sh -c "$shcmd" 2>&1
ex=$?
if ! sudo kill -0 "$server" 2> /dev/null; then
return 255
fi
if ! sudo kill -0 "$fuse" 2> /dev/null; then
if [ -z "$_fuse" ]; then
# Primary client died. Game over.
return 255
fi
fi
return $ex
}
fstats() {
tests="$(echo "$tests+1" | bc -l)"
local file="$1"; shift
local o
o=$(printf "${DOTS}: testing permissions on file %s\r" "$file")
local lastlen=${#o}
printf "%s" "$o"
local stats
local ex
stats=$(try "stat -c '%A %U %G' '$file'")
ex=$?
if [ $ex -ne 0 ]; then
if [ $ex -eq 255 ]; then
printf "\n"
cleanup 1
exit 1
fi
printf "\n%s\n" "$stats"
return 1
fi
local output=""
while [ $# -ne 0 ]; do
local f
local v
f=$(echo "$1" | awk -F= '{print $1}')
v=$(echo "$1" | awk -F= '{print $2}')
shift
printf "%${lastlen}s\r" " " # clear previous message
o=$(printf "${DOTS}: test %s of %s = %s\r" "$f" "$file" "$v")
lastlen=${#o}
printf "%s" "$o"
local real_v=""
case $f in
perm) real_v="$(echo "$stats" | awk '{print $1}')" ;;
uid) real_v="$(echo "$stats" | awk '{print $2}')" ;;
gid) real_v="$(echo "$stats" | awk '{print $3}')" ;;
esac
if [ "$real_v" != "$v" ]; then
printf "\n%s != %s (was %s)\n" "$f" "$v" "$real_v"
return 1
fi
done
printf "%${lastlen}s\r" " " # clear previous message
printf "${PASS}: correct permissions on %s\n" "$file"
passed="$(echo $passed+1 | bc -l)"
return 0
}
cant() {
tests="$(echo "$tests+1" | bc -l)"
local name="$1"
shift
local output=""
local lastlen=0
while [ $# -ne 0 ]; do
shcmd="$1"
shift
printf "%${lastlen}s\r" " " # clear previous message
o=$(printf "${DOTS}: ensure failure of %s\r" "$shcmd")
lastlen=${#o}
printf "%s" "$o"
output=$(try "$shcmd")
local ex=$?
if [ $ex -eq 0 ] || [ $ex -eq 255 ]; then
printf "%${lastlen}s\r" " " # clear previous message
printf "%s\n${FAIL}: could %s\n" "$output" "$name"
if [ $ex -eq 255 ]; then
cleanup 1
exit 1
fi
return 1
fi
done
printf "%${lastlen}s\r" " " # clear previous message
printf "${PASS}: can't %s\n" "$name"
passed="$(echo "$passed+1" | bc -l)"
return 0
}
expect() {
tests="$(echo "$tests+1" | bc -l)"
local output=""
local lastlen=0
local cmds=""
local ex=0
while [ $# -ne 1 ]; do
local shcmd="$1"
shift
if [ -z "$cmds" ]; then
cmds="$shcmd"
else
cmds="${cmds}; ${shcmd}"
fi
printf "%${lastlen}s\r" " " # clear previous message
o=$(printf "${DOTS}: test %s\r" "$shcmd")
lastlen=${#o}
printf "%s" "$o"
output=$(try "$shcmd")
ex=$?
if [ $ex -ne 0 ]; then
if [ $ex -eq 255 ]; then
printf "\n"
cleanup 1
exit 1
fi
printf "\n%s\n" "$output"
return 1
fi
done
local patt="$1"
if [ "$patt" = '^$' ]; then
echo "$output" | pcregrep -Mv "." > /dev/null
ex=$?
else
echo "$output" | pcregrep -M "$patt" > /dev/null
ex=$?
fi
if [ $ex -eq 0 ]; then
printf "%${lastlen}s\r" " " # clear previous message
printf "${PASS}: (%s) | grep '%s'\n" "$cmds" "$patt"
passed="$(echo "$passed+1" | bc -l)"
return 0
else
printf "\n%s\n" "$output"
return 1
fi
}
server_mem() {
tests="$(echo "$tests+1" | bc -l)"
local type="$1"
local string="$2"
o=$(printf "%s: ensure %s file data not in server memory\r" "$DOTS" "$type")
local lastlen=${#o}
printf "%s" "$o"
sudo gcore $server 2>/dev/null >/dev/null
grep -lia "$string" "core.$server" >/dev/null
local ex=$?
rm -f "core.$server"
if [ $ex -eq 0 ]; then
printf "%${lastlen}s\r${FAIL}: found $type data in server memory\n" " "
else
printf "%${lastlen}s\r${PASS}: $type data not in server memory\n" " "
passed="$(echo "$passed+1" | bc -l)"
fi
}