3 ### Test script for the main server
5 ### (c) 2008 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of Trivial IP Encryption (TrIPE).
12 ### TrIPE is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
17 ### TrIPE is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU General Public License for more details.
22 ### You should have received a copy of the GNU General Public License
23 ### along with TrIPE; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 ## Configure a directory ready for use by tripe.
30 m4_define([SETUPDIR], [
31 cp $abs_top_srcdir/t/keyring-$1 ./keyring
32 key extract -f-secret keyring.pub
35 ## Running standard programs with useful options.
37 [env TRIPE_PRIVHELPER=$abs_top_builddir/priv/tripe-privhelper \
38 $abs_top_builddir/server/tripe -F -d. -aadmin -p0 -b127.0.0.1 -talice])
39 m4_define([TRIPECTL], [$abs_top_builddir/client/tripectl -d. -aadmin])
40 m4_define([USLIP], [$abs_top_builddir/uslip/tripe-uslip])
41 m4_define([MITM], [$abs_top_builddir/proxy/tripe-mitm])
43 ## Sequences. (These are used for testing the replay protection machinery.)
44 m4_define([R32], [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 dnl
45 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31])
46 m4_define([P32], [21 26 14 12 25 18 2 27 10 31 24 29 0 20 17 11 dnl
47 8 3 7 23 19 1 13 30 6 9 5 22 15 28 16 4])
49 ###--------------------------------------------------------------------------
50 ### Scaffolding for running a TrIPE server.
52 ## WITH_TRIPEX(dir, args, body)
53 m4_define([WITH_TRIPEX], [
55 ## If this directory doesn't exist then Bad Things will happen.
57 echo >&2 "server directory \`$1' doesn't exist"
61 ## Remove the status file. This is how we notice that the server's died.
62 rm -f $1/server-status
63 > $1/expected-server-output
64 > $1/expected-server-errors
66 ## Autotest writes crap to standard output, which we don't want going to the
67 ## server. So keep a copy of the standard output, do the pipe, and recover
68 ## the old stdout inside the group.
73 ## Wait for the socket to appear. Watch for the server crashing during
74 ## initialization. Busy waiting is evil, but it's the best I can do and
75 ## not sleep for ages. (Yes, a second on each test group is a long time.)
76 while test ! -r $1/server-status && test ! -S $1/admin; do :; done
78 ## Make the port number availale.
79 AT_CHECK([TRIPECTL -d$1 PORT],, [stdout])
85 ## End of the test, now run the server. There's an awful hack here. If a
86 ## process running under strace exits with a signal, then strace will kill
87 ## itself with the same signal -- and therefore clobber the original
88 ## process's core file. So we arrange to run strace in one directory and
89 ## have the child process run in another.
95 ulimit -c hard >/dev/null 2>&1
96 strace -f -o ../tripe.trace \
97 TRIPE -d.. $2 >../server-output.full 2>../server-errors)
99 echo $stat >server-status
100 if test $stat -ne 0; then
101 echo "exit status: $stat" >>server-errors
104 ## We interrupt this relatively sensible macro for an especially awful
105 ## hack. The tripe server emits warnings which are often caused by lack of
106 ## synchronization between two peers. These are harmless and shouldn't
107 ## cause test failures. But we shouldn't just trim out all messages that
108 ## look like the spurious ones: if they appear when we're not expecting
109 ## them, that's bad and they should properly cause a test failure.
111 ## So we use the WARN command to insert magic directives into the server's
112 ## message stream. The directives begin with `WARN USER test'; remaining
113 ## tokens may be as follows.
115 ## PUSH Introduce a new layer of nesting. Settings between
116 ## this PUSH and the matching POP will be forgotten
117 ## following the POP.
119 ## POP End a layer of nesting. See PUSH above.
121 ## IGNORE tokens Ignore lines which begin with the tokens.
123 ## Token comparison isn't done very well, but it's good enough for these
126 ## We also trim out trace lines here, since it's useful to produce them for
127 ## debugging purposes and changing or introducing more of them shouldn't
134 $[]1 == "TRACE" { next; }
135 $[]1 == "WARN" && $[]2 == "USER" && $[]3 == "test" {
137 npatstk[[sp++]] = npat;
138 else if ($[]4 == "IGNORE") {
141 for (i = 5; i <= NF; i++) {
146 } else if ($[]4 == "POP")
147 npat = npatstk[[--sp]];
151 for (i = 0; i < npat; i++) {
152 n = length(pat[[i]]);
153 if (substr($[]0, 0, n) == pat[[i]])
158 ' server-output.full >server-output
162 ## Now check that the server's output matches our expectations.
163 mv $1/expected-server-output expout
164 mv $1/expected-server-errors experr
165 AT_CHECK([cat $1/server-output; cat >&2 $1/server-errors],,
169 ## WITH_TRIPE(args, body)
170 m4_define([WITH_TRIPE], [WITH_TRIPEX([.], [$1], [$2])])
172 ## WITH_2TRIPES(adir, bdir, bothargs, aargs, bargs, body)
173 m4_define([WITH_2TRIPES],
174 [WITH_TRIPEX([$1], [$3 $4], [WITH_TRIPEX([$2], [$3 $5], [$6])])])
179 while test $n -lt $1; do
188 ## COMMS_EPING(adir, aname, bdir, bname, [n])
189 m4_define([COMMS_EPING], [
190 AT_CHECK([RETRY([m4_default([$5], [1])],
191 [TRIPECTL -d$1 EPING $4])],, [ignore])
192 AT_CHECK([RETRY([m4_default([$5], [1])],
193 [TRIPECTL -d$3 EPING $2])],, [ignore])
196 ## COMMS_SLIP(adir, aname, bdir, bname)
197 m4_define([COMMS_SLIP], [
198 AT_CHECK([echo "from $1" | USLIP -p $1/$4])
199 AT_CHECK([USLIP -g $3/$2],, [from $1[]nl])
200 AT_CHECK([echo "from $3" | USLIP -p $3/$2])
201 AT_CHECK([USLIP -g $1/$4],, [from $3[]nl])
204 ## AWAIT_KXDONE(adir, aname, bdir, bname, body)
205 m4_define([AWAIT_KXDONE], [
207 ## Ignore some reports caused by races.
208 TRIPECTL -d$3 WARN test PUSH
209 TRIPECTL -d$3 WARN test IGNORE WARN KX $2 incorrect cookie
210 TRIPECTL -d$3 WARN test IGNORE WARN KX $2 unexpected pre-challenge
212 ## Watch for the key-exchange completion announcement in the background.
213 COPROCESSES([wait-$1], [
218 case "$[]1:$[]2:$[]3" in
220 NOTE:KXDONE:$4) break ;;
231 ## Collect the completion announcement.
232 wait $waiter_$1; waitrc=$?
233 AT_CHECK([echo $waitrc],, [0[]nl])
235 ## Be interested in key-exchange warnings again.
236 TRIPECTL -d$4 WARN test POP
239 ## ESTABLISH(adir, aname, aopts, bdir, bname, bopts)
240 m4_define([ESTABLISH], [
242 ## Set up the establishment.
243 AWAIT_KXDONE([$1], [$2], [$4], [$5], [
244 AT_CHECK([TRIPECTL -d$1 ADD -cork $6 $5 INET 127.0.0.1 $(cat $4/port)])
245 AT_CHECK([TRIPECTL -d$4 ADD $3 $2 INET 127.0.0.1 $(cat $1/port)])
248 ## Check transport pinging.
249 AT_CHECK([TRIPECTL -d$1 PING $5],, [ignore])
250 AT_CHECK([TRIPECTL -d$4 PING $2],, [ignore])
252 ## Check communication works.
253 COMMS_EPING([$1], [$2], [$4], [$5])
254 COMMS_SLIP([$1], [$2], [$4], [$5])
257 ###--------------------------------------------------------------------------
258 ### Very unpleasant coprocess handling.
260 ## COPROCESSES(TAG, PROC-A, PROC-B)
261 m4_define([COPROCESSES], [dnl
262 rm -f pipe-$1; mknod pipe-$1 p
263 { { $2 nl } <pipe-$1 | { $3 nl } >pipe-$1; } dnl
266 ## TRIPECTL_INTERACT(ARGS, SHELLSTUFF)
267 m4_define([TRIPECTL_INTERACT], [
269 COPROCESSES([client], [exec 4>&1 1>&3 $1], [TRIPECTL $2])
272 ## TRIPECTL_COMMAND(CMD, EXPECT)
273 m4_define([TRIPECTL_COMMAND], [
275 m4_if([$1], [!], [:], [echo "$1" >&4])
279 *) echo 2>&1 "submitted $1: expected $2, found $line"; exit 1 ;;
285 ###--------------------------------------------------------------------------
286 ### Make sure the thing basically works.
288 AT_SETUP([server basics])
290 AT_CHECK([echo port | TRIPE -p54321],, [INFO 54321[]nl[]OK[]nl])
293 ###--------------------------------------------------------------------------
296 AT_SETUP([server challenges])
302 AT_CHECK([chal=$(TRIPECTL GETCHAL); TRIPECTL checkchal $chal])
304 ## A wrong challenge. (This was valid once, but the probablity that the
305 ## server chose the same key is negligible.)
306 AT_CHECK([TRIPECTL checkchal AAAAAHyoOL+HMaE0Y9B3ivuszt0], [1],,
307 [tripectl: invalid-challenge[]nl])
308 echo WARN CHAL incorrect-tag >>expected-server-output
310 ## A duplicated challenge.
312 chal=$(TRIPECTL GETCHAL)
313 TRIPECTL CHECKCHAL $chal
314 TRIPECTL CHECKCHAL $chal
315 ], [1],, [tripectl: invalid-challenge[]nl])
316 echo WARN CHAL replay duplicated-sequence >>expected-server-output
318 ## Out-of-order reception. There should be a window of 32 challenges; we
319 ## make 33 and check them in a strange order.
321 echo "tripectl: invalid-challenge" >>experr
322 echo "WARN CHAL replay old-sequence" >>expected-server-output
324 echo "tripectl: invalid-challenge" >>experr
325 echo "WARN CHAL replay duplicated-sequence" >>expected-server-output
329 ## Make the challenges.
330 for i in old R32; do TRIPECTL GETCHAL >chal-$i || exit 2; done
332 ## Now check them back.
333 for i in P32; do TRIPECTL CHECKCHAL $(cat chal-$i) || exit 3; done
335 ## Check the one which should have fallen off the front.
336 TRIPECTL CHECKCHAL $(cat chal-old) && exit 4
338 ## And make sure that the others are now considered duplicates.
339 for i in R32; do TRIPECTL CHECKCHAL $(cat chal-$i) && exit 5; done
341 ## All done: tidy cruft away.
349 ###--------------------------------------------------------------------------
352 AT_SETUP([server communication])
354 export TRIPE_SLIPIF=USLIP
356 for i in alice bob; do (mkdir $i; cd $i; SETUPDIR([ec])); done
358 WITH_2TRIPES([alice], [bob], [-nslip], [-talice], [-tbob], [
359 ESTABLISH([alice], [not-alice], [-key alice],
365 ###--------------------------------------------------------------------------
366 ### Adverse communication.
368 AT_SETUP([server retry])
369 AT_KEYWORDS([backoff])
370 export TRIPE_SLIPIF=USLIP
372 for i in alice bob; do (mkdir $i; cd $i; SETUPDIR([dh])); done
374 WITH_2TRIPES([alice], [bob], [-nslip], [-talice], [-tbob], [
376 ## Set up the evil proxy.
377 alicemitm=24516 bobmitm=14016
378 MITM -kalice/keyring.pub >mitm.out 2>mitm.err \
379 peer:alice:$alicemitm:127.0.0.1:$(cat alice/port) \
380 peer:bob:$bobmitm:127.0.0.1:$(cat bob/port) \
381 filt:drop:5 filt:send& mitmpid=$!
382 strace -omitm.trace -p$mitmpid& mitmtrace=$!
383 trap 'kill $mitmpid $mitmtrace; exit 127' EXIT INT QUIT TERM HUP
385 ## Try to establish keys anyway.
386 AWAIT_KXDONE([alice], [alice], [bob], [bob], [
387 AT_CHECK([TRIPECTL -dalice ADD -cork bob INET 127.0.0.1 $alicemitm])
388 AT_CHECK([TRIPECTL -dbob ADD alice INET 127.0.0.1 $bobmitm])
392 COMMS_EPING([alice], [alice], [bob], [bob], [10])
393 COMMS_EPING([bob], [bob], [alice], [alice], [10])
395 ## Tear down the MITM proxy.
403 ###--------------------------------------------------------------------------
406 AT_SETUP([server services])
412 ## Make sure it's not running yet.
413 AT_CHECK([TRIPECTL SVCENSURE test], [1],,
414 [tripectl: unknown-service test[]nl])
416 ## Run a simple service.
417 rm -f svc-test-running tripectl-status
419 echo SVCCLAIM test 1.0.0
425 echo >&2 "SVCCLAIM failed: $line"
429 echo ok >svc-test-running
432 case "$[]1,$[]3,$[]4" in
434 echo SVCINFO try not to use this service for anything useful
441 echo SVCFAIL $[]2 this-command-always-fails
447 case "$[]1,$[]2,$[]3,$[]4" in
448 SVCCANCEL,$tag,,) break ;;
449 SVCJOB,*,test,ESCAPE)
450 echo >&2 "attempt to escape from alkatraz"
464 echo SVCFAIL $[]2 unknown-svc-command $[]4
472 echo "failure in service: $line" >&2
477 TRIPECTL; echo $? >tripectl-status
478 ]) 2>tripectl-errors &
480 ## Wait until it starts up.
481 while test ! -r svc-test-running && test ! -r tripectl-status; do :; done
483 ## Make sure it's running.
484 AT_CHECK([TRIPECTL SVCQUERY test],, [name=test version=1.0.0[]nl])
486 ## Try some simple commands.
487 AT_CHECK([TRIPECTL SVCSUBMIT test GOOD])
488 AT_CHECK([TRIPECTL SVCSUBMIT test BAD], [1],,
489 [tripectl: this-command-always-fails[]nl])
491 ## And now with commands in the background.
493 TRIPECTL_COMMAND([SVCSUBMIT test GOOD], [OK])
494 TRIPECTL_COMMAND([SVCSUBMIT -background foo test UGLY], [BGDETACH foo])
495 TRIPECTL_COMMAND([BGCANCEL foo], [OK])
496 TRIPECTL_COMMAND([SVCSUBMIT test ESCAPE],
497 [FAIL unknown-svc-command ESCAPE])
500 ## Out-of-order completion.
502 TRIPECTL_COMMAND([SVCSUBMIT -background one test FIRST], [BGDETACH one])
503 TRIPECTL_COMMAND([SVCSUBMIT -background two test SECOND], [BGDETACH two])
504 TRIPECTL_COMMAND([!], [BGOK one])
505 TRIPECTL_COMMAND([!], [BGOK two])
514 ###----- That's all, folks --------------------------------------------------