• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
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
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * Test Name: truncate02
22  *
23  * Test Description:
24  *  Verify that, truncate(2) succeeds to truncate a file to a certain length,
25  *  but the attempt to read past the truncated length will fail.$
26  *
27  * Expected Result:
28  *  truncate(2) should return a value 0 and the attempt to read past the
29  *  truncated length will fail. In case where the file before truncation was
30  *  shorter, the bytes between the old and new should  be all zeroes.
31  *
32  * Algorithm:
33  *  Setup:
34  *   Setup signal handling.
35  *   Create temporary directory.
36  *   Pause for SIGUSR1 if option specified.
37  *
38  *  Test:
39  *   Loop if the proper options are given.
40  *   Execute system call
41  *   Check return code, if system call failed (return=-1)
42  *   	Log the errno and Issue a FAIL message.
43  *   Otherwise,
44  *   	Verify the Functionality of system call
45  *      if successful,
46  *      	Issue Functionality-Pass message.
47  *      Otherwise,
48  *		Issue Functionality-Fail message.
49  *  Cleanup:
50  *   Print errno log and/or timing stats if options given
51  *   Delete the temporary directory created.
52  *
53  * Usage:  <for command-line>
54  *  truncate02 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
55  *	where,  -c n : Run n copies concurrently.
56  *		-e   : Turn on errno logging.
57  *		-f   : Turn off functionality Testing.
58  *		-i n : Execute test n times.
59  *		-I x : Execute test for x seconds.
60  *		-P x : Pause for x seconds between iterations.
61  *		-t   : Turn on syscall timing.
62  *
63  * History
64  *	07/2001 John George
65  *		-Ported
66  *
67  * Restrictions:
68  *  This test should be run by 'non-super-user' only.
69  *
70  */
71 
72 #include <sys/types.h>
73 #include <sys/stat.h>
74 #include <fcntl.h>
75 #include <errno.h>
76 #include <string.h>
77 #include <signal.h>
78 
79 #include "test.h"
80 
81 #define TESTFILE	"testfile"	/* file under test */
82 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
83 #define BUF_SIZE	256	/* buffer size */
84 #define FILE_SIZE	1024	/* test file size */
85 #define TRUNC_LEN1	256	/* truncation length */
86 #define TRUNC_LEN2	512	/* truncation length */
87 
88 TCID_DEFINE(truncate02);
89 int TST_TOTAL = 1;		/* Total number of test conditions */
90 int fd;				/* file descriptor of testfile */
91 char tst_buff[BUF_SIZE];	/* buffer to hold testfile contents */
92 
93 void setup();			/* setup function for the test */
94 void cleanup();			/* cleanup function for the test */
95 
main(int ac,char ** av)96 int main(int ac, char **av)
97 {
98 	struct stat stat_buf;	/* stat(2) struct contents */
99 	int lc, i;
100 	off_t file_length2;	/* test file length */
101 	off_t file_length1;	/* test file length */
102 	int rbytes;		/* bytes read from testfile */
103 	int read_len;		/* total no. of bytes read from testfile */
104 	int err_flag = 0;	/* error indicator flag */
105 
106 	tst_parse_opts(ac, av, NULL, NULL);
107 
108 	setup();
109 
110 	for (lc = 0; TEST_LOOPING(lc); lc++) {
111 
112 		tst_count = 0;
113 		read_len = 0;
114 
115 		/*
116 		 * Call truncate(2) to truncate a test file to a
117 		 * specified length (TRUNC_LEN1).
118 		 */
119 		TEST(truncate(TESTFILE, TRUNC_LEN1));
120 
121 		if (TEST_RETURN == -1) {
122 			tst_resm(TFAIL,
123 				 "truncate(%s, %d) Failed, errno=%d : %s",
124 				 TESTFILE, TRUNC_LEN1, TEST_ERRNO,
125 				 strerror(TEST_ERRNO));
126 		} else {
127 			/*
128 			 * Get the testfile information using
129 			 * stat(2).
130 			 */
131 			if (stat(TESTFILE, &stat_buf) < 0) {
132 				tst_brkm(TFAIL, cleanup, "stat(2) of "
133 					 "%s failed after 1st truncate, "
134 					 "error:%d", TESTFILE, errno);
135 			}
136 			file_length1 = stat_buf.st_size;
137 
138 			/*
139 			 * Set the file pointer of testfile to the
140 			 * beginning of the file.
141 			 */
142 			if (lseek(fd, 0, SEEK_SET) < 0) {
143 				tst_brkm(TFAIL, cleanup, "lseek(2) on "
144 					 "%s failed after 1st truncate, "
145 					 "error:%d", TESTFILE, errno);
146 			}
147 
148 			/* Read the testfile from the beginning. */
149 			while ((rbytes = read(fd, tst_buff,
150 					      sizeof(tst_buff))) > 0) {
151 				read_len += rbytes;
152 			}
153 
154 			/*
155 			 * Execute truncate(2) again to truncate
156 			 * testfile to a size TRUNC_LEN2.
157 			 */
158 			TEST(truncate(TESTFILE, TRUNC_LEN2));
159 
160 			if (TEST_RETURN == -1) {
161 				tst_resm(TFAIL, "truncate of %s to "
162 					 "size %d Failed, errno=%d : %s",
163 					 TESTFILE, TRUNC_LEN2,
164 					 TEST_ERRNO,
165 					 strerror(TEST_ERRNO));
166 			}
167 
168 			/*
169 			 * Get the testfile information using
170 			 * stat(2)
171 			 */
172 			if (stat(TESTFILE, &stat_buf) < 0) {
173 				tst_brkm(TFAIL, cleanup, "stat(2) of "
174 					 "%s failed after 2nd truncate, "
175 					 "error:%d", TESTFILE, errno);
176 			}
177 			file_length2 = stat_buf.st_size;
178 
179 			/*
180 			 * Set the file pointer of testfile to the
181 			 * offset TRUNC_LEN1 of testfile.
182 			 */
183 			if (lseek(fd, TRUNC_LEN1, SEEK_SET) < 0) {
184 				tst_brkm(TFAIL, cleanup, "lseek(2) on "
185 					 "%s failed after 2nd truncate, "
186 					 "error:%d", TESTFILE, errno);
187 			}
188 
189 			/* Read the testfile contents till EOF */
190 			while ((rbytes = read(fd, tst_buff,
191 					      sizeof(tst_buff))) > 0) {
192 				for (i = 0; i < rbytes; i++) {
193 					if (tst_buff[i] != 0) {
194 						err_flag++;
195 					}
196 				}
197 			}
198 
199 			/*
200 			 * Check for expected size of testfile after
201 			 * issuing truncate(2) on it.
202 			 */
203 			if ((file_length1 != TRUNC_LEN1) ||
204 			    (file_length2 != TRUNC_LEN2) ||
205 			    (read_len != TRUNC_LEN1) ||
206 			    (err_flag != 0)) {
207 				tst_resm(TFAIL, "Functionality of "
208 					 "truncate(2) on %s Failed",
209 					 TESTFILE);
210 			} else {
211 				tst_resm(TPASS,
212 					 "Functionality of truncate(2) "
213 					 "on %s successful", TESTFILE);
214 			}
215 		}
216 		tst_count++;	/* incr. TEST_LOOP counter */
217 	}
218 
219 	cleanup();
220 	tst_exit();
221 }
222 
223 /*
224  * void
225  * setup() - performs all ONE TIME setup for this test.
226  *  Create a temporary directory and change directory to it.
227  *  Create a test file under temporary directory and write some
228  *  data into it.
229  */
setup(void)230 void setup(void)
231 {
232 	int i;
233 	int wbytes;		/* bytes written to testfile */
234 	int write_len = 0;	/* total no. of bytes written to testfile */
235 
236 	tst_sig(FORK, DEF_HANDLER, cleanup);
237 
238 	/* Pause if that option was specified
239 	 * TEST_PAUSE contains the code to fork the test with the -i option.
240 	 * You want to make sure you do this before you create your temporary
241 	 * directory.
242 	 */
243 	TEST_PAUSE;
244 
245 	tst_tmpdir();
246 
247 	/* Fill the test buffer with the known data */
248 	for (i = 0; i < BUF_SIZE; i++) {
249 		tst_buff[i] = 'a';
250 	}
251 
252 	/* Creat a testfile  and write some data into it */
253 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
254 		tst_brkm(TBROK, cleanup,
255 			 "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s",
256 			 TESTFILE, FILE_MODE, errno, strerror(errno));
257 	}
258 
259 	/* Write to the file 1k data from the buffer */
260 	while (write_len < FILE_SIZE) {
261 		if ((wbytes = write(fd, tst_buff, sizeof(tst_buff))) <= 0) {
262 			tst_brkm(TBROK, cleanup,
263 				 "write(2) on %s Failed, errno=%d : %s",
264 				 TESTFILE, errno, strerror(errno));
265 		} else {
266 			write_len += wbytes;
267 		}
268 	}
269 }
270 
271 /*
272  * void
273  * cleanup() - performs all ONE TIME cleanup for this test at
274  *	       completion or premature exit.
275  *  Close the temporary file opened for reading/writing.
276  *  Remove the test directory and testfile created in the setup.
277  */
cleanup(void)278 void cleanup(void)
279 {
280 
281 	/* Close the testfile after writing data into it */
282 	if (close(fd) == -1) {
283 		tst_brkm(TFAIL, NULL,
284 			 "close(%s) Failed, errno=%d : %s",
285 			 TESTFILE, errno, strerror(errno));
286 	}
287 
288 	tst_rmdir();
289 
290 }
291