blob: 82fdfcc133313a0b9bfcaf006775dcb671156748 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
subrata_modak4bb656a2009-02-26 12:02:09 +000020/*
plars865695b2001-08-27 22:15:12 +000021 * Test Name: truncate02
22 *
23 * Test Description:
24 * Verify that, truncate(2) succeeds to truncate a file to a certain length,
subrata_modak56207ce2009-03-23 13:35:39 +000025 * but the attempt to read past the truncated length will fail.$
plars865695b2001-08-27 22:15:12 +000026 *
27 * Expected Result:
subrata_modak4bb656a2009-02-26 12:02:09 +000028 * 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
plars865695b2001-08-27 22:15:12 +000030 * shorter, the bytes between the old and new should be all zeroes.
subrata_modakbdbaec52009-02-26 12:14:51 +000031 *
plars865695b2001-08-27 22:15:12 +000032 * 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,
subrata_modakbdbaec52009-02-26 12:14:51 +000044 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000045 * 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 <sys/fcntl.h>
75#include <errno.h>
76#include <string.h>
77#include <signal.h>
78
79#include "test.h"
plars865695b2001-08-27 22:15:12 +000080
subrata_modak56207ce2009-03-23 13:35:39 +000081#define TESTFILE "testfile" /* file under test */
plars865695b2001-08-27 22:15:12 +000082#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
subrata_modak56207ce2009-03-23 13:35:39 +000083#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 */
plars865695b2001-08-27 22:15:12 +000087
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020088TCID_DEFINE(truncate02);
subrata_modak56207ce2009-03-23 13:35:39 +000089int TST_TOTAL = 1; /* Total number of test conditions */
plars865695b2001-08-27 22:15:12 +000090int fd; /* file descriptor of testfile */
91char tst_buff[BUF_SIZE]; /* buffer to hold testfile contents */
plars865695b2001-08-27 22:15:12 +000092
93void setup(); /* setup function for the test */
94void cleanup(); /* cleanup function for the test */
95
subrata_modak56207ce2009-03-23 13:35:39 +000096int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000097{
98 struct stat stat_buf; /* stat(2) struct contents */
Cyril Hrubis89af32a2012-10-24 16:39:11 +020099 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200100 const char *msg;
plars865695b2001-08-27 22:15:12 +0000101 off_t file_length2; /* test file length */
102 off_t file_length1; /* test file length */
103 int rbytes; /* bytes read from testfile */
104 int read_len = 0; /* total no. of bytes read from testfile */
105 int err_flag = 0; /* error indicator flag */
subrata_modak56207ce2009-03-23 13:35:39 +0000106
Garrett Cooper45e285d2010-11-22 12:19:25 -0800107 msg = parse_opts(ac, av, NULL, NULL);
108 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000109 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800110
plars865695b2001-08-27 22:15:12 +0000111 }
112
plars865695b2001-08-27 22:15:12 +0000113 setup();
114
plars865695b2001-08-27 22:15:12 +0000115 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800116
Caspar Zhangd59a6592013-03-07 14:59:12 +0800117 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000118
subrata_modak4bb656a2009-02-26 12:02:09 +0000119 /*
120 * Call truncate(2) to truncate a test file to a
plars865695b2001-08-27 22:15:12 +0000121 * specified length (TRUNC_LEN1).
122 */
123 TEST(truncate(TESTFILE, TRUNC_LEN1));
124
plars865695b2001-08-27 22:15:12 +0000125 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000126 tst_resm(TFAIL,
plars865695b2001-08-27 22:15:12 +0000127 "truncate(%s, %d) Failed, errno=%d : %s",
128 TESTFILE, TRUNC_LEN1, TEST_ERRNO,
129 strerror(TEST_ERRNO));
130 } else {
131 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200132 * Get the testfile information using
133 * stat(2).
plars865695b2001-08-27 22:15:12 +0000134 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200135 if (stat(TESTFILE, &stat_buf) < 0) {
136 tst_brkm(TFAIL, cleanup, "stat(2) of "
137 "%s failed after 1st truncate, "
138 "error:%d", TESTFILE, errno);
139 }
140 file_length1 = stat_buf.st_size;
plars865695b2001-08-27 22:15:12 +0000141
Cyril Hrubise38b9612014-06-02 17:20:57 +0200142 /*
143 * Set the file pointer of testfile to the
144 * beginning of the file.
145 */
146 if (lseek(fd, 0, SEEK_SET) < 0) {
147 tst_brkm(TFAIL, cleanup, "lseek(2) on "
148 "%s failed after 1st truncate, "
149 "error:%d", TESTFILE, errno);
150 }
plars865695b2001-08-27 22:15:12 +0000151
Cyril Hrubise38b9612014-06-02 17:20:57 +0200152 /* Read the testfile from the beginning. */
153 while ((rbytes = read(fd, tst_buff,
154 sizeof(tst_buff))) > 0) {
155 read_len += rbytes;
156 }
plars865695b2001-08-27 22:15:12 +0000157
Cyril Hrubise38b9612014-06-02 17:20:57 +0200158 /*
159 * Execute truncate(2) again to truncate
160 * testfile to a size TRUNC_LEN2.
161 */
162 TEST(truncate(TESTFILE, TRUNC_LEN2));
plars865695b2001-08-27 22:15:12 +0000163
Cyril Hrubise38b9612014-06-02 17:20:57 +0200164 if (TEST_RETURN == -1) {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200165 tst_resm(TFAIL, "truncate of %s to "
166 "size %d Failed, errno=%d : %s",
167 TESTFILE, TRUNC_LEN2,
168 TEST_ERRNO,
169 strerror(TEST_ERRNO));
170 }
plars865695b2001-08-27 22:15:12 +0000171
Cyril Hrubise38b9612014-06-02 17:20:57 +0200172 /*
173 * Get the testfile information using
174 * stat(2)
175 */
176 if (stat(TESTFILE, &stat_buf) < 0) {
177 tst_brkm(TFAIL, cleanup, "stat(2) of "
178 "%s failed after 2nd truncate, "
179 "error:%d", TESTFILE, errno);
180 }
181 file_length2 = stat_buf.st_size;
plars865695b2001-08-27 22:15:12 +0000182
Cyril Hrubise38b9612014-06-02 17:20:57 +0200183 /*
184 * Set the file pointer of testfile to the
185 * offset TRUNC_LEN1 of testfile.
186 */
187 if (lseek(fd, TRUNC_LEN1, SEEK_SET) < 0) {
188 tst_brkm(TFAIL, cleanup, "lseek(2) on "
189 "%s failed after 2nd truncate, "
190 "error:%d", TESTFILE, errno);
191 }
plars865695b2001-08-27 22:15:12 +0000192
Cyril Hrubise38b9612014-06-02 17:20:57 +0200193 /* Read the testfile contents till EOF */
194 while ((rbytes = read(fd, tst_buff,
195 sizeof(tst_buff))) > 0) {
196 for (i = 0; i < rbytes; i++) {
197 if (tst_buff[i] != 0) {
198 err_flag++;
plars865695b2001-08-27 22:15:12 +0000199 }
200 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200201 }
plars865695b2001-08-27 22:15:12 +0000202
Cyril Hrubise38b9612014-06-02 17:20:57 +0200203 /*
204 * Check for expected size of testfile after
205 * issuing truncate(2) on it.
206 */
207 if ((file_length1 != TRUNC_LEN1) ||
208 (file_length2 != TRUNC_LEN2) ||
209 (read_len != TRUNC_LEN1) ||
210 (err_flag != 0)) {
211 tst_resm(TFAIL, "Functionality of "
212 "truncate(2) on %s Failed",
213 TESTFILE);
plars865695b2001-08-27 22:15:12 +0000214 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200215 tst_resm(TPASS,
216 "Functionality of truncate(2) "
217 "on %s successful", TESTFILE);
plars865695b2001-08-27 22:15:12 +0000218 }
219 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800220 tst_count++; /* incr. TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800221 }
plars865695b2001-08-27 22:15:12 +0000222
plars865695b2001-08-27 22:15:12 +0000223 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800224 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800225}
plars865695b2001-08-27 22:15:12 +0000226
227/*
228 * void
229 * setup() - performs all ONE TIME setup for this test.
230 * Create a temporary directory and change directory to it.
231 * Create a test file under temporary directory and write some
232 * data into it.
233 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400234void setup(void)
plars865695b2001-08-27 22:15:12 +0000235{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200236 int i;
plars865695b2001-08-27 22:15:12 +0000237 int wbytes; /* bytes written to testfile */
238 int write_len = 0; /* total no. of bytes written to testfile */
239
plars865695b2001-08-27 22:15:12 +0000240 tst_sig(FORK, DEF_HANDLER, cleanup);
241
242 /* Pause if that option was specified
243 * TEST_PAUSE contains the code to fork the test with the -i option.
244 * You want to make sure you do this before you create your temporary
245 * directory.
246 */
247 TEST_PAUSE;
248
plars865695b2001-08-27 22:15:12 +0000249 tst_tmpdir();
250
plars865695b2001-08-27 22:15:12 +0000251 /* Fill the test buffer with the known data */
252 for (i = 0; i < BUF_SIZE; i++) {
253 tst_buff[i] = 'a';
254 }
255
256 /* Creat a testfile and write some data into it */
subrata_modak56207ce2009-03-23 13:35:39 +0000257 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
plars865695b2001-08-27 22:15:12 +0000258 tst_brkm(TBROK, cleanup,
259 "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s",
260 TESTFILE, FILE_MODE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800261 }
plars865695b2001-08-27 22:15:12 +0000262
263 /* Write to the file 1k data from the buffer */
264 while (write_len < FILE_SIZE) {
265 if ((wbytes = write(fd, tst_buff, sizeof(tst_buff))) <= 0) {
266 tst_brkm(TBROK, cleanup,
267 "write(2) on %s Failed, errno=%d : %s",
268 TESTFILE, errno, strerror(errno));
269 } else {
270 write_len += wbytes;
271 }
272 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800273}
plars865695b2001-08-27 22:15:12 +0000274
275/*
276 * void
277 * cleanup() - performs all ONE TIME cleanup for this test at
278 * completion or premature exit.
279 * Close the temporary file opened for reading/writing.
280 * Remove the test directory and testfile created in the setup.
281 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400282void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000283{
plars865695b2001-08-27 22:15:12 +0000284
285 /* Close the testfile after writing data into it */
286 if (close(fd) == -1) {
287 tst_brkm(TFAIL, NULL,
288 "close(%s) Failed, errno=%d : %s",
289 TESTFILE, errno, strerror(errno));
290 }
291
plars865695b2001-08-27 22:15:12 +0000292 tst_rmdir();
293
Chris Dearmanec6edca2012-10-17 19:54:01 -0700294}