blob: 20d4c772fbe46faa7cb8a2f8657435b85112940f [file] [log] [blame]
Yang Xu7782f402019-04-08 14:52:27 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/* Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 * 07/2001 John George
4 * -Ported
5 *
Yang Xu7782f402019-04-08 14:52:27 +08006 * check stat() with various error conditions that should produce
7 * EACCES, EFAULT, ENAMETOOLONG, ENOENT, ENOTDIR, ELOOP
plars865695b2001-08-27 22:15:12 +00008 */
9
plars865695b2001-08-27 22:15:12 +000010#include <fcntl.h>
11#include <errno.h>
plars865695b2001-08-27 22:15:12 +000012#include <sys/types.h>
13#include <sys/stat.h>
robbiewb1c11a12001-08-28 15:47:02 +000014#include <pwd.h>
Yang Xu7782f402019-04-08 14:52:27 +080015#include "tst_test.h"
plars865695b2001-08-27 22:15:12 +000016
Yang Xu7782f402019-04-08 14:52:27 +080017#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"
plars865695b2001-08-27 22:15:12 +000022
Yang Xu7782f402019-04-08 14:52:27 +080023#define MODE_RW 0666
24#define DIR_MODE 0755
plars865695b2001-08-27 22:15:12 +000025
robbiewb1c11a12001-08-28 15:47:02 +000026struct passwd *ltpuser;
27
Yang Xu7782f402019-04-08 14:52:27 +080028static char long_dir[PATH_MAX + 2] = {[0 ... PATH_MAX + 1] = 'a'};
29static char loop_dir[PATH_MAX] = ".";
plars865695b2001-08-27 22:15:12 +000030
Yang Xu7782f402019-04-08 14:52:27 +080031static struct tcase{
plars865695b2001-08-27 22:15:12 +000032 char *pathname;
plars865695b2001-08-27 22:15:12 +000033 int exp_errno;
Yang Xu7782f402019-04-08 14:52:27 +080034} 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}
plars865695b2001-08-27 22:15:12 +000041};
42
Yang Xu7782f402019-04-08 14:52:27 +080043static void verify_stat(unsigned int n)
plars865695b2001-08-27 22:15:12 +000044{
Yang Xu7782f402019-04-08 14:52:27 +080045 struct tcase *tc = TC + n;
46 struct stat stat_buf;
plars865695b2001-08-27 22:15:12 +000047
Yang Xu7782f402019-04-08 14:52:27 +080048 TEST(stat(tc->pathname, &stat_buf));
49 if (TST_RET != -1) {
50 tst_res(TFAIL, "stat() succeeded unexpectedly");
51 return;
Garrett Cooper2c282152010-12-16 00:55:50 -080052 }
plars865695b2001-08-27 22:15:12 +000053
Yang Xu7782f402019-04-08 14:52:27 +080054 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));
plars865695b2001-08-27 22:15:12 +000060 }
Garrett Cooper2c282152010-12-16 00:55:50 -080061}
plars865695b2001-08-27 22:15:12 +000062
Yang Xu7782f402019-04-08 14:52:27 +080063static void setup(void)
plars865695b2001-08-27 22:15:12 +000064{
Yang Xu7782f402019-04-08 14:52:27 +080065 unsigned int i;
plars865695b2001-08-27 22:15:12 +000066
Yang Xu7782f402019-04-08 14:52:27 +080067 ltpuser = SAFE_GETPWNAM("nobody");
68 SAFE_SETUID(ltpuser->pw_uid);
plars865695b2001-08-27 22:15:12 +000069
Yang Xu7782f402019-04-08 14:52:27 +080070 SAFE_MKDIR(TST_EACCES_DIR, DIR_MODE);
71 SAFE_TOUCH(TST_EACCES_FILE, DIR_MODE, NULL);
72 SAFE_CHMOD(TST_EACCES_DIR, MODE_RW);
plars865695b2001-08-27 22:15:12 +000073
Yang Xu7782f402019-04-08 14:52:27 +080074 for (i = 0; i < ARRAY_SIZE(TC); i++) {
75 if (TC[i].exp_errno == EFAULT)
76 TC[i].pathname = tst_get_bad_addr(NULL);
plars865695b2001-08-27 22:15:12 +000077 }
78
Yang Xu7782f402019-04-08 14:52:27 +080079 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");
Chris Dearmanec6edca2012-10-17 19:54:01 -070085}
Yang Xu7782f402019-04-08 14:52:27 +080086
87static 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};