From: Nikos Mavrogiannopoulos Date: Sat, 27 Jun 2015 05:57:21 +0000 (+0200) Subject: tests: added a test for the fork detection interface X-Git-Tag: gnutls_3_4_3~31 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a8bf8d26e7bca9613e6dea041f2a0efc6fa7401e;p=thirdparty%2Fgnutls.git tests: added a test for the fork detection interface --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 348bd10c95..d365b7f796 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 0000000000..2e9de45453 --- /dev/null +++ b/tests/atfork.c @@ -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 . + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#if !defined(_WIN32) +#include +#endif + +#include "utils.h" +#include +#include + +#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