blob: 1f6d5ad35214c00fdf3681c0700cc2f10b62d5db [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
nstrazfa31d552002-05-14 16:50:06 +000022 * read04.c
plars865695b2001-08-27 22:15:12 +000023 *
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>
nstrazfa31d552002-05-14 16:50:06 +000034 * read04 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000035 * 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 */
plars74948ad2002-11-14 16:16:14 +000048#include <sys/types.h>
49#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000050#include <stdio.h>
51#include <fcntl.h>
52#include <errno.h>
53#include "test.h"
54#include "usctest.h"
55
56void cleanup(void);
57void setup(void);
58
nstrazfa31d552002-05-14 16:50:06 +000059char *TCID = "read04";
plars865695b2001-08-27 22:15:12 +000060int TST_TOTAL = 1;
61extern int Tst_count;
62
subrata_modakb394f062007-04-13 10:43:39 +000063#define TST_SIZE 27 /* could also do strlen(palfa) */
vapierbf3b40b2007-03-13 20:09:37 +000064char fname[255];
65char palfa[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
plars865695b2001-08-27 22:15:12 +000066int fild;
67
plars74948ad2002-11-14 16:16:14 +000068int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000069{
70 int lc; /* loop counter */
71 char *msg; /* message returned from parse_opts */
72
plars865695b2001-08-27 22:15:12 +000073 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 }
vapierbf3b40b2007-03-13 20:09:37 +0000108 if (memcmp(palfa, prbuf, TST_SIZE) != 0) {
plars865695b2001-08-27 22:15:12 +0000109 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*/
robbiew09ebffd2003-03-27 17:01:00 +0000124 return(0);
plars865695b2001-08-27 22:15:12 +0000125}
126
127/*
vapierbf3b40b2007-03-13 20:09:37 +0000128 * setup() - performs all ONE TIME setup for this test
plars865695b2001-08-27 22:15:12 +0000129 */
130void
131setup(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_modakdad6e1a2007-10-30 10:46:58 +0000155 close(fild);
plars865695b2001-08-27 22:15:12 +0000156}
157
158/*
159 * cleanup() - performs all ONE TIME cleanup for this test at completion or
160 * premature exit.
161 */
162void
163cleanup(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}