4 # Copyright (C) 2010 Google Inc.
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.
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.
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
24 export PYTHON=${PYTHON:=python}
26 impexpd="$PYTHON daemons/import-export -d"
36 if [[ -s "$gencert_output" ]]; then
38 echo 'Generating certificates:'
41 if [[ -s "$dst_output" ]]; then
46 if [[ -s "$src_output" ]]; then
56 # Unlike combining the "wait" commands using || or &&, this ensures we
57 # actually wait for all PIDs.
68 echo "${TOP_SRCDIR:-.}/test"
72 echo "$(get_testpath)/data/$1"
76 echo "$(date '+%F %T'):" "$@" '...'
79 statusdir=$(mktemp -d)
80 trap "rm -rf $statusdir" EXIT
82 gencert_output=$statusdir/gencert.output
84 src_statusfile=$statusdir/src.status
85 src_output=$statusdir/src.output
86 src_x509=$statusdir/src.pem
88 dst_statusfile=$statusdir/dst.status
89 dst_output=$statusdir/dst.output
90 dst_x509=$statusdir/dst.pem
92 other_x509=$statusdir/other.pem
94 testdata=$statusdir/data1
95 largetestdata=$statusdir/data2
97 upto 'Command line parameter tests'
99 $impexpd >/dev/null 2>&1 &&
100 err "daemon-util succeeded without parameters"
102 $impexpd foo bar baz moo boo >/dev/null 2>&1 &&
103 err "daemon-util succeeded with wrong parameters"
105 $impexpd $src_statusfile >/dev/null 2>&1 &&
106 err "daemon-util succeeded with insufficient parameters"
108 $impexpd $src_statusfile invalidmode >/dev/null 2>&1 &&
109 err "daemon-util succeeded with invalid mode"
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"
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'"
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'"
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'"
132 upto 'Generate test data'
133 cat $(get_testfile proc_drbd8.txt) $(get_testfile cert1.pem) > $testdata
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"
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"
147 $PYTHON $(get_testpath)/import-export_unittest-helper "$@"
153 rm -f $src_statusfile $dst_output $dst_statusfile $dst_output
154 rm -f $gencert_output
167 wait_import_ready() {
168 # Wait for listening port
169 impexpd_helper $dst_statusfile listen-port
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"}
185 $impexpd $dst_statusfile import --bind=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"}
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'
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=$!
206 checkpids $exppid $imppid || err 'An error occurred'
207 cmp $testdata $statusdir/recv1 || err 'Received data does not match input'
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=$!
215 checkpids $exppid $imppid && err 'Export did not fail when using wrong CA'
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=$!
223 checkpids $exppid $imppid && err 'Import did not fail when using wrong CA'
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=$!
230 checkpids $exppid $imppid || err 'Testing additional commands failed'
231 cmp $statusdir/recv2 <(cksum < $testdata) || \
232 err 'Checksum of received data does not match'
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=$!
239 checkpids $exppid $imppid || err 'Testing additional commands failed'
240 cmp $statusdir/recv3 <(cksum < $testdata) || \
241 err 'Received checksum does not match'
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'
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=$!
252 checkpids $imppid $exppid && \
253 err 'Suffix command on export did not fail when it should'
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'
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=$!
264 checkpids $imppid $exppid && \
265 err 'Suffix command on import did not fail when it should'
267 start_test 'Listen timeout A'
268 # Setting lower timeout to not wait too long (there won't be anything trying to
270 connect_timeout=1 do_import &>$dst_output & imppid=$!
271 checkpids $imppid && \
272 err 'Listening with timeout did not fail when it should'
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=$!
279 checkpids $exppid $imppid || \
280 err 'Listening with timeout failed when it should not'
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'
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=$!
292 checkpids $exppid $imppid || err 'An error occurred'
293 cmp $testdata $statusdir/recv-nocompr || \
294 err 'Received data does not match input'
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=$!
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'
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=$!
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'
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=$!
321 checkpids $exppid $imppid || err 'An error occurred'
322 cmp $testdata $statusdir/recv-magic1 || err 'Received data does not match input'
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=$!
331 checkpids $exppid $imppid || err 'An error occurred'
332 cmp $testdata $statusdir/recv-magic2 || err 'Received data does not match input'
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=$!
339 checkpids $exppid $imppid && err 'Did not fail when it should'
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=$!
346 checkpids $exppid $imppid && err 'Did not fail when it should'
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=$!
353 checkpids $exppid $imppid || err 'An error occurred'
354 cmp $largetestdata $statusdir/recv-large || \
355 err 'Received data does not match input'