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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: llseek02 |
robbiew | 6a6069e | 2003-03-14 16:15:19 +0000 | [diff] [blame^] | 22 | * Note that glibc exports the llseek syscall as lseek64. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * Test Description: |
| 25 | * Verify that, |
| 26 | * 1. llseek() returns -1 and sets errno to EINVAL, if the 'Whence' argument |
| 27 | * is not a proper value. |
| 28 | * 2. llseek() returns -1 and sets errno to EBADF, if the file handle of |
| 29 | * the specified file is not valid. |
| 30 | * |
| 31 | * Expected Result: |
| 32 | * llseek() should fail with return value -1 and set expected errno. |
| 33 | * |
| 34 | * Algorithm: |
| 35 | * Setup: |
| 36 | * Setup signal handling. |
| 37 | * Create temporary directory. |
| 38 | * Pause for SIGUSR1 if option specified. |
| 39 | * |
| 40 | * Test: |
| 41 | * Loop if the proper options are given. |
| 42 | * Execute system call |
| 43 | * Check return code, if system call failed (return=-1) |
| 44 | * if errno set == expected errno |
| 45 | * Issue sys call fails with expected return value and errno. |
| 46 | * Otherwise, |
| 47 | * Issue sys call fails with unexpected errno. |
| 48 | * Otherwise, |
| 49 | * Issue sys call returns unexpected value. |
| 50 | * |
| 51 | * Cleanup: |
| 52 | * Print errno log and/or timing stats if options given |
| 53 | * Delete the temporary directory(s)/file(s) created. |
| 54 | * |
| 55 | * Usage: <for command-line> |
| 56 | * llseek02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 57 | * where, -c n : Run n copies concurrently. |
| 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 Ported by Wayne Boyer |
| 65 | * |
| 66 | * RESTRICTIONS: |
| 67 | * None. |
| 68 | */ |
| 69 | |
| 70 | #include <stdio.h> |
| 71 | #include <unistd.h> |
| 72 | #include <sys/types.h> |
| 73 | #include <errno.h> |
| 74 | #include <fcntl.h> |
| 75 | #include <utime.h> |
| 76 | #include <string.h> |
| 77 | #include <sys/stat.h> |
| 78 | #include <signal.h> |
| 79 | |
| 80 | #include "test.h" |
| 81 | #include "usctest.h" |
| 82 | |
| 83 | #define TEMP_FILE1 "tmp_file1" |
| 84 | #define TEMP_FILE2 "tmp_file2" |
| 85 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 86 | #define SEEK_TOP 10 |
| 87 | |
| 88 | char *TCID="llseek02"; /* Test program identifier. */ |
| 89 | int TST_TOTAL=2; /* Total number of test cases. */ |
| 90 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
| 91 | |
| 92 | int no_setup(); |
| 93 | int setup1(); /* setup function to test llseek() for EINVAL */ |
| 94 | int setup2(); /* setup function to test llseek() for EBADF */ |
| 95 | |
| 96 | int fd1; /* file handle for testfile1 */ |
| 97 | int fd2; /* file handle for testfile2 */ |
| 98 | |
| 99 | struct test_case_t { /* test case struct. to hold ref. test cond's*/ |
| 100 | int fd; |
| 101 | int Whence; |
| 102 | char *desc; |
| 103 | int exp_errno; |
| 104 | int (*setupfunc)(); |
| 105 | } Test_cases[] = { |
| 106 | { 1, SEEK_TOP, "'whence' argument is not valid", EINVAL, setup1 }, |
| 107 | { 2, SEEK_SET, "'fd' is not an open file descriptor", EBADF, setup2 }, |
| 108 | { 0, 0, NULL, 0, no_setup } |
| 109 | }; |
| 110 | |
| 111 | int exp_enos[] = {EINVAL, EBADF, 0}; |
| 112 | |
| 113 | void setup(); /* Main setup function of test */ |
| 114 | void cleanup(); /* cleanup function for the test */ |
| 115 | |
| 116 | int |
| 117 | main(int ac, char **av) |
| 118 | { |
| 119 | int lc; /* loop counter */ |
| 120 | char *msg; /* message returned from parse_opts */ |
| 121 | int fildes; /* file handle for testfile */ |
| 122 | int whence; /* position of file handle in the file */ |
| 123 | char *test_desc; /* test specific error message */ |
| 124 | loff_t offset; /* return value after llseek */ |
| 125 | int ind; /* counter to test different test conditions */ |
| 126 | |
| 127 | /* Parse standard options given to run the test. */ |
| 128 | msg = parse_opts(ac, av, (option_t *) NULL, NULL); |
| 129 | if (msg != (char *) NULL) { |
| 130 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 131 | tst_exit(); |
| 132 | } |
| 133 | |
| 134 | /* Perform global setup for test */ |
| 135 | setup(); |
| 136 | |
| 137 | /* set up expected error numbers */ |
| 138 | TEST_EXP_ENOS(exp_enos); |
| 139 | |
| 140 | /* Check looping state if -i option given */ |
| 141 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 142 | /* Reset Tst_count in case we are looping. */ |
| 143 | Tst_count=0; |
| 144 | |
| 145 | for (ind = 0; Test_cases[ind].desc != NULL; ind++) { |
| 146 | fildes = Test_cases[ind].fd; |
| 147 | test_desc = Test_cases[ind].desc; |
| 148 | whence = Test_cases[ind].Whence; |
| 149 | |
| 150 | /* Assign the 'fd' values appropriatly */ |
| 151 | if (fildes == 1) { |
| 152 | fildes = fd1; |
| 153 | } else { |
| 154 | fildes = fd2; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Invoke llseek(2) to test different test conditions. |
| 159 | * Verify that it fails with -1 return value and |
| 160 | * sets appropriate errno. |
| 161 | */ |
robbiew | 6a6069e | 2003-03-14 16:15:19 +0000 | [diff] [blame^] | 162 | TEST(lseek64(fildes, (loff_t)0, whence)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | |
| 164 | /* check return code of llseek(2) */ |
| 165 | if (TEST_RETURN != (loff_t)-1) { |
| 166 | tst_resm(TFAIL, "llseek() returned %d, expected" |
| 167 | " -1, errno:%d", TEST_RETURN, |
| 168 | Test_cases[ind].exp_errno); |
| 169 | continue; |
| 170 | } |
| 171 | TEST_ERROR_LOG(TEST_ERRNO); |
| 172 | if (TEST_ERRNO == Test_cases[ind].exp_errno) { |
| 173 | tst_resm(TPASS, "llseek() fails, %s, errno:%d", |
| 174 | test_desc, TEST_ERRNO); |
| 175 | } else { |
| 176 | tst_resm(TFAIL, "llseek() fails, %s, errno:%d, " |
| 177 | "expected errno:%d", test_desc, |
| 178 | TEST_ERRNO, Test_cases[ind].exp_errno); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* Call cleanup() to undo setup done for the test. */ |
| 184 | cleanup(); |
| 185 | |
| 186 | /*NOTREACHED*/ |
| 187 | } /* End main */ |
| 188 | |
| 189 | /* |
| 190 | * setup() - performs all ONE TIME setup for this test. |
| 191 | * Create a temporary directory and change directory to it. |
| 192 | * Invoke individual test setup functions according to the order |
| 193 | * set in test struct. definition. |
| 194 | */ |
| 195 | void |
| 196 | setup() |
| 197 | { |
| 198 | int ind; /* counter for test setup function */ |
| 199 | |
| 200 | /* capture signals */ |
| 201 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 202 | |
| 203 | /* Pause if that option was specified */ |
| 204 | TEST_PAUSE; |
| 205 | |
| 206 | /* make a temp directory and cd to it */ |
| 207 | tst_tmpdir(); |
| 208 | |
| 209 | /* call individual setup functions */ |
| 210 | for (ind = 0; Test_cases[ind].desc != NULL; ind++) { |
| 211 | Test_cases[ind].setupfunc(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * no_setup() - This is a dummy function which simply returns 0. |
| 217 | */ |
| 218 | int |
| 219 | no_setup() |
| 220 | { |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * setup1() - setup function for a test condition for which llseek(2) |
| 226 | * returns -1 and sets errno to EINVAL. |
| 227 | * Creat a temporary file for reading/writing and write some data |
| 228 | * into it. |
| 229 | * This function returns 0 on success. |
| 230 | */ |
| 231 | int |
| 232 | setup1() |
| 233 | { |
| 234 | char write_buff[BUFSIZ]; /* buffer to hold data */ |
| 235 | |
| 236 | /* Get the data to be written to temporary file */ |
| 237 | strcpy(write_buff, "abcdefg"); |
| 238 | |
| 239 | /* Creat/open a temporary file under above directory */ |
| 240 | if ((fd1 = open(TEMP_FILE1, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
| 241 | tst_brkm(TBROK, cleanup, |
| 242 | "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s", |
| 243 | TEMP_FILE1, FILE_MODE, errno, strerror(errno)); |
| 244 | } |
| 245 | |
| 246 | /* Write data into temporary file */ |
| 247 | if(write(fd1, write_buff, sizeof(write_buff)) <= 0) { |
| 248 | tst_brkm(TBROK, cleanup, "write(2) on %s Failed, errno=%d : %s", |
| 249 | TEMP_FILE1, errno, strerror(errno)); |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * setup2() - setup function for a test condition for which llseek(2) |
| 257 | * returns -1 and sets errno to EBADF. |
| 258 | * Creat a temporary file for reading/writing and close it. |
| 259 | * This function returns 0 on success. |
| 260 | */ |
| 261 | int |
| 262 | setup2() |
| 263 | { |
| 264 | /* Creat/open a temporary file under above directory */ |
| 265 | if ((fd2 = open(TEMP_FILE2, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
| 266 | tst_brkm(TBROK, cleanup, |
| 267 | "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s", |
| 268 | TEMP_FILE2, FILE_MODE, errno, strerror(errno)); |
| 269 | } |
| 270 | |
| 271 | /* Close the temporary file created above */ |
| 272 | if (close(fd2) < 0) { |
| 273 | tst_brkm(TBROK, cleanup, "close(%s) Failed, errno=%d : %s:", |
| 274 | TEMP_FILE2, errno, strerror(errno)); |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | /* |
| 280 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 281 | * completion or premature exit. |
| 282 | * Close the temporary file. |
| 283 | * Remove the test directory and testfile created in the setup. |
| 284 | */ |
| 285 | void |
| 286 | cleanup() |
| 287 | { |
| 288 | /* |
| 289 | * print timing stats if that option was specified. |
| 290 | * print errno log if that option was specified. |
| 291 | */ |
| 292 | TEST_CLEANUP; |
| 293 | |
| 294 | /* Close the temporary file(s) created in setup1/setup2*/ |
| 295 | if (close(fd1) < 0) { |
| 296 | tst_brkm(TFAIL, NULL, "close(%s) Failed, errno=%d : %s:", |
| 297 | TEMP_FILE1, errno, strerror(errno)); |
| 298 | } |
| 299 | |
| 300 | /* Remove tmp dir and all files in it */ |
| 301 | tst_rmdir(); |
| 302 | |
| 303 | /* exit with return code appropriate for results */ |
| 304 | tst_exit(); |
| 305 | } |