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 Merge common code for the chd...
details: https://anonhg.NetBSD.org/src/rev/daa43877a426
branches: trunk
changeset: 1026366:daa43877a426
user: christos <christos%NetBSD.org@localhost>
date: Sat Nov 20 14:28:29 2021 +0000
description:
Merge common code for the chdir cases (Piyush Sachdeva)
I also deduplicated some more code
diffstat:
tests/lib/libc/gen/posix_spawn/t_spawn.c | 418 ++++++++++--------------------
1 files changed, 135 insertions(+), 283 deletions(-)
diffs (truncated from 610 to 300 lines):
diff -r 3e2e2b7de92a -r daa43877a426 tests/lib/libc/gen/posix_spawn/t_spawn.c
--- a/tests/lib/libc/gen/posix_spawn/t_spawn.c Sat Nov 20 13:14:34 2021 +0000
+++ b/tests/lib/libc/gen/posix_spawn/t_spawn.c Sat Nov 20 14:28:29 2021 +0000
@@ -1,7 +1,7 @@
-/* $NetBSD: t_spawn.c,v 1.5 2021/11/15 13:59:16 christos Exp $ */
+/* $NetBSD: t_spawn.c,v 1.6 2021/11/20 14:28:29 christos Exp $ */
/*-
- * Copyright (c) 2012 The NetBSD Foundation, Inc.
+ * Copyright (c) 2012, 2021 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
@@ -30,7 +30,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: t_spawn.c,v 1.5 2021/11/15 13:59:16 christos Exp $");
+__RCSID("$NetBSD: t_spawn.c,v 1.6 2021/11/20 14:28:29 christos Exp $");
#include <atf-c.h>
@@ -87,6 +87,20 @@
ATF_REQUIRE(err == 0);
}
+static void
+spawn_error(const atf_tc_t *tc, const char *name, int error)
+{
+ char buf[FILENAME_MAX];
+ char * const args[] = { __UNCONST(name), NULL };
+ int err;
+
+ snprintf(buf, sizeof buf, "%s/%s",
+ atf_tc_get_config_var(tc, "srcdir"), name);
+ err = posix_spawn(NULL, buf, NULL, NULL, args, NULL);
+ ATF_REQUIRE_MSG(err == error, "expected error %d, "
+ "got %d when spawning %s", error, err, buf);
+}
+
ATF_TC(t_spawn_zero);
ATF_TC_HEAD(t_spawn_zero, tc)
@@ -97,13 +111,7 @@
ATF_TC_BODY(t_spawn_zero, tc)
{
- char buf[FILENAME_MAX];
- char * const args[] = { __UNCONST("h_zero"), NULL };
- int err;
-
- snprintf(buf, sizeof buf, "%s/h_zero", atf_tc_get_config_var(tc, "srcdir"));
- err = posix_spawn(NULL, buf, NULL, NULL, args, NULL);
- ATF_REQUIRE_MSG(err == ENOEXEC, "expected error %d, got %d when spawning %s", ENOEXEC, err, buf);
+ spawn_error(tc, "h_zero", ENOEXEC);
}
ATF_TC(t_spawn_missing);
@@ -116,14 +124,7 @@
ATF_TC_BODY(t_spawn_missing, tc)
{
- char buf[FILENAME_MAX];
- char * const args[] = { __UNCONST("h_nonexist"), NULL };
- int err;
-
- snprintf(buf, sizeof buf, "%s/h_nonexist",
- atf_tc_get_config_var(tc, "srcdir"));
- err = posix_spawn(NULL, buf, NULL, NULL, args, NULL);
- ATF_REQUIRE_MSG(err == ENOENT, "expected error %d, got %d when spawning %s", ENOENT, err, buf);
+ spawn_error(tc, "h_nonexist", ENOENT);
}
ATF_TC(t_spawn_nonexec);
@@ -136,14 +137,7 @@
ATF_TC_BODY(t_spawn_nonexec, tc)
{
- char buf[FILENAME_MAX];
- char * const args[] = { __UNCONST("h_nonexec"), NULL };
- int err;
-
- snprintf(buf, sizeof buf, "%s/h_nonexec",
- atf_tc_get_config_var(tc, "srcdir"));
- err = posix_spawn(NULL, buf, NULL, NULL, args, NULL);
- ATF_REQUIRE_MSG(err == ENOENT, "expected error %d, got %d when spawning %s", ENOENT, err, buf);
+ spawn_error(tc, "h_nonexec", ENOENT);
}
ATF_TC(t_spawn_child);
@@ -157,38 +151,33 @@
ATF_TC_BODY(t_spawn_child, tc)
{
char buf[FILENAME_MAX];
- char * const args0[] = { __UNCONST("h_spawn"), __UNCONST("0"), NULL };
- char * const args1[] = { __UNCONST("h_spawn"), __UNCONST("1"), NULL };
- char * const args7[] = { __UNCONST("h_spawn"), __UNCONST("7"), NULL };
+ char rv[2] = { '0', '\0' };
+ char * const args0[] = { __UNCONST("h_spawn"), rv, NULL };
+ int rets[] = { 0, 1, 7 };
int err, status;
pid_t pid;
snprintf(buf, sizeof buf, "%s/h_spawn",
atf_tc_get_config_var(tc, "srcdir"));
- err = posix_spawn(&pid, buf, NULL, NULL, args0, NULL);
- ATF_REQUIRE(err == 0);
- ATF_REQUIRE(pid > 0);
- waitpid(pid, &status, 0);
- ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
-
- err = posix_spawn(&pid, buf, NULL, NULL, args1, NULL);
- ATF_REQUIRE(err == 0);
- ATF_REQUIRE(pid > 0);
- waitpid(pid, &status, 0);
- ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 1);
-
- err = posix_spawn(&pid, buf, NULL, NULL, args7, NULL);
- ATF_REQUIRE(err == 0);
- ATF_REQUIRE(pid > 0);
- waitpid(pid, &status, 0);
- ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 7);
+ for (size_t i = 0; i < __arraycount(rets); i++) {
+ rv[0] = rets[i] + '0';
+ err = posix_spawn(&pid, buf, NULL, NULL, args0, NULL);
+ ATF_REQUIRE(err == 0);
+ ATF_REQUIRE(pid > 0);
+ waitpid(pid, &status, 0);
+ ATF_REQUIRE(WIFEXITED(status) &&
+ WEXITSTATUS(status) == rets[i]);
+ }
}
#define CHDIRPATH "/tmp"
#define FILENAME "output"
#define FILEPATH "/tmp/output"
+#define CHDIR 1
+#define FCHDIR 2
+
static void
check_success(const char *file, int argc, ...)
{
@@ -216,14 +205,15 @@
dir = CHDIRPATH;
fd = open(file, O_RDONLY);
- ATF_REQUIRE_MSG(fd != -1, "Can't open `%s' (%s)", file, strerror(errno));
+ ATF_REQUIRE_MSG(fd != -1, "Can't open `%s' (%s)", file,
+ strerror(errno));
/*
* file contains form feed i.e ASCII - 10 at the end.
* Therefore sizeOfFile - 1
*/
sizeOfStr = strlen(dir);
- ATF_CHECK_MSG(sizeOfStr == sizeOfFile - 1, "%zu (%s) != %zu (%s)",
+ ATF_CHECK_MSG(sizeOfStr == sizeOfFile - 1, "%zu (%s) != %zu (%s)",
sizeOfStr, dir, sizeOfFile - 1, file);
bytesRead = read(fd, contents, sizeOfFile - 1);
@@ -241,6 +231,87 @@
ATF_REQUIRE((size_t)bytesRead == sizeOfStr);
}
+static void
+spawn_chdir(const char *dirpath, const char *filepath, int operation,
+ int expected_error)
+{
+ int error, fd=-1, status;
+ char * const args[2] = { __UNCONST("pwd"), NULL };
+ pid_t pid;
+ posix_spawnattr_t attr, *attr_p;
+ posix_spawn_file_actions_t fa;
+
+ if (filepath)
+ empty_outfile(filepath);
+
+ error = posix_spawn_file_actions_init(&fa);
+ ATF_REQUIRE(error == 0);
+
+ switch(operation) {
+ case CHDIR:
+ error = posix_spawn_file_actions_addchdir(&fa, dirpath);
+ break;
+
+ case FCHDIR:
+ fd = open(dirpath, O_RDONLY);
+ ATF_REQUIRE(fd != -1);
+
+ error = posix_spawn_file_actions_addfchdir(&fa, fd);
+ break;
+ }
+ ATF_REQUIRE(error == 0);
+
+ /*
+ * if POSIX_SPAWN_RETURNERROR is expected, then no need to open the
+ * file
+ */
+ if (expected_error == 0) {
+ error = posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO,
+ FILENAME, O_WRONLY, 0);
+ ATF_REQUIRE(error == 0);
+ attr_p = NULL;
+ } else {
+ error = posix_spawnattr_init(&attr);
+ ATF_REQUIRE(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);
+ attr_p = &attr;
+ }
+
+ error = posix_spawn(&pid, "/bin/pwd", &fa, attr_p, args, NULL);
+ ATF_REQUIRE(error == expected_error);
+
+ /* wait for the child to finish only when no spawnattr */
+ if (attr_p) {
+ posix_spawnattr_destroy(&attr);
+ } else {
+ waitpid(pid, &status, 0);
+ ATF_REQUIRE_MSG(WIFEXITED(status) &&
+ WEXITSTATUS(status) == EXIT_SUCCESS,
+ "%s", "[f]chdir failed");
+ }
+
+ posix_spawn_file_actions_destroy(&fa);
+
+ /*
+ * The file incase of fchdir(),
+ * should be closed before reopening in 'check_success'
+ */
+ if (fd != -1) {
+ error = close(fd);
+ ATF_REQUIRE(error == 0);
+ }
+}
+
ATF_TC(t_spawn_chdir_abs);
ATF_TC_HEAD(t_spawn_chdir_abs, tc)
@@ -252,31 +323,7 @@
ATF_TC_BODY(t_spawn_chdir_abs, tc)
{
- char * const args[2] = { __UNCONST("pwd"), NULL };
- int error, status;
- pid_t pid;
- posix_spawn_file_actions_t fa;
-
- empty_outfile(FILEPATH);
-
- error = posix_spawn_file_actions_init(&fa);
- ATF_REQUIRE(error == 0);
-
- error = posix_spawn_file_actions_addchdir(&fa, CHDIRPATH);
- ATF_REQUIRE(error == 0);
-
- 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);
+ spawn_chdir(CHDIRPATH, FILEPATH, 1, 0);
/* finally cross check the output of "pwd" directory */
check_success(FILEPATH, 0);
@@ -294,21 +341,18 @@
ATF_TC_BODY(t_spawn_chdir_rel, tc)
{
+ int error;
const char *relative_dir = "ch-dir";
const char *testdir = getcwd(NULL, 0);
- char * const args[2] = { __UNCONST("pwd"), NULL };
char *chdirwd, *filepath;
- int error, status;
- pid_t pid;
- posix_spawn_file_actions_t fa;
/* cleanup previous */
error = asprintf(&filepath, "%s/%s", relative_dir, FILENAME);
ATF_CHECK(error != -1);
unlink(filepath);
Home |
Main Index |
Thread Index |
Old Index