1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 John George
4 * -Ported
5 *
6 * check stat() with various error conditions that should produce
7 * EACCES, EFAULT, ENAMETOOLONG, ENOENT, ENOTDIR, ELOOP
8 */
9
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <pwd.h>
15 #include "tst_test.h"
16
17 #define TST_EACCES_DIR "tst_eaccesdir"
18 #define TST_EACCES_FILE "tst_eaccesdir/tst"
19 #define TST_ENOENT "tst_enoent/tst"
20 #define TST_ENOTDIR_DIR "tst_enotdir/tst"
21 #define TST_ENOTDIR_FILE "tst_enotdir"
22
23 #define MODE_RW 0666
24 #define DIR_MODE 0755
25
26 struct passwd *ltpuser;
27
28 static char long_dir[PATH_MAX + 2] = {[0 ... PATH_MAX + 1] = 'a'};
29 static char loop_dir[PATH_MAX] = ".";
30
31 static struct tcase{
32 char *pathname;
33 int exp_errno;
34 } TC[] = {
35 {TST_EACCES_FILE, EACCES},
36 {NULL, EFAULT},
37 {long_dir, ENAMETOOLONG},
38 {TST_ENOENT, ENOENT},
39 {TST_ENOTDIR_DIR, ENOTDIR},
40 {loop_dir, ELOOP}
41 };
42
verify_stat(unsigned int n)43 static void verify_stat(unsigned int n)
44 {
45 struct tcase *tc = TC + n;
46 struct stat stat_buf;
47
48 TEST(stat(tc->pathname, &stat_buf));
49 if (TST_RET != -1) {
50 tst_res(TFAIL, "stat() succeeded unexpectedly");
51 return;
52 }
53
54 if (TST_ERR == tc->exp_errno) {
55 tst_res(TPASS | TTERRNO, "stat() failed as expected");
56 } else {
57 tst_res(TFAIL | TTERRNO,
58 "stat() failed unexpectedly; expected: %d - %s",
59 tc->exp_errno, tst_strerrno(tc->exp_errno));
60 }
61 }
62
setup(void)63 static void setup(void)
64 {
65 unsigned int i;
66
67 ltpuser = SAFE_GETPWNAM("nobody");
68 SAFE_SETUID(ltpuser->pw_uid);
69
70 SAFE_MKDIR(TST_EACCES_DIR, DIR_MODE);
71 SAFE_TOUCH(TST_EACCES_FILE, DIR_MODE, NULL);
72 SAFE_CHMOD(TST_EACCES_DIR, MODE_RW);
73
74 for (i = 0; i < ARRAY_SIZE(TC); i++) {
75 if (TC[i].exp_errno == EFAULT)
76 TC[i].pathname = tst_get_bad_addr(NULL);
77 }
78
79 SAFE_TOUCH(TST_ENOTDIR_FILE, DIR_MODE, NULL);
80
81 SAFE_MKDIR("test_eloop", DIR_MODE);
82 SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");
83 for (i = 0; i < 43; i++)
84 strcat(loop_dir, "/test_eloop");
85 }
86
87 static struct tst_test test = {
88 .tcnt = ARRAY_SIZE(TC),
89 .needs_tmpdir = 1,
90 .needs_root = 1,
91 .setup = setup,
92 .test = verify_stat,
93 };
94