chiark / gitweb /
server/tests.at: Test key exchange and retransmit with a flaky network.
[tripe] / server / tests.at
1 ### -*-autotest-*-
2 ###
3 ### Test script for the main server
4 ###
5 ### (c) 2008 Straylight/Edgeware
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of Trivial IP Encryption (TrIPE).
11 ###
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.
16 ###
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.
21 ###
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.
25
26 m4_define([nl], [
27 ])
28
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
33 ])
34
35 ## Running standard programs with useful options.
36 m4_define([TRIPE],
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])
42
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])
48
49 ###--------------------------------------------------------------------------
50 ### Scaffolding for running a TrIPE server.
51
52 ## WITH_TRIPEX(dir, args, body)
53 m4_define([WITH_TRIPEX], [
54
55 ## If this directory doesn't exist then Bad Things will happen.
56 if test ! -d $1; then
57   echo >&2 "server directory \`$1' doesn't exist"
58   exit 99
59 fi
60
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
65
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.
69 exec 3>&1
70 { (
71 exec 1>&3 3>&-
72
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
77
78 ## Make the port number availale.
79 AT_CHECK([TRIPECTL -d$1 PORT],, [stdout])
80 mv stdout $1/port
81
82 ## Test body...
83 $3
84
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.
90 ) && :; } | {
91   cd $1
92   mkdir -p strace-hack/
93   echo TRIPE $2 >&2
94   (cd strace-hack/
95    ulimit -c hard >/dev/null 2>&1
96    strace -f -o ../tripe.trace \
97      TRIPE -d.. $2 >../server-output.full 2>../server-errors)
98   stat=$?
99   echo $stat >server-status
100   if test $stat -ne 0; then
101     echo "exit status: $stat" >>server-errors
102   fi
103
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.
110   ##
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.
114   ##
115   ## PUSH               Introduce a new layer of nesting.  Settings between
116   ##                    this PUSH and the matching POP will be forgotten
117   ##                    following the POP.
118   ##
119   ## POP                End a layer of nesting.  See PUSH above.
120   ##
121   ## IGNORE tokens      Ignore lines which begin with the tokens.
122   ##
123   ## Token comparison isn't done very well, but it's good enough for these
124   ## purposes.
125   ##
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
128   ## cause failures.
129   awk '
130         BEGIN {
131           sp = 0;
132           npat = 0;
133         }
134         $[]1 == "TRACE" { next; }
135         $[]1 == "WARN" && $[]2 == "USER" && $[]3 == "test" {
136           if ($[]4 == "PUSH")
137             npatstk[[sp++]] = npat;
138           else if ($[]4 == "IGNORE") {
139             s = "";
140             p = "";
141             for (i = 5; i <= NF; i++) {
142               p = p s $[]i;
143               s = " ";
144             }
145             pat[[npat++]] = p;
146           } else if ($[]4 == "POP")
147             npat = npatstk[[--sp]];
148           next;
149         }
150         {
151           for (i = 0; i < npat; i++) {
152             n = length(pat[[i]]);
153             if (substr($[]0, 0, n) == pat[[i]])
154               next;
155           }
156           print $[]0;
157         }
158   ' server-output.full >server-output
159 }
160 exec 3>&-
161
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],,
166          [expout], [experr])
167 ])
168
169 ## WITH_TRIPE(args, body)
170 m4_define([WITH_TRIPE], [WITH_TRIPEX([.], [$1], [$2])])
171
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])])])
175
176 ## RETRY(n, body)
177 m4_define([RETRY], [
178   n=0 rc=1
179   while test $n -lt $1; do
180     if $2
181     then rc=0; break
182     fi
183     n=$(( $n + 1 ))
184   done
185   exit $rc
186 ])
187
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])
194 ])
195
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])
202 ])
203
204 ## AWAIT_KXDONE(adir, aname, bdir, bname, body)
205 m4_define([AWAIT_KXDONE], [
206
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
211
212   ## Watch for the key-exchange completion announcement in the background.
213   COPROCESSES([wait-$1], [
214     echo WATCH +n
215     while read line; do
216       set x $line; shift
217       echo >&2 ">>> $line"
218       case "$[]1:$[]2:$[]3" in
219         OK::) ;;
220         NOTE:KXDONE:$4) break ;;
221         NOTE:*) ;;
222         *) exit 63 ;;
223       esac
224     done
225   ], [
226     TRIPECTL -d$1
227   ]) & waiter_$1=$!
228
229   $5
230
231   ## Collect the completion announcement.
232   wait $waiter_$1; waitrc=$?
233   AT_CHECK([echo $waitrc],, [0[]nl])
234
235   ## Be interested in key-exchange warnings again.
236   TRIPECTL -d$4 WARN test POP
237 ])
238
239 ## ESTABLISH(adir, aname, aopts, bdir, bname, bopts)
240 m4_define([ESTABLISH], [
241
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)])
246   ])
247
248   ## Check transport pinging.
249   AT_CHECK([TRIPECTL -d$1 PING $5],, [ignore])
250   AT_CHECK([TRIPECTL -d$4 PING $2],, [ignore])
251
252   ## Check communication works.
253   COMMS_EPING([$1], [$2], [$4], [$5])
254   COMMS_SLIP([$1], [$2], [$4], [$5])
255 ])
256
257 ###--------------------------------------------------------------------------
258 ### Very unpleasant coprocess handling.
259
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
264 ])
265
266 ## TRIPECTL_INTERACT(ARGS, SHELLSTUFF)
267 m4_define([TRIPECTL_INTERACT], [
268   exec 3>&1
269   COPROCESSES([client], [exec 4>&1 1>&3 $1], [TRIPECTL $2])
270 ])
271
272 ## TRIPECTL_COMMAND(CMD, EXPECT)
273 m4_define([TRIPECTL_COMMAND], [
274   AT_CHECK([
275     m4_if([$1], [!], [:], [echo "$1" >&4])
276     read line
277     case "$line" in
278       "$2") ;;
279       *) echo 2>&1 "submitted $1: expected $2, found $line"; exit 1 ;;
280     esac
281   ])
282   exec 3>&-
283 ])
284
285 ###--------------------------------------------------------------------------
286 ### Make sure the thing basically works.
287
288 AT_SETUP([server basics])
289 SETUPDIR([ec])
290 AT_CHECK([echo port | TRIPE -p54321],, [INFO 54321[]nl[]OK[]nl])
291 AT_CLEANUP
292
293 ###--------------------------------------------------------------------------
294 ### Challenges.
295
296 AT_SETUP([server challenges])
297 AT_KEYWORDS([chal])
298 SETUPDIR([ec])
299
300 WITH_TRIPE(, [
301   ## A simple test.
302   AT_CHECK([chal=$(TRIPECTL GETCHAL); TRIPECTL checkchal $chal])
303
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
309
310   ## A duplicated challenge.
311   AT_CHECK([
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
317
318   ## Out-of-order reception.  There should be a window of 32 challenges; we
319   ## make 33 and check them in a strange order.
320   rm -f experr
321   echo "tripectl: invalid-challenge" >>experr
322   echo "WARN CHAL replay old-sequence" >>expected-server-output
323   for i in P32; do
324     echo "tripectl: invalid-challenge" >>experr
325     echo "WARN CHAL replay duplicated-sequence" >>expected-server-output
326   done
327   AT_CHECK([
328
329     ## Make the challenges.
330     for i in old R32; do TRIPECTL GETCHAL >chal-$i || exit 2; done
331
332     ## Now check them back.
333     for i in P32; do TRIPECTL CHECKCHAL $(cat chal-$i) || exit 3; done
334
335     ## Check the one which should have fallen off the front.
336     TRIPECTL CHECKCHAL $(cat chal-old) && exit 4
337
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
340
341     ## All done: tidy cruft away.
342     rm -f chal-*
343     exit 0
344   ], [0],, [experr])
345 ])
346
347 AT_CLEANUP
348
349 ###--------------------------------------------------------------------------
350 ### Communication.
351
352 AT_SETUP([server communication])
353 AT_KEYWORDS([comm])
354 export TRIPE_SLIPIF=USLIP
355
356 for i in alice bob; do (mkdir $i; cd $i; SETUPDIR([ec])); done
357
358 WITH_2TRIPES([alice], [bob], [-nslip], [-talice], [-tbob], [
359   ESTABLISH([alice], [not-alice], [-key alice],
360             [bob], [bob], [])
361 ])
362
363 AT_CLEANUP
364
365 ###--------------------------------------------------------------------------
366 ### Adverse communication.
367
368 AT_SETUP([server retry])
369 AT_KEYWORDS([backoff])
370 export TRIPE_SLIPIF=USLIP
371
372 for i in alice bob; do (mkdir $i; cd $i; SETUPDIR([dh])); done
373
374 WITH_2TRIPES([alice], [bob], [-nslip], [-talice], [-tbob], [
375
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
384
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])
389   ])
390
391   ## Check pinging.
392   COMMS_EPING([alice], [alice], [bob], [bob], [10])
393   COMMS_EPING([bob], [bob], [alice], [alice], [10])
394
395   ## Tear down the MITM proxy.
396   kill $mitmpid
397   wait $mitmpid
398   wait $mitmtrace
399 ])
400
401 AT_CLEANUP
402
403 ###--------------------------------------------------------------------------
404 ### Services.
405
406 AT_SETUP([server services])
407 AT_KEYWORDS([svc])
408 SETUPDIR([ec])
409
410 WITH_TRIPE(, [
411
412   ## Make sure it's not running yet.
413   AT_CHECK([TRIPECTL SVCENSURE test], [1],,
414            [tripectl: unknown-service test[]nl])
415
416   ## Run a simple service.
417   rm -f svc-test-running tripectl-status
418   COPROCESSES([svc], [
419     echo SVCCLAIM test 1.0.0
420     read line
421     case "$line" in
422       OK)
423         ;;
424       *)
425         echo >&2 "SVCCLAIM failed: $line"
426         exit 1
427         ;;
428     esac
429     echo ok >svc-test-running
430     while read line; do
431       set -- $line
432       case "$[]1,$[]3,$[]4" in
433         SVCJOB,test,HELP)
434           echo SVCINFO try not to use this service for anything useful
435           echo SVCOK $[]2
436           ;;
437         SVCJOB,test,GOOD)
438           echo SVCOK $[]2
439           ;;
440         SVCJOB,test,BAD)
441           echo SVCFAIL $[]2 this-command-always-fails
442           ;;
443         SVCJOB,test,UGLY)
444           tag=$2
445           while read line; do
446             set -- $line
447             case "$[]1,$[]2,$[]3,$[]4" in
448               SVCCANCEL,$tag,,) break ;;
449               SVCJOB,*,test,ESCAPE)
450                 echo >&2 "attempt to escape from alkatraz"
451                 exit 1
452                 ;;
453             esac
454           done
455           ;;
456         SVCJOB,test,FIRST)
457           firsttag=$[]2
458           ;;
459         SVCJOB,test,SECOND)
460           echo SVCOK $firsttag
461           echo SVCOK $[]2
462           ;;
463         SVCJOB,*)
464           echo SVCFAIL $[]2 unknown-svc-command $[]4
465           ;;
466         SVCCLAIM,*)
467           break
468           ;;
469         OK,* | INFO,*)
470           ;;
471         FAIL,*)
472           echo "failure in service: $line" >&2
473           ;;
474       esac
475     done
476   ], [
477     TRIPECTL; echo $? >tripectl-status
478   ]) 2>tripectl-errors &
479
480   ## Wait until it starts up.
481   while test ! -r svc-test-running && test ! -r tripectl-status; do :; done
482
483   ## Make sure it's running.
484   AT_CHECK([TRIPECTL SVCQUERY test],, [name=test version=1.0.0[]nl])
485
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])
490
491   ## And now with commands in the background.
492   TRIPECTL_INTERACT([
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])
498   ])
499
500   ## Out-of-order completion.
501   TRIPECTL_INTERACT([
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])
506   ])
507
508   ## All done.
509   exit 0
510 ])
511
512 AT_CLEANUP
513
514 ###----- That's all, folks --------------------------------------------------