Statistics
| Branch: | Tag: | Revision:

root / test / import-export_unittest.bash @ 7e3c1da6

History | View | Annotate | Download (7.6 kB)

1 2d76b580 Michael Hanselmann
#!/bin/bash
2 2d76b580 Michael Hanselmann
#
3 2d76b580 Michael Hanselmann
4 2d76b580 Michael Hanselmann
# Copyright (C) 2010 Google Inc.
5 2d76b580 Michael Hanselmann
#
6 2d76b580 Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 2d76b580 Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 2d76b580 Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 2d76b580 Michael Hanselmann
# (at your option) any later version.
10 2d76b580 Michael Hanselmann
#
11 2d76b580 Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 2d76b580 Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 2d76b580 Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 2d76b580 Michael Hanselmann
# General Public License for more details.
15 2d76b580 Michael Hanselmann
#
16 2d76b580 Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 2d76b580 Michael Hanselmann
# along with this program; if not, write to the Free Software
18 2d76b580 Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 2d76b580 Michael Hanselmann
# 02110-1301, USA.
20 2d76b580 Michael Hanselmann
21 2d76b580 Michael Hanselmann
set -e
22 2d76b580 Michael Hanselmann
set -o pipefail
23 2d76b580 Michael Hanselmann
24 2d76b580 Michael Hanselmann
export PYTHON=${PYTHON:=python}
25 2d76b580 Michael Hanselmann
26 9d198e6f Michael Hanselmann
impexpd="$PYTHON daemons/import-export -d"
27 2d76b580 Michael Hanselmann
28 2d76b580 Michael Hanselmann
err() {
29 2d76b580 Michael Hanselmann
  echo "$@"
30 2d76b580 Michael Hanselmann
  echo 'Aborting'
31 9d198e6f Michael Hanselmann
  show_output
32 2d76b580 Michael Hanselmann
  exit 1
33 2d76b580 Michael Hanselmann
}
34 2d76b580 Michael Hanselmann
35 9d198e6f Michael Hanselmann
show_output() {
36 9d198e6f Michael Hanselmann
  if [[ -s "$dst_output" ]]; then
37 9d198e6f Michael Hanselmann
    echo
38 9d198e6f Michael Hanselmann
    echo 'Import output:'
39 9d198e6f Michael Hanselmann
    cat $dst_output
40 9d198e6f Michael Hanselmann
  fi
41 9d198e6f Michael Hanselmann
  if [[ -s "$src_output" ]]; then
42 9d198e6f Michael Hanselmann
    echo
43 9d198e6f Michael Hanselmann
    echo 'Export output:'
44 9d198e6f Michael Hanselmann
    cat $src_output
45 9d198e6f Michael Hanselmann
  fi
46 9d198e6f Michael Hanselmann
}
47 9d198e6f Michael Hanselmann
48 2d76b580 Michael Hanselmann
checkpids() {
49 2d76b580 Michael Hanselmann
  local result=0
50 2d76b580 Michael Hanselmann
51 2d76b580 Michael Hanselmann
  # Unlike combining the "wait" commands using || or &&, this ensures we
52 2d76b580 Michael Hanselmann
  # actually wait for all PIDs.
53 2d76b580 Michael Hanselmann
  for pid in "$@"; do
54 2d76b580 Michael Hanselmann
    if ! wait $pid; then
55 2d76b580 Michael Hanselmann
      result=1
56 2d76b580 Michael Hanselmann
    fi
57 2d76b580 Michael Hanselmann
  done
58 2d76b580 Michael Hanselmann
59 2d76b580 Michael Hanselmann
  return $result
60 2d76b580 Michael Hanselmann
}
61 2d76b580 Michael Hanselmann
62 2d76b580 Michael Hanselmann
get_testpath() {
63 2d76b580 Michael Hanselmann
  echo "${TOP_SRCDIR:-.}/test"
64 2d76b580 Michael Hanselmann
}
65 2d76b580 Michael Hanselmann
66 2d76b580 Michael Hanselmann
get_testfile() {
67 2d76b580 Michael Hanselmann
  echo "$(get_testpath)/data/$1"
68 2d76b580 Michael Hanselmann
}
69 2d76b580 Michael Hanselmann
70 2d76b580 Michael Hanselmann
statusdir=$(mktemp -d)
71 2d76b580 Michael Hanselmann
trap "rm -rf $statusdir" EXIT
72 2d76b580 Michael Hanselmann
73 2d76b580 Michael Hanselmann
src_statusfile=$statusdir/src.status
74 9d198e6f Michael Hanselmann
src_output=$statusdir/src.output
75 2d76b580 Michael Hanselmann
src_x509=$statusdir/src.pem
76 2d76b580 Michael Hanselmann
77 2d76b580 Michael Hanselmann
dst_statusfile=$statusdir/dst.status
78 9d198e6f Michael Hanselmann
dst_output=$statusdir/dst.output
79 2d76b580 Michael Hanselmann
dst_x509=$statusdir/dst.pem
80 2d76b580 Michael Hanselmann
dst_portfile=$statusdir/dst.port
81 2d76b580 Michael Hanselmann
82 2d76b580 Michael Hanselmann
other_x509=$statusdir/other.pem
83 2d76b580 Michael Hanselmann
84 2d76b580 Michael Hanselmann
testdata=$statusdir/data1
85 2d76b580 Michael Hanselmann
86 2d76b580 Michael Hanselmann
cmd_prefix=
87 2d76b580 Michael Hanselmann
cmd_suffix=
88 9d198e6f Michael Hanselmann
connect_timeout=10
89 9d198e6f Michael Hanselmann
connect_retries=1
90 7e3c1da6 Michael Hanselmann
compress=gzip
91 2d76b580 Michael Hanselmann
92 2d76b580 Michael Hanselmann
$impexpd >/dev/null 2>&1 &&
93 2d76b580 Michael Hanselmann
  err "daemon-util succeeded without parameters"
94 2d76b580 Michael Hanselmann
95 2d76b580 Michael Hanselmann
$impexpd foo bar baz moo boo >/dev/null 2>&1 &&
96 2d76b580 Michael Hanselmann
  err "daemon-util succeeded with wrong parameters"
97 2d76b580 Michael Hanselmann
98 2d76b580 Michael Hanselmann
$impexpd $src_statusfile >/dev/null 2>&1 &&
99 2d76b580 Michael Hanselmann
  err "daemon-util succeeded with insufficient parameters"
100 2d76b580 Michael Hanselmann
101 2d76b580 Michael Hanselmann
$impexpd $src_statusfile invalidmode >/dev/null 2>&1 &&
102 2d76b580 Michael Hanselmann
  err "daemon-util succeeded with invalid mode"
103 2d76b580 Michael Hanselmann
104 7e3c1da6 Michael Hanselmann
$impexpd $src_statusfile import --compression=rot13 >/dev/null 2>&1 &&
105 7e3c1da6 Michael Hanselmann
  err "daemon-util succeeded with invalid compression"
106 7e3c1da6 Michael Hanselmann
107 2d76b580 Michael Hanselmann
cat $(get_testfile proc_drbd8.txt) $(get_testfile cert1.pem) > $testdata
108 2d76b580 Michael Hanselmann
109 2d76b580 Michael Hanselmann
impexpd_helper() {
110 2d76b580 Michael Hanselmann
  $PYTHON $(get_testpath)/import-export_unittest-helper "$@"
111 2d76b580 Michael Hanselmann
}
112 2d76b580 Michael Hanselmann
113 9d198e6f Michael Hanselmann
upto() {
114 9d198e6f Michael Hanselmann
  echo "$(date '+%F %T'):" "$@" '...'
115 9d198e6f Michael Hanselmann
}
116 9d198e6f Michael Hanselmann
117 2d76b580 Michael Hanselmann
reset_status() {
118 9d198e6f Michael Hanselmann
  rm -f $src_statusfile $dst_output $dst_statusfile $dst_output $dst_portfile
119 2d76b580 Michael Hanselmann
}
120 2d76b580 Michael Hanselmann
121 2d76b580 Michael Hanselmann
write_data() {
122 2d76b580 Michael Hanselmann
  # Wait for connection to be established
123 2d76b580 Michael Hanselmann
  impexpd_helper $dst_statusfile connected
124 2d76b580 Michael Hanselmann
125 2d76b580 Michael Hanselmann
  cat $testdata
126 2d76b580 Michael Hanselmann
}
127 2d76b580 Michael Hanselmann
128 2d76b580 Michael Hanselmann
do_export() {
129 2d76b580 Michael Hanselmann
  # Wait for listening port
130 2d76b580 Michael Hanselmann
  impexpd_helper $dst_statusfile listen-port > $dst_portfile
131 2d76b580 Michael Hanselmann
132 2d76b580 Michael Hanselmann
  local port=$(< $dst_portfile)
133 2d76b580 Michael Hanselmann
134 2d76b580 Michael Hanselmann
  test -n "$port" || err 'Empty port file'
135 2d76b580 Michael Hanselmann
136 2d76b580 Michael Hanselmann
  do_export_to_port $port
137 2d76b580 Michael Hanselmann
}
138 2d76b580 Michael Hanselmann
139 2d76b580 Michael Hanselmann
do_export_to_port() {
140 2d76b580 Michael Hanselmann
  local port=$1
141 2d76b580 Michael Hanselmann
142 2d76b580 Michael Hanselmann
  $impexpd $src_statusfile export --bind=127.0.0.1 \
143 2d76b580 Michael Hanselmann
    --host=127.0.0.1 --port=$port \
144 2d76b580 Michael Hanselmann
    --key=$src_x509 --cert=$src_x509 --ca=$dst_x509 \
145 9d198e6f Michael Hanselmann
    --cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \
146 9d198e6f Michael Hanselmann
    --connect-timeout=$connect_timeout \
147 7e3c1da6 Michael Hanselmann
    --connect-retries=$connect_retries \
148 7e3c1da6 Michael Hanselmann
    --compress=$compress
149 2d76b580 Michael Hanselmann
}
150 2d76b580 Michael Hanselmann
151 2d76b580 Michael Hanselmann
do_import() {
152 2d76b580 Michael Hanselmann
  $impexpd $dst_statusfile import --bind=127.0.0.1 \
153 2d76b580 Michael Hanselmann
    --host=127.0.0.1 \
154 2d76b580 Michael Hanselmann
    --key=$dst_x509 --cert=$dst_x509 --ca=$src_x509 \
155 9d198e6f Michael Hanselmann
    --cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \
156 9d198e6f Michael Hanselmann
    --connect-timeout=$connect_timeout \
157 7e3c1da6 Michael Hanselmann
    --connect-retries=$connect_retries \
158 7e3c1da6 Michael Hanselmann
    --compress=$compress
159 2d76b580 Michael Hanselmann
}
160 2d76b580 Michael Hanselmann
161 9d198e6f Michael Hanselmann
upto 'Generate X509 certificates and keys'
162 2d76b580 Michael Hanselmann
impexpd_helper $src_x509 gencert
163 2d76b580 Michael Hanselmann
impexpd_helper $dst_x509 gencert
164 2d76b580 Michael Hanselmann
impexpd_helper $other_x509 gencert
165 2d76b580 Michael Hanselmann
166 9d198e6f Michael Hanselmann
upto 'Normal case'
167 2d76b580 Michael Hanselmann
reset_status
168 9d198e6f Michael Hanselmann
do_import > $statusdir/recv1 2>$dst_output & imppid=$!
169 9d198e6f Michael Hanselmann
{ write_data | do_export; } &>$src_output & exppid=$!
170 2d76b580 Michael Hanselmann
checkpids $exppid $imppid || err 'An error occurred'
171 2d76b580 Michael Hanselmann
cmp $testdata $statusdir/recv1 || err 'Received data does not match input'
172 2d76b580 Michael Hanselmann
173 9d198e6f Michael Hanselmann
upto 'Export using wrong CA'
174 2d76b580 Michael Hanselmann
reset_status
175 9d198e6f Michael Hanselmann
# Setting lower timeout to not wait for too long
176 9d198e6f Michael Hanselmann
connect_timeout=1 do_import &>$dst_output & imppid=$!
177 9d198e6f Michael Hanselmann
: | dst_x509=$other_x509 do_export &>$src_output & exppid=$!
178 2d76b580 Michael Hanselmann
checkpids $exppid $imppid && err 'Export did not fail when using wrong CA'
179 2d76b580 Michael Hanselmann
180 9d198e6f Michael Hanselmann
upto 'Import using wrong CA'
181 2d76b580 Michael Hanselmann
reset_status
182 9d198e6f Michael Hanselmann
# Setting lower timeout to not wait for too long
183 9d198e6f Michael Hanselmann
src_x509=$other_x509 connect_timeout=1 do_import &>$dst_output & imppid=$!
184 9d198e6f Michael Hanselmann
: | do_export &>$src_output & exppid=$!
185 2d76b580 Michael Hanselmann
checkpids $exppid $imppid && err 'Import did not fail when using wrong CA'
186 2d76b580 Michael Hanselmann
187 9d198e6f Michael Hanselmann
upto 'Suffix command on import'
188 2d76b580 Michael Hanselmann
reset_status
189 9d198e6f Michael Hanselmann
cmd_suffix="| cksum > $statusdir/recv2" do_import &>$dst_output & imppid=$!
190 9d198e6f Michael Hanselmann
{ write_data | do_export; } &>$src_output & exppid=$!
191 2d76b580 Michael Hanselmann
checkpids $exppid $imppid || err 'Testing additional commands failed'
192 2d76b580 Michael Hanselmann
cmp $statusdir/recv2 <(cksum < $testdata) || \
193 2d76b580 Michael Hanselmann
  err 'Checksum of received data does not match'
194 2d76b580 Michael Hanselmann
195 9d198e6f Michael Hanselmann
upto 'Prefix command on export'
196 2d76b580 Michael Hanselmann
reset_status
197 9d198e6f Michael Hanselmann
do_import > $statusdir/recv3 2>$dst_output & imppid=$!
198 9d198e6f Michael Hanselmann
{ write_data | cmd_prefix="cksum |" do_export; } &>$src_output & exppid=$!
199 2d76b580 Michael Hanselmann
checkpids $exppid $imppid || err 'Testing additional commands failed'
200 2d76b580 Michael Hanselmann
cmp $statusdir/recv3 <(cksum < $testdata) || \
201 2d76b580 Michael Hanselmann
  err 'Received checksum does not match'
202 2d76b580 Michael Hanselmann
203 9d198e6f Michael Hanselmann
upto 'Failing prefix command on export'
204 2d76b580 Michael Hanselmann
reset_status
205 9d198e6f Michael Hanselmann
: | cmd_prefix='exit 1;' do_export_to_port 0 &>$src_output & exppid=$!
206 2d76b580 Michael Hanselmann
checkpids $exppid && err 'Prefix command on export did not fail when it should'
207 2d76b580 Michael Hanselmann
208 9d198e6f Michael Hanselmann
upto 'Failing suffix command on export'
209 2d76b580 Michael Hanselmann
reset_status
210 9d198e6f Michael Hanselmann
do_import >&$src_output & imppid=$!
211 9d198e6f Michael Hanselmann
: | cmd_suffix='| exit 1' do_export &>$dst_output & exppid=$!
212 2d76b580 Michael Hanselmann
checkpids $imppid $exppid && \
213 2d76b580 Michael Hanselmann
  err 'Suffix command on export did not fail when it should'
214 2d76b580 Michael Hanselmann
215 9d198e6f Michael Hanselmann
upto 'Failing prefix command on import'
216 2d76b580 Michael Hanselmann
reset_status
217 9d198e6f Michael Hanselmann
cmd_prefix='exit 1;' do_import &>$dst_output & imppid=$!
218 2d76b580 Michael Hanselmann
checkpids $imppid && err 'Prefix command on import did not fail when it should'
219 2d76b580 Michael Hanselmann
220 9d198e6f Michael Hanselmann
upto 'Failing suffix command on import'
221 2d76b580 Michael Hanselmann
reset_status
222 9d198e6f Michael Hanselmann
cmd_suffix='| exit 1' do_import &>$dst_output & imppid=$!
223 9d198e6f Michael Hanselmann
: | do_export &>$src_output & exppid=$!
224 2d76b580 Michael Hanselmann
checkpids $imppid $exppid && \
225 2d76b580 Michael Hanselmann
  err 'Suffix command on import did not fail when it should'
226 2d76b580 Michael Hanselmann
227 9d198e6f Michael Hanselmann
upto 'Listen timeout A'
228 9d198e6f Michael Hanselmann
reset_status
229 9d198e6f Michael Hanselmann
# Setting lower timeout to not wait too long (there won't be anything trying to
230 9d198e6f Michael Hanselmann
# connect)
231 9d198e6f Michael Hanselmann
connect_timeout=1 do_import &>$dst_output & imppid=$!
232 9d198e6f Michael Hanselmann
checkpids $imppid && \
233 9d198e6f Michael Hanselmann
  err 'Listening with timeout did not fail when it should'
234 9d198e6f Michael Hanselmann
235 9d198e6f Michael Hanselmann
upto 'Listen timeout B'
236 9d198e6f Michael Hanselmann
reset_status
237 9d198e6f Michael Hanselmann
do_import &>$dst_output & imppid=$!
238 9d198e6f Michael Hanselmann
{ sleep 1; : | do_export; } &>$src_output & exppid=$!
239 9d198e6f Michael Hanselmann
checkpids $exppid $imppid || \
240 9d198e6f Michael Hanselmann
  err 'Listening with timeout failed when it should not'
241 9d198e6f Michael Hanselmann
242 7e3c1da6 Michael Hanselmann
upto 'No compression'
243 7e3c1da6 Michael Hanselmann
reset_status
244 7e3c1da6 Michael Hanselmann
compress=none do_import > $statusdir/recv-nocompr 2>$dst_output & imppid=$!
245 7e3c1da6 Michael Hanselmann
{ write_data | compress=none do_export; } &>$src_output & exppid=$!
246 7e3c1da6 Michael Hanselmann
checkpids $exppid $imppid || err 'An error occurred'
247 7e3c1da6 Michael Hanselmann
cmp $testdata $statusdir/recv-nocompr || \
248 7e3c1da6 Michael Hanselmann
  err 'Received data does not match input'
249 7e3c1da6 Michael Hanselmann
250 7e3c1da6 Michael Hanselmann
upto 'Compression mismatch A'
251 7e3c1da6 Michael Hanselmann
reset_status
252 7e3c1da6 Michael Hanselmann
compress=none do_import > $statusdir/recv-miscompr 2>$dst_output & imppid=$!
253 7e3c1da6 Michael Hanselmann
{ write_data | compress=gzip do_export; } &>$src_output & exppid=$!
254 7e3c1da6 Michael Hanselmann
checkpids $exppid $imppid || err 'An error occurred'
255 7e3c1da6 Michael Hanselmann
cmp -s $testdata $statusdir/recv-miscompr && \
256 7e3c1da6 Michael Hanselmann
  err 'Received data matches input when it should not'
257 7e3c1da6 Michael Hanselmann
258 7e3c1da6 Michael Hanselmann
upto 'Compression mismatch B'
259 7e3c1da6 Michael Hanselmann
reset_status
260 7e3c1da6 Michael Hanselmann
compress=gzip do_import > $statusdir/recv-miscompr2 2>$dst_output & imppid=$!
261 7e3c1da6 Michael Hanselmann
{ write_data | compress=none do_export; } &>$src_output & exppid=$!
262 7e3c1da6 Michael Hanselmann
checkpids $exppid $imppid && err 'Did not fail when it should'
263 7e3c1da6 Michael Hanselmann
cmp -s $testdata $statusdir/recv-miscompr2 && \
264 7e3c1da6 Michael Hanselmann
  err 'Received data matches input when it should not'
265 7e3c1da6 Michael Hanselmann
266 2d76b580 Michael Hanselmann
exit 0