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 * Copyright (c) Linux Test Project, 2002-2022
6 */
7
8 /*\
9 * [Description]
10 *
11 * Verify that, stat(2) succeeds to get the status of a file and fills the
12 * stat structure elements regardless of whether process has or doesn't
13 * have read access to the file.
14 */
15
16 #include <pwd.h>
17 #include "tst_test.h"
18
19 #define FILE_SIZE 1024
20 #define TST_FILEREAD "test_fileread"
21 #define TST_FILENOREAD "test_filenoread"
22 #define READ_MODE 0666
23 #define NEW_MODE 0222
24 #define MASK 0777
25
26 static uid_t user_id;
27 static gid_t group_id;
28 static struct passwd *ltpuser;
29
30 static struct tcase{
31 char *pathname;
32 unsigned int mode;
33 } TC[] = {
34 {TST_FILEREAD, READ_MODE},
35 {TST_FILENOREAD, NEW_MODE}
36 };
37
verify_stat(unsigned int n)38 static void verify_stat(unsigned int n)
39 {
40 struct tcase *tc = TC + n;
41 struct stat stat_buf;
42
43 TST_EXP_PASS(stat(tc->pathname, &stat_buf));
44
45 TST_EXP_EQ_LU(stat_buf.st_uid, user_id);
46 TST_EXP_EQ_LU(stat_buf.st_gid, group_id);
47 TST_EXP_EQ_LI(stat_buf.st_size, FILE_SIZE);
48 TST_EXP_EQ_LU(stat_buf.st_mode & MASK, tc->mode);
49 TST_EXP_EQ_LU(stat_buf.st_nlink, 1);
50 }
51
setup(void)52 static void setup(void)
53 {
54 unsigned int i;
55
56 ltpuser = SAFE_GETPWNAM("nobody");
57 SAFE_SETUID(ltpuser->pw_uid);
58
59 for (i = 0; i < ARRAY_SIZE(TC); i++) {
60 if (tst_fill_file(TC[i].pathname, 'a', 256, 4))
61 tst_brk(TBROK, "Failed to create tst file %s",
62 TC[i].pathname);
63 SAFE_CHMOD(TC[i].pathname, TC[i].mode);
64 }
65
66 user_id = getuid();
67 group_id = getgid();
68 }
69
70 static struct tst_test test = {
71 .tcnt = ARRAY_SIZE(TC),
72 .setup = setup,
73 .test = verify_stat,
74 .needs_root = 1,
75 .needs_tmpdir = 1,
76 };
77