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 $impexpd $src_statusfile import --compression=rot13 >/dev/null 2>&1 &&
112 err "daemon-util succeeded with invalid compression"
114 upto 'Generate test data'
115 cat $(get_testfile proc_drbd8.txt) $(get_testfile cert1.pem) > $testdata
117 # Generate about 7.5 MB of test data
118 { tmp="$(<$testdata)"
119 for (( i=0; i < 100; ++i )); do
120 echo "$tmp $tmp $tmp $tmp $tmp $tmp"
122 dd if=/dev/zero bs=1024 count=4096 2>/dev/null
123 for (( i=0; i < 100; ++i )); do
124 echo "$tmp $tmp $tmp $tmp $tmp $tmp"
129 $PYTHON $(get_testpath)/import-export_unittest-helper "$@"
135 rm -f $src_statusfile $dst_output $dst_statusfile $dst_output
136 rm -f $gencert_output
148 wait_import_ready() {
149 # Wait for listening port
150 impexpd_helper $dst_statusfile listen-port
156 $impexpd $src_statusfile export --bind=127.0.0.1 \
157 --host=127.0.0.1 --port=$port \
158 --key=$src_x509 --cert=$src_x509 --ca=$dst_x509 \
159 --cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \
160 --connect-timeout=$connect_timeout \
161 --connect-retries=$connect_retries \
166 $impexpd $dst_statusfile import --bind=127.0.0.1 \
168 --key=$dst_x509 --cert=$dst_x509 --ca=$src_x509 \
169 --cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \
170 --connect-timeout=$connect_timeout \
171 --connect-retries=$connect_retries \
175 upto 'Generate X509 certificates and keys'
176 impexpd_helper $src_x509 gencert 2>$gencert_output & srccertpid=$!
177 impexpd_helper $dst_x509 gencert 2>$gencert_output & dstcertpid=$!
178 impexpd_helper $other_x509 gencert 2>$gencert_output & othercertpid=$!
179 checkpids $srccertpid $dstcertpid $othercertpid || \
180 err 'Failed to generate certificates'
182 start_test 'Normal case'
183 do_import > $statusdir/recv1 2>$dst_output & imppid=$!
184 if port=$(wait_import_ready 2>$src_output); then
185 do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
187 checkpids $exppid $imppid || err 'An error occurred'
188 cmp $testdata $statusdir/recv1 || err 'Received data does not match input'
190 start_test 'Export using wrong CA'
191 # Setting lower timeout to not wait for too long
192 connect_timeout=1 do_import &>$dst_output & imppid=$!
193 if port=$(wait_import_ready 2>$src_output); then
194 : | dst_x509=$other_x509 do_export $port >>$src_output 2>&1 & exppid=$!
196 checkpids $exppid $imppid && err 'Export did not fail when using wrong CA'
198 start_test 'Import using wrong CA'
199 # Setting lower timeout to not wait for too long
200 src_x509=$other_x509 connect_timeout=1 do_import &>$dst_output & imppid=$!
201 if port=$(wait_import_ready 2>$src_output); then
202 : | do_export $port >>$src_output 2>&1 & exppid=$!
204 checkpids $exppid $imppid && err 'Import did not fail when using wrong CA'
206 start_test 'Suffix command on import'
207 cmd_suffix="| cksum > $statusdir/recv2" do_import &>$dst_output & imppid=$!
208 if port=$(wait_import_ready 2>$src_output); then
209 do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
211 checkpids $exppid $imppid || err 'Testing additional commands failed'
212 cmp $statusdir/recv2 <(cksum < $testdata) || \
213 err 'Checksum of received data does not match'
215 start_test 'Prefix command on export'
216 do_import > $statusdir/recv3 2>$dst_output & imppid=$!
217 if port=$(wait_import_ready 2>$src_output); then
218 cmd_prefix='cksum |' do_export $port <$testdata >>$src_output 2>&1 & exppid=$!
220 checkpids $exppid $imppid || err 'Testing additional commands failed'
221 cmp $statusdir/recv3 <(cksum < $testdata) || \
222 err 'Received checksum does not match'
224 start_test 'Failing prefix command on export'
225 : | cmd_prefix='exit 1;' do_export 0 &>$src_output & exppid=$!
226 checkpids $exppid && err 'Prefix command on export did not fail when it should'
228 start_test 'Failing suffix command on export'
229 do_import >&$dst_output & imppid=$!
230 if port=$(wait_import_ready 2>$src_output); then
231 : | cmd_suffix='| exit 1' do_export $port >>$src_output 2>&1 & exppid=$!
233 checkpids $imppid $exppid && \
234 err 'Suffix command on export did not fail when it should'
236 start_test 'Failing prefix command on import'
237 cmd_prefix='exit 1;' do_import &>$dst_output & imppid=$!
238 checkpids $imppid && err 'Prefix command on import did not fail when it should'
240 start_test 'Failing suffix command on import'
241 cmd_suffix='| exit 1' do_import &>$dst_output & imppid=$!
242 if port=$(wait_import_ready 2>$src_output); then
243 : | do_export $port >>$src_output 2>&1 & exppid=$!
245 checkpids $imppid $exppid && \
246 err 'Suffix command on import did not fail when it should'
248 start_test 'Listen timeout A'
249 # Setting lower timeout to not wait too long (there won't be anything trying to
251 connect_timeout=1 do_import &>$dst_output & imppid=$!
252 checkpids $imppid && \
253 err 'Listening with timeout did not fail when it should'
255 start_test 'Listen timeout B'
256 do_import &>$dst_output & imppid=$!
257 if port=$(wait_import_ready 2>$src_output); then
258 { sleep 1; : | do_export $port; } >>$src_output 2>&1 & exppid=$!
260 checkpids $exppid $imppid || \
261 err 'Listening with timeout failed when it should not'
263 start_test 'Connect timeout'
264 # Setting lower timeout as nothing will be listening on port 0
265 : | connect_timeout=1 do_export 0 &>$src_output & exppid=$!
266 checkpids $exppid && err 'Connection did not time out when it should'
268 start_test 'No compression'
269 compress=none do_import > $statusdir/recv-nocompr 2>$dst_output & imppid=$!
270 if port=$(wait_import_ready 2>$src_output); then
271 compress=none do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
273 checkpids $exppid $imppid || err 'An error occurred'
274 cmp $testdata $statusdir/recv-nocompr || \
275 err 'Received data does not match input'
277 start_test 'Compression mismatch A'
278 compress=none do_import > $statusdir/recv-miscompr 2>$dst_output & imppid=$!
279 if port=$(wait_import_ready 2>$src_output); then
280 compress=gzip do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
282 checkpids $exppid $imppid || err 'An error occurred'
283 cmp -s $testdata $statusdir/recv-miscompr && \
284 err 'Received data matches input when it should not'
286 start_test 'Compression mismatch B'
287 compress=gzip do_import > $statusdir/recv-miscompr2 2>$dst_output & imppid=$!
288 if port=$(wait_import_ready 2>$src_output); then
289 compress=none do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
291 checkpids $exppid $imppid && err 'Did not fail when it should'
292 cmp -s $testdata $statusdir/recv-miscompr2 && \
293 err 'Received data matches input when it should not'
295 start_test 'Large transfer'
296 do_import > $statusdir/recv-large 2>$dst_output & imppid=$!
297 if port=$(wait_import_ready 2>$src_output); then
298 do_export $port < $largetestdata >>$src_output 2>&1 & exppid=$!
300 checkpids $exppid $imppid || err 'An error occurred'
301 cmp $largetestdata $statusdir/recv-large || \
302 err 'Received data does not match input'