[LTP] [PATCH 2/2] syscalls/faccessat202: Add new testcase

Yang Xu xuyang2018.jy@fujitsu.com
Sat Aug 5 06:08:00 CEST 2023


Check various errnos for faccessat2().

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

diff --git a/runtest/syscalls b/runtest/syscalls
index 948c39030..9ccc1c7b6 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -218,6 +218,7 @@ faccessat01 faccessat01
 
 #faccessat2 test cases
 faccessat201 faccessat201
+faccessat202 faccessat202
 
 #fallocate test cases
 fallocate01 fallocate01
diff --git a/testcases/kernel/syscalls/faccessat2/.gitignore b/testcases/kernel/syscalls/faccessat2/.gitignore
index 53f700bac..f58f045f4 100644
--- a/testcases/kernel/syscalls/faccessat2/.gitignore
+++ b/testcases/kernel/syscalls/faccessat2/.gitignore
@@ -1 +1,2 @@
 /faccessat201
+/faccessat202
diff --git a/testcases/kernel/syscalls/faccessat2/faccessat202.c b/testcases/kernel/syscalls/faccessat2/faccessat202.c
new file mode 100644
index 000000000..061dcf8ce
--- /dev/null
+++ b/testcases/kernel/syscalls/faccessat2/faccessat202.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2023 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test basic error handling of faccessat2 syscall:
+ *
+ * - faccessat2() fails with EFAULT if pathname is a bad pathname point.
+ * - faccessat2() fails with EINVAL if flags is -1.
+ * - faccessat2() fails with EINVAL if mode is -1.
+ * - faccessat2() fails with EBADF if dirfd is -1.
+ * - faccessat2() fails with ENOTDIR if pathname is relative path to the
+ *   file and dir_fd is file descriptor for this file.
+ *
+ * Minimum Linux version required is v5.8.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <pwd.h>
+
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+#include "lapi/faccessat2.h"
+
+#define TESTDIR		"faccessat2dir"
+#define TESTFILE	"faccessat2dir/faccessat2file"
+
+static int fd = -1, bad_fd = -1;
+static int atcwd_fd = AT_FDCWD;
+static char *bad_path, *file_path;
+static struct passwd *ltpuser;
+
+static struct tcase {
+	int *fd;
+	char **filepath;
+	int mode;
+	int flags;
+	int exp_errno;
+} tcases[] = {
+	{&atcwd_fd, &bad_path, R_OK, 0, EFAULT},
+	{&atcwd_fd, &file_path, R_OK, -1, EINVAL},
+	{&atcwd_fd, &file_path, -1, 0, EINVAL},
+	{&bad_fd, &file_path, R_OK, 0, EBADF},
+	{&fd, &file_path, R_OK, 0, ENOTDIR},
+};
+
+static void verify_faccessat2(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+
+	TST_EXP_FAIL(faccessat2(*tc->fd, *tc->filepath, tc->mode, tc->flags),
+		     tc->exp_errno);
+}
+
+static void setup(void)
+{
+	SAFE_MKDIR(TESTDIR, 0666);
+	SAFE_TOUCH(TESTFILE, 0444, NULL);
+
+	fd = SAFE_OPEN(TESTFILE, O_RDONLY);
+
+	bad_path = tst_get_bad_addr(NULL);
+	file_path = TESTFILE;
+}
+
+static void cleanup(void)
+{
+	if (fd > 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.test = verify_faccessat2,
+	.tcnt = ARRAY_SIZE(tcases),
+	.setup = setup,
+	.cleanup = cleanup,
+	.bufs = (struct tst_buffers []) {
+		{&bad_path, .size = sizeof(*bad_path)},
+		{&file_path, .size = sizeof(*file_path)},
+		{&ltpuser, .size = sizeof(ltpuser)},
+		{},
+	},
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
-- 
2.23.0



More information about the ltp mailing list