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: ftruncate02
22 *
23 * Test Description:
24 * Verify that, ftruncate(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 * ftruncate(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 * ftruncate02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
55 * where, -c n : Run n copies concurrently.
56 * -f : Turn off functionality Testing.
57 * -i n : Execute test n times.
58 * -I x : Execute test for x seconds.
59 * -P x : Pause for x seconds between iterations.
60 * -t : Turn on syscall timing.
61 *
62 * HISTORY
63 * 07/2001 Ported by Wayne Boyer
64 *
65 * RESTRICTIONS:
66 *
67 */
68
69 #include <stdio.h>
70 #include <sys/types.h>
71 #include <sys/stat.h>
72 #include <fcntl.h>
73 #include <errno.h>
74 #include <string.h>
75 #include <signal.h>
76
77 #include "test.h"
78 #include "safe_macros.h"
79
80 #define TESTFILE "testfile" /* file under test */
81 #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
82 #define BUF_SIZE 256 /* buffer size */
83 #define FILE_SIZE 1024 /* test file size */
84 #define TRUNC_LEN1 256 /* truncation length */
85 #define TRUNC_LEN2 512 /* truncation length */
86
87 TCID_DEFINE(ftruncate02);
88 int TST_TOTAL = 1; /* Total number of test conditions */
89 int fd; /* file descriptor of testfile */
90 char tst_buff[BUF_SIZE]; /* buffer to hold testfile contents */
91
92 void setup(); /* setup function for the test */
93 void cleanup(); /* cleanup function for the test */
94
main(int ac,char ** av)95 int main(int ac, char **av)
96 {
97 struct stat stat_buf; /* stat(2) struct contents */
98 int lc;
99 off_t file_length2; /* test file length */
100 off_t file_length1; /* test file length */
101 int rbytes, i; /* bytes read from testfile */
102 int read_len; /* total no. of bytes read from testfile */
103 int err_flag = 0; /* error indicator flag */
104
105 tst_parse_opts(ac, av, NULL, NULL);
106
107 setup();
108
109 for (lc = 0; TEST_LOOPING(lc); lc++) {
110
111 tst_count = 0;
112 read_len = 0;
113
114 /*
115 * Call ftruncate(2) to truncate a test file to a
116 * specified length (TRUNC_LEN1).
117 */
118 TEST(ftruncate(fd, TRUNC_LEN1));
119
120 if (TEST_RETURN == -1) {
121 tst_resm(TFAIL | TTERRNO,
122 "ftruncate(%s) to size %d failed", TESTFILE,
123 TRUNC_LEN1);
124 continue;
125 }
126 /*
127 * Get the testfile information using
128 * fstat(2).
129 */
130 if (fstat(fd, &stat_buf) < 0) {
131 tst_brkm(TFAIL, cleanup, "fstat(2) of %s failed"
132 " after 1st truncate, error:%d",
133 TESTFILE, errno);
134 }
135 stat_buf.st_mode &= ~S_IFREG;
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 %s failed"
144 " after 1st ftruncate, error:%d",
145 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 ftruncate(2) again to truncate
156 * testfile to a size TRUNC_LEN2.
157 */
158 TEST(ftruncate(fd, TRUNC_LEN2));
159
160 /*
161 * Get the testfile information using
162 * fstat(2)
163 */
164 if (fstat(fd, &stat_buf) < 0) {
165 tst_brkm(TFAIL, cleanup, "fstat(2) of %s failed"
166 " after 2nd truncate, error:%d",
167 TESTFILE, errno);
168 }
169 stat_buf.st_mode &= ~S_IFREG;
170 file_length2 = stat_buf.st_size;
171
172 /*
173 * Set the file pointer of testfile to the
174 * offset TRUNC_LEN1 of testfile.
175 */
176 if (lseek(fd, TRUNC_LEN1, SEEK_SET) < 0) {
177 tst_brkm(TFAIL, cleanup, "lseek(2) on %s failed"
178 " after 2nd ftruncate, error:%d",
179 TESTFILE, errno);
180 }
181
182 /* Read the testfile contents till EOF */
183 while ((rbytes = read(fd, tst_buff,
184 sizeof(tst_buff))) > 0) {
185 for (i = 0; i < rbytes; i++) {
186 if (tst_buff[i] != 0) {
187 err_flag++;
188 }
189 }
190 }
191
192 /*
193 * Check for expected size of testfile after
194 * issuing ftruncate(2) on it. If the ftruncate(2)
195 * to a smaller file passed, then check to see
196 * if file size was increased. If the ftruncate(2)
197 * to a smaller file failed, then don't check.
198 * Both results are allowed according to the SUS.
199 */
200
201 if (TEST_RETURN != -1) {
202 if ((file_length1 != TRUNC_LEN1) ||
203 (file_length2 != TRUNC_LEN2) ||
204 (read_len != TRUNC_LEN1) ||
205 (err_flag != 0)) {
206 tst_resm(TFAIL,
207 "Functionality of ftruncate(2) "
208 "on %s Failed", TESTFILE);
209 } else {
210 tst_resm(TPASS,
211 "Functionality of ftruncate(2) "
212 "on %s successful", TESTFILE);
213 }
214 }
215 if (TEST_RETURN == -1) {
216 if ((file_length1 != TRUNC_LEN1) ||
217 (read_len != TRUNC_LEN1) ||
218 (err_flag != 0)) {
219 tst_resm(TFAIL,
220 "Functionality of ftruncate(2) "
221 "on %s Failed", TESTFILE);
222 } else {
223 tst_resm(TPASS,
224 "Functionality of ftruncate(2) "
225 "on %s successful", TESTFILE);
226 }
227 }
228 }
229
230 cleanup();
231 tst_exit();
232 }
233
234 /*
235 * void
236 * setup() - performs all ONE TIME setup for this test.
237 * Create a temporary directory and change directory to it.
238 * Create a test file under temporary directory and write some
239 * data into it.
240 */
setup(void)241 void setup(void)
242 {
243 int i;
244 int wbytes; /* bytes written to testfile */
245 int write_len = 0; /* total no. of bytes written to testfile */
246
247 tst_sig(NOFORK, DEF_HANDLER, cleanup);
248
249 TEST_PAUSE;
250
251 tst_tmpdir();
252
253 /* Fill the test buffer with the known data */
254 for (i = 0; i < BUF_SIZE; i++) {
255 tst_buff[i] = 'a';
256 }
257 /* open a file for reading/writing */
258 fd = SAFE_OPEN(cleanup, TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
259
260 /* Write to the file 1k data from the buffer */
261 while (write_len < FILE_SIZE) {
262 if ((wbytes = write(fd, tst_buff, sizeof(tst_buff))) <= 0) {
263 tst_brkm(TBROK, cleanup, "write(%s) failed", TESTFILE);
264 } else {
265 write_len += wbytes;
266 }
267 }
268 }
269
270 /*
271 * void
272 * cleanup() - performs all ONE TIME cleanup for this test at
273 * completion or premature exit.
274 * Close the testfile.
275 * Remove the test directory and testfile created in the setup.
276 */
cleanup(void)277 void cleanup(void)
278 {
279
280 /* Close the testfile after writing data into it */
281 if (close(fd) == -1)
282 tst_brkm(TFAIL | TERRNO, NULL, "close(%s) failed", TESTFILE);
283
284 tst_rmdir();
285
286 }
287