plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * Test Name: truncate02 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, truncate(2) succeeds to truncate a file to a certain length, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 25 | * but the attempt to read past the truncated length will fail.$ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * |
| 27 | * Expected Result: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | * shorter, the bytes between the old and new should be all zeroes. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 31 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 44 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 <sys/fcntl.h> |
| 75 | #include <errno.h> |
| 76 | #include <string.h> |
| 77 | #include <signal.h> |
| 78 | |
| 79 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | #define TESTFILE "testfile" /* file under test */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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 */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 88 | TCID_DEFINE(truncate02); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | int TST_TOTAL = 1; /* Total number of test conditions */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | int fd; /* file descriptor of testfile */ |
| 91 | char tst_buff[BUF_SIZE]; /* buffer to hold testfile contents */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
| 93 | void setup(); /* setup function for the test */ |
| 94 | void cleanup(); /* cleanup function for the test */ |
| 95 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | { |
| 98 | struct stat stat_buf; /* stat(2) struct contents */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 99 | int lc, i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 100 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 107 | msg = parse_opts(ac, av, NULL, NULL); |
| 108 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 110 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | } |
| 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | setup(); |
| 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 116 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 117 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 119 | /* |
| 120 | * Call truncate(2) to truncate a test file to a |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | * specified length (TRUNC_LEN1). |
| 122 | */ |
| 123 | TEST(truncate(TESTFILE, TRUNC_LEN1)); |
| 124 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | tst_resm(TFAIL, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | "truncate(%s, %d) Failed, errno=%d : %s", |
| 128 | TESTFILE, TRUNC_LEN1, TEST_ERRNO, |
| 129 | strerror(TEST_ERRNO)); |
| 130 | } else { |
| 131 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 132 | * Get the testfile information using |
| 133 | * stat(2). |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 135 | 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; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 142 | /* |
| 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 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 152 | /* Read the testfile from the beginning. */ |
| 153 | while ((rbytes = read(fd, tst_buff, |
| 154 | sizeof(tst_buff))) > 0) { |
| 155 | read_len += rbytes; |
| 156 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 158 | /* |
| 159 | * Execute truncate(2) again to truncate |
| 160 | * testfile to a size TRUNC_LEN2. |
| 161 | */ |
| 162 | TEST(truncate(TESTFILE, TRUNC_LEN2)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 164 | if (TEST_RETURN == -1) { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 165 | 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 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 172 | /* |
| 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; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 183 | /* |
| 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 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 193 | /* 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++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 201 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 203 | /* |
| 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 215 | tst_resm(TPASS, |
| 216 | "Functionality of truncate(2) " |
| 217 | "on %s successful", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 220 | tst_count++; /* incr. TEST_LOOP counter */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 221 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 224 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 225 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 226 | |
| 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 234 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 235 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 236 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 237 | int wbytes; /* bytes written to testfile */ |
| 238 | int write_len = 0; /* total no. of bytes written to testfile */ |
| 239 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 240 | 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 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 249 | tst_tmpdir(); |
| 250 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 251 | /* 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 257 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 258 | tst_brkm(TBROK, cleanup, |
| 259 | "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s", |
| 260 | TESTFILE, FILE_MODE, errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 261 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 262 | |
| 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 273 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 274 | |
| 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 282 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 283 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 284 | |
| 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 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 292 | tst_rmdir(); |
| 293 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 294 | } |