lib/util: add a test for tfork()
authorRalph Boehme <slow@samba.org>
Tue, 11 Apr 2017 15:32:01 +0000 (17:32 +0200)
committerRalph Boehme <slow@samba.org>
Thu, 20 Apr 2017 14:53:16 +0000 (16:53 +0200)
Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/tests/tfork.c [new file with mode: 0644]
source4/torture/local/local.c
source4/torture/local/wscript_build

diff --git a/lib/util/tests/tfork.c b/lib/util/tests/tfork.c
new file mode 100644 (file)
index 0000000..7963aaf
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Tests for tfork
+ *
+ * Copyright Ralph Boehme <slow@samba.org> 2017
+ *
+ * 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
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "replace.h"
+#include <talloc.h>
+#include "system/filesys.h"
+#include "libcli/util/ntstatus.h"
+#include "torture/torture.h"
+#include "lib/util/data_blob.h"
+#include "torture/local/proto.h"
+#include "lib/util/tfork.h"
+#include "lib/util/samba_util.h"
+#include "lib/util/sys_rw.h"
+
+static bool test_tfork_simple(struct torture_context *tctx)
+{
+       pid_t pid;
+       pid_t parent = getpid();
+       pid_t parent_arg;
+
+       pid = tfork(NULL, &parent_arg);
+       if (pid == 0) {
+               torture_comment(tctx, "my parent pid is %d\n", parent);
+               torture_assert(tctx, parent == parent_arg, "tfork failed\n");
+               _exit(0);
+       }
+       if (pid == -1) {
+               torture_fail(tctx, "tfork failed\n");
+               return false;
+       }
+
+       return true;
+}
+
+static bool test_tfork_status(struct torture_context *tctx)
+{
+       pid_t child;
+       int status;
+       ssize_t nread;
+       int status_fd = -1;
+       bool ok = true;
+
+       child = tfork(&status_fd, NULL);
+       if (child == 0) {
+               _exit(123);
+       }
+       if (child == -1) {
+               torture_fail(tctx, "tfork failed\n");
+               return false;
+       }
+
+       nread = sys_read(status_fd, &status, sizeof(status));
+       if (nread != sizeof(status)) {
+               torture_fail(tctx, "sys_read failed\n");
+       }
+
+       torture_assert_goto(tctx, WIFEXITED(status) == true, ok, done,
+                           "tfork failed\n");
+       torture_assert_goto(tctx, WEXITSTATUS(status) == 123, ok, done,
+                           "tfork failed\n");
+
+       torture_comment(tctx, "exit status [%d]\n", WEXITSTATUS(status));
+
+done:
+       if (status_fd != -1) {
+               close(status_fd);
+       }
+
+       return ok;
+}
+
+struct torture_suite *torture_local_tfork(TALLOC_CTX *mem_ctx)
+{
+       struct torture_suite *suite =
+               torture_suite_create(mem_ctx, "tfork");
+
+       torture_suite_add_simple_test(suite,
+                                     "tfork_simple",
+                                     test_tfork_simple);
+
+       torture_suite_add_simple_test(suite,
+                                     "tfork_status",
+                                     test_tfork_status);
+
+       return suite;
+}
index 89066c5f52fc4ca514475d60a828962d03524882..353cc270fa4a37bcae7dd3847f6afeaa828a1756 100644 (file)
@@ -75,6 +75,7 @@
        torture_local_nss,
        torture_local_fsrvp,
        torture_local_util_str_escape,
+       torture_local_tfork,
        NULL
 };
 
index 2f1a7c8415e3558ed59393fe80acde88e281c690..6cbb14d86e48d40981cc9e6a848ca592a3a677eb 100644 (file)
@@ -21,6 +21,7 @@ TORTURE_LOCAL_SOURCE = '''../../../lib/util/charset/tests/iconv.c
        ../../../lib/util/tests/strv_util.c
        ../../../lib/util/tests/util.c
        ../../../lib/util/tests/util_str_escape.c
+       ../../../lib/util/tests/tfork.c
        verif_trailer.c
        nss_tests.c
        fsrvp_state.c'''