• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *  07/2001 John George
4  *
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program;  if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 /*
20  *  Verify that, stat(2) succeeds to get the status of a file and fills the
21  *  stat structure elements.
22  */
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <pwd.h>
31 
32 #include "test.h"
33 #include "safe_macros.h"
34 
35 #define FILE_MODE	0644
36 #define TESTFILE	"testfile"
37 #define FILE_SIZE       1024
38 #define BUF_SIZE	256
39 #define MASK		0777
40 
41 char *TCID = "stat01";
42 int TST_TOTAL = 1;
43 
44 static uid_t user_id;
45 static gid_t group_id;
46 
47 static void setup(void);
48 static void cleanup(void);
49 
verify(void)50 static void verify(void)
51 {
52 	struct stat stat_buf;
53 	int fail = 0;
54 
55 	TEST(stat(TESTFILE, &stat_buf));
56 
57 	if (TEST_RETURN == -1) {
58 		tst_resm(TFAIL | TTERRNO, "fstat(%s) failed", TESTFILE);
59 		return;
60 	}
61 
62 	if (stat_buf.st_uid != user_id) {
63 		tst_resm(TINFO, "stat_buf.st_uid = %i expected %i",
64 		         stat_buf.st_uid, user_id);
65 		fail++;
66 	}
67 
68 	if (stat_buf.st_gid != group_id) {
69 		tst_resm(TINFO, "stat_buf.st_gid = %i expected %i",
70 		         stat_buf.st_gid, group_id);
71 		fail++;
72 	}
73 
74 	if (stat_buf.st_size != FILE_SIZE) {
75 		tst_resm(TINFO, "stat_buf.st_size = %li expected %i",
76 		         (long)stat_buf.st_size, FILE_SIZE);
77 		fail++;
78 	}
79 
80         if ((stat_buf.st_mode & MASK) != FILE_MODE) {
81 		tst_resm(TINFO, "stat_buf.st_mode = %o expected %o",
82 		         (stat_buf.st_mode & MASK), FILE_MODE);
83 		fail++;
84 	}
85 
86 	if (fail) {
87 		tst_resm(TFAIL, "functionality of fstat incorrect");
88 		return;
89 	}
90 
91 	tst_resm(TPASS, "functionality of fstat correct");
92 }
93 
main(int ac,char ** av)94 int main(int ac, char **av)
95 {
96 	int lc;
97 
98 	tst_parse_opts(ac, av, NULL, NULL);
99 
100 	setup();
101 
102 	for (lc = 0; TEST_LOOPING(lc); lc++)
103 		verify();
104 
105 	cleanup();
106 	tst_exit();
107 }
108 
setup(void)109 static void setup(void)
110 {
111 	struct passwd *ltpuser;
112 	char tst_buff[BUF_SIZE];
113 	int wbytes;
114 	int write_len = 0;
115 	int fd;
116 
117 	tst_require_root();
118 
119 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
120 
121 	ltpuser = SAFE_GETPWNAM(NULL, "nobody");
122 	SAFE_SETUID(NULL, ltpuser->pw_uid);
123 
124 	TEST_PAUSE;
125 
126 	tst_tmpdir();
127 
128 	umask(022);
129 
130 	fd = SAFE_OPEN(tst_rmdir, TESTFILE, O_WRONLY | O_CREAT, FILE_MODE);
131 
132 	/* Fill the test buffer with the known data */
133 	memset(tst_buff, 'a', BUF_SIZE - 1);
134 
135 	/* Write to the file 1k data from the buffer */
136 	while (write_len < FILE_SIZE) {
137 		if ((wbytes = write(fd, tst_buff, sizeof(tst_buff))) <= 0)
138 			tst_brkm(TBROK | TERRNO, cleanup, "write failed");
139 		else
140 			write_len += wbytes;
141 	}
142 
143 	SAFE_CLOSE(tst_rmdir, fd);
144 
145 	user_id = getuid();
146 	group_id = getgid();
147 }
148 
cleanup(void)149 static void cleanup(void)
150 {
151 	tst_rmdir();
152 }
153