blob: 9a2fdd2fbf710a69c18cfd46a7c6ac5e16e14d11 [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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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"
plars865695b2001-08-27 22:15:12 +000054
55void cleanup(void);
56void setup(void);
57
nstrazfa31d552002-05-14 16:50:06 +000058char *TCID = "read04";
plars865695b2001-08-27 22:15:12 +000059int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000060
subrata_modak56207ce2009-03-23 13:35:39 +000061#define TST_SIZE 27 /* could also do strlen(palfa) */
vapierbf3b40b2007-03-13 20:09:37 +000062char fname[255];
63char palfa[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
plars865695b2001-08-27 22:15:12 +000064int fild;
65
plars74948ad2002-11-14 16:16:14 +000066int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000067{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020068 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020069 const char *msg;
plars865695b2001-08-27 22:15:12 +000070
plars865695b2001-08-27 22:15:12 +000071 int rfild;
72 char prbuf[BUFSIZ];
73
74 /*
75 * parse standard options
76 */
Garrett Cooper45e285d2010-11-22 12:19:25 -080077 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080078 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 }
plars865695b2001-08-27 22:15:12 +000080
81 setup(); /* global setup for test */
82
plars865695b2001-08-27 22:15:12 +000083 for (lc = 0; TEST_LOOPING(lc); lc++) {
84
Caspar Zhangd59a6592013-03-07 14:59:12 +080085 tst_count = 0; /* reset tst_count while looping */
plars865695b2001-08-27 22:15:12 +000086
87 if ((rfild = open(fname, O_RDONLY)) == -1) {
88 tst_brkm(TBROK, cleanup, "can't open for reading");
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 }
plars865695b2001-08-27 22:15:12 +000090 TEST(read(rfild, prbuf, BUFSIZ));
91
92 if (TEST_RETURN == -1) {
93 tst_resm(TFAIL, "call failed unexpectedly");
94 continue;
95 }
96
Cyril Hrubise38b9612014-06-02 17:20:57 +020097 if (TEST_RETURN != TST_SIZE) {
98 tst_resm(TFAIL, "Bad read count - got %ld - "
99 "expected %d", TEST_RETURN, TST_SIZE);
100 continue;
plars865695b2001-08-27 22:15:12 +0000101 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200102 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
plars865695b2001-08-27 22:15:12 +0000109 if (close(rfild) == -1) {
110 tst_brkm(TBROK, cleanup, "close() failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111 }
plars865695b2001-08-27 22:15:12 +0000112 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200113
plars865695b2001-08-27 22:15:12 +0000114 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800115 tst_exit();
plars865695b2001-08-27 22:15:12 +0000116}
117
118/*
vapierbf3b40b2007-03-13 20:09:37 +0000119 * setup() - performs all ONE TIME setup for this test
plars865695b2001-08-27 22:15:12 +0000120 */
subrata_modak56207ce2009-03-23 13:35:39 +0000121void setup(void)
plars865695b2001-08-27 22:15:12 +0000122{
Garrett Cooper2c282152010-12-16 00:55:50 -0800123
plars865695b2001-08-27 22:15:12 +0000124 tst_sig(NOFORK, DEF_HANDLER, cleanup);
125
126 umask(0);
127
plars865695b2001-08-27 22:15:12 +0000128 TEST_PAUSE;
129
plars865695b2001-08-27 22:15:12 +0000130 tst_tmpdir();
131
subrata_modak56207ce2009-03-23 13:35:39 +0000132 sprintf(fname, "tfile_%d", getpid());
plars865695b2001-08-27 22:15:12 +0000133
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 Gao354ebb42012-12-07 10:10:04 +0800137 }
plars865695b2001-08-27 22:15:12 +0000138 if (write(fild, palfa, TST_SIZE) != TST_SIZE) {
139 tst_brkm(TBROK, cleanup, "can't write to Xread");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140 }
subrata_modakdad6e1a2007-10-30 10:46:58 +0000141 close(fild);
plars865695b2001-08-27 22:15:12 +0000142}
143
144/*
145 * cleanup() - performs all ONE TIME cleanup for this test at completion or
146 * premature exit.
147 */
subrata_modak56207ce2009-03-23 13:35:39 +0000148void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000149{
plars865695b2001-08-27 22:15:12 +0000150
plars865695b2001-08-27 22:15:12 +0000151 unlink(fname);
152 tst_rmdir();
153
Chris Dearmanec6edca2012-10-17 19:54:01 -0700154}