• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2023 FUJITSU LIMITED. All rights reserved.
4  * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
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 	abs_path = tst_tmpdir_genpath(FILEPATH);
74 
75 	SAFE_CHMOD(tst_tmpdir_path(), 0);
76 
77 	memset(path, 'a', PATH_LEN);
78 
79 	SAFE_SYMLINK("test_eloop1", "test_eloop2");
80 	SAFE_SYMLINK("test_eloop2", "test_eloop1");
81 }
82 
83 static struct tst_test test = {
84 	.tcnt = ARRAY_SIZE(tcases),
85 	.test = verify_fpathconf,
86 	.setup = setup,
87 	.needs_tmpdir = 1,
88 	.bufs = (struct tst_buffers []) {
89 		{&fpath, .str = FILEPATH},
90 		{&emptypath, .str = ""},
91 		{&testeloop, .str = TESTELOOP},
92 		{},
93 	},
94 	.needs_root = 1,
95 };
96