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 | * NAME |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 22 | * read04.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check if read returns the number of bytes read correctly. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Create a file and write some bytes out to it. |
| 29 | * Attempt to read more than written. |
| 30 | * Check the return count, and the read buffer. The read buffer should be |
| 31 | * same as the write buffer. |
| 32 | * |
| 33 | * USAGE: <for command-line> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 34 | * read04 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | * where, -c n : Run n copies concurrently. |
| 36 | * -f : Turn off functionality Testing. |
| 37 | * -i n : Execute test n times. |
| 38 | * -I x : Execute test for x seconds. |
| 39 | * -P x : Pause for x seconds between iterations. |
| 40 | * -t : Turn on syscall timing. |
| 41 | * |
| 42 | * HISTORY |
| 43 | * 07/2001 Ported by Wayne Boyer |
| 44 | * |
| 45 | * RESTRICTIONS |
| 46 | * None |
| 47 | */ |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 48 | #include <sys/types.h> |
| 49 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | #include <stdio.h> |
| 51 | #include <fcntl.h> |
| 52 | #include <errno.h> |
| 53 | #include "test.h" |
| 54 | #include "usctest.h" |
| 55 | |
| 56 | void cleanup(void); |
| 57 | void setup(void); |
| 58 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 59 | char *TCID = "read04"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | int TST_TOTAL = 1; |
| 61 | extern int Tst_count; |
| 62 | |
subrata_modak | b394f06 | 2007-04-13 10:43:39 +0000 | [diff] [blame] | 63 | #define TST_SIZE 27 /* could also do strlen(palfa) */ |
vapier | bf3b40b | 2007-03-13 20:09:37 +0000 | [diff] [blame] | 64 | char fname[255]; |
| 65 | char palfa[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | int fild; |
| 67 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 68 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | { |
| 70 | int lc; /* loop counter */ |
| 71 | char *msg; /* message returned from parse_opts */ |
| 72 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | int rfild; |
| 74 | char prbuf[BUFSIZ]; |
| 75 | |
| 76 | /* |
| 77 | * parse standard options |
| 78 | */ |
| 79 | if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ |
| 80 | tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
| 81 | /*NOTREACHED*/ |
| 82 | } |
| 83 | |
| 84 | setup(); /* global setup for test */ |
| 85 | |
| 86 | /* check looping state if -i option given */ |
| 87 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 88 | |
| 89 | Tst_count = 0; /* reset Tst_count while looping */ |
| 90 | |
| 91 | if ((rfild = open(fname, O_RDONLY)) == -1) { |
| 92 | tst_brkm(TBROK, cleanup, "can't open for reading"); |
| 93 | /*NOTREACHED*/ |
| 94 | } |
| 95 | TEST(read(rfild, prbuf, BUFSIZ)); |
| 96 | |
| 97 | if (TEST_RETURN == -1) { |
| 98 | tst_resm(TFAIL, "call failed unexpectedly"); |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | if (STD_FUNCTIONAL_TEST) { |
| 103 | if (TEST_RETURN != TST_SIZE) { |
| 104 | tst_resm(TFAIL, "Bad read count - got %d - " |
| 105 | "expected %d", TEST_RETURN, TST_SIZE); |
| 106 | continue; |
| 107 | } |
vapier | bf3b40b | 2007-03-13 20:09:37 +0000 | [diff] [blame] | 108 | if (memcmp(palfa, prbuf, TST_SIZE) != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | tst_resm(TFAIL, "read buffer not equal " |
| 110 | "to write buffer"); |
| 111 | continue; |
| 112 | } |
| 113 | tst_resm(TPASS, "functionality of read() is correct"); |
| 114 | } else { |
| 115 | tst_resm(TPASS, "call succeeded"); |
| 116 | } |
| 117 | if (close(rfild) == -1) { |
| 118 | tst_brkm(TBROK, cleanup, "close() failed"); |
| 119 | /*NOTREACHED*/ |
| 120 | } |
| 121 | } |
| 122 | cleanup(); |
| 123 | /*NOTREACHED*/ |
robbiew | 09ebffd | 2003-03-27 17:01:00 +0000 | [diff] [blame] | 124 | return(0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
vapier | bf3b40b | 2007-03-13 20:09:37 +0000 | [diff] [blame] | 128 | * setup() - performs all ONE TIME setup for this test |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | */ |
| 130 | void |
| 131 | setup(void) |
| 132 | { |
| 133 | /* capture signals */ |
| 134 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 135 | |
| 136 | umask(0); |
| 137 | |
| 138 | /* Pause if that option was specified */ |
| 139 | TEST_PAUSE; |
| 140 | |
| 141 | /* make a temp directory and cd to it */ |
| 142 | tst_tmpdir(); |
| 143 | |
| 144 | sprintf(fname,"tfile_%d",getpid()); |
| 145 | |
| 146 | if ((fild = creat(fname, 0777)) == -1) { |
| 147 | tst_brkm(TBROK, cleanup, "creat(%s, 0777) Failed, errno = %d" |
| 148 | " : %s", fname, errno, strerror(errno)); |
| 149 | /*NOTREACHED*/ |
| 150 | } |
| 151 | if (write(fild, palfa, TST_SIZE) != TST_SIZE) { |
| 152 | tst_brkm(TBROK, cleanup, "can't write to Xread"); |
| 153 | /*NOTREACHED*/ |
| 154 | } |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame^] | 155 | close(fild); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
| 159 | * cleanup() - performs all ONE TIME cleanup for this test at completion or |
| 160 | * premature exit. |
| 161 | */ |
| 162 | void |
| 163 | cleanup(void) |
| 164 | { |
| 165 | /* |
| 166 | * print timing stats if that option was specified. |
| 167 | * print errno log if that option was specified. |
| 168 | */ |
| 169 | TEST_CLEANUP; |
| 170 | |
| 171 | /* Remove tmp dir and all files in it */ |
| 172 | unlink(fname); |
| 173 | tst_rmdir(); |
| 174 | |
| 175 | /* exit with return code appropriate for results */ |
| 176 | tst_exit(); |
| 177 | } |