diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 37 | ||||
-rw-r--r-- | tests/common.h | 149 | ||||
-rw-r--r-- | tests/fdpassing.c | 213 |
3 files changed, 399 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..ef2510e --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,37 @@ +# Makefile for Assuan regression tests +# Copyright (C) 2006 Free Software Foundation, Inc. +# +# This file is part of Assuan. +# +# Assuan is free software; you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation; either version 2.1 of +# the License, or (at your option) any later version. +# +# Assuan is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA + +## Process this file with automake to produce Makefile.in + +TESTS_ENVIRONMENT = + +EXTRA_DIST = + +BUILT_SOURCES = +CLEANFILES = + +TESTS = fdpassing + +AM_CFLAGS = # $(GPG_ERROR_CFLAGS) + +noinst_HEADERS = common.h +noinst_PROGRAMS = $(TESTS) +LDADD = ../src/libassuan.a # $(GPG_ERROR_LIBS) + diff --git a/tests/common.h b/tests/common.h new file mode 100644 index 0000000..180dc47 --- /dev/null +++ b/tests/common.h @@ -0,0 +1,149 @@ +/* common.h - Common functions for the tests. + * Copyright (C) 2006 Free Software Foundation, Inc. + * + * This file is part of Assuan. + * + * Assuan is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * Assuan is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ + +#include <stdarg.h> + + +static const char *log_prefix; +static int errorcount; +static int verbose; +static int debug; + + +void * +xmalloc (size_t n) +{ + char *p = malloc (n); + if (!p) + { + fprintf (stderr, "out of core\n"); + exit (1); + } + return p; +} + +void * +xcalloc (size_t n, size_t m) +{ + char *p = calloc (n, m); + if (!p) + { + fprintf (stderr, "out of core\n"); + exit (1); + } + return p; +} + +void +xfree (void *a) +{ + if (a) + free (a); +} + + +void +log_set_prefix (const char *s) +{ + log_prefix = strrchr (s, '/'); + if (log_prefix) + log_prefix++; + else + log_prefix = s; +} + + +void +log_info (const char *format, ...) +{ + va_list arg_ptr ; + + if (!verbose) + return; + + va_start (arg_ptr, format) ; + if (log_prefix) + fprintf (stderr, "%s[%u]: ", log_prefix, (unsigned int)getpid ()); + vfprintf (stderr, format, arg_ptr ); + va_end (arg_ptr); +} + + +void +log_error (const char *format, ...) +{ + va_list arg_ptr ; + + va_start (arg_ptr, format) ; + if (log_prefix) + fprintf (stderr, "%s[%u]: ", log_prefix, (unsigned int)getpid ()); + vfprintf (stderr, format, arg_ptr ); + va_end (arg_ptr); + errorcount++; +} + + +void +log_fatal (const char *format, ...) +{ + va_list arg_ptr ; + + va_start (arg_ptr, format) ; + if (log_prefix) + fprintf (stderr, "%s[%u]: ", log_prefix, (unsigned int)getpid ()); + vfprintf (stderr, format, arg_ptr ); + va_end (arg_ptr); + exit (2); +} + + +void +log_printhex (const char *text, const void *buffer, size_t length) +{ + const unsigned char *s; + + if (log_prefix) + fprintf (stderr, "%s[%u]: ", log_prefix, (unsigned int)getpid ()); + fputs (text, stderr); + for (s=buffer; length; s++, length--) + fprintf (stderr, "%02X", *s); + putc ('\n', stderr); +} + + +/* Prepend FNAME with the srcdir environment variable's value and + return an allocated filename. */ +char * +prepend_srcdir (const char *fname) +{ + static const char *srcdir; + char *result; + + if (!srcdir && !(srcdir = getenv ("srcdir"))) + srcdir = "."; + + result = xmalloc (strlen (srcdir) + 1 + strlen (fname) + 1); + strcpy (result, srcdir); + strcat (result, "/"); + strcat (result, fname); + return result; +} + diff --git a/tests/fdpassing.c b/tests/fdpassing.c new file mode 100644 index 0000000..9317ed2 --- /dev/null +++ b/tests/fdpassing.c @@ -0,0 +1,213 @@ +/* fdpassing - Check the fiel descriptor passing. + * Copyright (C) 2006 Free Software Foundation, Inc. + * + * This file is part of Assuan. + * + * Assuan is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * Assuan is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> +#include <sys/stat.h> +#include <sys/socket.h> +#include <unistd.h> +#include <errno.h> +#include <sys/wait.h> /* Used by main driver. */ + +#include "../src/assuan.h" +#include "common.h" + + +/* + + S E R V E R + +*/ + +static int +cmd_echo (assuan_context_t ctx, char *line) +{ + log_info ("got ECHO command (%s)\n", line); + return 0; +} + +static assuan_error_t +register_commands (assuan_context_t ctx) +{ + static struct { + const char *name; + int (*handler)(assuan_context_t, char *line); + } table[] = { + { "ECHO", cmd_echo }, + { "INPUT", NULL }, + { "OUTPUT", NULL }, + { NULL } + }; + int i; + assuan_error_t rc; + + for (i=0; table[i].name; i++) + { + rc = assuan_register_command (ctx, table[i].name, table[i].handler); + if (rc) + return rc; + } + return 0; +} + + +static void +server (int fd) +{ + int rc; + assuan_context_t ctx; + + log_info ("server started on fd %d\n", fd); + + rc = assuan_init_domain_server (&ctx, fd, (pid_t)(-1)); + if (rc) + log_fatal ("assuan_init_domain_server failed: %s\n", assuan_strerror (rc)); + + rc = register_commands (ctx); + if (rc) + log_fatal ("register_commands failed: %s\n", assuan_strerror(rc)); + + assuan_set_assuan_log_prefix (log_prefix); + assuan_set_log_stream (ctx, stderr); + + for (;;) + { + rc = assuan_accept (ctx); + if (rc) + { + log_error ("assuan_accept failed: %s\n", assuan_strerror (rc)); + break; + } + + rc = assuan_process (ctx); + if (rc) + log_error ("assuan_process failed: %s\n", assuan_strerror (rc)); + } + + assuan_deinit_server (ctx); +} + + + + +/* + + C L I E N T + +*/ + + +/* Client main. If true is returned, a disconnect has not been done. */ +static int +client (int fd) +{ + int rc; + assuan_context_t ctx; + + log_info ("client started on fd %d\n", fd); + + rc = assuan_domain_connect (&ctx, fd, (pid_t)(-1)); + if (rc) + { + log_error ("assuan_domain_connect failed: %s\n", assuan_strerror (rc)); + return -1; + } + + + assuan_disconnect (ctx); + return 0; +} + + + + +/* + + M A I N + +*/ +int +main (int argc, char **argv) +{ + int last_argc = -1; + const char *srcdir = getenv ("srcdir"); + int fds[2]; + pid_t pid; + + if (!srcdir) + srcdir = "."; + + if (argc) + { + log_set_prefix (*argv); + argc--; argv++; + } + while (argc && last_argc != argc ) + { + last_argc = argc; + if (!strcmp (*argv, "--help")) + { + puts ( +"usage: ./fdpassing [options]\n" +"\n" +" Options are --verbose and --debug"); + exit (0); + } + if (!strcmp (*argv, "--verbose")) + { + verbose = 1; + argc--; argv++; + } + else if (!strcmp (*argv, "--debug")) + { + verbose = debug = 1; + argc--; argv++; + } + } + + /* Create a socketpair. */ + if ( socketpair (AF_LOCAL, SOCK_STREAM, 0, fds) ) + log_fatal ("socketpair failed: %s\n", strerror (errno)); + + /* Fork and run server and client. */ + pid = fork (); + if (pid == (pid_t)(-1)) + log_fatal ("fork failed: %s\n", strerror (errno)); + if (!pid) + { + server (fds[0]); /* The child is our server. */ + log_info ("server finished\n"); + } + else + { + if (client (fds[1])) /* The parent is the client. */ + { + log_info ("waiting for server to terminate...\n"); + waitpid (pid, NULL, 0); + } + log_info ("client finished\n"); + } + + return errorcount? 1:0; +} + |