[LTP] [PATCH 2/2] Add error tests for pathconf() system call

Yang Xu xuyang2018.jy@fujitsu.com
Thu Nov 2 09:05:35 CET 2023


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

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

diff --git a/runtest/syscalls b/runtest/syscalls
index 1851752cf..e20f0c49e 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -971,6 +971,7 @@ madvise11 madvise11
 newuname01 newuname01
 
 pathconf01 pathconf01
+pathconf02 pathconf02
 
 pause01 pause01
 pause02 pause02
diff --git a/testcases/kernel/syscalls/pathconf/.gitignore b/testcases/kernel/syscalls/pathconf/.gitignore
index 31abe8a28..82e38b253 100644
--- a/testcases/kernel/syscalls/pathconf/.gitignore
+++ b/testcases/kernel/syscalls/pathconf/.gitignore
@@ -1 +1,2 @@
 /pathconf01
+/pathconf02
diff --git a/testcases/kernel/syscalls/pathconf/pathconf02.c b/testcases/kernel/syscalls/pathconf/pathconf02.c
new file mode 100644
index 000000000..8d7996190
--- /dev/null
+++ b/testcases/kernel/syscalls/pathconf/pathconf02.c
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2023 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that,
+ *
+ * - pathconf() fails with ENOTDIR if A component used as a directory
+ *   in path is not in fact a directory.
+ * - pathconf() fails with ENOENT if path is an empty string.
+ * - pathconf() fails with ENAMETOOLONG if path is too long.
+ * - pathconf() fails with EINVA if name is invalid.
+ * - pathconf() fails with EACCES if search permission is denied for
+ *   one of the directories in the path prefix of path.
+ * - pathconf() fails with ELOOP if too many symbolic links were
+ *   encountered while resolving path.
+ */
+
+#define FILEPATH "testfile/testfile_1"
+#define TESTELOOP "test_eloop1"
+
+#include <stdlib.h>
+#include <pwd.h>
+#include "tst_test.h"
+
+static char *fpath;
+static char *emptypath;
+static char path[PATH_MAX + 2];
+static char *long_path = path;
+static char *abs_path;
+static char *testeloop;
+static struct passwd *user;
+
+static struct tcase {
+	char **path;
+	int name;
+	int exp_errno;
+	char *desc;
+} tcases[] = {
+	{&fpath, 0, ENOTDIR, "path prefix is not a directory"},
+	{&emptypath, 0, ENOENT, "path is an empty string"},
+	{&long_path, 0, ENAMETOOLONG, "path is too long"},
+	{&abs_path, -1, EINVAL, "name is invalid"},
+	{&abs_path, 0, EACCES, "without full permissions of the path prefix"},
+	{&testeloop, 0, ELOOP, "too many symbolic links"},
+};
+
+static void verify_fpathconf(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+
+	if (tc->exp_errno == EACCES)
+		SAFE_SETEUID(user->pw_uid);
+
+	TST_EXP_FAIL(pathconf(*tc->path, tc->name), tc->exp_errno,
+		     "pathconf() fail with %s", tc->desc);
+
+	if (tc->exp_errno == EACCES)
+		SAFE_SETEUID(0);
+}
+
+static void setup(void)
+{
+	user = SAFE_GETPWNAM("nobody");
+
+	SAFE_TOUCH("testfile", 0777, NULL);
+
+	char *tmpdir =  tst_get_tmpdir();
+
+	abs_path = tst_aprintf("%s/%s", tmpdir, FILEPATH);
+
+	SAFE_CHMOD(tmpdir, 0);
+	free(tmpdir);
+
+	memset(path, 'a', PATH_MAX + 2);
+
+	SAFE_SYMLINK("test_eloop1", "test_eloop2");
+	SAFE_SYMLINK("test_eloop2", "test_eloop1");
+}
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_fpathconf,
+	.setup = setup,
+	.needs_tmpdir = 1,
+	.bufs = (struct tst_buffers []) {
+		{&fpath, .str = FILEPATH},
+		{&emptypath, .str = ""},
+		{&testeloop, .str = TESTELOOP},
+		{},
+	},
+	.needs_root = 1,
+};
-- 
2.39.1



More information about the ltp mailing list