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