[LTP] [PATCH 3/3] syscalls/fchownat03: Move errnos check to fchownat03

Yang Xu xuyang2018.jy@fujitsu.com
Tue Oct 10 07:28:29 CEST 2023


Check errnos for fchownat().

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

diff --git a/runtest/syscalls b/runtest/syscalls
index 4f1ee1f34..fbbab829f 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -274,6 +274,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..56b090895
--- /dev/null
+++ b/testcases/kernel/syscalls/fchownat/fchownat03.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines  Corp., 2006
+ * Copyright (c) Linux Test Project, 2003-2023
+ * AUTHOR: Yi Yang <yyangcdl@cn.ibm.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.
+ */
+
+#include <stdlib.h>
+#include "tst_test.h"
+
+#define TESTFILE	"testfile"
+
+static int dir_fd, fd;
+static int no_fd = -1;
+static char *testfile;
+
+static struct tcase {
+	int *fds;
+	char **filenames;
+	int flag;
+	int exp_errno;
+} tcases[] = {
+	{&fd, &testfile, 0, ENOTDIR},
+	{&no_fd, &testfile, 0, EBADF},
+	{&dir_fd, &testfile, -1, EINVAL},
+};
+
+static void setup(void)
+{
+	dir_fd = SAFE_OPEN("./", O_DIRECTORY);
+
+	SAFE_TOUCH(TESTFILE, 0600, NULL);
+
+	fd = SAFE_OPEN("testfile2", O_CREAT | O_RDWR, 0600);
+}
+
+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(%d, %s, %d, %d, %d)",
+	     *tc->fds, *tc->filenames, geteuid(), getegid(), tc->flag);
+}
+
+static void cleanup(void)
+{
+	if (fd > -1)
+		close(fd);
+
+	if (dir_fd > -1)
+		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},
+		{},
+	},
+	.needs_tmpdir = 1,
+};
-- 
2.39.1



More information about the ltp mailing list