read04.c revision aabb8340f63ed31afe995fd97795e542dc68b93c
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 * NAME 22 * read04.c 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> 34 * read04 [-c n] [-f] [-i n] [-I x] [-P x] [-t] 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 */ 48#include <sys/types.h> 49#include <sys/stat.h> 50#include <stdio.h> 51#include <fcntl.h> 52#include <errno.h> 53#include "test.h" 54 55void cleanup(void); 56void setup(void); 57 58char *TCID = "read04"; 59int TST_TOTAL = 1; 60 61#define TST_SIZE 27 /* could also do strlen(palfa) */ 62char fname[255]; 63char palfa[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 64int fild; 65 66int main(int ac, char **av) 67{ 68 int lc; 69 const char *msg; 70 71 int rfild; 72 char prbuf[BUFSIZ]; 73 74 /* 75 * parse standard options 76 */ 77 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { 78 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 79 } 80 81 setup(); /* global setup for test */ 82 83 for (lc = 0; TEST_LOOPING(lc); lc++) { 84 85 tst_count = 0; /* reset tst_count while looping */ 86 87 if ((rfild = open(fname, O_RDONLY)) == -1) { 88 tst_brkm(TBROK, cleanup, "can't open for reading"); 89 } 90 TEST(read(rfild, prbuf, BUFSIZ)); 91 92 if (TEST_RETURN == -1) { 93 tst_resm(TFAIL, "call failed unexpectedly"); 94 continue; 95 } 96 97 if (TEST_RETURN != TST_SIZE) { 98 tst_resm(TFAIL, "Bad read count - got %ld - " 99 "expected %d", TEST_RETURN, TST_SIZE); 100 continue; 101 } 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 109 if (close(rfild) == -1) { 110 tst_brkm(TBROK, cleanup, "close() failed"); 111 } 112 } 113 114 cleanup(); 115 tst_exit(); 116} 117 118/* 119 * setup() - performs all ONE TIME setup for this test 120 */ 121void setup(void) 122{ 123 124 tst_sig(NOFORK, DEF_HANDLER, cleanup); 125 126 umask(0); 127 128 TEST_PAUSE; 129 130 tst_tmpdir(); 131 132 sprintf(fname, "tfile_%d", getpid()); 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)); 137 } 138 if (write(fild, palfa, TST_SIZE) != TST_SIZE) { 139 tst_brkm(TBROK, cleanup, "can't write to Xread"); 140 } 141 close(fild); 142} 143 144/* 145 * cleanup() - performs all ONE TIME cleanup for this test at completion or 146 * premature exit. 147 */ 148void cleanup(void) 149{ 150 151 unlink(fname); 152 tst_rmdir(); 153 154} 155