3 * Copyright (c) International Business Machines Corp., 2001
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Test Name: truncate02
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.
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.
34 * Setup signal handling.
35 * Create temporary directory.
36 * Pause for SIGUSR1 if option specified.
39 * Loop if the proper options are given.
41 * Check return code, if system call failed (return=-1)
42 * Log the errno and Issue a FAIL message.
44 * Verify the Functionality of system call
46 * Issue Functionality-Pass message.
48 * Issue Functionality-Fail message.
50 * Print errno log and/or timing stats if options given
51 * Delete the temporary directory created.
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.
68 * This test should be run by 'non-super-user' only.
72 #include <sys/types.h>
74 #include <sys/fcntl.h>
82 #define TESTFILE "testfile" /* file under test */
83 #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
84 #define BUF_SIZE 256 /* buffer size */
85 #define FILE_SIZE 1024 /* test file size */
86 #define TRUNC_LEN1 256 /* truncation length */
87 #define TRUNC_LEN2 512 /* truncation length */
89 const char *TCID="truncate02"; /* Test program identifier. */
90 int TST_TOTAL=1; /* Total number of test conditions */
91 extern int Tst_count; /* Test Case counter for tst_* routines */
92 int fd; /* file descriptor of testfile */
93 char tst_buff[BUF_SIZE]; /* buffer to hold testfile contents */
96 void setup(); /* setup function for the test */
97 void cleanup(void) __attribute__((noreturn)); /* cleanup function for the test */
100 main(int ac, char **av)
102 struct stat stat_buf; /* stat(2) struct contents */
103 int lc, i; /* loop counter */
104 const char *msg; /* message returned from parse_opts */
105 off_t file_length2; /* test file length */
106 off_t file_length1; /* test file length */
107 int rbytes; /* bytes read from testfile */
108 int read_len = 0; /* total no. of bytes read from testfile */
109 int err_flag = 0; /* error indicator flag */
111 /* Parse standard options given to run the test. */
112 msg = parse_opts(ac, av, (option_t *) NULL, NULL);
113 if (msg != (char *) NULL) {
114 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
118 /* Perform global setup for test */
121 /* set the expected errnos... */
122 TEST_EXP_ENOS(exp_enos);
124 /* Check looping state if -i option given */
125 for (lc = 0; TEST_LOOPING(lc); lc++) {
126 /* Reset Tst_count in case we are looping. */
130 * Call truncate(2) to truncate a test file to a
131 * specified length (TRUNC_LEN1).
133 TEST(truncate(TESTFILE, TRUNC_LEN1));
135 /* check return code of truncate(2) */
136 if (TEST_RETURN == -1) {
137 TEST_ERROR_LOG(TEST_ERRNO);
139 "truncate(%s, %d) Failed, errno=%d : %s",
140 TESTFILE, TRUNC_LEN1, TEST_ERRNO,
141 strerror(TEST_ERRNO));
144 * Perform functional verification if test
145 * executed without (-f) option.
147 if (STD_FUNCTIONAL_TEST) {
149 * Get the testfile information using
152 if (stat(TESTFILE, &stat_buf) < 0) {
153 tst_brkm(TFAIL, cleanup, "stat(2) of "
154 "%s failed after 1st truncate, "
155 "error:%d", TESTFILE, errno);
158 file_length1 = stat_buf.st_size;
161 * Set the file pointer of testfile to the
162 * beginning of the file.
164 if (lseek(fd, 0, SEEK_SET) < 0) {
165 tst_brkm(TFAIL, cleanup, "lseek(2) on "
166 "%s failed after 1st truncate, "
167 "error:%d", TESTFILE, errno);
171 /* Read the testfile from the beginning. */
172 while ((rbytes = read(fd, tst_buff, \
173 sizeof(tst_buff))) > 0) {
178 * Execute truncate(2) again to truncate
179 * testfile to a size TRUNC_LEN2.
181 TEST(truncate(TESTFILE, TRUNC_LEN2));
183 /* check return code of truncate(2) */
184 if (TEST_RETURN == -1) {
185 TEST_ERROR_LOG(TEST_ERRNO);
186 tst_resm(TFAIL, "truncate of %s to "
187 "size %d Failed, errno=%d : %s",
188 TESTFILE, TRUNC_LEN2,
190 strerror(TEST_ERRNO));
194 * Get the testfile information using
197 if (stat(TESTFILE, &stat_buf) < 0) {
198 tst_brkm(TFAIL, cleanup, "stat(2) of "
199 "%s failed after 2nd truncate, "
200 "error:%d", TESTFILE, errno);
203 file_length2 = stat_buf.st_size;
206 * Set the file pointer of testfile to the
207 * offset TRUNC_LEN1 of testfile.
209 if (lseek(fd, TRUNC_LEN1, SEEK_SET) < 0) {
210 tst_brkm(TFAIL, cleanup, "lseek(2) on "
211 "%s failed after 2nd truncate, "
212 "error:%d", TESTFILE, errno);
216 /* Read the testfile contents till EOF */
217 while((rbytes = read(fd, tst_buff, \
218 sizeof(tst_buff))) > 0) {
219 for (i = 0; i < rbytes; i++) {
220 if (tst_buff[i] != 0) {
227 * Check for expected size of testfile after
228 * issuing truncate(2) on it.
230 if ((file_length1 != TRUNC_LEN1) || \
231 (file_length2 != TRUNC_LEN2) || \
232 (read_len != TRUNC_LEN1) || \
234 tst_resm(TFAIL, "Functionality of "
235 "truncate(2) on %s Failed",
239 "Functionality of truncate(2) "
240 "on %s successful", TESTFILE);
243 tst_resm(TPASS, "%s call succeeded", TCID);
246 Tst_count++; /* incr. TEST_LOOP counter */
247 } /* End for TEST_LOOPING */
249 /* Call cleanup() to undo setup done for the test. */
257 * setup() - performs all ONE TIME setup for this test.
258 * Create a temporary directory and change directory to it.
259 * Create a test file under temporary directory and write some
265 int i; /* counter variable */
266 int wbytes; /* bytes written to testfile */
267 int write_len = 0; /* total no. of bytes written to testfile */
269 /* capture signals */
270 tst_sig(FORK, DEF_HANDLER, cleanup);
272 /* Pause if that option was specified
273 * TEST_PAUSE contains the code to fork the test with the -i option.
274 * You want to make sure you do this before you create your temporary
279 /* make a temp directory and cd to it */
283 /* Fill the test buffer with the known data */
284 for (i = 0; i < BUF_SIZE; i++) {
288 /* Creat a testfile and write some data into it */
289 if ((fd = open(TESTFILE, O_RDWR|O_CREAT, FILE_MODE)) == -1) {
290 tst_brkm(TBROK, cleanup,
291 "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s",
292 TESTFILE, FILE_MODE, errno, strerror(errno));
296 /* Write to the file 1k data from the buffer */
297 while (write_len < FILE_SIZE) {
298 if ((wbytes = write(fd, tst_buff, sizeof(tst_buff))) <= 0) {
299 tst_brkm(TBROK, cleanup,
300 "write(2) on %s Failed, errno=%d : %s",
301 TESTFILE, errno, strerror(errno));
310 * cleanup() - performs all ONE TIME cleanup for this test at
311 * completion or premature exit.
312 * Close the temporary file opened for reading/writing.
313 * Remove the test directory and testfile created in the setup.
319 * print timing stats if that option was specified.
323 /* Close the testfile after writing data into it */
324 if (close(fd) == -1) {
325 tst_brkm(TFAIL, NULL,
326 "close(%s) Failed, errno=%d : %s",
327 TESTFILE, errno, strerror(errno));
330 /* Remove tmp dir and all files in it */
333 /* exit with return code appropriate for results */
335 } /* End cleanup() */