[LTP] [PATCH v2 3/3] fchownat03: Move error tests from fchownat01

Shiyang Ruan ruansy.fnst@fujitsu.com
Wed Jan 15 10:12:18 CET 2025


Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
---
 testcases/kernel/syscalls/fchownat/.gitignore |  1 +
 .../kernel/syscalls/fchownat/fchownat03.c     | 77 +++++++++++++++++++
 2 files changed, 78 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fchownat/fchownat03.c

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..3c50eb39e
--- /dev/null
+++ b/testcases/kernel/syscalls/fchownat/fchownat03.c
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines  Corp., 2006
+ * Copyright (c) Linux Test Project, 2007-2025
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that:
+ *
+ * 1. fchownat() returns -1 and sets errno to ENOTDIR if the file descriptor is
+ *    a file.
+ * 2. fchownat() returns -1 and sets errno to EBADF if the file descriptor
+ *    of the specified file is not valid.
+ * 3. fchownat() returns -1 and sets errno to EINVAL if the flag is not valid.
+ */
+
+#define _GNU_SOURCE
+#include "tst_test.h"
+
+#define TESTFILE	"testfile"
+#define TESTFILE2	"testfile2"
+
+static int fd;
+static int no_fd = -1;
+static int dir_fd;
+
+static struct tcase {
+	int exp_errno;
+	int flag;
+	int *fd;
+	char *filename;
+} tcases[] = {
+	{ ENOTDIR, 0, &fd, TESTFILE },
+	{ EBADF, 0, &no_fd, TESTFILE },
+	{ EINVAL, 9999, &dir_fd, TESTFILE },
+};
+
+static void fchownat_verify(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+	uid_t euid = geteuid();
+	gid_t egid = getegid();
+	int fd = *tc->fd;
+	int flag = tc->flag;
+	const char *filename = tc->filename;
+
+	TST_EXP_FAIL(fchownat(fd, filename, euid, egid, flag),
+			tc->exp_errno,
+			"fchownat(%d, %s, %d, %d, %d)",
+			fd, filename, euid, egid, flag);
+}
+
+static void setup(void)
+{
+	SAFE_TOUCH(TESTFILE, 0600, NULL);
+	fd = SAFE_OPEN(TESTFILE2, O_CREAT | O_RDWR, 0600);
+	dir_fd = SAFE_OPEN("./", O_DIRECTORY);
+}
+
+static void cleanup(void)
+{
+	if (fd > 0)
+		SAFE_CLOSE(fd);
+
+	if (dir_fd > 0)
+		SAFE_CLOSE(dir_fd);
+}
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test = fchownat_verify,
+	.tcnt = ARRAY_SIZE(tcases),
+	.setup = setup,
+	.cleanup = cleanup,
+};
-- 
2.43.0



More information about the ltp mailing list