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: truncate01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, truncate(2) succeeds to truncate a file to a specified |
| 25 | * length. |
| 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 length of the file after |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | * truncation should be equal to the length it is truncated to. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 30 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | * Algorithm: |
| 32 | * Setup: |
| 33 | * Setup signal handling. |
| 34 | * Create temporary directory. |
| 35 | * Pause for SIGUSR1 if option specified. |
| 36 | * |
| 37 | * Test: |
| 38 | * Loop if the proper options are given. |
| 39 | * Execute system call |
| 40 | * Check return code, if system call failed (return=-1) |
| 41 | * Log the errno and Issue a FAIL message. |
| 42 | * Otherwise, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 43 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * if successful, |
| 45 | * Issue Functionality-Pass message. |
| 46 | * Otherwise, |
| 47 | * Issue Functionality-Fail message. |
| 48 | * Cleanup: |
| 49 | * Print errno log and/or timing stats if options given |
| 50 | * Delete the temporary directory created. |
| 51 | * |
| 52 | * Usage: <for command-line> |
| 53 | * truncate01 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t] |
| 54 | * where, -c n : Run n copies concurrently. |
| 55 | * -e : Turn on errno logging. |
| 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 John George |
| 64 | * -Ported |
| 65 | * |
| 66 | * Restrictions: |
| 67 | * This test should be run by 'non-super-user' only. |
| 68 | * |
| 69 | */ |
| 70 | |
| 71 | #include <sys/types.h> |
| 72 | #include <sys/stat.h> |
| 73 | #include <sys/fcntl.h> |
| 74 | #include <errno.h> |
| 75 | #include <string.h> |
| 76 | #include <signal.h> |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 77 | #include <inttypes.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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_LEN 256 /* truncation length */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 87 | TCID_DEFINE(truncate01); |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 88 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
| 90 | void setup(); /* setup function for the test */ |
| 91 | void cleanup(); /* cleanup function for the test */ |
| 92 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | { |
| 95 | struct stat stat_buf; /* stat(2) struct contents */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 96 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 97 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | off_t file_length; /* test file length */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 100 | msg = parse_opts(ac, av, NULL, NULL); |
| 101 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 103 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | } |
| 105 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | setup(); |
| 107 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 109 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 110 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 112 | /* |
| 113 | * Call truncate(2) to truncate a test file to a |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | * specified length. |
| 115 | */ |
| 116 | TEST(truncate(TESTFILE, TRUNC_LEN)); |
| 117 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | if (TEST_RETURN == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | tst_resm(TFAIL, |
| 120 | "truncate(%s, %d) Failed, errno=%d : %s", |
| 121 | TESTFILE, TRUNC_LEN, TEST_ERRNO, |
| 122 | strerror(TEST_ERRNO)); |
| 123 | } else { |
| 124 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 125 | * Get the testfile information using |
| 126 | * stat(2). |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 128 | if (stat(TESTFILE, &stat_buf) < 0) { |
| 129 | tst_brkm(TFAIL, cleanup, "stat(2) of " |
| 130 | "%s failed, error:%d", |
| 131 | TESTFILE, errno); |
| 132 | } |
| 133 | stat_buf.st_mode &= ~S_IFREG; |
| 134 | file_length = stat_buf.st_size; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 136 | /* |
| 137 | * Check for expected size of testfile after |
| 138 | * truncate(2) on it. |
| 139 | */ |
| 140 | if (file_length != TRUNC_LEN) { |
| 141 | tst_resm(TFAIL, "%s: Incorrect file " |
| 142 | "size %" PRId64 |
| 143 | ", Expected %d", TESTFILE, |
| 144 | (int64_t) file_length, |
| 145 | TRUNC_LEN); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 147 | tst_resm(TPASS, "Functionality of " |
| 148 | "truncate(%s, %d) successful", |
| 149 | TESTFILE, TRUNC_LEN); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 152 | tst_count++; /* incr TEST_LOOP counter */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 153 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 156 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 157 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * void |
| 161 | * setup() - performs all ONE TIME setup for this test. |
| 162 | * Create a temporary directory and change directory to it. |
| 163 | * Fill the buffer with some arbitrary data to be written to a file. |
| 164 | * Create a test file under temporary directory and close it |
| 165 | * write arbitrary data into testfile. |
| 166 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 167 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | int fd, i; /* file handler for testfile */ |
| 170 | int c, c_total = 0; /* no. bytes to be written to file */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | char tst_buff[BUF_SIZE]; /* buffer to hold data */ |
| 172 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 174 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | /* Pause if that option was specified |
| 176 | * TEST_PAUSE contains the code to fork the test with the -i option. |
| 177 | * You want to make sure you do this before you create your temporary |
| 178 | * directory. |
| 179 | */ |
| 180 | TEST_PAUSE; |
| 181 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | tst_tmpdir(); |
| 183 | |
| 184 | /* Fill the test buffer with the known data */ |
| 185 | for (i = 0; i < BUF_SIZE; i++) { |
| 186 | tst_buff[i] = 'a'; |
| 187 | } |
| 188 | |
| 189 | /* Creat a testfile under temporary directory */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | tst_brkm(TBROK, cleanup, |
| 192 | "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s", |
| 193 | TESTFILE, FILE_MODE, errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 194 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | |
| 196 | /* Write to the file 1k data from the buffer */ |
| 197 | while (c_total < FILE_SIZE) { |
| 198 | if ((c = write(fd, tst_buff, sizeof(tst_buff))) <= 0) { |
| 199 | tst_brkm(TBROK, cleanup, |
| 200 | "write(2) on %s Failed, errno=%d : %s", |
| 201 | TESTFILE, errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 202 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | c_total += c; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /* Close the testfile after writing data into it */ |
| 208 | if (close(fd) == -1) { |
| 209 | tst_brkm(TBROK, cleanup, |
| 210 | "close(%s) Failed, errno=%d : %s", |
| 211 | TESTFILE, errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 212 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 213 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * void |
| 217 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 218 | * completion or premature exit. |
| 219 | * Remove the test directory and testfile created in the setup. |
| 220 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 221 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 224 | tst_rmdir(); |
| 225 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 226 | } |