Statistics
| Branch: | Tag: | Revision:

root / test / import-export_unittest.bash @ c6ccba7e

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