[LTP] [PATCH v2 3/3] fchownat03: Add error tests for fchownat() system call

Yang Xu xuyang2018.jy@fujitsu.com
Fri Dec 22 10:44:55 CET 2023


A series of tests were added to test the negative results of fchownat()

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 runtest/syscalls                              |   1 +
 testcases/kernel/syscalls/fchownat/.gitignore |   1 +
 .../kernel/syscalls/fchownat/fchownat03.c     | 101 ++++++++++++++++++
 3 files changed, 103 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fchownat/fchownat03.c

diff --git a/runtest/syscalls b/runtest/syscalls
index b7ceb25d3..589c2c9a3 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -275,6 +275,7 @@ fchown05_16 fchown05_16
 #fchownat test case
 fchownat01 fchownat01
 fchownat02 fchownat02
+fchownat03 fchownat03
 
 fcntl01 fcntl01
 fcntl01_64 fcntl01_64
diff --git a/testcases/kernel/syscalls/fchownat/.gitignore b/testcases/kernel/syscalls/fchownat/.gitignore
index 35c00345b..60fac7e69 100644
--- a/testcases/kernel/syscalls/fchownat/.gitignore
+++ b/testcases/kernel/syscalls/fchownat/.gitignore
@@ -1,2 +1,3 @@
 /fchownat01
 /fchownat02
+/fchownat03
diff --git a/testcases/kernel/syscalls/fchownat/fchownat03.c b/testcases/kernel/syscalls/fchownat/fchownat03.c
new file mode 100644
index 000000000..85591dd93
--- /dev/null
+++ b/testcases/kernel/syscalls/fchownat/fchownat03.c
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) Linux Test Project, 2006-2023
+ * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * - fchownat() fails with ENOTDIR  if dir_fd is file descriptor to the file
+ *   and pathname is relative path of the file.
+ * - fchownat() fails with EBADF if dir_fd is invalid.
+ * - fchownat() fails with EINVAL if flag is invalid.
+ * - fchownat() fails with ENAMETOOLONG if pathname is too long.
+ * - fchownat() fails with ELOOP if too many symbolic links were
+ *   encountered while resolving path.
+ * - fchownat() fails with ENOENT if the file does not exist.
+ * - fchownat() fails with EFAULT if pathname points outside accessible
+ *   address space.
+ */
+
+#include <stdlib.h>
+#include "tst_test.h"
+
+#define TESTFILE  "testfile"
+#define TESTELOOP "test_eloop1"
+#define PATH_LEN (PATH_MAX + 2)
+
+static int dir_fd, fd;
+static int invalid_fd = -1;
+static char *testfile;
+static char path[PATH_LEN];
+static char *long_path = path;
+static char *testeloop;
+static char *emptypath;
+static char *bad_path;
+
+static struct tcase {
+	int *fds;
+	char **filenames;
+	int flag;
+	int exp_errno;
+	char *desc;
+} tcases[] = {
+	{&fd, &testfile, 0, ENOTDIR, "path prefix is not a directory"},
+	{&invalid_fd, &testfile, 0, EBADF, "invalid fd"},
+	{&dir_fd, &testfile, -1, EINVAL, "invalid flag"},
+	{&dir_fd, &long_path, 0, ENAMETOOLONG, "pathname is too long"},
+	{&dir_fd, &testeloop, 0, ELOOP, "too many symbolic links"},
+	{&dir_fd, &emptypath, 0, ENOENT, "the file does not exist"},
+	{&dir_fd, &bad_path, 0, EFAULT, "inaccessible address space"},
+};
+
+static void setup(void)
+{
+	dir_fd = SAFE_OPEN("./", O_DIRECTORY);
+
+	SAFE_TOUCH(TESTFILE, 0600, NULL);
+
+	fd = SAFE_OPEN("testfile2", O_CREAT | O_RDWR, 0600);
+
+	memset(path, 'a', PATH_LEN);
+
+	SAFE_SYMLINK("test_eloop1", "test_eloop2");
+	SAFE_SYMLINK("test_eloop2", "test_eloop1");
+
+	bad_path = tst_get_bad_addr(NULL);
+}
+
+static void fchownat_verify(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+
+	TST_EXP_FAIL(fchownat(*tc->fds, *tc->filenames, geteuid(), getegid(),
+		     tc->flag), tc->exp_errno, "fchownat() fails with %s",
+		     tc->desc);
+}
+
+static void cleanup(void)
+{
+	if (fd > -1)
+		SAFE_CLOSE(fd);
+
+	if (dir_fd > -1)
+		SAFE_CLOSE(dir_fd);
+}
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = fchownat_verify,
+	.setup = setup,
+	.cleanup = cleanup,
+	.bufs = (struct tst_buffers []) {
+		{&testfile, .str = TESTFILE},
+		{&testeloop, .str = TESTELOOP},
+		{&emptypath, .str = ""},
+		{},
+	},
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
-- 
2.39.1



More information about the ltp mailing list