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
55 void cleanup(void);
56 void setup(void);
57
58 char *TCID = "read04";
59 int TST_TOTAL = 1;
60
61 #define TST_SIZE 27 /* could also do strlen(palfa) */
62 char fname[255];
63 char palfa[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
64 int fild;
65
main(int ac,char ** av)66 int main(int ac, char **av)
67 {
68 int lc;
69
70 int rfild;
71 char prbuf[BUFSIZ];
72
73 /*
74 * parse standard options
75 */
76 tst_parse_opts(ac, av, NULL, NULL);
77
78 setup(); /* global setup for test */
79
80 for (lc = 0; TEST_LOOPING(lc); lc++) {
81
82 tst_count = 0; /* reset tst_count while looping */
83
84 if ((rfild = open(fname, O_RDONLY)) == -1) {
85 tst_brkm(TBROK, cleanup, "can't open for reading");
86 }
87 TEST(read(rfild, prbuf, BUFSIZ));
88
89 if (TEST_RETURN == -1) {
90 tst_resm(TFAIL, "call failed unexpectedly");
91 continue;
92 }
93
94 if (TEST_RETURN != TST_SIZE) {
95 tst_resm(TFAIL, "Bad read count - got %ld - "
96 "expected %d", TEST_RETURN, TST_SIZE);
97 continue;
98 }
99 if (memcmp(palfa, prbuf, TST_SIZE) != 0) {
100 tst_resm(TFAIL, "read buffer not equal "
101 "to write buffer");
102 continue;
103 }
104 tst_resm(TPASS, "functionality of read() is correct");
105
106 if (close(rfild) == -1) {
107 tst_brkm(TBROK, cleanup, "close() failed");
108 }
109 }
110
111 cleanup();
112 tst_exit();
113 }
114
115 /*
116 * setup() - performs all ONE TIME setup for this test
117 */
setup(void)118 void setup(void)
119 {
120
121 tst_sig(NOFORK, DEF_HANDLER, cleanup);
122
123 umask(0);
124
125 TEST_PAUSE;
126
127 tst_tmpdir();
128
129 sprintf(fname, "tfile_%d", getpid());
130
131 if ((fild = creat(fname, 0777)) == -1) {
132 tst_brkm(TBROK, cleanup, "creat(%s, 0777) Failed, errno = %d"
133 " : %s", fname, errno, strerror(errno));
134 }
135 if (write(fild, palfa, TST_SIZE) != TST_SIZE) {
136 tst_brkm(TBROK, cleanup, "can't write to Xread");
137 }
138 close(fild);
139 }
140
141 /*
142 * cleanup() - performs all ONE TIME cleanup for this test at completion or
143 * premature exit.
144 */
cleanup(void)145 void cleanup(void)
146 {
147
148 unlink(fname);
149 tst_rmdir();
150
151 }
152