chiark / gitweb /
server/peer.c, server/tests.at: Handle NAT swapping over peer addresses.
[tripe] / server / tests.at
index 188d65041a2696a6f5a058c747762120c6276928..ab64c538ff3a47634075ea05e15af1cc39f76409 100644 (file)
@@ -38,6 +38,8 @@ m4_define([TRIPE],
      $abs_top_builddir/server/tripe -F -d. -aadmin -p0 -b127.0.0.1 -talice])
 m4_define([TRIPECTL], [$abs_top_builddir/client/tripectl -d. -aadmin])
 m4_define([USLIP], [$abs_top_builddir/uslip/tripe-uslip])
+m4_define([PKSTREAM],
+  [$abs_top_builddir/pkstream/pkstream -b127.0.0.1 -p127.0.0.1])
 
 ## Sequences.  (These are used for testing the replay protection machinery.)
 m4_define([R32], [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15   dnl
@@ -48,8 +50,15 @@ m4_define([P32], [21 26 14 12 25 18  2 27 10 31 24 29  0 20 17 11   dnl
 ###--------------------------------------------------------------------------
 ### Scaffolding for running a TrIPE server.
 
+## WITH_TRIPEX(dir, args, body)
 m4_define([WITH_TRIPEX], [
 
+## If this directory doesn't exist then Bad Things will happen.
+if test ! -d $1; then
+  echo >&2 "server directory \`$1' doesn't exist"
+  exit 99
+fi
+
 ## Remove the status file.  This is how we notice that the server's died.
 rm -f $1/server-status
 > $1/expected-server-output
@@ -67,6 +76,10 @@ exec 1>&3 3>&-
 ## not sleep for ages.  (Yes, a second on each test group is a long time.)
 while test ! -r $1/server-status && test ! -S $1/admin; do :; done
 
+## Make the port number availale.
+AT_CHECK([TRIPECTL -d$1 PORT],, [stdout])
+mv stdout $1/port
+
 ## Test body...
 $3
 
@@ -82,12 +95,68 @@ $3
   (cd strace-hack/
    ulimit -c hard >/dev/null 2>&1
    strace -f -o ../tripe.trace \
-     TRIPE -d.. $2 >../server-output 2>../server-errors)
+     TRIPE -d.. $2 >../server-output.full 2>../server-errors)
   stat=$?
   echo $stat >server-status
   if test $stat -ne 0; then
     echo "exit status: $stat" >>server-errors
   fi
+
+  ## We interrupt this relatively sensible macro for an especially awful
+  ## hack.  The tripe server emits warnings which are often caused by lack of
+  ## synchronization between two peers.  These are harmless and shouldn't
+  ## cause test failures.  But we shouldn't just trim out all messages that
+  ## look like the spurious ones: if they appear when we're not expecting
+  ## them, that's bad and they should properly cause a test failure.
+  ##
+  ## So we use the WARN command to insert magic directives into the server's
+  ## message stream.  The directives begin with `WARN USER test'; remaining
+  ## tokens may be as follows.
+  ##
+  ## PUSH              Introduce a new layer of nesting.  Settings between
+  ##                   this PUSH and the matching POP will be forgotten
+  ##                   following the POP.
+  ##
+  ## POP               End a layer of nesting.  See PUSH above.
+  ##
+  ## IGNORE tokens     Ignore lines which begin with the tokens.
+  ##
+  ## Token comparison isn't done very well, but it's good enough for these
+  ## purposes.
+  ##
+  ## We also trim out trace lines here, since it's useful to produce them for
+  ## debugging purposes and changing or introducing more of them shouldn't
+  ## cause failures.
+  awk '
+       BEGIN {
+         sp = 0;
+         npat = 0;
+       }
+       $[]1 == "TRACE" { next; }
+       $[]1 == "WARN" && $[]2 == "USER" && $[]3 == "test" {
+         if ($[]4 == "PUSH")
+           npatstk[[sp++]] = npat;
+         else if ($[]4 == "IGNORE") {
+           s = "";
+           p = "";
+           for (i = 5; i <= NF; i++) {
+             p = p s $[]i;
+             s = " ";
+           }
+           pat[[npat++]] = p;
+         } else if ($[]4 == "POP")
+           npat = npatstk[[--sp]];
+         next;
+       }
+       {
+         for (i = 0; i < npat; i++) {
+           n = length(pat[[i]]);
+           if (substr($[]0, 0, n) == pat[[i]])
+             next;
+         }
+         print $[]0;
+       }
+  ' server-output.full >server-output
 }
 exec 3>&-
 
@@ -98,11 +167,89 @@ AT_CHECK([cat $1/server-output; cat >&2 $1/server-errors],,
         [expout], [experr])
 ])
 
+## WITH_TRIPE(args, body)
 m4_define([WITH_TRIPE], [WITH_TRIPEX([.], [$1], [$2])])
 
+## WITH_2TRIPES(adir, bdir, bothargs, aargs, bargs, body)
 m4_define([WITH_2TRIPES],
          [WITH_TRIPEX([$1], [$3 $4], [WITH_TRIPEX([$2], [$3 $5], [$6])])])
 
+## WITH_3TRIPES(adir, bdir, cdir, allargs, aargs, bargs, cargs, body)
+m4_define([WITH_3TRIPES],
+         [WITH_TRIPEX([$1], [$4 $5],
+         [WITH_TRIPEX([$2], [$4 $6],
+         [WITH_TRIPEX([$3], [$4 $7],
+         [$8])])])])
+
+## COMMS_EPING(adir, aname, bdir, bname)
+m4_define([COMMS_EPING], [
+  AT_CHECK([TRIPECTL -d$1 EPING $4],, [ignore])
+  AT_CHECK([TRIPECTL -d$3 EPING $2],, [ignore])
+])
+
+## COMMS_SLIP(adir, aname, bdir, bname)
+m4_define([COMMS_SLIP], [
+  AT_CHECK([echo "from $1" | USLIP -p $1/$4])
+  AT_CHECK([USLIP -g $3/$2],, [from $1[]nl])
+  AT_CHECK([echo "from $3" | USLIP -p $3/$2])
+  AT_CHECK([USLIP -g $1/$4],, [from $3[]nl])
+])
+
+## AWAIT_KXDONE(adir, aname, bdir, bname, body)
+m4_define([AWAIT_KXDONE], [
+
+  ## Ignore some reports caused by races.
+  TRIPECTL -d$3 WARN test PUSH
+  TRIPECTL -d$3 WARN test IGNORE WARN KX $2 incorrect cookie
+  TRIPECTL -d$3 WARN test IGNORE WARN KX $2 unexpected pre-challenge
+
+  ## Watch for the key-exchange completion announcement in the background.
+  COPROCESSES([wait-$1], [
+    echo WATCH +n
+    while read line; do
+      set x $line; shift
+      echo >&2 ">>> $line"
+      case "$[]1:$[]2:$[]3" in
+       OK::) ;;
+       NOTE:KXDONE:$4) break ;;
+       NOTE:*) ;;
+       *) exit 63 ;;
+      esac
+    done
+  ], [
+    TRIPECTL -d$1
+  ]) & waiter_$1=$!
+
+  $5
+
+  ## Collect the completion announcement.
+  wait $waiter_$1; waitrc=$?
+  AT_CHECK([echo $waitrc],, [0[]nl])
+
+  ## Be interested in key-exchange warnings again.
+  TRIPECTL -d$4 WARN test POP
+])
+
+## ESTABLISH(adir, aname, aopts, bdir, bname, bopts, [aport], [bport])
+m4_define([ESTABLISH], [
+
+  ## Set up the establishment.
+  AWAIT_KXDONE([$1], [$2], [$4], [$5], [
+    AT_CHECK([TRIPECTL -d$1 ADD -cork $6 $5 INET 127.0.0.1 \
+      m4_if([$8], [], [$(cat $4/port)], [$8])])
+    AT_CHECK([TRIPECTL -d$4 ADD $3 $2 INET 127.0.0.1 \
+      m4_if([$7], [], [$(cat $1/port)], [$7])])
+  ])
+
+  ## Check transport pinging.
+  AT_CHECK([TRIPECTL -d$1 PING $5],, [ignore])
+  AT_CHECK([TRIPECTL -d$4 PING $2],, [ignore])
+
+  ## Check communication works.
+  COMMS_EPING([$1], [$2], [$4], [$5])
+  COMMS_SLIP([$1], [$2], [$4], [$5])
+])
+
 ###--------------------------------------------------------------------------
 ### Very unpleasant coprocess handling.
 
@@ -135,7 +282,7 @@ m4_define([TRIPECTL_COMMAND], [
 ### Make sure the thing basically works.
 
 AT_SETUP([server basics])
-SETUPDIR([ec])
+SETUPDIR([alpha])
 AT_CHECK([echo port | TRIPE -p54321],, [INFO 54321[]nl[]OK[]nl])
 AT_CLEANUP
 
@@ -144,7 +291,7 @@ AT_CLEANUP
 
 AT_SETUP([server challenges])
 AT_KEYWORDS([chal])
-SETUPDIR([ec])
+SETUPDIR([alpha])
 
 WITH_TRIPE(, [
   ## A simple test.
@@ -202,56 +349,76 @@ AT_SETUP([server communication])
 AT_KEYWORDS([comm])
 export TRIPE_SLIPIF=USLIP
 
-for i in alice bob; do (mkdir $i; cd $i; SETUPDIR([ec])); done
+for p in alice bob; do (mkdir $p; cd $p; SETUPDIR([alpha])); done
 
 WITH_2TRIPES([alice], [bob], [-nslip], [-talice], [-tbob], [
+  ESTABLISH([alice], [not-alice], [-key alice],
+           [bob], [bob], [])
+])
 
-  AT_CHECK([TRIPECTL -dalice PORT],, [stdout])
-  mv stdout alice/port
+AT_CLEANUP
 
-  AT_CHECK([TRIPECTL -dbob PORT],, [stdout])
-  mv stdout bob/port
+###--------------------------------------------------------------------------
+### Mobile peer tracking.
 
-  ## Watch for the key-exchange completion announcement, and then exit.
-  COPROCESSES([wait], [
-    echo WATCH +n
-    while read line; do
-      case "$line" in
-       OK) ;;
-       "NOTE KXDONE "*) break ;;
-       NOTE*) ;;
-       *) exit 63 ;;
-      esac
-    done
-  ], [
-    TRIPECTL -dalice
-  ]) &
+AT_SETUP([peer tracking])
+AT_KEYWORDS([mobile])
+export TRIPE_SLIPIF=USLIP
 
-  ## Don't panic if you don't see the unexpected-source warning.  It happens
-  ## for me, but it's not important either way.
-  AT_CHECK([TRIPECTL -dalice ADD bob INET 127.0.0.1 $(cat bob/port)])
-  echo >>bob/expected-server-output \
-    "WARN PEER - unexpected-source INET 127.0.0.1 $(cat alice/port)"
-  AT_CHECK([TRIPECTL -dbob ADD -key alice not-alice \
-                              INET 127.0.0.1 $(cat alice/port)])
+for p in alice bob carol; do (mkdir $p; cd $p; SETUPDIR([alpha])); done
+
+## WITH_PKSTREAM(adir, aport, bdir, bport, body)
+m4_define([WITH_PKSTREAM], [
+  echo >&2 "pkstream: $1 <--> :$2 <-pkstream-> :$4 <--> $3"
+  PKSTREAM -l$4 127.0.0.1:$4 127.0.0.1:$(cat $3/port)& pkstream_$3_$1=$!
+  sleep 1
+  PKSTREAM -c127.0.0.1:$4 127.0.0.1:$2 127.0.0.1:$(cat $1/port)&
+  pkstream_$1_$3=$!
+  set +x
+  $5
+  kill $pkstream_$3_$1 $pkstream_$1_$3
+])
 
-  ## Check transport pinging.
-  AT_CHECK([TRIPECTL -dalice PING bob],, [ignore])
-  AT_CHECK([TRIPECTL -dbob PING not-alice],, [ignore])
+WITH_3TRIPES([alice], [bob], [carol], [-nslip],
+            [-talice], [-tbob], [-tcarol], [
 
-  ## Wait for the completion announcement.
-  wait
+  ## We need an indirection layer between the two peers so that we can
+  ## simulate the effects of NAT remapping.  The nearest thing we have to
+  ## this is pkstream, so we may as well use that.
+  ##
+  ## alice <--> :5311 <-pkstream-> :5312 <--> bob
+  ## alice <--> :5321 <-pkstream-> :5322 <--> carol
 
-  ## Check encrypted pinging.
-  AT_CHECK([TRIPECTL -dalice EPING bob],, [ignore])
-  AT_CHECK([TRIPECTL -dbob EPING not-alice],, [ignore])
+  WITH_PKSTREAM([alice], [5311], [bob], [5312], [
+    ESTABLISH([alice], [alice], [], [bob], [bob], [-mobile], [5312], [5311])
+  ])
 
-  ## Check that packets can flow from one to the other.
-  AT_CHECK([echo "from alice" | USLIP -p alice/bob])
-  AT_CHECK([USLIP -g bob/not-alice],, [from alice[]nl])
+  WITH_PKSTREAM([alice], [5319], [bob], [5312], [
+    COMMS_EPING([bob], [bob], [alice], [alice])
+    COMMS_SLIP([bob], [bob], [alice], [alice])
+  ])
 
-  AT_CHECK([echo "from bob" | USLIP -p bob/not-alice])
-  AT_CHECK([USLIP -g alice/bob],, [from bob[]nl])
+  WITH_PKSTREAM([alice], [5321], [carol], [5322], [
+    ESTABLISH([alice], [alice], [], [carol], [carol], [-mobile],
+       [5322], [5321])
+  ])
+
+  WITH_PKSTREAM([alice], [5311], [bob], [5312], [
+  WITH_PKSTREAM([alice], [5321], [carol], [5322], [
+    COMMS_EPING([bob], [bob], [alice], [alice])
+    COMMS_EPING([carol], [carol], [alice], [alice])
+    COMMS_SLIP([bob], [bob], [alice], [alice])
+    COMMS_SLIP([carol], [carol], [alice], [alice])
+  ])])
+
+  WITH_PKSTREAM([alice], [5321], [bob], [5312], [
+  WITH_PKSTREAM([alice], [5311], [carol], [5322], [
+    COMMS_EPING([bob], [bob], [alice], [alice])
+    COMMS_EPING([carol], [carol], [alice], [alice])
+    COMMS_SLIP([bob], [bob], [alice], [alice])
+    COMMS_SLIP([carol], [carol], [alice], [alice])
+  ])])
+  wait
 ])
 
 AT_CLEANUP
@@ -261,7 +428,7 @@ AT_CLEANUP
 
 AT_SETUP([server services])
 AT_KEYWORDS([svc])
-SETUPDIR([ec])
+SETUPDIR([alpha])
 
 WITH_TRIPE(, [