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 | |
| 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
| 55 | void cleanup(void); |
| 56 | void setup(void); |
| 57 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 58 | char *TCID = "read04"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 61 | #define TST_SIZE 27 /* could also do strlen(palfa) */ |
vapier | bf3b40b | 2007-03-13 20:09:37 +0000 | [diff] [blame] | 62 | char fname[255]; |
| 63 | char palfa[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | int fild; |
| 65 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 66 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 68 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 69 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | int rfild; |
| 72 | char prbuf[BUFSIZ]; |
| 73 | |
| 74 | /* |
| 75 | * parse standard options |
| 76 | */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 77 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 78 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | setup(); /* global setup for test */ |
| 82 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 84 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 85 | tst_count = 0; /* reset tst_count while looping */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
| 87 | if ((rfild = open(fname, O_RDONLY)) == -1) { |
| 88 | tst_brkm(TBROK, cleanup, "can't open for reading"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | TEST(read(rfild, prbuf, BUFSIZ)); |
| 91 | |
| 92 | if (TEST_RETURN == -1) { |
| 93 | tst_resm(TFAIL, "call failed unexpectedly"); |
| 94 | continue; |
| 95 | } |
| 96 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 97 | if (TEST_RETURN != TST_SIZE) { |
| 98 | tst_resm(TFAIL, "Bad read count - got %ld - " |
| 99 | "expected %d", TEST_RETURN, TST_SIZE); |
| 100 | continue; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 102 | if (memcmp(palfa, prbuf, TST_SIZE) != 0) { |
| 103 | tst_resm(TFAIL, "read buffer not equal " |
| 104 | "to write buffer"); |
| 105 | continue; |
| 106 | } |
| 107 | tst_resm(TPASS, "functionality of read() is correct"); |
| 108 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | if (close(rfild) == -1) { |
| 110 | tst_brkm(TBROK, cleanup, "close() failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 111 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 113 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 115 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* |
vapier | bf3b40b | 2007-03-13 20:09:37 +0000 | [diff] [blame] | 119 | * setup() - performs all ONE TIME setup for this test |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 125 | |
| 126 | umask(0); |
| 127 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | TEST_PAUSE; |
| 129 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | tst_tmpdir(); |
| 131 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | sprintf(fname, "tfile_%d", getpid()); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | |
| 134 | if ((fild = creat(fname, 0777)) == -1) { |
| 135 | tst_brkm(TBROK, cleanup, "creat(%s, 0777) Failed, errno = %d" |
| 136 | " : %s", fname, errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 137 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | if (write(fild, palfa, TST_SIZE) != TST_SIZE) { |
| 139 | tst_brkm(TBROK, cleanup, "can't write to Xread"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | } |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 141 | close(fild); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
| 145 | * cleanup() - performs all ONE TIME cleanup for this test at completion or |
| 146 | * premature exit. |
| 147 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | unlink(fname); |
| 152 | tst_rmdir(); |
| 153 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 154 | } |