xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fstat/fstat02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  *  07/2001 Ported by Wayne Boyer
5  *  05/2019 Ported to new library: Christian Amann <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Tests if fstat() returns correctly and reports correct file information
12  * using the stat structure.
13  */
14 
15 #include "tst_test.h"
16 
17 #define TESTFILE        "test_file"
18 #define LINK_TESTFILE   "link_test_file"
19 #define FILE_SIZE       1024
20 #define FILE_MODE	0644
21 #define NLINK	        2
22 
23 static struct stat stat_buf;
24 static uid_t user_id;
25 static gid_t group_id;
26 static int fildes;
27 
run(void)28 static void run(void)
29 {
30 	TST_EXP_PASS(fstat(fildes, &stat_buf));
31 	TST_EXP_EQ_LU(stat_buf.st_uid, user_id);
32 	TST_EXP_EQ_LU(stat_buf.st_gid, group_id);
33 	TST_EXP_EQ_LI(stat_buf.st_size, FILE_SIZE);
34 	TST_EXP_EQ_LU(stat_buf.st_mode & 0777, FILE_MODE);
35 	TST_EXP_EQ_LU(stat_buf.st_nlink, NLINK);
36 }
37 
setup(void)38 static void setup(void)
39 {
40 	user_id  = getuid();
41 	group_id = getgid();
42 
43 	umask(0);
44 
45 	fildes = SAFE_OPEN(TESTFILE, O_WRONLY | O_CREAT, FILE_MODE);
46 
47 	if (tst_fill_file(TESTFILE, 'a', FILE_SIZE, 1))
48 		tst_brk(TBROK, "Could not fill test file");
49 
50 	SAFE_LINK(TESTFILE, LINK_TESTFILE);
51 }
52 
cleanup(void)53 static void cleanup(void)
54 {
55 	if (fildes > 0)
56 		SAFE_CLOSE(fildes);
57 }
58 
59 static struct tst_test test = {
60 	.test_all = run,
61 	.setup = setup,
62 	.cleanup = cleanup,
63 	.needs_tmpdir = 1,
64 };
65