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: ftruncate01 22 * 23 * Test Description: 24 * Verify that, ftruncate(2) succeeds to truncate a file to a specified 25 * length if the file indicated by file descriptor opened for writing. 26 * 27 * Expected Result: 28 * ftruncate(2) should return a value 0 and the length of the file after 29 * truncation should be equal to the length it is truncated to. 30 * 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, 43 * Verify the Functionality of system call 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 * ftruncate01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] 54 * where, -c n : Run n copies concurrently. 55 * -f : Turn off functionality Testing. 56 * -i n : Execute test n times. 57 * -I x : Execute test for x seconds. 58 * -P x : Pause for x seconds between iterations. 59 * -t : Turn on syscall timing. 60 * 61 * HISTORY 62 * 07/2001 Ported by Wayne Boyer 63 * 64 * RESTRICTIONS: 65 * This test should be run by 'non-super-user' only. 66 * 67 */ 68 69 #include <sys/types.h> 70 #include <sys/stat.h> 71 #include <fcntl.h> 72 #include <errno.h> 73 #include <string.h> 74 #include <signal.h> 75 #include <inttypes.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_LEN 256 /* truncation length */ 85 86 TCID_DEFINE(ftruncate01); 87 int TST_TOTAL = 1; /* Total number of test conditions */ 88 int fildes; /* file descriptor for test file */ 89 90 void setup(); /* setup function for the test */ 91 void cleanup(); /* cleanup function for the test */ 92 93 int main(int ac, char **av) 94 { 95 struct stat stat_buf; /* stat(2) struct contents */ 96 int lc; 97 off_t file_length; /* test file length */ 98 99 tst_parse_opts(ac, av, NULL, NULL); 100 101 setup(); 102 103 for (lc = 0; TEST_LOOPING(lc); lc++) { 104 105 tst_count = 0; 106 107 /* 108 * Call ftruncate(2) to truncate a test file to a 109 * specified length. 110 */ 111 TEST(ftruncate(fildes, TRUNC_LEN)); 112 113 if (TEST_RETURN == -1) { 114 tst_resm(TFAIL | TTERRNO, "ftruncate(%s) failed", 115 TESTFILE); 116 continue; 117 } 118 /* 119 * Get the testfile information using 120 * fstat(2). 121 */ 122 if (fstat(fildes, &stat_buf) < 0) { 123 tst_brkm(TFAIL, cleanup, 124 "stat(2) of %s failed, error:%d", 125 TESTFILE, errno); 126 } 127 stat_buf.st_mode &= ~S_IFREG; 128 file_length = stat_buf.st_size; 129 130 /* 131 * Check for expected size of testfile after 132 * truncate(2) on it. 133 */ 134 if (file_length != TRUNC_LEN) { 135 tst_resm(TFAIL, 136 "%s: Incorrect file size %" PRId64 ", " 137 "Expected %d", TESTFILE, 138 (int64_t) file_length, TRUNC_LEN); 139 } else { 140 tst_resm(TPASS, "Functionality of ftruncate() " 141 "on %s successful", TESTFILE); 142 } 143 } 144 145 cleanup(); 146 tst_exit(); 147 } 148 149 /* 150 * void 151 * setup() - performs all ONE TIME setup for this test. 152 * Create a temporary directory and change directory to it. 153 * Create a test file under temporary directory and write some 154 * data into it. 155 */ 156 void setup(void) 157 { 158 int i; 159 int c, c_total = 0; /* bytes to be written to file */ 160 char tst_buff[BUF_SIZE]; /* buffer to hold data */ 161 162 tst_sig(NOFORK, DEF_HANDLER, cleanup); 163 164 TEST_PAUSE; 165 166 tst_tmpdir(); 167 168 /* Fill the test buffer with the known data */ 169 for (i = 0; i < BUF_SIZE; i++) { 170 tst_buff[i] = 'a'; 171 } 172 173 /* open a file for reading/writing */ 174 fildes = SAFE_OPEN(cleanup, TESTFILE, O_RDWR | O_CREAT, FILE_MODE); 175 176 /* Write to the file 1k data from the buffer */ 177 while (c_total < FILE_SIZE) { 178 if ((c = write(fildes, tst_buff, sizeof(tst_buff))) <= 0) { 179 tst_brkm(TBROK | TERRNO, cleanup, "write(%s) failed", 180 TESTFILE); 181 } else { 182 c_total += c; 183 } 184 } 185 } 186 187 /* 188 * void 189 * cleanup() - performs all ONE TIME cleanup for this test at 190 * completion or premature exit. 191 * Close the temporary file. 192 * Remove the test directory and testfile created in the setup. 193 */ 194 void cleanup(void) 195 { 196 197 /* Close the testfile after writing data into it */ 198 if (close(fildes) == -1) 199 tst_brkm(TFAIL | TERRNO, NULL, "close(%s) failed", TESTFILE); 200 201 tst_rmdir(); 202 203 } 204