• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <camann@suse.com>
6  */
7 /*
8  * Tests if fstat() returns correctly and reports correct file information
9  * using the stat structure.
10  */
11 
12 #include <errno.h>
13 #include <unistd.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include "tst_test.h"
17 #include "tst_safe_macros.h"
18 
19 #define TESTFILE        "test_file"
20 #define FILE_SIZE       1024
21 #define FILE_MODE	0644
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 	int fail = 0;
31 
32 	TEST(fstat(fildes, &stat_buf));
33 
34 	if (TST_RET != 0) {
35 		tst_res(TFAIL | TTERRNO, "fstat() failed");
36 		return;
37 	}
38 
39 	fail = 0;
40 	if (stat_buf.st_uid != user_id) {
41 		tst_res(TFAIL, "stat_buf.st_uid = %i expected %i",
42 			stat_buf.st_uid, user_id);
43 		fail++;
44 	}
45 
46 	if (stat_buf.st_gid != group_id) {
47 		tst_res(TFAIL, "stat_buf.st_gid = %i expected %i",
48 			stat_buf.st_gid, group_id);
49 		fail++;
50 	}
51 
52 	if (stat_buf.st_size != FILE_SIZE) {
53 		tst_res(TFAIL, "stat_buf.st_size = %li expected %i",
54 			(long)stat_buf.st_size, FILE_SIZE);
55 		fail++;
56 	}
57 
58 	if ((stat_buf.st_mode & 0777) != FILE_MODE) {
59 		tst_res(TFAIL, "stat_buf.st_mode = %o expected %o",
60 			(stat_buf.st_mode & 0777), FILE_MODE);
61 		fail++;
62 	}
63 
64 	if (fail)
65 		return;
66 
67 	tst_res(TPASS, "fstat() reported correct values.");
68 }
69 
setup(void)70 static void setup(void)
71 {
72 	user_id  = getuid();
73 	group_id = getgid();
74 
75 	umask(0);
76 
77 	fildes = SAFE_OPEN(TESTFILE, O_WRONLY | O_CREAT, FILE_MODE);
78 
79 	if (tst_fill_file(TESTFILE, 'a', FILE_SIZE, 1))
80 		tst_brk(TBROK, "Could not fill Testfile!");
81 }
82 
cleanup(void)83 static void cleanup(void)
84 {
85 	if (fildes > 0)
86 		SAFE_CLOSE(fildes);
87 }
88 
89 static struct tst_test test = {
90 	.test_all = run,
91 	.setup = setup,
92 	.cleanup = cleanup,
93 	.needs_tmpdir = 1,
94 };
95