root / test / py / import-export_unittest.bash @ 07809f7d
History | View | Annotate | Download (11.3 kB)
1 |
#!/bin/bash |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2010, 2012 Google Inc. |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, but |
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
# General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 |
# 02110-1301, USA. |
20 |
|
21 |
set -e |
22 |
set -o pipefail |
23 |
|
24 |
export PYTHON=${PYTHON:=python} |
25 |
|
26 |
impexpd="$PYTHON daemons/import-export -d" |
27 |
|
28 |
err() { |
29 |
echo "$@" |
30 |
echo 'Aborting' |
31 |
show_output |
32 |
exit 1 |
33 |
} |
34 |
|
35 |
show_output() { |
36 |
if [[ -s "$gencert_output" ]]; then |
37 |
echo |
38 |
echo 'Generating certificates:' |
39 |
cat $gencert_output |
40 |
fi |
41 |
if [[ -s "$dst_output" ]]; then |
42 |
echo |
43 |
echo 'Import output:' |
44 |
cat $dst_output |
45 |
fi |
46 |
if [[ -s "$src_output" ]]; then |
47 |
echo |
48 |
echo 'Export output:' |
49 |
cat $src_output |
50 |
fi |
51 |
} |
52 |
|
53 |
checkpids() { |
54 |
local result=0 |
55 |
|
56 |
# Unlike combining the "wait" commands using || or &&, this ensures we |
57 |
# actually wait for all PIDs. |
58 |
for pid in "$@"; do |
59 |
if ! wait $pid; then |
60 |
result=1 |
61 |
fi |
62 |
done |
63 |
|
64 |
return $result |
65 |
} |
66 |
|
67 |
get_testpath() { |
68 |
echo "${TOP_SRCDIR:-.}/test" |
69 |
} |
70 |
|
71 |
get_testfile() { |
72 |
echo "$(get_testpath)/data/$1" |
73 |
} |
74 |
|
75 |
upto() { |
76 |
echo "$(date '+%F %T'):" "$@" '...' |
77 |
} |
78 |
|
79 |
statusdir=$(mktemp -d) |
80 |
trap "rm -rf $statusdir" EXIT |
81 |
|
82 |
gencert_output=$statusdir/gencert.output |
83 |
|
84 |
src_statusfile=$statusdir/src.status |
85 |
src_output=$statusdir/src.output |
86 |
src_x509=$statusdir/src.pem |
87 |
|
88 |
dst_statusfile=$statusdir/dst.status |
89 |
dst_output=$statusdir/dst.output |
90 |
dst_x509=$statusdir/dst.pem |
91 |
|
92 |
other_x509=$statusdir/other.pem |
93 |
|
94 |
testdata=$statusdir/data1 |
95 |
largetestdata=$statusdir/data2 |
96 |
|
97 |
upto 'Command line parameter tests' |
98 |
|
99 |
$impexpd >/dev/null 2>&1 && |
100 |
err "daemon-util succeeded without parameters" |
101 |
|
102 |
$impexpd foo bar baz moo boo >/dev/null 2>&1 && |
103 |
err "daemon-util succeeded with wrong parameters" |
104 |
|
105 |
$impexpd $src_statusfile >/dev/null 2>&1 && |
106 |
err "daemon-util succeeded with insufficient parameters" |
107 |
|
108 |
$impexpd $src_statusfile invalidmode >/dev/null 2>&1 && |
109 |
err "daemon-util succeeded with invalid mode" |
110 |
|
111 |
for mode in import export; do |
112 |
$impexpd $src_statusfile $mode --compression=rot13 >/dev/null 2>&1 && |
113 |
err "daemon-util succeeded with invalid compression" |
114 |
|
115 |
for host in '' ' ' ' s p a c e' ... , foo.example.net... \ |
116 |
'some"evil"name' 'x\ny\tmoo'; do |
117 |
$impexpd $src_statusfile $mode --host="$host" >/dev/null 2>&1 && |
118 |
err "daemon-util succeeded with invalid host '$host'" |
119 |
done |
120 |
|
121 |
for port in '' ' ' -1234 'some ` port " here'; do |
122 |
$impexpd $src_statusfile $mode --port="$port" >/dev/null 2>&1 && |
123 |
err "daemon-util succeeded with invalid port '$port'" |
124 |
done |
125 |
|
126 |
for magic in '' ' ' 'this`is' 'invalid!magic' 'he"re'; do |
127 |
$impexpd $src_statusfile $mode --magic="$magic" >/dev/null 2>&1 && |
128 |
err "daemon-util succeeded with invalid magic '$magic'" |
129 |
done |
130 |
done |
131 |
|
132 |
upto 'Generate test data' |
133 |
cat $(get_testfile proc_drbd8.txt) $(get_testfile cert1.pem) > $testdata |
134 |
|
135 |
# Generate about 7.5 MB of test data |
136 |
{ tmp="$(<$testdata)" |
137 |
for (( i=0; i < 100; ++i )); do |
138 |
echo "$tmp $tmp $tmp $tmp $tmp $tmp" |
139 |
done |
140 |
dd if=/dev/zero bs=1024 count=4096 2>/dev/null |
141 |
for (( i=0; i < 100; ++i )); do |
142 |
echo "$tmp $tmp $tmp $tmp $tmp $tmp" |
143 |
done |
144 |
} > $largetestdata |
145 |
|
146 |
impexpd_helper() { |
147 |
$PYTHON $(get_testpath)/py/import-export_unittest-helper "$@" |
148 |
} |
149 |
|
150 |
start_test() { |
151 |
upto "$@" |
152 |
|
153 |
rm -f $src_statusfile $dst_output $dst_statusfile $dst_output |
154 |
rm -f $gencert_output |
155 |
|
156 |
imppid= |
157 |
exppid= |
158 |
|
159 |
cmd_prefix= |
160 |
cmd_suffix= |
161 |
connect_timeout=30 |
162 |
connect_retries=1 |
163 |
compress=gzip |
164 |
magic= |
165 |
} |
166 |
|
167 |
wait_import_ready() { |
168 |
# Wait for listening port |
169 |
impexpd_helper $dst_statusfile listen-port |
170 |
} |
171 |
|
172 |
do_export() { |
173 |
local port=$1 |
174 |
|
175 |
$impexpd $src_statusfile export --bind=127.0.0.1 \ |
176 |
--host=127.0.0.1 --port=$port \ |
177 |
--key=$src_x509 --cert=$src_x509 --ca=$dst_x509 \ |
178 |
--cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \ |
179 |
--connect-timeout=$connect_timeout \ |
180 |
--connect-retries=$connect_retries \ |
181 |
--compress=$compress ${magic:+--magic="$magic"} |
182 |
} |
183 |
|
184 |
do_import() { |
185 |
$impexpd $dst_statusfile import --bind=127.0.0.1 \ |
186 |
--host=127.0.0.1 \ |
187 |
--key=$dst_x509 --cert=$dst_x509 --ca=$src_x509 \ |
188 |
--cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \ |
189 |
--connect-timeout=$connect_timeout \ |
190 |
--connect-retries=$connect_retries \ |
191 |
--compress=$compress ${magic:+--magic="$magic"} |
192 |
} |
193 |
|
194 |
upto 'Generate X509 certificates and keys' |
195 |
impexpd_helper $src_x509 gencert 2>$gencert_output & srccertpid=$! |
196 |
impexpd_helper $dst_x509 gencert 2>$gencert_output & dstcertpid=$! |
197 |
impexpd_helper $other_x509 gencert 2>$gencert_output & othercertpid=$! |
198 |
checkpids $srccertpid $dstcertpid $othercertpid || \ |
199 |
err 'Failed to generate certificates' |
200 |
|
201 |
start_test 'Normal case' |
202 |
do_import > $statusdir/recv1 2>$dst_output & imppid=$! |
203 |
if port=$(wait_import_ready 2>$src_output); then |
204 |
do_export $port < $testdata >>$src_output 2>&1 & exppid=$! |
205 |
fi |
206 |
checkpids $exppid $imppid || err 'An error occurred' |
207 |
cmp $testdata $statusdir/recv1 || err 'Received data does not match input' |
208 |
|
209 |
start_test 'Export using wrong CA' |
210 |
# Setting lower timeout to not wait for too long |
211 |
connect_timeout=1 do_import &>$dst_output & imppid=$! |
212 |
if port=$(wait_import_ready 2>$src_output); then |
213 |
: | dst_x509=$other_x509 do_export $port >>$src_output 2>&1 & exppid=$! |
214 |
fi |
215 |
checkpids $exppid $imppid && err 'Export did not fail when using wrong CA' |
216 |
|
217 |
start_test 'Import using wrong CA' |
218 |
# Setting lower timeout to not wait for too long |
219 |
src_x509=$other_x509 connect_timeout=1 do_import &>$dst_output & imppid=$! |
220 |
if port=$(wait_import_ready 2>$src_output); then |
221 |
: | do_export $port >>$src_output 2>&1 & exppid=$! |
222 |
fi |
223 |
checkpids $exppid $imppid && err 'Import did not fail when using wrong CA' |
224 |
|
225 |
start_test 'Suffix command on import' |
226 |
cmd_suffix="| cksum > $statusdir/recv2" do_import &>$dst_output & imppid=$! |
227 |
if port=$(wait_import_ready 2>$src_output); then |
228 |
do_export $port < $testdata >>$src_output 2>&1 & exppid=$! |
229 |
fi |
230 |
checkpids $exppid $imppid || err 'Testing additional commands failed' |
231 |
cmp $statusdir/recv2 <(cksum < $testdata) || \ |
232 |
err 'Checksum of received data does not match' |
233 |
|
234 |
start_test 'Prefix command on export' |
235 |
do_import > $statusdir/recv3 2>$dst_output & imppid=$! |
236 |
if port=$(wait_import_ready 2>$src_output); then |
237 |
cmd_prefix='cksum |' do_export $port <$testdata >>$src_output 2>&1 & exppid=$! |
238 |
fi |
239 |
checkpids $exppid $imppid || err 'Testing additional commands failed' |
240 |
cmp $statusdir/recv3 <(cksum < $testdata) || \ |
241 |
err 'Received checksum does not match' |
242 |
|
243 |
start_test 'Failing prefix command on export' |
244 |
: | cmd_prefix='exit 1;' do_export 0 &>$src_output & exppid=$! |
245 |
checkpids $exppid && err 'Prefix command on export did not fail when it should' |
246 |
|
247 |
start_test 'Failing suffix command on export' |
248 |
do_import >&$dst_output & imppid=$! |
249 |
if port=$(wait_import_ready 2>$src_output); then |
250 |
: | cmd_suffix='| exit 1' do_export $port >>$src_output 2>&1 & exppid=$! |
251 |
fi |
252 |
checkpids $imppid $exppid && \ |
253 |
err 'Suffix command on export did not fail when it should' |
254 |
|
255 |
start_test 'Failing prefix command on import' |
256 |
cmd_prefix='exit 1;' do_import &>$dst_output & imppid=$! |
257 |
checkpids $imppid && err 'Prefix command on import did not fail when it should' |
258 |
|
259 |
start_test 'Failing suffix command on import' |
260 |
cmd_suffix='| exit 1' do_import &>$dst_output & imppid=$! |
261 |
if port=$(wait_import_ready 2>$src_output); then |
262 |
: | do_export $port >>$src_output 2>&1 & exppid=$! |
263 |
fi |
264 |
checkpids $imppid $exppid && \ |
265 |
err 'Suffix command on import did not fail when it should' |
266 |
|
267 |
start_test 'Listen timeout A' |
268 |
# Setting lower timeout to not wait too long (there won't be anything trying to |
269 |
# connect) |
270 |
connect_timeout=1 do_import &>$dst_output & imppid=$! |
271 |
checkpids $imppid && \ |
272 |
err 'Listening with timeout did not fail when it should' |
273 |
|
274 |
start_test 'Listen timeout B' |
275 |
do_import &>$dst_output & imppid=$! |
276 |
if port=$(wait_import_ready 2>$src_output); then |
277 |
{ sleep 1; : | do_export $port; } >>$src_output 2>&1 & exppid=$! |
278 |
fi |
279 |
checkpids $exppid $imppid || \ |
280 |
err 'Listening with timeout failed when it should not' |
281 |
|
282 |
start_test 'Connect timeout' |
283 |
# Setting lower timeout as nothing will be listening on port 0 |
284 |
: | connect_timeout=1 do_export 0 &>$src_output & exppid=$! |
285 |
checkpids $exppid && err 'Connection did not time out when it should' |
286 |
|
287 |
start_test 'No compression' |
288 |
compress=none do_import > $statusdir/recv-nocompr 2>$dst_output & imppid=$! |
289 |
if port=$(wait_import_ready 2>$src_output); then |
290 |
compress=none do_export $port < $testdata >>$src_output 2>&1 & exppid=$! |
291 |
fi |
292 |
checkpids $exppid $imppid || err 'An error occurred' |
293 |
cmp $testdata $statusdir/recv-nocompr || \ |
294 |
err 'Received data does not match input' |
295 |
|
296 |
start_test 'Compression mismatch A' |
297 |
compress=none do_import > $statusdir/recv-miscompr 2>$dst_output & imppid=$! |
298 |
if port=$(wait_import_ready 2>$src_output); then |
299 |
compress=gzip do_export $port < $testdata >>$src_output 2>&1 & exppid=$! |
300 |
fi |
301 |
checkpids $exppid $imppid || err 'An error occurred' |
302 |
cmp -s $testdata $statusdir/recv-miscompr && \ |
303 |
err 'Received data matches input when it should not' |
304 |
|
305 |
start_test 'Compression mismatch B' |
306 |
compress=gzip do_import > $statusdir/recv-miscompr2 2>$dst_output & imppid=$! |
307 |
if port=$(wait_import_ready 2>$src_output); then |
308 |
compress=none do_export $port < $testdata >>$src_output 2>&1 & exppid=$! |
309 |
fi |
310 |
checkpids $exppid $imppid && err 'Did not fail when it should' |
311 |
cmp -s $testdata $statusdir/recv-miscompr2 && \ |
312 |
err 'Received data matches input when it should not' |
313 |
|
314 |
start_test 'Magic without compression' |
315 |
compress=none magic=MagicValue13582 \ |
316 |
do_import > $statusdir/recv-magic1 2>$dst_output & imppid=$! |
317 |
if port=$(wait_import_ready 2>$src_output); then |
318 |
compress=none magic=MagicValue13582 \ |
319 |
do_export $port < $testdata >>$src_output 2>&1 & exppid=$! |
320 |
fi |
321 |
checkpids $exppid $imppid || err 'An error occurred' |
322 |
cmp $testdata $statusdir/recv-magic1 || err 'Received data does not match input' |
323 |
|
324 |
start_test 'Magic with compression' |
325 |
compress=gzip magic=yzD1FBH7Iw \ |
326 |
do_import > $statusdir/recv-magic2 2>$dst_output & imppid=$! |
327 |
if port=$(wait_import_ready 2>$src_output); then |
328 |
compress=gzip magic=yzD1FBH7Iw \ |
329 |
do_export $port < $testdata >>$src_output 2>&1 & exppid=$! |
330 |
fi |
331 |
checkpids $exppid $imppid || err 'An error occurred' |
332 |
cmp $testdata $statusdir/recv-magic2 || err 'Received data does not match input' |
333 |
|
334 |
start_test 'Magic mismatch A (same length)' |
335 |
magic=h0tmIKXK do_import > $statusdir/recv-magic3 2>$dst_output & imppid=$! |
336 |
if port=$(wait_import_ready 2>$src_output); then |
337 |
magic=bo6m9uAw do_export $port < $testdata >>$src_output 2>&1 & exppid=$! |
338 |
fi |
339 |
checkpids $exppid $imppid && err 'Did not fail when it should' |
340 |
|
341 |
start_test 'Magic mismatch B' |
342 |
magic=AUxVEWXVr5GK do_import > $statusdir/recv-magic4 2>$dst_output & imppid=$! |
343 |
if port=$(wait_import_ready 2>$src_output); then |
344 |
magic=74RiP9KP do_export $port < $testdata >>$src_output 2>&1 & exppid=$! |
345 |
fi |
346 |
checkpids $exppid $imppid && err 'Did not fail when it should' |
347 |
|
348 |
start_test 'Large transfer' |
349 |
do_import > $statusdir/recv-large 2>$dst_output & imppid=$! |
350 |
if port=$(wait_import_ready 2>$src_output); then |
351 |
do_export $port < $largetestdata >>$src_output 2>&1 & exppid=$! |
352 |
fi |
353 |
checkpids $exppid $imppid || err 'An error occurred' |
354 |
cmp $largetestdata $statusdir/recv-large || \ |
355 |
err 'Received data does not match input' |
356 |
|
357 |
exit 0 |