]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
tests: added a test for the fork detection interface
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Sat, 27 Jun 2015 05:57:21 +0000 (07:57 +0200)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Sat, 27 Jun 2015 05:57:21 +0000 (07:57 +0200)
tests/Makefile.am
tests/atfork.c [new file with mode: 0644]

index 348bd10c9517af8ba9e5a5d96fb7a4a6d98022ff..d365b7f79675323baa47fa1a869b037f0905eb55 100644 (file)
@@ -88,7 +88,7 @@ ctests = mini-record-2 simple gc set_pkcs12_cred certder certuniqueid \
         strict-der mini-extension mini-supplementaldata mini-dtls0-9 \
         mini-record-retvals mini-server-name mini-etm mini-x509-cert-callback \
         sign-md5-rep keygen mini-tls-nonblock no-signal pkcs7-gen \
-        x509sign-verify2 mini-alignment oids
+        x509sign-verify2 mini-alignment oids atfork
  
 mini_dtls_pthread_LDADD = $(LDADD) -lpthread
 
@@ -139,6 +139,14 @@ mpi_CPPFLAGS = $(CPPFLAGS) \
        -I$(top_srcdir)/gl      \
        -I$(top_builddir)/gl
 
+atfork_CPPFLAGS = $(CPPFLAGS) \
+       -I$(top_srcdir)/lib/includes            \
+       -I$(top_builddir)/lib/includes          \
+       -I$(top_srcdir)/lib/minitasn1           \
+       -I$(top_srcdir)/lib                     \
+       -I$(top_srcdir)/gl      \
+       -I$(top_builddir)/gl
+
 pkcs12_s2k_CPPFLAGS = $(CPPFLAGS) \
        -I$(top_srcdir)/lib/includes            \
        -I$(top_builddir)/lib/includes          \
diff --git a/tests/atfork.c b/tests/atfork.c
new file mode 100644 (file)
index 0000000..2e9de45
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2008-2012 Free Software Foundation, Inc.
+ *
+ * Author: Nikos Mavrogiannopoulos
+ *
+ * This file is part of GnuTLS.
+ *
+ * GnuTLS is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuTLS 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GnuTLS.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#if !defined(_WIN32)
+#include <sys/wait.h>
+#endif
+
+#include "utils.h"
+#include <gnutls/gnutls.h>
+#include <gnutls/crypto.h>
+
+#if defined(_WIN32)
+void doit(void)
+{
+       exit(77);
+}
+#else
+
+#include "../lib/atfork.h"
+#include "../lib/atfork.c"
+
+void doit(void)
+{
+       pid_t pid;
+       int status;
+       unsigned forkid;
+
+       _gnutls_register_fork_handler();
+
+       forkid = _gnutls_get_forkid();
+       if (_gnutls_detect_fork(forkid) != 0) {
+               fail("Detected fork on parent!\n");
+               exit(1);
+       }
+
+       pid = fork();
+       if (pid == -1) {
+               fail("error in fork\n");
+               exit(1);
+       }
+
+       if (pid == 0) {
+               pid = fork();
+               if (pid == -1) {
+                       fail("error in fork\n");
+                       exit(1);
+               }
+
+               if (pid == 0) {
+                       if (_gnutls_detect_fork(forkid) == 0) {
+                               fail("child: didn't detect fork on grandchild!\n");
+                               exit(1);
+                       }
+                       exit(0);
+               }
+
+               if (waitpid(pid, &status, 0) < 0) {
+                       fail("error in waitpid\n");
+                       exit(2);
+               }
+
+               if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
+                       fail("Didn't detect fork on grandchild\n");
+                       exit(2);
+               }
+
+               if (_gnutls_detect_fork(forkid) == 0) {
+                       fail("child: didn't detect fork on child!\n");
+                       exit(1);
+               }
+
+               exit(0);
+       }
+
+       if (waitpid(pid, &status, 0) < 0) {
+               fail("error in waitpid\n");
+               exit(1);
+       }
+
+       if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
+               fail("Didn't detect fork on child\n");
+               exit(1);
+       }
+
+       if (_gnutls_detect_fork(forkid) != 0) {
+               fail("Detected fork on parent after fork!\n");
+               exit(1);
+       }
+
+       success("all tests ok\n");
+       return;
+}
+
+#endif