chiark / gitweb /
b3e01a0eca5f775351a59e5b89448ab7a3fe74a0
[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
42 ## Sequences.  (These are used for testing the replay protection machinery.)
43 m4_define([R32], [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15   dnl
44                   16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31])
45 m4_define([P32], [21 26 14 12 25 18  2 27 10 31 24 29  0 20 17 11   dnl
46                    8  3  7 23 19  1 13 30  6  9  5 22 15 28 16  4])
47
48 ###--------------------------------------------------------------------------
49 ### Scaffolding for running a TrIPE server.
50
51 ## WITH_TRIPEX(dir, args, body)
52 m4_define([WITH_TRIPEX], [
53
54 ## If this directory doesn't exist then Bad Things will happen.
55 if test ! -d $1; then
56   echo >&2 "server directory \`$1' doesn't exist"
57   exit 99
58 fi
59
60 ## Remove the status file.  This is how we notice that the server's died.
61 rm -f $1/server-status
62 > $1/expected-server-output
63 > $1/expected-server-errors
64
65 ## Autotest writes crap to standard output, which we don't want going to the
66 ## server.  So keep a copy of the standard output, do the pipe, and recover
67 ## the old stdout inside the group.
68 exec 3>&1
69 { (
70 exec 1>&3 3>&-
71
72 ## Wait for the socket to appear.  Watch for the server crashing during
73 ## initialization.  Busy waiting is evil, but it's the best I can do and
74 ## not sleep for ages.  (Yes, a second on each test group is a long time.)
75 while test ! -r $1/server-status && test ! -S $1/admin; do :; done
76
77 ## Make the port number availale.
78 AT_CHECK([TRIPECTL -d$1 PORT],, [stdout])
79 mv stdout $1/port
80
81 ## Test body...
82 $3
83
84 ## End of the test, now run the server.  There's an awful hack here.  If a
85 ## process running under strace exits with a signal, then strace will kill
86 ## itself with the same signal -- and therefore clobber the original
87 ## process's core file.  So we arrange to run strace in one directory and
88 ## have the child process run in another.
89 ) && :; } | {
90   cd $1
91   mkdir -p strace-hack/
92   echo TRIPE $2 >&2
93   (cd strace-hack/
94    ulimit -c hard >/dev/null 2>&1
95    strace -f -o ../tripe.trace \
96      TRIPE -d.. $2 >../server-output.full 2>../server-errors)
97   stat=$?
98   echo $stat >server-status
99   if test $stat -ne 0; then
100     echo "exit status: $stat" >>server-errors
101   fi
102
103   ## We interrupt this relatively sensible macro for an especially awful
104   ## hack.  The tripe server emits warnings which are often caused by lack of
105   ## synchronization between two peers.  These are harmless and shouldn't
106   ## cause test failures.  But we shouldn't just trim out all messages that
107   ## look like the spurious ones: if they appear when we're not expecting
108   ## them, that's bad and they should properly cause a test failure.
109   ##
110   ## So we use the WARN command to insert magic directives into the server's
111   ## message stream.  The directives begin with `WARN USER test'; remaining
112   ## tokens may be as follows.
113   ##
114   ## PUSH               Introduce a new layer of nesting.  Settings between
115   ##                    this PUSH and the matching POP will be forgotten
116   ##                    following the POP.
117   ##
118   ## POP                End a layer of nesting.  See PUSH above.
119   ##
120   ## IGNORE tokens      Ignore lines which begin with the tokens.
121   ##
122   ## Token comparison isn't done very well, but it's good enough for these
123   ## purposes.
124   ##
125   ## We also trim out trace lines here, since it's useful to produce them for
126   ## debugging purposes and changing or introducing more of them shouldn't
127   ## cause failures.
128   awk '
129         BEGIN {
130           sp = 0;
131           npat = 0;
132         }
133         $[]1 == "TRACE" { next; }
134         $[]1 == "WARN" && $[]2 == "USER" && $[]3 == "test" {
135           if ($[]4 == "PUSH")
136             npatstk[[sp++]] = npat;
137           else if ($[]4 == "IGNORE") {
138             s = "";
139             p = "";
140             for (i = 5; i <= NF; i++) {
141               p = p s $[]i;
142               s = " ";
143             }
144             pat[[npat++]] = p;
145           } else if ($[]4 == "POP")
146             npat = npatstk[[--sp]];
147           next;
148         }
149         {
150           for (i = 0; i < npat; i++) {
151             n = length(pat[[i]]);
152             if (substr($[]0, 0, n) == pat[[i]])
153               next;
154           }
155           print $[]0;
156         }
157   ' server-output.full >server-output
158 }
159 exec 3>&-
160
161 ## Now check that the server's output matches our expectations.
162 mv $1/expected-server-output expout
163 mv $1/expected-server-errors experr
164 AT_CHECK([cat $1/server-output; cat >&2 $1/server-errors],,
165          [expout], [experr])
166 ])
167
168 ## WITH_TRIPE(args, body)
169 m4_define([WITH_TRIPE], [WITH_TRIPEX([.], [$1], [$2])])
170
171 ## WITH_2TRIPES(adir, bdir, bothargs, aargs, bargs, body)
172 m4_define([WITH_2TRIPES],
173           [WITH_TRIPEX([$1], [$3 $4], [WITH_TRIPEX([$2], [$3 $5], [$6])])])
174
175 ## RETRY(n, body)
176 m4_define([RETRY], [
177   n=0 rc=1
178   while test $n -lt $1; do
179     if $2
180     then rc=0; break
181     fi
182     n=$(( $n + 1 ))
183   done
184   exit $rc
185 ])
186
187 ## COMMS_EPING(adir, aname, bdir, bname, [n])
188 m4_define([COMMS_EPING], [
189   AT_CHECK([RETRY([m4_default([$5], [1])],
190     [TRIPECTL -d$1 EPING $4])],, [ignore])
191   AT_CHECK([RETRY([m4_default([$5], [1])],
192     [TRIPECTL -d$3 EPING $2])],, [ignore])
193 ])
194
195 ## COMMS_SLIP(adir, aname, bdir, bname)
196 m4_define([COMMS_SLIP], [
197   AT_CHECK([echo "from $1" | USLIP -p $1/$4])
198   AT_CHECK([USLIP -g $3/$2],, [from $1[]nl])
199   AT_CHECK([echo "from $3" | USLIP -p $3/$2])
200   AT_CHECK([USLIP -g $1/$4],, [from $3[]nl])
201 ])
202
203 ## AWAIT_KXDONE(adir, aname, bdir, bname, body)
204 m4_define([AWAIT_KXDONE], [
205
206   ## Ignore some reports caused by races.
207   TRIPECTL -d$3 WARN test PUSH
208   TRIPECTL -d$3 WARN test IGNORE WARN KX $2 incorrect cookie
209   TRIPECTL -d$3 WARN test IGNORE WARN KX $2 unexpected pre-challenge
210
211   ## Watch for the key-exchange completion announcement in the background.
212   COPROCESSES([wait-$1], [
213     echo WATCH +n
214     while read line; do
215       set x $line; shift
216       echo >&2 ">>> $line"
217       case "$[]1:$[]2:$[]3" in
218         OK::) ;;
219         NOTE:KXDONE:$4) break ;;
220         NOTE:*) ;;
221         *) exit 63 ;;
222       esac
223     done
224   ], [
225     TRIPECTL -d$1
226   ]) & waiter_$1=$!
227
228   $5
229
230   ## Collect the completion announcement.
231   wait $waiter_$1; waitrc=$?
232   AT_CHECK([echo $waitrc],, [0[]nl])
233
234   ## Be interested in key-exchange warnings again.
235   TRIPECTL -d$4 WARN test POP
236 ])
237
238 ## ESTABLISH(adir, aname, aopts, bdir, bname, bopts)
239 m4_define([ESTABLISH], [
240
241   ## Set up the establishment.
242   AWAIT_KXDONE([$1], [$2], [$4], [$5], [
243     AT_CHECK([TRIPECTL -d$1 ADD -cork $6 $5 INET 127.0.0.1 $(cat $4/port)])
244     AT_CHECK([TRIPECTL -d$4 ADD $3 $2 INET 127.0.0.1 $(cat $1/port)])
245   ])
246
247   ## Check transport pinging.
248   AT_CHECK([TRIPECTL -d$1 PING $5],, [ignore])
249   AT_CHECK([TRIPECTL -d$4 PING $2],, [ignore])
250
251   ## Check communication works.
252   COMMS_EPING([$1], [$2], [$4], [$5])
253   COMMS_SLIP([$1], [$2], [$4], [$5])
254 ])
255
256 ###--------------------------------------------------------------------------
257 ### Very unpleasant coprocess handling.
258
259 ## COPROCESSES(TAG, PROC-A, PROC-B)
260 m4_define([COPROCESSES], [dnl
261 rm -f pipe-$1; mknod pipe-$1 p
262 { { $2 nl } <pipe-$1 | { $3 nl } >pipe-$1; } dnl
263 ])
264
265 ## TRIPECTL_INTERACT(ARGS, SHELLSTUFF)
266 m4_define([TRIPECTL_INTERACT], [
267   exec 3>&1
268   COPROCESSES([client], [exec 4>&1 1>&3 $1], [TRIPECTL $2])
269 ])
270
271 ## TRIPECTL_COMMAND(CMD, EXPECT)
272 m4_define([TRIPECTL_COMMAND], [
273   AT_CHECK([
274     m4_if([$1], [!], [:], [echo "$1" >&4])
275     read line
276     case "$line" in
277       "$2") ;;
278       *) echo 2>&1 "submitted $1: expected $2, found $line"; exit 1 ;;
279     esac
280   ])
281   exec 3>&-
282 ])
283
284 ###--------------------------------------------------------------------------
285 ### Make sure the thing basically works.
286
287 AT_SETUP([server basics])
288 SETUPDIR([ec])
289 AT_CHECK([echo port | TRIPE -p54321],, [INFO 54321[]nl[]OK[]nl])
290 AT_CLEANUP
291
292 ###--------------------------------------------------------------------------
293 ### Challenges.
294
295 AT_SETUP([server challenges])
296 AT_KEYWORDS([chal])
297 SETUPDIR([ec])
298
299 WITH_TRIPE(, [
300   ## A simple test.
301   AT_CHECK([chal=$(TRIPECTL GETCHAL); TRIPECTL checkchal $chal])
302
303   ## A wrong challenge.  (This was valid once, but the probablity that the
304   ## server chose the same key is negligible.)
305   AT_CHECK([TRIPECTL checkchal AAAAAHyoOL+HMaE0Y9B3ivuszt0], [1],,
306            [tripectl: invalid-challenge[]nl])
307   echo WARN CHAL incorrect-tag >>expected-server-output
308
309   ## A duplicated challenge.
310   AT_CHECK([
311     chal=$(TRIPECTL GETCHAL)
312     TRIPECTL CHECKCHAL $chal
313     TRIPECTL CHECKCHAL $chal
314   ], [1],, [tripectl: invalid-challenge[]nl])
315   echo WARN CHAL replay duplicated-sequence >>expected-server-output
316
317   ## Out-of-order reception.  There should be a window of 32 challenges; we
318   ## make 33 and check them in a strange order.
319   rm -f experr
320   echo "tripectl: invalid-challenge" >>experr
321   echo "WARN CHAL replay old-sequence" >>expected-server-output
322   for i in P32; do
323     echo "tripectl: invalid-challenge" >>experr
324     echo "WARN CHAL replay duplicated-sequence" >>expected-server-output
325   done
326   AT_CHECK([
327
328     ## Make the challenges.
329     for i in old R32; do TRIPECTL GETCHAL >chal-$i || exit 2; done
330
331     ## Now check them back.
332     for i in P32; do TRIPECTL CHECKCHAL $(cat chal-$i) || exit 3; done
333
334     ## Check the one which should have fallen off the front.
335     TRIPECTL CHECKCHAL $(cat chal-old) && exit 4
336
337     ## And make sure that the others are now considered duplicates.
338     for i in R32; do TRIPECTL CHECKCHAL $(cat chal-$i) && exit 5; done
339
340     ## All done: tidy cruft away.
341     rm -f chal-*
342     exit 0
343   ], [0],, [experr])
344 ])
345
346 AT_CLEANUP
347
348 ###--------------------------------------------------------------------------
349 ### Communication.
350
351 AT_SETUP([server communication])
352 AT_KEYWORDS([comm])
353 export TRIPE_SLIPIF=USLIP
354
355 for i in alice bob; do (mkdir $i; cd $i; SETUPDIR([ec])); done
356
357 WITH_2TRIPES([alice], [bob], [-nslip], [-talice], [-tbob], [
358   ESTABLISH([alice], [not-alice], [-key alice],
359             [bob], [bob], [])
360 ])
361
362 AT_CLEANUP
363
364 ###--------------------------------------------------------------------------
365 ### Services.
366
367 AT_SETUP([server services])
368 AT_KEYWORDS([svc])
369 SETUPDIR([ec])
370
371 WITH_TRIPE(, [
372
373   ## Make sure it's not running yet.
374   AT_CHECK([TRIPECTL SVCENSURE test], [1],,
375            [tripectl: unknown-service test[]nl])
376
377   ## Run a simple service.
378   rm -f svc-test-running tripectl-status
379   COPROCESSES([svc], [
380     echo SVCCLAIM test 1.0.0
381     read line
382     case "$line" in
383       OK)
384         ;;
385       *)
386         echo >&2 "SVCCLAIM failed: $line"
387         exit 1
388         ;;
389     esac
390     echo ok >svc-test-running
391     while read line; do
392       set -- $line
393       case "$[]1,$[]3,$[]4" in
394         SVCJOB,test,HELP)
395           echo SVCINFO try not to use this service for anything useful
396           echo SVCOK $[]2
397           ;;
398         SVCJOB,test,GOOD)
399           echo SVCOK $[]2
400           ;;
401         SVCJOB,test,BAD)
402           echo SVCFAIL $[]2 this-command-always-fails
403           ;;
404         SVCJOB,test,UGLY)
405           tag=$2
406           while read line; do
407             set -- $line
408             case "$[]1,$[]2,$[]3,$[]4" in
409               SVCCANCEL,$tag,,) break ;;
410               SVCJOB,*,test,ESCAPE)
411                 echo >&2 "attempt to escape from alkatraz"
412                 exit 1
413                 ;;
414             esac
415           done
416           ;;
417         SVCJOB,test,FIRST)
418           firsttag=$[]2
419           ;;
420         SVCJOB,test,SECOND)
421           echo SVCOK $firsttag
422           echo SVCOK $[]2
423           ;;
424         SVCJOB,*)
425           echo SVCFAIL $[]2 unknown-svc-command $[]4
426           ;;
427         SVCCLAIM,*)
428           break
429           ;;
430         OK,* | INFO,*)
431           ;;
432         FAIL,*)
433           echo "failure in service: $line" >&2
434           ;;
435       esac
436     done
437   ], [
438     TRIPECTL; echo $? >tripectl-status
439   ]) 2>tripectl-errors &
440
441   ## Wait until it starts up.
442   while test ! -r svc-test-running && test ! -r tripectl-status; do :; done
443
444   ## Make sure it's running.
445   AT_CHECK([TRIPECTL SVCQUERY test],, [name=test version=1.0.0[]nl])
446
447   ## Try some simple commands.
448   AT_CHECK([TRIPECTL SVCSUBMIT test GOOD])
449   AT_CHECK([TRIPECTL SVCSUBMIT test BAD], [1],,
450            [tripectl: this-command-always-fails[]nl])
451
452   ## And now with commands in the background.
453   TRIPECTL_INTERACT([
454     TRIPECTL_COMMAND([SVCSUBMIT test GOOD], [OK])
455     TRIPECTL_COMMAND([SVCSUBMIT -background foo test UGLY], [BGDETACH foo])
456     TRIPECTL_COMMAND([BGCANCEL foo], [OK])
457     TRIPECTL_COMMAND([SVCSUBMIT test ESCAPE],
458                      [FAIL unknown-svc-command ESCAPE])
459   ])
460
461   ## Out-of-order completion.
462   TRIPECTL_INTERACT([
463     TRIPECTL_COMMAND([SVCSUBMIT -background one test FIRST], [BGDETACH one])
464     TRIPECTL_COMMAND([SVCSUBMIT -background two test SECOND], [BGDETACH two])
465     TRIPECTL_COMMAND([!], [BGOK one])
466     TRIPECTL_COMMAND([!], [BGOK two])
467   ])
468
469   ## All done.
470   exit 0
471 ])
472
473 AT_CLEANUP
474
475 ###----- That's all, folks --------------------------------------------------