root / test / import-export_unittest.bash @ 7e3c1da6
History | View | Annotate | Download (7.6 kB)
1 |
#!/bin/bash |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2010 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 "$dst_output" ]]; then |
37 |
echo |
38 |
echo 'Import output:' |
39 |
cat $dst_output |
40 |
fi |
41 |
if [[ -s "$src_output" ]]; then |
42 |
echo |
43 |
echo 'Export output:' |
44 |
cat $src_output |
45 |
fi |
46 |
} |
47 |
|
48 |
checkpids() { |
49 |
local result=0 |
50 |
|
51 |
# Unlike combining the "wait" commands using || or &&, this ensures we |
52 |
# actually wait for all PIDs. |
53 |
for pid in "$@"; do |
54 |
if ! wait $pid; then |
55 |
result=1 |
56 |
fi |
57 |
done |
58 |
|
59 |
return $result |
60 |
} |
61 |
|
62 |
get_testpath() { |
63 |
echo "${TOP_SRCDIR:-.}/test" |
64 |
} |
65 |
|
66 |
get_testfile() { |
67 |
echo "$(get_testpath)/data/$1" |
68 |
} |
69 |
|
70 |
statusdir=$(mktemp -d) |
71 |
trap "rm -rf $statusdir" EXIT |
72 |
|
73 |
src_statusfile=$statusdir/src.status |
74 |
src_output=$statusdir/src.output |
75 |
src_x509=$statusdir/src.pem |
76 |
|
77 |
dst_statusfile=$statusdir/dst.status |
78 |
dst_output=$statusdir/dst.output |
79 |
dst_x509=$statusdir/dst.pem |
80 |
dst_portfile=$statusdir/dst.port |
81 |
|
82 |
other_x509=$statusdir/other.pem |
83 |
|
84 |
testdata=$statusdir/data1 |
85 |
|
86 |
cmd_prefix= |
87 |
cmd_suffix= |
88 |
connect_timeout=10 |
89 |
connect_retries=1 |
90 |
compress=gzip |
91 |
|
92 |
$impexpd >/dev/null 2>&1 && |
93 |
err "daemon-util succeeded without parameters" |
94 |
|
95 |
$impexpd foo bar baz moo boo >/dev/null 2>&1 && |
96 |
err "daemon-util succeeded with wrong parameters" |
97 |
|
98 |
$impexpd $src_statusfile >/dev/null 2>&1 && |
99 |
err "daemon-util succeeded with insufficient parameters" |
100 |
|
101 |
$impexpd $src_statusfile invalidmode >/dev/null 2>&1 && |
102 |
err "daemon-util succeeded with invalid mode" |
103 |
|
104 |
$impexpd $src_statusfile import --compression=rot13 >/dev/null 2>&1 && |
105 |
err "daemon-util succeeded with invalid compression" |
106 |
|
107 |
cat $(get_testfile proc_drbd8.txt) $(get_testfile cert1.pem) > $testdata |
108 |
|
109 |
impexpd_helper() { |
110 |
$PYTHON $(get_testpath)/import-export_unittest-helper "$@" |
111 |
} |
112 |
|
113 |
upto() { |
114 |
echo "$(date '+%F %T'):" "$@" '...' |
115 |
} |
116 |
|
117 |
reset_status() { |
118 |
rm -f $src_statusfile $dst_output $dst_statusfile $dst_output $dst_portfile |
119 |
} |
120 |
|
121 |
write_data() { |
122 |
# Wait for connection to be established |
123 |
impexpd_helper $dst_statusfile connected |
124 |
|
125 |
cat $testdata |
126 |
} |
127 |
|
128 |
do_export() { |
129 |
# Wait for listening port |
130 |
impexpd_helper $dst_statusfile listen-port > $dst_portfile |
131 |
|
132 |
local port=$(< $dst_portfile) |
133 |
|
134 |
test -n "$port" || err 'Empty port file' |
135 |
|
136 |
do_export_to_port $port |
137 |
} |
138 |
|
139 |
do_export_to_port() { |
140 |
local port=$1 |
141 |
|
142 |
$impexpd $src_statusfile export --bind=127.0.0.1 \ |
143 |
--host=127.0.0.1 --port=$port \ |
144 |
--key=$src_x509 --cert=$src_x509 --ca=$dst_x509 \ |
145 |
--cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \ |
146 |
--connect-timeout=$connect_timeout \ |
147 |
--connect-retries=$connect_retries \ |
148 |
--compress=$compress |
149 |
} |
150 |
|
151 |
do_import() { |
152 |
$impexpd $dst_statusfile import --bind=127.0.0.1 \ |
153 |
--host=127.0.0.1 \ |
154 |
--key=$dst_x509 --cert=$dst_x509 --ca=$src_x509 \ |
155 |
--cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \ |
156 |
--connect-timeout=$connect_timeout \ |
157 |
--connect-retries=$connect_retries \ |
158 |
--compress=$compress |
159 |
} |
160 |
|
161 |
upto 'Generate X509 certificates and keys' |
162 |
impexpd_helper $src_x509 gencert |
163 |
impexpd_helper $dst_x509 gencert |
164 |
impexpd_helper $other_x509 gencert |
165 |
|
166 |
upto 'Normal case' |
167 |
reset_status |
168 |
do_import > $statusdir/recv1 2>$dst_output & imppid=$! |
169 |
{ write_data | do_export; } &>$src_output & exppid=$! |
170 |
checkpids $exppid $imppid || err 'An error occurred' |
171 |
cmp $testdata $statusdir/recv1 || err 'Received data does not match input' |
172 |
|
173 |
upto 'Export using wrong CA' |
174 |
reset_status |
175 |
# Setting lower timeout to not wait for too long |
176 |
connect_timeout=1 do_import &>$dst_output & imppid=$! |
177 |
: | dst_x509=$other_x509 do_export &>$src_output & exppid=$! |
178 |
checkpids $exppid $imppid && err 'Export did not fail when using wrong CA' |
179 |
|
180 |
upto 'Import using wrong CA' |
181 |
reset_status |
182 |
# Setting lower timeout to not wait for too long |
183 |
src_x509=$other_x509 connect_timeout=1 do_import &>$dst_output & imppid=$! |
184 |
: | do_export &>$src_output & exppid=$! |
185 |
checkpids $exppid $imppid && err 'Import did not fail when using wrong CA' |
186 |
|
187 |
upto 'Suffix command on import' |
188 |
reset_status |
189 |
cmd_suffix="| cksum > $statusdir/recv2" do_import &>$dst_output & imppid=$! |
190 |
{ write_data | do_export; } &>$src_output & exppid=$! |
191 |
checkpids $exppid $imppid || err 'Testing additional commands failed' |
192 |
cmp $statusdir/recv2 <(cksum < $testdata) || \ |
193 |
err 'Checksum of received data does not match' |
194 |
|
195 |
upto 'Prefix command on export' |
196 |
reset_status |
197 |
do_import > $statusdir/recv3 2>$dst_output & imppid=$! |
198 |
{ write_data | cmd_prefix="cksum |" do_export; } &>$src_output & exppid=$! |
199 |
checkpids $exppid $imppid || err 'Testing additional commands failed' |
200 |
cmp $statusdir/recv3 <(cksum < $testdata) || \ |
201 |
err 'Received checksum does not match' |
202 |
|
203 |
upto 'Failing prefix command on export' |
204 |
reset_status |
205 |
: | cmd_prefix='exit 1;' do_export_to_port 0 &>$src_output & exppid=$! |
206 |
checkpids $exppid && err 'Prefix command on export did not fail when it should' |
207 |
|
208 |
upto 'Failing suffix command on export' |
209 |
reset_status |
210 |
do_import >&$src_output & imppid=$! |
211 |
: | cmd_suffix='| exit 1' do_export &>$dst_output & exppid=$! |
212 |
checkpids $imppid $exppid && \ |
213 |
err 'Suffix command on export did not fail when it should' |
214 |
|
215 |
upto 'Failing prefix command on import' |
216 |
reset_status |
217 |
cmd_prefix='exit 1;' do_import &>$dst_output & imppid=$! |
218 |
checkpids $imppid && err 'Prefix command on import did not fail when it should' |
219 |
|
220 |
upto 'Failing suffix command on import' |
221 |
reset_status |
222 |
cmd_suffix='| exit 1' do_import &>$dst_output & imppid=$! |
223 |
: | do_export &>$src_output & exppid=$! |
224 |
checkpids $imppid $exppid && \ |
225 |
err 'Suffix command on import did not fail when it should' |
226 |
|
227 |
upto 'Listen timeout A' |
228 |
reset_status |
229 |
# Setting lower timeout to not wait too long (there won't be anything trying to |
230 |
# connect) |
231 |
connect_timeout=1 do_import &>$dst_output & imppid=$! |
232 |
checkpids $imppid && \ |
233 |
err 'Listening with timeout did not fail when it should' |
234 |
|
235 |
upto 'Listen timeout B' |
236 |
reset_status |
237 |
do_import &>$dst_output & imppid=$! |
238 |
{ sleep 1; : | do_export; } &>$src_output & exppid=$! |
239 |
checkpids $exppid $imppid || \ |
240 |
err 'Listening with timeout failed when it should not' |
241 |
|
242 |
upto 'No compression' |
243 |
reset_status |
244 |
compress=none do_import > $statusdir/recv-nocompr 2>$dst_output & imppid=$! |
245 |
{ write_data | compress=none do_export; } &>$src_output & exppid=$! |
246 |
checkpids $exppid $imppid || err 'An error occurred' |
247 |
cmp $testdata $statusdir/recv-nocompr || \ |
248 |
err 'Received data does not match input' |
249 |
|
250 |
upto 'Compression mismatch A' |
251 |
reset_status |
252 |
compress=none do_import > $statusdir/recv-miscompr 2>$dst_output & imppid=$! |
253 |
{ write_data | compress=gzip do_export; } &>$src_output & exppid=$! |
254 |
checkpids $exppid $imppid || err 'An error occurred' |
255 |
cmp -s $testdata $statusdir/recv-miscompr && \ |
256 |
err 'Received data matches input when it should not' |
257 |
|
258 |
upto 'Compression mismatch B' |
259 |
reset_status |
260 |
compress=gzip do_import > $statusdir/recv-miscompr2 2>$dst_output & imppid=$! |
261 |
{ write_data | compress=none do_export; } &>$src_output & exppid=$! |
262 |
checkpids $exppid $imppid && err 'Did not fail when it should' |
263 |
cmp -s $testdata $statusdir/recv-miscompr2 && \ |
264 |
err 'Received data matches input when it should not' |
265 |
|
266 |
exit 0 |