root / test / import-export_unittest.bash @ c6ccba7e
History | View | Annotate | Download (6.4 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 |
|
91 |
$impexpd >/dev/null 2>&1 && |
92 |
err "daemon-util succeeded without parameters" |
93 |
|
94 |
$impexpd foo bar baz moo boo >/dev/null 2>&1 && |
95 |
err "daemon-util succeeded with wrong parameters" |
96 |
|
97 |
$impexpd $src_statusfile >/dev/null 2>&1 && |
98 |
err "daemon-util succeeded with insufficient parameters" |
99 |
|
100 |
$impexpd $src_statusfile invalidmode >/dev/null 2>&1 && |
101 |
err "daemon-util succeeded with invalid mode" |
102 |
|
103 |
cat $(get_testfile proc_drbd8.txt) $(get_testfile cert1.pem) > $testdata |
104 |
|
105 |
impexpd_helper() { |
106 |
$PYTHON $(get_testpath)/import-export_unittest-helper "$@" |
107 |
} |
108 |
|
109 |
upto() { |
110 |
echo "$(date '+%F %T'):" "$@" '...' |
111 |
} |
112 |
|
113 |
reset_status() { |
114 |
rm -f $src_statusfile $dst_output $dst_statusfile $dst_output $dst_portfile |
115 |
} |
116 |
|
117 |
write_data() { |
118 |
# Wait for connection to be established |
119 |
impexpd_helper $dst_statusfile connected |
120 |
|
121 |
cat $testdata |
122 |
} |
123 |
|
124 |
do_export() { |
125 |
# Wait for listening port |
126 |
impexpd_helper $dst_statusfile listen-port > $dst_portfile |
127 |
|
128 |
local port=$(< $dst_portfile) |
129 |
|
130 |
test -n "$port" || err 'Empty port file' |
131 |
|
132 |
do_export_to_port $port |
133 |
} |
134 |
|
135 |
do_export_to_port() { |
136 |
local port=$1 |
137 |
|
138 |
$impexpd $src_statusfile export --bind=127.0.0.1 \ |
139 |
--host=127.0.0.1 --port=$port \ |
140 |
--key=$src_x509 --cert=$src_x509 --ca=$dst_x509 \ |
141 |
--cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \ |
142 |
--connect-timeout=$connect_timeout \ |
143 |
--connect-retries=$connect_retries |
144 |
} |
145 |
|
146 |
do_import() { |
147 |
$impexpd $dst_statusfile import --bind=127.0.0.1 \ |
148 |
--host=127.0.0.1 \ |
149 |
--key=$dst_x509 --cert=$dst_x509 --ca=$src_x509 \ |
150 |
--cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \ |
151 |
--connect-timeout=$connect_timeout \ |
152 |
--connect-retries=$connect_retries |
153 |
} |
154 |
|
155 |
upto 'Generate X509 certificates and keys' |
156 |
impexpd_helper $src_x509 gencert |
157 |
impexpd_helper $dst_x509 gencert |
158 |
impexpd_helper $other_x509 gencert |
159 |
|
160 |
upto 'Normal case' |
161 |
reset_status |
162 |
do_import > $statusdir/recv1 2>$dst_output & imppid=$! |
163 |
{ write_data | do_export; } &>$src_output & exppid=$! |
164 |
checkpids $exppid $imppid || err 'An error occurred' |
165 |
cmp $testdata $statusdir/recv1 || err 'Received data does not match input' |
166 |
|
167 |
upto 'Export using wrong CA' |
168 |
reset_status |
169 |
# Setting lower timeout to not wait for too long |
170 |
connect_timeout=1 do_import &>$dst_output & imppid=$! |
171 |
: | dst_x509=$other_x509 do_export &>$src_output & exppid=$! |
172 |
checkpids $exppid $imppid && err 'Export did not fail when using wrong CA' |
173 |
|
174 |
upto 'Import using wrong CA' |
175 |
reset_status |
176 |
# Setting lower timeout to not wait for too long |
177 |
src_x509=$other_x509 connect_timeout=1 do_import &>$dst_output & imppid=$! |
178 |
: | do_export &>$src_output & exppid=$! |
179 |
checkpids $exppid $imppid && err 'Import did not fail when using wrong CA' |
180 |
|
181 |
upto 'Suffix command on import' |
182 |
reset_status |
183 |
cmd_suffix="| cksum > $statusdir/recv2" do_import &>$dst_output & imppid=$! |
184 |
{ write_data | do_export; } &>$src_output & exppid=$! |
185 |
checkpids $exppid $imppid || err 'Testing additional commands failed' |
186 |
cmp $statusdir/recv2 <(cksum < $testdata) || \ |
187 |
err 'Checksum of received data does not match' |
188 |
|
189 |
upto 'Prefix command on export' |
190 |
reset_status |
191 |
do_import > $statusdir/recv3 2>$dst_output & imppid=$! |
192 |
{ write_data | cmd_prefix="cksum |" do_export; } &>$src_output & exppid=$! |
193 |
checkpids $exppid $imppid || err 'Testing additional commands failed' |
194 |
cmp $statusdir/recv3 <(cksum < $testdata) || \ |
195 |
err 'Received checksum does not match' |
196 |
|
197 |
upto 'Failing prefix command on export' |
198 |
reset_status |
199 |
: | cmd_prefix='exit 1;' do_export_to_port 0 &>$src_output & exppid=$! |
200 |
checkpids $exppid && err 'Prefix command on export did not fail when it should' |
201 |
|
202 |
upto 'Failing suffix command on export' |
203 |
reset_status |
204 |
do_import >&$src_output & imppid=$! |
205 |
: | cmd_suffix='| exit 1' do_export &>$dst_output & exppid=$! |
206 |
checkpids $imppid $exppid && \ |
207 |
err 'Suffix command on export did not fail when it should' |
208 |
|
209 |
upto 'Failing prefix command on import' |
210 |
reset_status |
211 |
cmd_prefix='exit 1;' do_import &>$dst_output & imppid=$! |
212 |
checkpids $imppid && err 'Prefix command on import did not fail when it should' |
213 |
|
214 |
upto 'Failing suffix command on import' |
215 |
reset_status |
216 |
cmd_suffix='| exit 1' do_import &>$dst_output & imppid=$! |
217 |
: | do_export &>$src_output & exppid=$! |
218 |
checkpids $imppid $exppid && \ |
219 |
err 'Suffix command on import did not fail when it should' |
220 |
|
221 |
upto 'Listen timeout A' |
222 |
reset_status |
223 |
# Setting lower timeout to not wait too long (there won't be anything trying to |
224 |
# connect) |
225 |
connect_timeout=1 do_import &>$dst_output & imppid=$! |
226 |
checkpids $imppid && \ |
227 |
err 'Listening with timeout did not fail when it should' |
228 |
|
229 |
upto 'Listen timeout B' |
230 |
reset_status |
231 |
do_import &>$dst_output & imppid=$! |
232 |
{ sleep 1; : | do_export; } &>$src_output & exppid=$! |
233 |
checkpids $exppid $imppid || \ |
234 |
err 'Listening with timeout failed when it should not' |
235 |
|
236 |
exit 0 |