1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2023 FUJITSU LIMITED. All rights reserved.
4 * Author: Yang Xu <[email protected]>
5 */
6
7 /*\
8 * [Description]
9 *
10 * Verify that,
11 *
12 * - pathconf() fails with ENOTDIR if a component used as a directory
13 * in path is not in fact a directory.
14 * - pathconf() fails with ENOENT if path is an empty string.
15 * - pathconf() fails with ENAMETOOLONG if path is too long.
16 * - pathconf() fails with EINVA if name is invalid.
17 * - pathconf() fails with EACCES if search permission is denied for
18 * one of the directories in the path prefix of path.
19 * - pathconf() fails with ELOOP if too many symbolic links were
20 * encountered while resolving path.
21 */
22
23 #define FILEPATH "testfile/testfile_1"
24 #define TESTELOOP "test_eloop1"
25 #define PATH_LEN (PATH_MAX + 2)
26
27 #include <stdlib.h>
28 #include <pwd.h>
29 #include "tst_test.h"
30
31 static char *fpath;
32 static char *emptypath;
33 static char path[PATH_LEN];
34 static char *long_path = path;
35 static char *abs_path;
36 static char *testeloop;
37 static struct passwd *user;
38
39 static struct tcase {
40 char **path;
41 int name;
42 int exp_errno;
43 char *desc;
44 } tcases[] = {
45 {&fpath, 0, ENOTDIR, "path prefix is not a directory"},
46 {&emptypath, 0, ENOENT, "path is an empty string"},
47 {&long_path, 0, ENAMETOOLONG, "path is too long"},
48 {&abs_path, -1, EINVAL, "name is invalid"},
49 {&abs_path, 0, EACCES, "without full permissions of the path prefix"},
50 {&testeloop, 0, ELOOP, "too many symbolic links"},
51 };
52
verify_fpathconf(unsigned int i)53 static void verify_fpathconf(unsigned int i)
54 {
55 struct tcase *tc = &tcases[i];
56
57 if (tc->exp_errno == EACCES)
58 SAFE_SETEUID(user->pw_uid);
59
60 TST_EXP_FAIL(pathconf(*tc->path, tc->name), tc->exp_errno,
61 "pathconf() fail with %s", tc->desc);
62
63 if (tc->exp_errno == EACCES)
64 SAFE_SETEUID(0);
65 }
66
setup(void)67 static void setup(void)
68 {
69 user = SAFE_GETPWNAM("nobody");
70
71 SAFE_TOUCH("testfile", 0777, NULL);
72
73 char *tmpdir = tst_get_tmpdir();
74
75 abs_path = tst_aprintf("%s/%s", tmpdir, FILEPATH);
76
77 SAFE_CHMOD(tmpdir, 0);
78 free(tmpdir);
79
80 memset(path, 'a', PATH_LEN);
81
82 SAFE_SYMLINK("test_eloop1", "test_eloop2");
83 SAFE_SYMLINK("test_eloop2", "test_eloop1");
84 }
85
86 static struct tst_test test = {
87 .tcnt = ARRAY_SIZE(tcases),
88 .test = verify_fpathconf,
89 .setup = setup,
90 .needs_tmpdir = 1,
91 .bufs = (struct tst_buffers []) {
92 {&fpath, .str = FILEPATH},
93 {&emptypath, .str = ""},
94 {&testeloop, .str = TESTELOOP},
95 {},
96 },
97 .needs_root = 1,
98 };
99