[gnutls-devel] Parallel test bug in gnutls-3.2.2

Adam Sampson ats at offog.org
Tue Jul 16 17:45:29 CEST 2013


On Tue, Jul 16, 2013 at 10:35:55AM +0200, Nikos Mavrogiannopoulos wrote:
> Or using socketpair() for all tests. Any patches are appreciated.

Sounds like a good idea. Patches attached:

- fix gdoc to make it work with Perl 5.18;
- fix a typo in the old TCP error handling;
- replace the TCP code with socketpair().

Note that while the socketpair() code only uses features in POSIX:2001,
I've only tested it on Linux/glibc...

DCO for Adam Sampson <ats at offog.org>:

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

-- 
Adam Sampson <ats at offog.org>                         <http://offog.org/>
-------------- next part --------------
From 24d15445a6851befcc7389493684856831a93c4c Mon Sep 17 00:00:00 2001
From: Adam Sampson <ats at offog.org>
Date: Tue, 16 Jul 2013 14:17:18 +0100
Subject: [PATCH 1/3] Avoid depending on hash order in gdoc.

Previously, gdoc had a hash of regexp replacements for each output
format, and applied the replacements in the order that "keys" returned
for the hash. However, not all orders are safe -- and now that Perl 5.18
randomises hash order per-process, it only worked sometimes!

For example, this order is OK:

'is a #gnutls_session_t structure.'
'\@([A-Za-z0-9_]+)\s*' -> 'is a #gnutls_session_t structure.'
'\%([A-Za-z0-9_]+)' -> 'is a #gnutls_session_t structure.'
'\#([A-Za-z0-9_]+)' -> 'is a @code{gnutls_session_t}  structure.'
'([A-Za-z0-9_]+\(\))' -> 'is a @code{gnutls_session_t}  structure.'

This one, however, winds up producing invalid texinfo:

'is a #gnutls_session_t structure.'
'\%([A-Za-z0-9_]+)' -> 'is a #gnutls_session_t structure.'
'([A-Za-z0-9_]+\(\))' -> 'is a #gnutls_session_t structure.'
'\#([A-Za-z0-9_]+)' -> 'is a @code{gnutls_session_t}  structure.'
'\@([A-Za-z0-9_]+)\s*' -> 'is a  @code{code} {gnutls_session_t}  structure.'

This patch turns the hash into a list, so the replacements will always
be done in the intended order.

Signed-off-by: Adam Sampson <ats at offog.org>
---
 doc/scripts/gdoc | 72 +++++++++++++++++++++++++++++---------------------------
 1 file changed, 37 insertions(+), 35 deletions(-)

diff --git a/doc/scripts/gdoc b/doc/scripts/gdoc
index 953cd57..dbe2efe 100755
--- a/doc/scripts/gdoc
+++ b/doc/scripts/gdoc
@@ -9,6 +9,8 @@ eval '(exit $?0)' && eval 'exec perl "$0" ${1+"$@"}'
 ## Copyright (c) 2001, 2002 Nikos Mavrogiannopoulos
 ##                    added -tex
 ## Copyright (c) 1998 Michael Zucchi
+## Copyright (c) 2013 Adam Sampson
+##                    made highlighting not depend on hash order, for Perl 5.18
 
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -145,46 +147,46 @@ $type_env = "(\\\$[A-Za-z0-9_]+)";
 #  One for each output format
 
 # these work fairly well
-%highlights_html = ( $type_constant, '"<i>$1</i>"',
-		     $type_func, '"<b>$1</b>"',
-		     $type_struct, '"<i>$1</i>"',
-		     $type_param, '" <tt><b>$1</b></tt> "' );
+ at highlights_html = ( [$type_constant, '"<i>$1</i>"'],
+		     [$type_func, '"<b>$1</b>"'],
+		     [$type_struct, '"<i>$1</i>"'],
+		     [$type_param, '" <tt><b>$1</b></tt> "'] );
 $blankline_html = "<p>";
 
-%highlights_texinfo = ( $type_param, '" \@code{$1} "',
-			$type_constant, '"\@code{$1} "',
-			$type_func, '"\@code{$1} "',
-			$type_struct, '"\@code{$1} "',
+ at highlights_texinfo = ( [$type_param, '" \@code{$1} "'],
+			[$type_constant, '"\@code{$1} "'],
+			[$type_func, '"\@code{$1} "'],
+			[$type_struct, '"\@code{$1} "'],
 			 );
 $blankline_texinfo = "";
 
-%highlights_tex = ( $type_param, '" {\\\bf $1} "',
-		$type_constant, '"{\\\it $1}"',
-		$type_func, '"{\\\bf $1}"',
-		$type_struct, '"{\\\it $1}"',
+ at highlights_tex = ( [$type_param, '" {\\\bf $1} "'],
+		[$type_constant, '"{\\\it $1}"'],
+		[$type_func, '"{\\\bf $1}"'],
+		[$type_struct, '"{\\\it $1}"'],
 		      );
 $blankline_tex = "\\\\";
 
 # sgml, docbook format
-%highlights_sgml = ( $type_constant, '"<replaceable class=\"option\">$1</replaceable>"',
-		     $type_func, '"<function>$1</function>"',
-		     $type_struct, '"<structname>$1</structname>"',
-		     $type_env, '"<envar>$1</envar>"',
-		     $type_param, '" <parameter>$1</parameter> "' );
+ at highlights_sgml = ( [$type_constant, '"<replaceable class=\"option\">$1</replaceable>"'],
+		     [$type_func, '"<function>$1</function>"'],
+		     [$type_struct, '"<structname>$1</structname>"'],
+		     [$type_env, '"<envar>$1</envar>"'],
+		     [$type_param, '" <parameter>$1</parameter> "'] );
 $blankline_sgml = "</para><para>\n";
 
 # these are pretty rough
-%highlights_man = ( $type_constant, '"\\\fB$1\\\fP"',
-		    $type_func, '"\\\fB$1\\\fP"',
-		    $type_struct, '"\\\fB$1\\\fP"',
-		    $type_param, '" \\\fI$1\\\fP "' );
+ at highlights_man = ( [$type_constant, '"\\\fB$1\\\fP"'],
+		    [$type_func, '"\\\fB$1\\\fP"'],
+		    [$type_struct, '"\\\fB$1\\\fP"'],
+		    [$type_param, '" \\\fI$1\\\fP "'] );
 $blankline_man = "";
 
 # text-mode
-%highlights_text = ( $type_constant, '"$1"',
-		     $type_func, '"$1"',
-		     $type_struct, '"$1"',
-		     $type_param, '"$1 "' );
+ at highlights_text = ( [$type_constant, '"$1"'],
+		     [$type_func, '"$1"'],
+		     [$type_struct, '"$1"'],
+		     [$type_param, '"$1 "'] );
 $blankline_text = "";
 my $lineprefix = "";
 
@@ -205,7 +207,7 @@ if ($#ARGV==-1) {
 
 $verbose = 0;
 $output_mode = "man";
-%highlights = %highlights_man;
+ at highlights = @highlights_man;
 $blankline = $blankline_man;
 $modulename = "API Documentation";
 $sourceversion = strftime "%Y-%m-%d", localtime;
@@ -214,27 +216,27 @@ while ($ARGV[0] =~ m/^-(.*)/) {
     $cmd = shift @ARGV;
     if ($cmd eq "-html") {
 	$output_mode = "html";
-	%highlights = %highlights_html;
+	@highlights = @highlights_html;
 	$blankline = $blankline_html;
     } elsif ($cmd eq "-man") {
 	$output_mode = "man";
-	%highlights = %highlights_man;
+	@highlights = @highlights_man;
 	$blankline = $blankline_man;
     } elsif ($cmd eq "-tex") {
 	$output_mode = "tex";
-	%highlights = %highlights_tex;
+	@highlights = @highlights_tex;
 	$blankline = $blankline_tex;
     } elsif ($cmd eq "-texinfo") {
 	$output_mode = "texinfo";
-	%highlights = %highlights_texinfo;
+	@highlights = @highlights_texinfo;
 	$blankline = $blankline_texinfo;
     } elsif ($cmd eq "-text") {
 	$output_mode = "text";
-	%highlights = %highlights_text;
+	@highlights = @highlights_text;
 	$blankline = $blankline_text;
     } elsif ($cmd eq "-docbook") {
 	$output_mode = "sgml";
-	%highlights = %highlights_sgml;
+	@highlights = @highlights_sgml;
 	$blankline = $blankline_sgml;
     } elsif ($cmd eq "-listfunc") {
 	$output_mode = "listfunc";
@@ -308,9 +310,9 @@ sub just_highlight {
     my $line;
     my $ret = "";
 
-    foreach $pattern (keys %highlights) {
-	#print "scanning pattern $pattern ($highlights{$pattern})\n";
-	my $replace = $highlights{$pattern}; 
+    foreach $highlight (@highlights) {
+	my ($pattern, $replace) = @$highlight;
+	#print "scanning pattern $pattern ($replace)\n";
 	$contents =~ s/$pattern/$replace/gees;
     }
     foreach $line (split "\n", $contents) {
-- 
1.8.3

-------------- next part --------------
From 3523df705f69d8fe38b6791e95c0c39871244822 Mon Sep 17 00:00:00 2001
From: Adam Sampson <ats at offog.org>
Date: Tue, 16 Jul 2013 15:16:22 +0100
Subject: [PATCH 2/3] Detect socket() error responses correctly.

The code was testing the wrong variable...

Signed-off-by: Adam Sampson <ats at offog.org>
---
 tests/anonself.c    | 2 +-
 tests/dhepskself.c  | 2 +-
 tests/openpgpself.c | 2 +-
 tests/pskself.c     | 2 +-
 tests/resume-dtls.c | 2 +-
 tests/resume.c      | 2 +-
 tests/x509dn.c      | 2 +-
 tests/x509self.c    | 2 +-
 8 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/tests/anonself.c b/tests/anonself.c
index 3aa8e6b..ef69d52 100644
--- a/tests/anonself.c
+++ b/tests/anonself.c
@@ -209,7 +209,7 @@ server_start (void)
   /* Socket operations
    */
   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (err == -1)
+  if (listen_sd == -1)
     {
       perror ("socket");
       fail ("server: socket failed\n");
diff --git a/tests/dhepskself.c b/tests/dhepskself.c
index 5ca7e99..18c0366 100644
--- a/tests/dhepskself.c
+++ b/tests/dhepskself.c
@@ -218,7 +218,7 @@ server_start (void)
   /* Socket operations
    */
   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (err == -1)
+  if (listen_sd == -1)
     {
       perror ("socket");
       fail ("server: socket failed\n");
diff --git a/tests/openpgpself.c b/tests/openpgpself.c
index fb30229..e904acc 100644
--- a/tests/openpgpself.c
+++ b/tests/openpgpself.c
@@ -493,7 +493,7 @@ server_start (void)
   /* Socket operations
    */
   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (err == -1)
+  if (listen_sd == -1)
     {
       perror ("socket");
       fail ("server: socket failed\n");
diff --git a/tests/pskself.c b/tests/pskself.c
index aa698df..e04914e 100644
--- a/tests/pskself.c
+++ b/tests/pskself.c
@@ -209,7 +209,7 @@ server_start (void)
   /* Socket operations
    */
   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (err == -1)
+  if (listen_sd == -1)
     {
       perror ("socket");
       fail ("server: socket failed\n");
diff --git a/tests/resume-dtls.c b/tests/resume-dtls.c
index 7fbe7b0..89b88d0 100644
--- a/tests/resume-dtls.c
+++ b/tests/resume-dtls.c
@@ -305,7 +305,7 @@ global_start (void)
   /* Socket operations
    */
   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (err == -1)
+  if (listen_sd == -1)
     {
       perror ("socket");
       fail ("server: socket failed\n");
diff --git a/tests/resume.c b/tests/resume.c
index 03b5164..a87e80f 100644
--- a/tests/resume.c
+++ b/tests/resume.c
@@ -296,7 +296,7 @@ global_start (void)
   /* Socket operations
    */
   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (err == -1)
+  if (listen_sd == -1)
     {
       perror ("socket");
       fail ("server: socket failed\n");
diff --git a/tests/x509dn.c b/tests/x509dn.c
index ff79fb8..81402d1 100644
--- a/tests/x509dn.c
+++ b/tests/x509dn.c
@@ -389,7 +389,7 @@ server_start (void)
   /* Socket operations
    */
   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (err == -1)
+  if (listen_sd == -1)
     {
       perror ("socket");
       fail ("server: socket failed\n");
diff --git a/tests/x509self.c b/tests/x509self.c
index 859a0b1..14b6f3b 100644
--- a/tests/x509self.c
+++ b/tests/x509self.c
@@ -351,7 +351,7 @@ server_start (void)
   /* Socket operations
    */
   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (err == -1)
+  if (listen_sd == -1)
     {
       perror ("socket");
       fail ("server: socket failed\n");
-- 
1.8.3

-------------- next part --------------
From 32f522843dd6402a6b82bfd1b136df10c7eb0ea5 Mon Sep 17 00:00:00 2001
From: Adam Sampson <ats at offog.org>
Date: Tue, 16 Jul 2013 16:22:37 +0100
Subject: [PATCH 3/3] Use socketpair() rather than TCP connections.

Besides simplifying the code, this also makes it possible to run "make check"
in parallel -- previously this didn't work because several tests were trying to
bind the same port.

Signed-off-by: Adam Sampson <ats at offog.org>
---
 tests/anonself.c    |  92 ++++++---------------------------
 tests/dhepskself.c  |  96 ++++++----------------------------
 tests/openpgpself.c | 145 +++++++++++++++++-----------------------------------
 tests/pskself.c     |  96 ++++++----------------------------
 tests/resume-dtls.c | 113 ++++++++++++----------------------------
 tests/resume.c      | 113 ++++++++++++----------------------------
 tests/x509dn.c      |  95 +++++++---------------------------
 tests/x509self.c    |  98 +++++++----------------------------
 8 files changed, 196 insertions(+), 652 deletions(-)

diff --git a/tests/anonself.c b/tests/anonself.c
index ef69d52..c76e8ba 100644
--- a/tests/anonself.c
+++ b/tests/anonself.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2004-2012 Free Software Foundation, Inc.
+ * Copyright (C) 2013 Adam Sampson <ats at offog.org>
  *
  * Author: Simon Josefsson
  *
@@ -34,13 +35,10 @@
 #include <sys/socket.h>
 #if !defined(_WIN32)
 #include <sys/wait.h>
-#include <arpa/inet.h>
 #endif
 #include <unistd.h>
 #include <gnutls/gnutls.h>
 
-#include "tcp.c"
-
 #include "utils.h"
 
 static void
@@ -56,9 +54,9 @@ tls_log_func (int level, const char *str)
 #define MSG "Hello TLS"
 
 static void
-client (void)
+client (int sd)
 {
-  int ret, sd, ii;
+  int ret, ii;
   gnutls_session_t session;
   char buffer[MAX_BUF + 1];
   gnutls_anon_client_credentials_t anoncred;
@@ -83,10 +81,6 @@ client (void)
    */
   gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
 
-  /* connect to the peer
-   */
-  sd = tcp_connect ();
-
   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
 
   /* Perform the TLS handshake
@@ -139,7 +133,7 @@ client (void)
 
 end:
 
-  tcp_close (sd);
+  close (sd);
 
   gnutls_deinit (session);
 
@@ -151,9 +145,7 @@ end:
 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
  */
 
-#define SA struct sockaddr
 #define MAX_BUF 1024
-#define PORT 5556               /* listen to 5556 port */
 #define DH_BITS 1024
 
 /* These are global */
@@ -193,59 +185,14 @@ generate_dh_params (void)
   return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
 }
 
-int err, listen_sd, i;
-int sd, ret;
-struct sockaddr_in sa_serv;
-struct sockaddr_in sa_cli;
-socklen_t client_len;
+int err, ret;
 char topbuf[512];
 gnutls_session_t session;
 char buffer[MAX_BUF + 1];
 int optval = 1;
 
 static void
-server_start (void)
-{
-  /* Socket operations
-   */
-  listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (listen_sd == -1)
-    {
-      perror ("socket");
-      fail ("server: socket failed\n");
-      return;
-    }
-
-  memset (&sa_serv, '\0', sizeof (sa_serv));
-  sa_serv.sin_family = AF_INET;
-  sa_serv.sin_addr.s_addr = INADDR_ANY;
-  sa_serv.sin_port = htons (PORT);      /* Server Port number */
-
-  setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
-              sizeof (int));
-
-  err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
-  if (err == -1)
-    {
-      perror ("bind");
-      fail ("server: bind failed\n");
-      return;
-    }
-
-  err = listen (listen_sd, 1024);
-  if (err == -1)
-    {
-      perror ("listen");
-      fail ("server: listen failed\n");
-      return;
-    }
-
-  if (debug)
-    success ("server: ready. Listening to port '%d'.\n", PORT);
-}
-
-static void
-server (void)
+server (int sd)
 {
   /* this must be called once in the program
    */
@@ -264,17 +211,8 @@ server (void)
 
   gnutls_anon_set_server_dh_params (anoncred, dh_params);
 
-  client_len = sizeof (sa_cli);
-
   session = initialize_tls_session ();
 
-  sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
-
-  if (debug)
-    success ("server: connection from %s, port %d\n",
-             inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-                        sizeof (topbuf)), ntohs (sa_cli.sin_port));
-
   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
   ret = gnutls_handshake (session);
   if (ret < 0)
@@ -295,7 +233,6 @@ server (void)
   /* see the Getting peer's information example */
   /* print_info(session); */
 
-  i = 0;
   for (;;)
     {
       memset (buffer, 0, MAX_BUF + 1);
@@ -326,8 +263,6 @@ server (void)
   close (sd);
   gnutls_deinit (session);
 
-  close (listen_sd);
-
   gnutls_anon_free_server_credentials (anoncred);
 
   gnutls_dh_params_deinit (dh_params);
@@ -342,10 +277,15 @@ void
 doit (void)
 {
   pid_t child;
+  int sockets[2];
 
-  server_start ();
-  if (error_count)
-    return;
+  err = socketpair (AF_UNIX, SOCK_STREAM, 0, sockets);
+  if (err == -1)
+    {
+      perror ("socketpair");
+      fail ("socketpair failed\n");
+      return;
+    }
 
   child = fork ();
   if (child < 0)
@@ -359,9 +299,9 @@ doit (void)
     {
       int status;
       /* parent */
-      server ();
+      server (sockets[0]);
       wait (&status);
     }
   else
-    client ();
+    client (sockets[1]);
 }
diff --git a/tests/dhepskself.c b/tests/dhepskself.c
index 18c0366..854a711 100644
--- a/tests/dhepskself.c
+++ b/tests/dhepskself.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2004-2012 Free Software Foundation, Inc.
+ * Copyright (C) 2013 Adam Sampson <ats at offog.org>
  *
  * Author: Simon Josefsson
  *
@@ -33,14 +34,10 @@
 #include <sys/socket.h>
 #if !defined(_WIN32)
 #include <sys/wait.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 #endif
 #include <unistd.h>
 #include <gnutls/gnutls.h>
 
-#include "tcp.c"
-
 #include "utils.h"
 
 /* A very basic TLS client, with PSK authentication.
@@ -56,9 +53,9 @@ tls_log_func (int level, const char *str)
 }
 
 static void
-client (void)
+client (int sd)
 {
-  int ret, sd, ii;
+  int ret, ii;
   gnutls_session_t session;
   char buffer[MAX_BUF + 1];
   gnutls_psk_client_credentials_t pskcred;
@@ -85,10 +82,6 @@ client (void)
    */
   gnutls_credentials_set (session, GNUTLS_CRD_PSK, pskcred);
 
-  /* connect to the peer
-   */
-  sd = tcp_connect ();
-
   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
 
   /* Perform the TLS handshake
@@ -134,7 +127,7 @@ client (void)
 
 end:
 
-  tcp_close (sd);
+  close (sd);
 
   gnutls_deinit (session);
 
@@ -146,9 +139,7 @@ end:
 /* This is a sample TLS 1.0 echo server, for PSK authentication.
  */
 
-#define SA struct sockaddr
 #define MAX_BUF 1024
-#define PORT 5556               /* listen to 5556 port */
 
 /* These are global */
 gnutls_psk_server_credentials_t server_pskcred;
@@ -199,62 +190,14 @@ pskfunc (gnutls_session_t session, const char *username, gnutls_datum_t * key)
   return 0;
 }
 
-int err, listen_sd, i;
-int sd, ret;
-struct sockaddr_in sa_serv;
-struct sockaddr_in sa_cli;
-socklen_t client_len;
+int err, ret;
 char topbuf[512];
 gnutls_session_t session;
 char buffer[MAX_BUF + 1];
 int optval = 1;
 
 static void
-server_start (void)
-{
-  if (debug)
-    success ("Launched, generating DH parameters...\n");
-
-  /* Socket operations
-   */
-  listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (listen_sd == -1)
-    {
-      perror ("socket");
-      fail ("server: socket failed\n");
-      return;
-    }
-
-  memset (&sa_serv, '\0', sizeof (sa_serv));
-  sa_serv.sin_family = AF_INET;
-  sa_serv.sin_addr.s_addr = INADDR_ANY;
-  sa_serv.sin_port = htons (PORT);      /* Server Port number */
-
-  setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
-              sizeof (int));
-
-  err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
-  if (err == -1)
-    {
-      perror ("bind");
-      fail ("server: bind failed\n");
-      return;
-    }
-
-  err = listen (listen_sd, 1024);
-  if (err == -1)
-    {
-      perror ("listen");
-      fail ("server: listen failed\n");
-      return;
-    }
-
-  if (debug)
-    success ("server: ready. Listening to port '%d'.\n", PORT);
-}
-
-static void
-server (void)
+server (int sd)
 {
   /* this must be called once in the program
    */
@@ -270,17 +213,8 @@ server (void)
   gnutls_psk_set_server_credentials_function (server_pskcred, pskfunc);
   gnutls_psk_set_server_dh_params (server_pskcred, dh_params);
 
-  client_len = sizeof (sa_cli);
-
   session = initialize_tls_session ();
 
-  sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
-
-  if (debug)
-    success ("server: connection from %s, port %d\n",
-             inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-                        sizeof (topbuf)), ntohs (sa_cli.sin_port));
-
   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
   ret = gnutls_handshake (session);
   if (ret < 0)
@@ -296,7 +230,6 @@ server (void)
   /* see the Getting peer's information example */
   /* print_info(session); */
 
-  i = 0;
   for (;;)
     {
       memset (buffer, 0, MAX_BUF + 1);
@@ -327,8 +260,6 @@ server (void)
   close (sd);
   gnutls_deinit (session);
 
-  close (listen_sd);
-
   gnutls_psk_free_server_credentials (server_pskcred);
 
   gnutls_dh_params_deinit (dh_params);
@@ -343,10 +274,15 @@ void
 doit (void)
 {
   pid_t child;
+  int sockets[2];
 
-  server_start ();
-  if (error_count)
-    return;
+  err = socketpair (AF_UNIX, SOCK_STREAM, 0, sockets);
+  if (err == -1)
+    {
+      perror ("socketpair");
+      fail ("socketpair failed\n");
+      return;
+    }
 
   child = fork ();
   if (child < 0)
@@ -360,9 +296,9 @@ doit (void)
     {
       int status;
       /* parent */
-      server ();
+      server (sockets[0]);
       wait (&status);
     }
   else
-    client ();
+    client (sockets[1]);
 }
diff --git a/tests/openpgpself.c b/tests/openpgpself.c
index e904acc..9c21117 100644
--- a/tests/openpgpself.c
+++ b/tests/openpgpself.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2004-2012 Free Software Foundation, Inc.
+ * Copyright (C) 2013 Adam Sampson <ats at offog.org>
  *
  * Author: Simon Josefsson
  *
@@ -32,9 +33,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #if !defined(_WIN32)
-#include <netinet/in.h>
 #include <sys/wait.h>
-#include <arpa/inet.h>
 #endif
 #include <unistd.h>
 #include <gnutls/gnutls.h>
@@ -44,7 +43,6 @@
 
 #include "ex-session-info.c"
 #include "ex-x509-info.c"
-#include "tcp.c"
 
 pid_t child;
 
@@ -57,6 +55,7 @@ tls_log_func (int level, const char *str)
 /* A very basic TLS client, with anonymous authentication.
  */
 
+#define SESSIONS 2
 #define MAX_BUF 1024
 #define MSG "Hello TLS"
 
@@ -132,9 +131,9 @@ const gnutls_datum_t key = { key_txt, sizeof (key_txt) };
 
 
 static void
-client (void)
+client (int sds[])
 {
-  int ret, sd, ii, j;
+  int ret, ii, j;
   gnutls_session_t session;
   char buffer[MAX_BUF + 1];
   gnutls_certificate_credentials_t xcred;
@@ -160,9 +159,9 @@ client (void)
       return;
     }
 
-  for (j = 0; j < 2; j++)
+  for (j = 0; j < SESSIONS; j++)
     {
-
+      int sd = sds[j];
 
       /* Initialize TLS session
        */
@@ -175,12 +174,6 @@ client (void)
        */
       gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
 
-      /* connect to the peer
-       */
-      if (debug)
-        success ("Connecting...\n");
-      sd = tcp_connect ();
-
       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
 
       /* Perform the TLS handshake
@@ -234,8 +227,7 @@ client (void)
 
       gnutls_bye (session, GNUTLS_SHUT_RDWR);
 
-
-      tcp_close (sd);
+      close (sd);
 
       gnutls_deinit (session);
 
@@ -251,9 +243,7 @@ end:
 /* This is a sample TLS 1.0 echo server, using X.509 authentication.
  */
 
-#define SA struct sockaddr
 #define MAX_BUF 1024
-#define PORT 5556               /* listen to 5556 port */
 #define DH_BITS 1024
 
 /* These are global */
@@ -297,11 +287,7 @@ generate_dh_params (void)
   return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
 }
 
-int err, listen_sd, i;
-int sd, ret;
-struct sockaddr_in sa_serv;
-struct sockaddr_in sa_cli;
-socklen_t client_len;
+int err, ret;
 char topbuf[512];
 gnutls_session_t session;
 char buffer[MAX_BUF + 1];
@@ -488,48 +474,7 @@ const gnutls_datum_t key2048 = { key2048_txt, sizeof (key2048_txt) };
 
 
 static void
-server_start (void)
-{
-  /* Socket operations
-   */
-  listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (listen_sd == -1)
-    {
-      perror ("socket");
-      fail ("server: socket failed\n");
-      return;
-    }
-
-  memset (&sa_serv, '\0', sizeof (sa_serv));
-  sa_serv.sin_family = AF_INET;
-  sa_serv.sin_addr.s_addr = INADDR_ANY;
-  sa_serv.sin_port = htons (PORT);      /* Server Port number */
-
-  setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
-              sizeof (int));
-
-  err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
-  if (err == -1)
-    {
-      perror ("bind");
-      fail ("server: bind failed\n");
-      return;
-    }
-
-  err = listen (listen_sd, 1024);
-  if (err == -1)
-    {
-      perror ("listen");
-      fail ("server: listen failed\n");
-      return;
-    }
-
-  if (debug)
-    success ("server: ready. Listening to port '%d'.\n", PORT);
-}
-
-static void
-server (void)
+server (int sds[])
 {
   int j;
   /* this must be called once in the program
@@ -545,25 +490,25 @@ server (void)
 
   generate_dh_params ();
 
-  client_len = sizeof (sa_cli);
-
-  for (j = 0; j < 2; j++)
+  for (j = 0; j < SESSIONS; j++)
     {
-     if (j==0)
-       {
-         gnutls_certificate_allocate_credentials (&pgp_cred);
-         ret = gnutls_certificate_set_openpgp_key_mem2 (pgp_cred, &server_crt,
-                                                 &server_key, "auto",
-                                                 GNUTLS_OPENPGP_FMT_BASE64);
-      }
-     else
-       {
-         gnutls_certificate_free_credentials (pgp_cred);
-         gnutls_certificate_allocate_credentials (&pgp_cred);
-         ret =
-           gnutls_certificate_set_openpgp_key_mem2 (pgp_cred, &cert2048, &key2048,
-             "auto", GNUTLS_OPENPGP_FMT_BASE64);
-       }
+      int sd = sds[j];
+
+      if (j == 0)
+        {
+          gnutls_certificate_allocate_credentials (&pgp_cred);
+          ret = gnutls_certificate_set_openpgp_key_mem2 (pgp_cred, &server_crt,
+                                                         &server_key, "auto",
+                                                         GNUTLS_OPENPGP_FMT_BASE64);
+        }
+      else
+        {
+          gnutls_certificate_free_credentials (pgp_cred);
+          gnutls_certificate_allocate_credentials (&pgp_cred);
+          ret =
+            gnutls_certificate_set_openpgp_key_mem2 (pgp_cred, &cert2048, &key2048,
+              "auto", GNUTLS_OPENPGP_FMT_BASE64);
+        }
 
       if (ret < 0)
         {
@@ -575,13 +520,6 @@ server (void)
 
       session = initialize_tls_session ();
 
-      sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
-
-      if (debug)
-        success ("server: connection from %s, port %d\n",
-                 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-                            sizeof (topbuf)), ntohs (sa_cli.sin_port));
-
       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
       ret = gnutls_handshake (session);
       if (ret < 0)
@@ -604,7 +542,6 @@ server (void)
       if (debug)
         print_info (session);
 
-      i = 0;
       for (;;)
         {
           memset (buffer, 0, MAX_BUF + 1);
@@ -637,8 +574,6 @@ server (void)
     }
 
 end:
-  close (listen_sd);
-
   gnutls_certificate_free_credentials (pgp_cred);
 
   gnutls_dh_params_deinit (dh_params);
@@ -649,13 +584,27 @@ end:
     success ("server: finished\n");
 }
 
-
 void
 doit (void)
 {
-  server_start ();
-  if (error_count)
-    return;
+  int client_sds[SESSIONS], server_sds[SESSIONS];
+  int i;
+
+  for (i = 0; i < SESSIONS; i++)
+    {
+      int sockets[2];
+
+      err = socketpair (AF_UNIX, SOCK_STREAM, 0, sockets);
+      if (err == -1)
+        {
+          perror ("socketpair");
+          fail ("socketpair failed\n");
+          return;
+        }
+
+      server_sds[i] = sockets[0];
+      client_sds[i] = sockets[1];
+    }
 
   child = fork ();
   if (child < 0)
@@ -669,9 +618,9 @@ doit (void)
     {
       int status;
       /* parent */
-      server ();
+      server (server_sds);
       wait (&status);
     }
   else
-    client ();
+    client (client_sds);
 }
diff --git a/tests/pskself.c b/tests/pskself.c
index e04914e..96ad965 100644
--- a/tests/pskself.c
+++ b/tests/pskself.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2004-2012 Free Software Foundation, Inc.
+ * Copyright (C) 2013 Adam Sampson <ats at offog.org>
  *
  * Author: Simon Josefsson
  *
@@ -33,14 +34,10 @@
 #include <sys/socket.h>
 #if !defined(_WIN32)
 #include <sys/wait.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 #endif
 #include <unistd.h>
 #include <gnutls/gnutls.h>
 
-#include "tcp.c"
-
 #include "utils.h"
 
 /* A very basic TLS client, with PSK authentication.
@@ -58,9 +55,9 @@ tls_log_func (int level, const char *str)
 #define MSG "Hello TLS"
 
 static void
-client (void)
+client (int sd)
 {
-  int ret, sd, ii;
+  int ret, ii;
   gnutls_session_t session;
   char buffer[MAX_BUF + 1];
   gnutls_psk_client_credentials_t pskcred;
@@ -89,10 +86,6 @@ client (void)
    */
   gnutls_credentials_set (session, GNUTLS_CRD_PSK, pskcred);
 
-  /* connect to the peer
-   */
-  sd = tcp_connect ();
-
   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
 
   /* Perform the TLS handshake
@@ -140,7 +133,7 @@ client (void)
 
 end:
 
-  tcp_close (sd);
+  close (sd);
 
   gnutls_deinit (session);
 
@@ -152,9 +145,7 @@ end:
 /* This is a sample TLS 1.0 echo server, for PSK authentication.
  */
 
-#define SA struct sockaddr
 #define MAX_BUF 1024
-#define PORT 5556               /* listen to 5556 port */
 
 /* These are global */
 gnutls_psk_server_credentials_t server_pskcred;
@@ -190,62 +181,14 @@ pskfunc (gnutls_session_t session, const char *username, gnutls_datum_t * key)
   return 0;
 }
 
-int err, listen_sd, i;
-int sd, ret;
-struct sockaddr_in sa_serv;
-struct sockaddr_in sa_cli;
-socklen_t client_len;
+int err, ret;
 char topbuf[512];
 gnutls_session_t session;
 char buffer[MAX_BUF + 1];
 int optval = 1;
 
 static void
-server_start (void)
-{
-  if (debug)
-    success ("Launched...\n");
-
-  /* Socket operations
-   */
-  listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (listen_sd == -1)
-    {
-      perror ("socket");
-      fail ("server: socket failed\n");
-      return;
-    }
-
-  memset (&sa_serv, '\0', sizeof (sa_serv));
-  sa_serv.sin_family = AF_INET;
-  sa_serv.sin_addr.s_addr = INADDR_ANY;
-  sa_serv.sin_port = htons (PORT);      /* Server Port number */
-
-  setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
-              sizeof (int));
-
-  err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
-  if (err == -1)
-    {
-      perror ("bind");
-      fail ("server: bind failed\n");
-      return;
-    }
-
-  err = listen (listen_sd, 1024);
-  if (err == -1)
-    {
-      perror ("listen");
-      fail ("server: listen failed\n");
-      return;
-    }
-
-  if (debug)
-    success ("server: ready. Listening to port '%d'.\n", PORT);
-}
-
-static void
-server (void)
+server (int sd)
 {
   /* this must be called once in the program
    */
@@ -259,17 +202,8 @@ server (void)
   gnutls_psk_allocate_server_credentials (&server_pskcred);
   gnutls_psk_set_server_credentials_function (server_pskcred, pskfunc);
 
-  client_len = sizeof (sa_cli);
-
   session = initialize_tls_session ();
 
-  sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
-
-  if (debug)
-    success ("server: connection from %s, port %d\n",
-             inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-                        sizeof (topbuf)), ntohs (sa_cli.sin_port));
-
   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
   ret = gnutls_handshake (session);
   if (ret < 0)
@@ -285,7 +219,6 @@ server (void)
   /* see the Getting peer's information example */
   /* print_info(session); */
 
-  i = 0;
   for (;;)
     {
       memset (buffer, 0, MAX_BUF + 1);
@@ -316,8 +249,6 @@ server (void)
   close (sd);
   gnutls_deinit (session);
 
-  close (listen_sd);
-
   gnutls_psk_free_server_credentials (server_pskcred);
 
   gnutls_global_deinit ();
@@ -330,10 +261,15 @@ void
 doit (void)
 {
   pid_t child;
+  int sockets[2];
 
-  server_start ();
-  if (error_count)
-    return;
+  err = socketpair (AF_UNIX, SOCK_STREAM, 0, sockets);
+  if (err == -1)
+    {
+      perror ("socketpair");
+      fail ("socketpair failed\n");
+      return;
+    }
 
   child = fork ();
   if (child < 0)
@@ -347,9 +283,9 @@ doit (void)
     {
       int status;
       /* parent */
-      server ();
+      server (sockets[0]);
       wait (&status);
     }
   else
-    client ();
+    client (sockets[1]);
 }
diff --git a/tests/resume-dtls.c b/tests/resume-dtls.c
index 89b88d0..aa28b74 100644
--- a/tests/resume-dtls.c
+++ b/tests/resume-dtls.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2004-2012 Free Software Foundation, Inc.
+ * Copyright (C) 2013 Adam Sampson <ats at offog.org>
  *
  * Author: Nikos Mavrogiannopoulos
  *
@@ -33,8 +34,6 @@
 #include <sys/socket.h>
 #if !defined(_WIN32)
 #include <sys/wait.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 #endif
 #include <unistd.h>
 #include <gnutls/gnutls.h>
@@ -48,8 +47,6 @@ int main()
 
 #else
 
-#include "tcp.c"
-
 #include "utils.h"
 
 static void wrap_db_init (void);
@@ -82,6 +79,7 @@ struct params_res resume_tests[] = {
 /* A very basic TLS client, with anonymous authentication.
  */
 
+#define SESSIONS 2
 #define MAX_BUF 5*1024
 #define MSG "Hello TLS"
 
@@ -92,9 +90,9 @@ tls_log_func (int level, const char *str)
 }
 
 static void
-client (struct params_res *params)
+client (int sds[], struct params_res *params)
 {
-  int ret, sd, ii;
+  int ret, ii;
   gnutls_session_t session;
   char buffer[MAX_BUF + 1];
   gnutls_anon_client_credentials_t anoncred;
@@ -114,11 +112,9 @@ client (struct params_res *params)
 
   gnutls_anon_allocate_client_credentials (&anoncred);
 
-  for (t = 0; t < 2; t++)
-    {                           /* connect 2 times to the server */
-      /* connect to the peer
-       */
-      sd = tcp_connect ();
+  for (t = 0; t < SESSIONS; t++)
+    {
+      int sd = sds[t];
 
       /* Initialize TLS session
        */
@@ -223,8 +219,7 @@ client (struct params_res *params)
 
       gnutls_bye (session, GNUTLS_SHUT_RDWR);
 
-
-      tcp_close (sd);
+      close (sd);
 
       gnutls_deinit (session);
     }
@@ -236,8 +231,6 @@ end:
 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
  */
 
-#define SA struct sockaddr
-#define PORT 5556               /* listen to 5556 port */
 #define DH_BITS 1024
 
 /* These are global */
@@ -289,58 +282,13 @@ generate_dh_params (void)
   return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
 }
 
-int err, listen_sd, i;
-int sd, ret;
-struct sockaddr_in sa_serv;
-struct sockaddr_in sa_cli;
-socklen_t client_len;
+int err, ret;
 char topbuf[512];
 gnutls_session_t session;
 char buffer[MAX_BUF + 1];
 int optval = 1;
 
 static void
-global_start (void)
-{
-  /* Socket operations
-   */
-  listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (listen_sd == -1)
-    {
-      perror ("socket");
-      fail ("server: socket failed\n");
-      return;
-    }
-
-  memset (&sa_serv, '\0', sizeof (sa_serv));
-  sa_serv.sin_family = AF_INET;
-  sa_serv.sin_addr.s_addr = INADDR_ANY;
-  sa_serv.sin_port = htons (PORT);      /* Server Port number */
-
-  setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
-              sizeof (int));
-
-  err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
-  if (err == -1)
-    {
-      perror ("bind");
-      fail ("server: bind failed\n");
-      return;
-    }
-
-  err = listen (listen_sd, 1024);
-  if (err == -1)
-    {
-      perror ("listen");
-      fail ("server: listen failed\n");
-      return;
-    }
-
-  if (debug)
-    success ("server: ready. Listening to port '%d'.\n", PORT);
-}
-
-static void
 global_stop (void)
 {
   if (debug)
@@ -351,12 +299,10 @@ global_stop (void)
   gnutls_dh_params_deinit (dh_params);
 
   gnutls_global_deinit ();
-
-  shutdown (listen_sd, SHUT_RDWR);
 }
 
 static void
-server (struct params_res *params)
+server (int sds[], struct params_res *params)
 {
   size_t t;
 
@@ -386,19 +332,12 @@ server (struct params_res *params)
   if (params->enable_session_ticket_server)
     gnutls_session_ticket_key_generate (&session_ticket_key);
 
-  for (t = 0; t < 2; t++)
+  for (t = 0; t < SESSIONS; t++)
     {
-      client_len = sizeof (sa_cli);
+      int sd = sds[t];
 
       session = initialize_tls_session (params);
 
-      sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
-
-      if (debug)
-        success ("server: connection from %s, port %d\n",
-                 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-                            sizeof (topbuf)), ntohs (sa_cli.sin_port));
-
       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
       ret = gnutls_handshake (session);
       if (ret < 0)
@@ -415,7 +354,6 @@ server (struct params_res *params)
       /* see the Getting peer's information example */
       /* print_info(session); */
 
-      i = 0;
       for (;;)
         {
           memset (buffer, 0, MAX_BUF + 1);
@@ -448,8 +386,6 @@ server (struct params_res *params)
       gnutls_deinit (session);
     }
 
-  close (listen_sd);
-
   if (params->enable_db)
     {
       wrap_db_deinit ();
@@ -469,11 +405,26 @@ doit (void)
 
   for (i = 0; resume_tests[i].desc; i++)
     {
+      int client_sds[SESSIONS], server_sds[SESSIONS];
+      int j;
+
       printf ("%s\n", resume_tests[i].desc);
 
-      global_start ();
-      if (error_count)
-        return;
+      for (j = 0; j < SESSIONS; j++)
+        {
+          int sockets[2];
+
+          err = socketpair (AF_UNIX, SOCK_STREAM, 0, sockets);
+          if (err == -1)
+            {
+              perror ("socketpair");
+              fail ("socketpair failed\n");
+              return;
+            }
+
+          server_sds[j] = sockets[0];
+          client_sds[j] = sockets[1];
+        }
 
       child = fork ();
       if (child < 0)
@@ -487,7 +438,7 @@ doit (void)
         {
           int status;
           /* parent */
-          server (&resume_tests[i]);
+          server (server_sds, &resume_tests[i]);
           wait (&status);
           if (WEXITSTATUS(status) > 0)
             error_count++;
@@ -495,7 +446,7 @@ doit (void)
         }
       else
         {
-          client (&resume_tests[i]);
+          client (client_sds, &resume_tests[i]);
           gnutls_global_deinit ();
           if (error_count)
             exit(1);
diff --git a/tests/resume.c b/tests/resume.c
index a87e80f..b0d137b 100644
--- a/tests/resume.c
+++ b/tests/resume.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2004-2012 Free Software Foundation, Inc.
+ * Copyright (C) 2013 Adam Sampson <ats at offog.org>
  *
  * Author: Simon Josefsson
  *
@@ -33,14 +34,10 @@
 #include <sys/socket.h>
 #if !defined(_WIN32)
 #include <sys/wait.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 #endif
 #include <unistd.h>
 #include <gnutls/gnutls.h>
 
-#include "tcp.c"
-
 #include "utils.h"
 
 static void wrap_db_init (void);
@@ -73,6 +70,7 @@ struct params_res resume_tests[] = {
 /* A very basic TLS client, with anonymous authentication.
  */
 
+#define SESSIONS 2
 #define MAX_BUF 5*1024
 #define MSG "Hello TLS"
 
@@ -83,9 +81,9 @@ tls_log_func (int level, const char *str)
 }
 
 static void
-client (struct params_res *params)
+client (int sds[], struct params_res *params)
 {
-  int ret, sd, ii;
+  int ret, ii;
   gnutls_session_t session;
   char buffer[MAX_BUF + 1];
   gnutls_anon_client_credentials_t anoncred;
@@ -105,11 +103,9 @@ client (struct params_res *params)
 
   gnutls_anon_allocate_client_credentials (&anoncred);
 
-  for (t = 0; t < 2; t++)
-    {                           /* connect 2 times to the server */
-      /* connect to the peer
-       */
-      sd = tcp_connect ();
+  for (t = 0; t < SESSIONS; t++)
+    {
+      int sd = sds[t];
 
       /* Initialize TLS session
        */
@@ -214,8 +210,7 @@ client (struct params_res *params)
 
       gnutls_bye (session, GNUTLS_SHUT_RDWR);
 
-
-      tcp_close (sd);
+      close (sd);
 
       gnutls_deinit (session);
     }
@@ -227,8 +222,6 @@ end:
 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
  */
 
-#define SA struct sockaddr
-#define PORT 5556               /* listen to 5556 port */
 #define DH_BITS 1024
 
 /* These are global */
@@ -280,58 +273,13 @@ generate_dh_params (void)
   return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
 }
 
-int err, listen_sd, i;
-int sd, ret;
-struct sockaddr_in sa_serv;
-struct sockaddr_in sa_cli;
-socklen_t client_len;
+int err, ret;
 char topbuf[512];
 gnutls_session_t session;
 char buffer[MAX_BUF + 1];
 int optval = 1;
 
 static void
-global_start (void)
-{
-  /* Socket operations
-   */
-  listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (listen_sd == -1)
-    {
-      perror ("socket");
-      fail ("server: socket failed\n");
-      return;
-    }
-
-  memset (&sa_serv, '\0', sizeof (sa_serv));
-  sa_serv.sin_family = AF_INET;
-  sa_serv.sin_addr.s_addr = INADDR_ANY;
-  sa_serv.sin_port = htons (PORT);      /* Server Port number */
-
-  setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
-              sizeof (int));
-
-  err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
-  if (err == -1)
-    {
-      perror ("bind");
-      fail ("server: bind failed\n");
-      return;
-    }
-
-  err = listen (listen_sd, 1024);
-  if (err == -1)
-    {
-      perror ("listen");
-      fail ("server: listen failed\n");
-      return;
-    }
-
-  if (debug)
-    success ("server: ready. Listening to port '%d'.\n", PORT);
-}
-
-static void
 global_stop (void)
 {
   if (debug)
@@ -342,12 +290,10 @@ global_stop (void)
   gnutls_dh_params_deinit (dh_params);
 
   gnutls_global_deinit ();
-
-  shutdown (listen_sd, SHUT_RDWR);
 }
 
 static void
-server (struct params_res *params)
+server (int sds[], struct params_res *params)
 {
   size_t t;
 
@@ -377,19 +323,12 @@ server (struct params_res *params)
   if (params->enable_session_ticket_server)
     gnutls_session_ticket_key_generate (&session_ticket_key);
 
-  for (t = 0; t < 2; t++)
+  for (t = 0; t < SESSIONS; t++)
     {
-      client_len = sizeof (sa_cli);
+      int sd = sds[t];
 
       session = initialize_tls_session (params);
 
-      sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
-
-      if (debug)
-        success ("server: connection from %s, port %d\n",
-                 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-                            sizeof (topbuf)), ntohs (sa_cli.sin_port));
-
       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
       ret = gnutls_handshake (session);
       if (ret < 0)
@@ -406,7 +345,6 @@ server (struct params_res *params)
       /* see the Getting peer's information example */
       /* print_info(session); */
 
-      i = 0;
       for (;;)
         {
           memset (buffer, 0, MAX_BUF + 1);
@@ -439,8 +377,6 @@ server (struct params_res *params)
       gnutls_deinit (session);
     }
 
-  close (listen_sd);
-
   if (params->enable_db)
     {
       wrap_db_deinit ();
@@ -460,11 +396,26 @@ doit (void)
 
   for (i = 0; resume_tests[i].desc; i++)
     {
+      int client_sds[SESSIONS], server_sds[SESSIONS];
+      int j;
+
       printf ("%s\n", resume_tests[i].desc);
 
-      global_start ();
-      if (error_count)
-        return;
+      for (j = 0; j < SESSIONS; j++)
+        {
+          int sockets[2];
+
+          err = socketpair (AF_UNIX, SOCK_STREAM, 0, sockets);
+          if (err == -1)
+            {
+              perror ("socketpair");
+              fail ("socketpair failed\n");
+              return;
+            }
+
+          server_sds[j] = sockets[0];
+          client_sds[j] = sockets[1];
+        }
 
       child = fork ();
       if (child < 0)
@@ -478,7 +429,7 @@ doit (void)
         {
           int status;
           /* parent */
-          server (&resume_tests[i]);
+          server (server_sds, &resume_tests[i]);
           wait (&status);
           if (WEXITSTATUS(status) > 0)
             error_count++;
@@ -486,7 +437,7 @@ doit (void)
         }
       else
         {
-          client (&resume_tests[i]);
+          client (client_sds, &resume_tests[i]);
           gnutls_global_deinit ();
           if (error_count)
             exit(1);
diff --git a/tests/x509dn.c b/tests/x509dn.c
index 81402d1..af41083 100644
--- a/tests/x509dn.c
+++ b/tests/x509dn.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2004-2012 Free Software Foundation, Inc.
+ * Copyright (C) 2013 Adam Sampson <ats at offog.org>
  *
  * Author: Simon Josefsson
  *
@@ -32,9 +33,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #if !defined(_WIN32)
-# include <netinet/in.h>
-# include <sys/wait.h>
-# include <arpa/inet.h>
+#include <sys/wait.h>
 #endif
 #include <unistd.h>
 #include <gnutls/gnutls.h>
@@ -44,7 +43,6 @@
 
 #include "ex-session-info.c"
 #include "ex-x509-info.c"
-#include "tcp.c"
 
 pid_t child;
 
@@ -181,9 +179,9 @@ cert_callback (gnutls_session_t session,
 
 
 static void
-client (void)
+client (int sd)
 {
-  int ret, sd, ii;
+  int ret, ii;
   gnutls_session_t session;
   char buffer[MAX_BUF + 1];
   gnutls_certificate_credentials_t xcred;
@@ -213,10 +211,6 @@ client (void)
    */
   gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
 
-  /* connect to the peer
-   */
-  sd = tcp_connect ();
-
   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
 
   /* Perform the TLS handshake
@@ -273,7 +267,7 @@ client (void)
 
 end:
 
-  tcp_close (sd);
+  close (sd);
 
   gnutls_deinit (session);
 
@@ -285,9 +279,7 @@ end:
 /* This is a sample TLS 1.0 echo server, using X.509 authentication.
  */
 
-#define SA struct sockaddr
 #define MAX_BUF 1024
-#define PORT 5556               /* listen to 5556 port */
 #define DH_BITS 1024
 
 /* These are global */
@@ -331,11 +323,7 @@ generate_dh_params (void)
   return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
 }
 
-int err, listen_sd, i;
-int sd, ret;
-struct sockaddr_in sa_serv;
-struct sockaddr_in sa_cli;
-socklen_t client_len;
+int err, ret;
 char topbuf[512];
 gnutls_session_t session;
 char buffer[MAX_BUF + 1];
@@ -384,48 +372,7 @@ const gnutls_datum_t server_key = { server_key_pem,
 };
 
 static void
-server_start (void)
-{
-  /* Socket operations
-   */
-  listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (listen_sd == -1)
-    {
-      perror ("socket");
-      fail ("server: socket failed\n");
-      return;
-    }
-
-  memset (&sa_serv, '\0', sizeof (sa_serv));
-  sa_serv.sin_family = AF_INET;
-  sa_serv.sin_addr.s_addr = INADDR_ANY;
-  sa_serv.sin_port = htons (PORT);      /* Server Port number */
-
-  setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
-              sizeof (int));
-
-  err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
-  if (err == -1)
-    {
-      perror ("bind");
-      fail ("server: bind failed\n");
-      return;
-    }
-
-  err = listen (listen_sd, 1024);
-  if (err == -1)
-    {
-      perror ("listen");
-      fail ("server: listen failed\n");
-      return;
-    }
-
-  if (debug)
-    success ("server: ready. Listening to port '%d'.\n", PORT);
-}
-
-static void
-server (void)
+server (int sd)
 {
   /* this must be called once in the program
    */
@@ -448,17 +395,8 @@ server (void)
 
   gnutls_certificate_set_dh_params (x509_cred, dh_params);
 
-  client_len = sizeof (sa_cli);
-
   session = initialize_tls_session ();
 
-  sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
-
-  if (debug)
-    success ("server: connection from %s, port %d\n",
-             inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-                        sizeof (topbuf)), ntohs (sa_cli.sin_port));
-
   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
   ret = gnutls_handshake (session);
   if (ret < 0)
@@ -480,7 +418,6 @@ server (void)
   if (debug)
     print_info (session);
 
-  i = 0;
   for (;;)
     {
       memset (buffer, 0, MAX_BUF + 1);
@@ -511,8 +448,6 @@ server (void)
   close (sd);
   gnutls_deinit (session);
 
-  close (listen_sd);
-
   gnutls_certificate_free_credentials (x509_cred);
 
   gnutls_dh_params_deinit (dh_params);
@@ -527,9 +462,15 @@ server (void)
 void
 doit (void)
 {
-  server_start ();
-  if (error_count)
-    return;
+  int sockets[2];
+
+  err = socketpair (AF_UNIX, SOCK_STREAM, 0, sockets);
+  if (err == -1)
+    {
+      perror ("socketpair");
+      fail ("socketpair failed\n");
+      return;
+    }
 
   child = fork ();
   if (child < 0)
@@ -543,7 +484,7 @@ doit (void)
     {
       int status;
       /* parent */
-      server ();
+      server (sockets[0]);
       wait (&status);
 
 #if defined WIFEXITED && defined WEXITSTATUS
@@ -564,5 +505,5 @@ doit (void)
 
     }
   else
-    client ();
+    client (sockets[1]);
 }
diff --git a/tests/x509self.c b/tests/x509self.c
index 14b6f3b..e0b55f0 100644
--- a/tests/x509self.c
+++ b/tests/x509self.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2004-2012 Free Software Foundation, Inc.
+ * Copyright (C) 2013 Adam Sampson <ats at offog.org>
  *
  * Author: Simon Josefsson
  *
@@ -30,11 +31,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
-# include <sys/socket.h>
+#include <sys/socket.h>
 #if !defined(_WIN32)
-# include <netinet/in.h>
-# include <sys/wait.h>
-# include <arpa/inet.h>
+#include <sys/wait.h>
 #endif
 #include <unistd.h>
 #include <gnutls/gnutls.h>
@@ -43,7 +42,6 @@
 
 #include "ex-session-info.c"
 #include "ex-x509-info.c"
-#include "tcp.c"
 
 pid_t child;
 
@@ -110,9 +108,9 @@ static unsigned char key_pem[] =
 const gnutls_datum_t key = { key_pem, sizeof (key_pem) };
 
 static void
-client (void)
+client (int sd)
 {
-  int ret, sd, ii;
+  int ret, ii;
   gnutls_session_t session;
   char buffer[MAX_BUF + 1];
   gnutls_certificate_credentials_t xcred;
@@ -142,10 +140,6 @@ client (void)
    */
   gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
 
-  /* connect to the peer
-   */
-  sd = tcp_connect ();
-
   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
 
   /* Perform the TLS handshake
@@ -234,7 +228,7 @@ client (void)
 
 end:
 
-  tcp_close (sd);
+  close (sd);
 
   gnutls_deinit (session);
 
@@ -246,9 +240,7 @@ end:
 /* This is a sample TLS 1.0 echo server, using X.509 authentication.
  */
 
-#define SA struct sockaddr
 #define MAX_BUF 1024
-#define PORT 5556               /* listen to 5556 port */
 #define DH_BITS 1024
 
 /* These are global */
@@ -293,11 +285,7 @@ generate_dh_params (void)
   return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
 }
 
-int err, listen_sd, i;
-int sd, ret;
-struct sockaddr_in sa_serv;
-struct sockaddr_in sa_cli;
-socklen_t client_len;
+int err, ret;
 char topbuf[512];
 gnutls_session_t session;
 char buffer[MAX_BUF + 1];
@@ -346,48 +334,7 @@ const gnutls_datum_t server_key = { server_key_pem,
 };
 
 static void
-server_start (void)
-{
-  /* Socket operations
-   */
-  listen_sd = socket (AF_INET, SOCK_STREAM, 0);
-  if (listen_sd == -1)
-    {
-      perror ("socket");
-      fail ("server: socket failed\n");
-      return;
-    }
-
-  memset (&sa_serv, '\0', sizeof (sa_serv));
-  sa_serv.sin_family = AF_INET;
-  sa_serv.sin_addr.s_addr = INADDR_ANY;
-  sa_serv.sin_port = htons (PORT);      /* Server Port number */
-
-  setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
-              sizeof (int));
-
-  err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
-  if (err == -1)
-    {
-      perror ("bind");
-      fail ("server: bind failed\n");
-      return;
-    }
-
-  err = listen (listen_sd, 1024);
-  if (err == -1)
-    {
-      perror ("listen");
-      fail ("server: listen failed\n");
-      return;
-    }
-
-  if (debug)
-    success ("server: ready. Listening to port '%d'.\n", PORT);
-}
-
-static void
-server (void)
+server (int sd)
 {
   /* this must be called once in the program
    */
@@ -410,17 +357,8 @@ server (void)
 
   gnutls_certificate_set_dh_params (x509_cred, dh_params);
 
-  client_len = sizeof (sa_cli);
-
   session = initialize_tls_session ();
 
-  sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
-
-  if (debug)
-    success ("server: connection from %s, port %d\n",
-             inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-                        sizeof (topbuf)), ntohs (sa_cli.sin_port));
-
   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
   ret = gnutls_handshake (session);
   if (ret < 0)
@@ -442,7 +380,6 @@ server (void)
   if (debug)
     print_info (session);
 
-  i = 0;
   for (;;)
     {
       memset (buffer, 0, MAX_BUF + 1);
@@ -498,8 +435,6 @@ server (void)
   close (sd);
   gnutls_deinit (session);
 
-  close (listen_sd);
-
   gnutls_certificate_free_credentials (x509_cred);
 
   gnutls_dh_params_deinit (dh_params);
@@ -514,10 +449,15 @@ server (void)
 void
 doit (void)
 {
-  /* parent */
-  server_start ();
-  if (error_count)
-    return;
+  int sockets[2];
+
+  err = socketpair (AF_UNIX, SOCK_STREAM, 0, sockets);
+  if (err == -1)
+    {
+      perror ("socketpair");
+      fail ("socketpair failed\n");
+      return;
+    }
 
   child = fork ();
   if (child < 0)
@@ -531,9 +471,9 @@ doit (void)
     {
       int status;
 
-      server ();
+      server (sockets[0]);
       wait (&status);
     }
   else
-    client ();
+    client (sockets[1]);
 }
-- 
1.8.3

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: not available
URL: </pipermail/attachments/20130716/d3011079/attachment-0001.sig>


More information about the Gnutls-devel mailing list