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