Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libc/gen/posix_spawn Add missing test



details:   https://anonhg.NetBSD.org/src/rev/53380d0d60e6
branches:  trunk
changeset: 1026263:53380d0d60e6
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Nov 15 13:59:16 2021 +0000

description:
Add missing test

diffstat:

 tests/lib/libc/gen/posix_spawn/t_spawn.c |  188 ++++++++++++++++++++----------
 1 files changed, 126 insertions(+), 62 deletions(-)

diffs (289 lines):

diff -r e8c1744867f6 -r 53380d0d60e6 tests/lib/libc/gen/posix_spawn/t_spawn.c
--- a/tests/lib/libc/gen/posix_spawn/t_spawn.c  Mon Nov 15 12:38:41 2021 +0000
+++ b/tests/lib/libc/gen/posix_spawn/t_spawn.c  Mon Nov 15 13:59:16 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: t_spawn.c,v 1.4 2021/11/07 15:46:20 christos Exp $ */
+/* $NetBSD: t_spawn.c,v 1.5 2021/11/15 13:59:16 christos Exp $ */
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_spawn.c,v 1.4 2021/11/07 15:46:20 christos Exp $");
+__RCSID("$NetBSD: t_spawn.c,v 1.5 2021/11/15 13:59:16 christos Exp $");
 
 #include <atf-c.h>
 
@@ -193,7 +193,8 @@
 check_success(const char *file, int argc, ...)
 {
        va_list ap;
-       int bytesRead, fd;
+       ssize_t bytesRead;
+       int fd;
        size_t sizeOfFile = (size_t)filesize(file);
        size_t sizeOfStr;
        char *contents;
@@ -204,7 +205,7 @@
 
        /*
         * for now only 1 variadic argument expected
-        * only from t_spawn_chdir_rel
+        * only from t_spawn_[f]chdir_rel
         */
        if (argc != 0) {
                va_start(ap, argc);
@@ -237,7 +238,7 @@
        free(contents);
 
        /* XXX not really required */
-       ATF_REQUIRE(bytesRead = sizeOfStr);
+       ATF_REQUIRE((size_t)bytesRead == sizeOfStr);
 }
 
 ATF_TC(t_spawn_chdir_abs);
@@ -492,15 +493,15 @@
 }
 
        
-ATF_TC(t_spawn_fchdir);
+ATF_TC(t_spawn_fchdir_abs);
 
-ATF_TC_HEAD(t_spawn_fchdir, tc)
+ATF_TC_HEAD(t_spawn_fchdir_abs, tc)
 {
        atf_tc_set_md_var(tc, "descr", "Test posix_spawn_fa_fchdir");
        atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
 }
 
-ATF_TC_BODY(t_spawn_fchdir, tc)
+ATF_TC_BODY(t_spawn_fchdir_abs, tc)
 {
        int error, fd, status;
        pid_t pid;
@@ -510,7 +511,7 @@
        empty_outfile(FILEPATH);
 
        fd = open(CHDIRPATH, O_RDONLY);
-       ATF_REQUIRE(fd >= 0);
+       ATF_REQUIRE(fd != -1);
 
        error = posix_spawn_file_actions_init(&fa);
        ATF_REQUIRE(error == 0);
@@ -539,68 +540,44 @@
        check_success(FILEPATH, 0);
 }
 
-ATF_TC(t_spawn_fchdir_neg_fd);
+ATF_TC(t_spawn_fchdir_rel);
 
-ATF_TC_HEAD(t_spawn_fchdir_neg_fd, tc)
+ATF_TC_HEAD(t_spawn_fchdir_rel, tc)
 {
        atf_tc_set_md_var(tc, "descr",
-           "Testing posix_spawn_file_actions_addfchdir on a negative file "
-           "descriptor");
+           "Testing posix_spawn_file_actions_addfchdir on a relative "
+           "directory");
        atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
 }
 
-ATF_TC_BODY(t_spawn_fchdir_neg_fd, tc)
+ATF_TC_BODY(t_spawn_fchdir_rel, tc)
 {
-       int error, fd;
+       int error, fd, status;
+       pid_t pid;
+       const char *relative_dir = "ch-dir";
+       const char *testdir = getcwd(NULL, 0);
+       char * const args[2] = { __UNCONST("pwd"), NULL };
+       char *chdirwd, *filepath;
        posix_spawn_file_actions_t fa;
 
-       fd = -1;
-
-       error = posix_spawn_file_actions_init(&fa);
+       error = mkdir(relative_dir, 0755);
        ATF_REQUIRE(error == 0);
 
-       error = posix_spawn_file_actions_addfchdir(&fa, fd);
-       ATF_REQUIRE(error == EBADF);
-
-       posix_spawn_file_actions_destroy(&fa);
-}
-
-ATF_TC(t_spawn_fchdir_closed);
-
-ATF_TC_HEAD(t_spawn_fchdir_closed, tc)
-{
-       atf_tc_set_md_var(tc, "descr",
-           "Testing posix_spawn_file_actions_addfchdir for a closed fd");
-       atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
-}
+       /*
+        * This is done in parts purposely.
+        * It enbales the abs path of the relative dir
+        * to be passed to 'check_success()' for comparing
+        */
+       error = asprintf(&chdirwd, "%s/%s", testdir, relative_dir);
+       ATF_CHECK(error != -1);
 
-ATF_TC_BODY(t_spawn_fchdir_closed, tc)
-{
-       int error, fd;
-       pid_t pid;
-       char * const args[2] = { __UNCONST("pwd"), NULL };
-       posix_spawnattr_t attr;
-       posix_spawn_file_actions_t fa;
+       error = asprintf(&filepath, "%s/%s", chdirwd, FILENAME);
+       ATF_CHECK(error != -1);
 
-       /*
-        * fd = open(CHDIRPATH, O_RDONLY);
-        * ATF_REQUIRE(fd >= 0);
-        * error = close(fd);
-        * ATF_REQUIRE(error == 0);
-        */
-       fd = 3;
+       empty_outfile(filepath);
 
-       error = posix_spawnattr_init(&attr);
-       ATF_CHECK(error == 0);
-       /*
-        * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
-        * will cause a "proper" return value from posix_spawn(2)
-        * instead of a (potential) success there and a 127 exit
-        * status from the child process (c.f. the non-diag variant
-        * of this test).
-        */
-       error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
-       ATF_REQUIRE(error == 0);
+       fd = open(relative_dir, O_RDONLY);
+       ATF_REQUIRE(fd != -1);
 
        error = posix_spawn_file_actions_init(&fa);
        ATF_REQUIRE(error == 0);
@@ -608,11 +585,28 @@
        error = posix_spawn_file_actions_addfchdir(&fa, fd);
        ATF_REQUIRE(error == 0);
 
-       error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
-       ATF_REQUIRE(error == EBADF);
+       error = posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO, FILENAME,
+           O_WRONLY, 0);
+       ATF_REQUIRE(error == 0);
+
+       error = posix_spawn(&pid, "/bin/pwd", &fa, NULL, args, NULL);
+       ATF_REQUIRE(error == 0);
+
+       /* wait for the child to finish */
+       waitpid(pid, &status, 0);
+       ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS,
+           "%s", "chdir failed");
 
        posix_spawn_file_actions_destroy(&fa);
-       posix_spawnattr_destroy(&attr);
+
+       error = close(fd);
+       ATF_REQUIRE(error == 0);
+
+       /* finally cross check the directory */
+       check_success(filepath, 1, chdirwd);
+       free(chdirwd);
+       free(filepath);
+       rmdir(relative_dir);
 }
 
 ATF_TC(t_spawn_fchdir_file);
@@ -668,6 +662,75 @@
 
 }
 
+ATF_TC(t_spawn_fchdir_neg_fd);
+
+ATF_TC_HEAD(t_spawn_fchdir_neg_fd, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Testing posix_spawn_file_actions_addfchdir on a negative file "
+           "descriptor");
+       atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
+}
+
+ATF_TC_BODY(t_spawn_fchdir_neg_fd, tc)
+{
+       int error, fd;
+       posix_spawn_file_actions_t fa;
+
+       fd = -1;
+
+       error = posix_spawn_file_actions_init(&fa);
+       ATF_REQUIRE(error == 0);
+
+       error = posix_spawn_file_actions_addfchdir(&fa, fd);
+       ATF_REQUIRE(error == EBADF);
+
+       posix_spawn_file_actions_destroy(&fa);
+}
+
+ATF_TC(t_spawn_fchdir_closed);
+
+ATF_TC_HEAD(t_spawn_fchdir_closed, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Testing posix_spawn_file_actions_addfchdir for a closed fd");
+       atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
+}
+
+ATF_TC_BODY(t_spawn_fchdir_closed, tc)
+{
+       int error, fd;
+       pid_t pid;
+       char * const args[2] = { __UNCONST("pwd"), NULL };
+       posix_spawnattr_t attr;
+       posix_spawn_file_actions_t fa;
+
+       fd = 3;
+       error = posix_spawnattr_init(&attr);
+       ATF_CHECK(error == 0);
+       /*
+        * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
+        * will cause a "proper" return value from posix_spawn(2)
+        * instead of a (potential) success there and a 127 exit
+        * status from the child process (c.f. the non-diag variant
+        * of this test).
+        */
+       error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
+       ATF_REQUIRE(error == 0);
+
+       error = posix_spawn_file_actions_init(&fa);
+       ATF_REQUIRE(error == 0);
+
+       error = posix_spawn_file_actions_addfchdir(&fa, fd);
+       ATF_REQUIRE(error == 0);
+
+       error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
+       ATF_REQUIRE(error == EBADF);
+
+       posix_spawn_file_actions_destroy(&fa);
+       posix_spawnattr_destroy(&attr);
+}
+
 #undef CHDIRPATH
 #undef FILENAME
 #undef FILEPATH
@@ -685,10 +748,11 @@
        ATF_TP_ADD_TC(tp, t_spawn_chdir_file);
        ATF_TP_ADD_TC(tp, t_spawn_chdir_invalid);
        ATF_TP_ADD_TC(tp, t_spawn_chdir_permissions);
-       ATF_TP_ADD_TC(tp, t_spawn_fchdir);
+       ATF_TP_ADD_TC(tp, t_spawn_fchdir_abs);
+       ATF_TP_ADD_TC(tp, t_spawn_fchdir_rel);
+       ATF_TP_ADD_TC(tp, t_spawn_fchdir_file);
        ATF_TP_ADD_TC(tp, t_spawn_fchdir_neg_fd);
        ATF_TP_ADD_TC(tp, t_spawn_fchdir_closed);
-       ATF_TP_ADD_TC(tp, t_spawn_fchdir_file);
 
        return atf_no_error();
 }



Home | Main Index | Thread Index | Old Index