[LTP] [PATCH v2 2/2] syscalls/fchmodat02: Move errnos check to fchmodat02

Yang Xu xuyang2018.jy@fujitsu.com
Thu Oct 19 04:55:52 CEST 2023


Check errnos for fchmodat().

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

diff --git a/runtest/syscalls b/runtest/syscalls
index 1851752cf..8c62dc309 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -259,6 +259,7 @@ fchmod06 fchmod06
 
 #fchmodat test cases
 fchmodat01 fchmodat01
+fchmodat02 fchmodat02
 
 fchown01 fchown01
 fchown01_16 fchown01_16
diff --git a/testcases/kernel/syscalls/fchmodat/.gitignore b/testcases/kernel/syscalls/fchmodat/.gitignore
index a9508bc5a..09d5c47d5 100644
--- a/testcases/kernel/syscalls/fchmodat/.gitignore
+++ b/testcases/kernel/syscalls/fchmodat/.gitignore
@@ -1 +1,2 @@
 /fchmodat01
+/fchmodat02
diff --git a/testcases/kernel/syscalls/fchmodat/fchmodat02.c b/testcases/kernel/syscalls/fchmodat/fchmodat02.c
new file mode 100644
index 000000000..3539753b8
--- /dev/null
+++ b/testcases/kernel/syscalls/fchmodat/fchmodat02.c
@@ -0,0 +1,85 @@
+// 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]
+ *
+ * - fchmodat() fails with ENOTDIR if dir_fd is file descriptor
+ *   to the file and pathname is relative path of the file.
+ * - fchmodat() fails with EBADF if dir_fd is invalid.
+ * - fchmodat() fails with EFAULT if pathname points outside
+ *   the accessible address space.
+ * - fchmodat() fails with ENAMETOOLONG if path is too long.
+ * - fchmodat() fails with ENOENT if pathname does not exist.
+ * - fchmodat() fails with EINVAL if flag is invalid.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include "tst_test.h"
+
+#define TESTFILE        "fchmodatfile"
+
+static int file_fd;
+static int bad_fd = -1;
+static char path[PATH_MAX + 2];
+static char *long_path = path;
+static int fd_atcwd = AT_FDCWD;
+static char *bad_path;
+static char *test_path;
+static char *empty_path;
+
+static struct tcase {
+	int *fd;
+	char **filenames;
+	int flag;
+	int exp_errno;
+	const char *desc;
+} tcases[] = {
+	{&file_fd, &test_path, 0, ENOTDIR, "fd pointing to file"},
+	{&bad_fd, &test_path, 0, EBADF, "invalid fd"},
+	{&file_fd, &bad_path, 0, EFAULT, "invalid address"},
+	{&file_fd, &long_path, 0, ENAMETOOLONG, "pathname too long"},
+	{&file_fd, &empty_path, 0, ENOENT, "path is empty"},
+	{&fd_atcwd, &test_path, -1, EINVAL, "invalid flag"},
+};
+
+static void verify_fchmodat(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+
+	TST_EXP_FAIL(fchmodat(*tc->fd, *tc->filenames, 0600, tc->flag),
+		     tc->exp_errno, "fchmodat() with %s", tc->desc);
+}
+
+static void setup(void)
+{
+	file_fd = SAFE_OPEN(TESTFILE, O_CREAT | O_RDWR, 0600);
+
+	bad_path = tst_get_bad_addr(NULL);
+
+	memset(path, 'a', PATH_MAX + 2);
+}
+
+static void cleanup(void)
+{
+	if (file_fd > -1)
+		SAFE_CLOSE(file_fd);
+}
+
+static struct tst_test test = {
+	.test = verify_fchmodat,
+	.tcnt = ARRAY_SIZE(tcases),
+	.setup = setup,
+	.cleanup = cleanup,
+	.bufs = (struct tst_buffers []) {
+		{&test_path, .str = TESTFILE},
+		{&empty_path, .str = ""},
+		{},
+	},
+	.needs_tmpdir = 1,
+};
-- 
2.39.1



More information about the ltp mailing list