Statistics
| Branch: | Tag: | Revision:

root / test / import-export_unittest.bash @ acd65a16

History | View | Annotate | Download (9.7 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 "$gencert_output" ]]; then
37
    echo
38
    echo 'Generating certificates:'
39
    cat $gencert_output
40
  fi
41
  if [[ -s "$dst_output" ]]; then
42
    echo
43
    echo 'Import output:'
44
    cat $dst_output
45
  fi
46
  if [[ -s "$src_output" ]]; then
47
    echo
48
    echo 'Export output:'
49
    cat $src_output
50
  fi
51
}
52

    
53
checkpids() {
54
  local result=0
55

    
56
  # Unlike combining the "wait" commands using || or &&, this ensures we
57
  # actually wait for all PIDs.
58
  for pid in "$@"; do
59
    if ! wait $pid; then
60
      result=1
61
    fi
62
  done
63

    
64
  return $result
65
}
66

    
67
get_testpath() {
68
  echo "${TOP_SRCDIR:-.}/test"
69
}
70

    
71
get_testfile() {
72
  echo "$(get_testpath)/data/$1"
73
}
74

    
75
upto() {
76
  echo "$(date '+%F %T'):" "$@" '...'
77
}
78

    
79
statusdir=$(mktemp -d)
80
trap "rm -rf $statusdir" EXIT
81

    
82
gencert_output=$statusdir/gencert.output
83

    
84
src_statusfile=$statusdir/src.status
85
src_output=$statusdir/src.output
86
src_x509=$statusdir/src.pem
87

    
88
dst_statusfile=$statusdir/dst.status
89
dst_output=$statusdir/dst.output
90
dst_x509=$statusdir/dst.pem
91

    
92
other_x509=$statusdir/other.pem
93

    
94
testdata=$statusdir/data1
95
largetestdata=$statusdir/data2
96

    
97
upto 'Command line parameter tests'
98

    
99
$impexpd >/dev/null 2>&1 &&
100
  err "daemon-util succeeded without parameters"
101

    
102
$impexpd foo bar baz moo boo >/dev/null 2>&1 &&
103
  err "daemon-util succeeded with wrong parameters"
104

    
105
$impexpd $src_statusfile >/dev/null 2>&1 &&
106
  err "daemon-util succeeded with insufficient parameters"
107

    
108
$impexpd $src_statusfile invalidmode >/dev/null 2>&1 &&
109
  err "daemon-util succeeded with invalid mode"
110

    
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"
114

    
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'"
119
  done
120

    
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'"
124
  done
125
done
126

    
127
upto 'Generate test data'
128
cat $(get_testfile proc_drbd8.txt) $(get_testfile cert1.pem) > $testdata
129

    
130
# Generate about 7.5 MB of test data
131
{ tmp="$(<$testdata)"
132
  for (( i=0; i < 100; ++i )); do
133
    echo "$tmp $tmp $tmp $tmp $tmp $tmp"
134
  done
135
  dd if=/dev/zero bs=1024 count=4096 2>/dev/null
136
  for (( i=0; i < 100; ++i )); do
137
    echo "$tmp $tmp $tmp $tmp $tmp $tmp"
138
  done
139
} > $largetestdata
140

    
141
impexpd_helper() {
142
  $PYTHON $(get_testpath)/import-export_unittest-helper "$@"
143
}
144

    
145
start_test() {
146
  upto "$@"
147

    
148
  rm -f $src_statusfile $dst_output $dst_statusfile $dst_output
149
  rm -f $gencert_output
150

    
151
  imppid=
152
  exppid=
153

    
154
  cmd_prefix=
155
  cmd_suffix=
156
  connect_timeout=30
157
  connect_retries=1
158
  compress=gzip
159
}
160

    
161
wait_import_ready() {
162
  # Wait for listening port
163
  impexpd_helper $dst_statusfile listen-port
164
}
165

    
166
do_export() {
167
  local port=$1
168

    
169
  $impexpd $src_statusfile export --bind=127.0.0.1 \
170
    --host=127.0.0.1 --port=$port \
171
    --key=$src_x509 --cert=$src_x509 --ca=$dst_x509 \
172
    --cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \
173
    --connect-timeout=$connect_timeout \
174
    --connect-retries=$connect_retries \
175
    --compress=$compress
176
}
177

    
178
do_import() {
179
  $impexpd $dst_statusfile import --bind=127.0.0.1 \
180
    --host=127.0.0.1 \
181
    --key=$dst_x509 --cert=$dst_x509 --ca=$src_x509 \
182
    --cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \
183
    --connect-timeout=$connect_timeout \
184
    --connect-retries=$connect_retries \
185
    --compress=$compress
186
}
187

    
188
upto 'Generate X509 certificates and keys'
189
impexpd_helper $src_x509 gencert 2>$gencert_output & srccertpid=$!
190
impexpd_helper $dst_x509 gencert 2>$gencert_output & dstcertpid=$!
191
impexpd_helper $other_x509 gencert 2>$gencert_output & othercertpid=$!
192
checkpids $srccertpid $dstcertpid $othercertpid || \
193
  err 'Failed to generate certificates'
194

    
195
start_test 'Normal case'
196
do_import > $statusdir/recv1 2>$dst_output & imppid=$!
197
if port=$(wait_import_ready 2>$src_output); then
198
  do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
199
fi
200
checkpids $exppid $imppid || err 'An error occurred'
201
cmp $testdata $statusdir/recv1 || err 'Received data does not match input'
202

    
203
start_test 'Export using wrong CA'
204
# Setting lower timeout to not wait for too long
205
connect_timeout=1 do_import &>$dst_output & imppid=$!
206
if port=$(wait_import_ready 2>$src_output); then
207
  : | dst_x509=$other_x509 do_export $port >>$src_output 2>&1 & exppid=$!
208
fi
209
checkpids $exppid $imppid && err 'Export did not fail when using wrong CA'
210

    
211
start_test 'Import using wrong CA'
212
# Setting lower timeout to not wait for too long
213
src_x509=$other_x509 connect_timeout=1 do_import &>$dst_output & imppid=$!
214
if port=$(wait_import_ready 2>$src_output); then
215
  : | do_export $port >>$src_output 2>&1 & exppid=$!
216
fi
217
checkpids $exppid $imppid && err 'Import did not fail when using wrong CA'
218

    
219
start_test 'Suffix command on import'
220
cmd_suffix="| cksum > $statusdir/recv2" do_import &>$dst_output & imppid=$!
221
if port=$(wait_import_ready 2>$src_output); then
222
  do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
223
fi
224
checkpids $exppid $imppid || err 'Testing additional commands failed'
225
cmp $statusdir/recv2 <(cksum < $testdata) || \
226
  err 'Checksum of received data does not match'
227

    
228
start_test 'Prefix command on export'
229
do_import > $statusdir/recv3 2>$dst_output & imppid=$!
230
if port=$(wait_import_ready 2>$src_output); then
231
  cmd_prefix='cksum |' do_export $port <$testdata >>$src_output 2>&1 & exppid=$!
232
fi
233
checkpids $exppid $imppid || err 'Testing additional commands failed'
234
cmp $statusdir/recv3 <(cksum < $testdata) || \
235
  err 'Received checksum does not match'
236

    
237
start_test 'Failing prefix command on export'
238
: | cmd_prefix='exit 1;' do_export 0 &>$src_output & exppid=$!
239
checkpids $exppid && err 'Prefix command on export did not fail when it should'
240

    
241
start_test 'Failing suffix command on export'
242
do_import >&$dst_output & imppid=$!
243
if port=$(wait_import_ready 2>$src_output); then
244
  : | cmd_suffix='| exit 1' do_export $port >>$src_output 2>&1 & exppid=$!
245
fi
246
checkpids $imppid $exppid && \
247
  err 'Suffix command on export did not fail when it should'
248

    
249
start_test 'Failing prefix command on import'
250
cmd_prefix='exit 1;' do_import &>$dst_output & imppid=$!
251
checkpids $imppid && err 'Prefix command on import did not fail when it should'
252

    
253
start_test 'Failing suffix command on import'
254
cmd_suffix='| exit 1' do_import &>$dst_output & imppid=$!
255
if port=$(wait_import_ready 2>$src_output); then
256
  : | do_export $port >>$src_output 2>&1 & exppid=$!
257
fi
258
checkpids $imppid $exppid && \
259
  err 'Suffix command on import did not fail when it should'
260

    
261
start_test 'Listen timeout A'
262
# Setting lower timeout to not wait too long (there won't be anything trying to
263
# connect)
264
connect_timeout=1 do_import &>$dst_output & imppid=$!
265
checkpids $imppid && \
266
  err 'Listening with timeout did not fail when it should'
267

    
268
start_test 'Listen timeout B'
269
do_import &>$dst_output & imppid=$!
270
if port=$(wait_import_ready 2>$src_output); then
271
  { sleep 1; : | do_export $port; } >>$src_output 2>&1 & exppid=$!
272
fi
273
checkpids $exppid $imppid || \
274
  err 'Listening with timeout failed when it should not'
275

    
276
start_test 'Connect timeout'
277
# Setting lower timeout as nothing will be listening on port 0
278
: | connect_timeout=1 do_export 0 &>$src_output & exppid=$!
279
checkpids $exppid && err 'Connection did not time out when it should'
280

    
281
start_test 'No compression'
282
compress=none do_import > $statusdir/recv-nocompr 2>$dst_output & imppid=$!
283
if port=$(wait_import_ready 2>$src_output); then
284
  compress=none do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
285
fi
286
checkpids $exppid $imppid || err 'An error occurred'
287
cmp $testdata $statusdir/recv-nocompr || \
288
  err 'Received data does not match input'
289

    
290
start_test 'Compression mismatch A'
291
compress=none do_import > $statusdir/recv-miscompr 2>$dst_output & imppid=$!
292
if port=$(wait_import_ready 2>$src_output); then
293
  compress=gzip do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
294
fi
295
checkpids $exppid $imppid || err 'An error occurred'
296
cmp -s $testdata $statusdir/recv-miscompr && \
297
  err 'Received data matches input when it should not'
298

    
299
start_test 'Compression mismatch B'
300
compress=gzip do_import > $statusdir/recv-miscompr2 2>$dst_output & imppid=$!
301
if port=$(wait_import_ready 2>$src_output); then
302
  compress=none do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
303
fi
304
checkpids $exppid $imppid && err 'Did not fail when it should'
305
cmp -s $testdata $statusdir/recv-miscompr2 && \
306
  err 'Received data matches input when it should not'
307

    
308
start_test 'Large transfer'
309
do_import > $statusdir/recv-large 2>$dst_output & imppid=$!
310
if port=$(wait_import_ready 2>$src_output); then
311
  do_export $port < $largetestdata >>$src_output 2>&1 & exppid=$!
312
fi
313
checkpids $exppid $imppid || err 'An error occurred'
314
cmp $largetestdata $statusdir/recv-large || \
315
  err 'Received data does not match input'
316

    
317
exit 0