xref: /minix3/tests/lib/libc/sys/t_write.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /* $NetBSD: t_write.c,v 1.2 2011/10/19 16:19:30 jruoho Exp $ */
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc  * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc  */
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include <sys/cdefs.h>
30*11be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008\
31*11be35a1SLionel Sambuc  The NetBSD Foundation, inc. All rights reserved.");
32*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_write.c,v 1.2 2011/10/19 16:19:30 jruoho Exp $");
33*11be35a1SLionel Sambuc 
34*11be35a1SLionel Sambuc #include <sys/uio.h>
35*11be35a1SLionel Sambuc #include <sys/syslimits.h>
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include <atf-c.h>
38*11be35a1SLionel Sambuc #include <errno.h>
39*11be35a1SLionel Sambuc #include <fcntl.h>
40*11be35a1SLionel Sambuc #include <signal.h>
41*11be35a1SLionel Sambuc #include <stdio.h>
42*11be35a1SLionel Sambuc #include <stdint.h>
43*11be35a1SLionel Sambuc #include <string.h>
44*11be35a1SLionel Sambuc #include <unistd.h>
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc static void		 sighandler(int);
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc static bool		 fail = false;
49*11be35a1SLionel Sambuc static const char	*path = "write";
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc static void
sighandler(int signo)52*11be35a1SLionel Sambuc sighandler(int signo)
53*11be35a1SLionel Sambuc {
54*11be35a1SLionel Sambuc 	fail = false;
55*11be35a1SLionel Sambuc }
56*11be35a1SLionel Sambuc 
57*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(write_err);
ATF_TC_HEAD(write_err,tc)58*11be35a1SLionel Sambuc ATF_TC_HEAD(write_err, tc)
59*11be35a1SLionel Sambuc {
60*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks errors from write(2)");
61*11be35a1SLionel Sambuc }
62*11be35a1SLionel Sambuc 
ATF_TC_BODY(write_err,tc)63*11be35a1SLionel Sambuc ATF_TC_BODY(write_err, tc)
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc 	char rbuf[3] = { 'a', 'b', 'c' };
66*11be35a1SLionel Sambuc 	char wbuf[3] = { 'x', 'y', 'z' };
67*11be35a1SLionel Sambuc 	int fd;
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc 	errno = 0;
70*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EBADF, write(-1, wbuf, sizeof(wbuf)) == -1);
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc 	fd = open(path, O_RDWR | O_CREAT);
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc 	if (fd >= 0) {
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc 		errno = 0;
77*11be35a1SLionel Sambuc 		ATF_REQUIRE_ERRNO(0, write(fd, wbuf, 3) == 3);
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc 		errno = 0;
80*11be35a1SLionel Sambuc 		ATF_REQUIRE_ERRNO(EINVAL, write(fd, wbuf, SIZE_MAX) == -1);
81*11be35a1SLionel Sambuc 
82*11be35a1SLionel Sambuc 		errno = 0;
83*11be35a1SLionel Sambuc 		ATF_REQUIRE_ERRNO(EFAULT, write(fd, (void *)-1, 1) == -1);
84*11be35a1SLionel Sambuc 
85*11be35a1SLionel Sambuc 		/*
86*11be35a1SLionel Sambuc 		 * Check that the above bogus write(2)
87*11be35a1SLionel Sambuc 		 * calls did not corrupt the file.
88*11be35a1SLionel Sambuc 		 */
89*11be35a1SLionel Sambuc 		ATF_REQUIRE(lseek(fd, 0, SEEK_SET) == 0);
90*11be35a1SLionel Sambuc 		ATF_REQUIRE(read(fd, rbuf, 3) == 3);
91*11be35a1SLionel Sambuc 		ATF_REQUIRE(memcmp(rbuf, wbuf, 3) == 0);
92*11be35a1SLionel Sambuc 
93*11be35a1SLionel Sambuc 		(void)close(fd);
94*11be35a1SLionel Sambuc 		(void)unlink(path);
95*11be35a1SLionel Sambuc 	}
96*11be35a1SLionel Sambuc }
97*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(write_err,tc)98*11be35a1SLionel Sambuc ATF_TC_CLEANUP(write_err, tc)
99*11be35a1SLionel Sambuc {
100*11be35a1SLionel Sambuc 	(void)unlink(path);
101*11be35a1SLionel Sambuc }
102*11be35a1SLionel Sambuc 
103*11be35a1SLionel Sambuc ATF_TC(write_pipe);
ATF_TC_HEAD(write_pipe,tc)104*11be35a1SLionel Sambuc ATF_TC_HEAD(write_pipe, tc)
105*11be35a1SLionel Sambuc {
106*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks for EPIPE from write(2)");
107*11be35a1SLionel Sambuc }
108*11be35a1SLionel Sambuc 
ATF_TC_BODY(write_pipe,tc)109*11be35a1SLionel Sambuc ATF_TC_BODY(write_pipe, tc)
110*11be35a1SLionel Sambuc {
111*11be35a1SLionel Sambuc 	int fds[2];
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc 	ATF_REQUIRE(pipe(fds) == 0);
114*11be35a1SLionel Sambuc 	ATF_REQUIRE(signal(SIGPIPE, sighandler) == 0);
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc 	ATF_REQUIRE(write(fds[1], "x", 1) != -1);
117*11be35a1SLionel Sambuc 	ATF_REQUIRE(close(fds[0]) == 0);
118*11be35a1SLionel Sambuc 
119*11be35a1SLionel Sambuc 	errno = 0;
120*11be35a1SLionel Sambuc 	fail = true;
121*11be35a1SLionel Sambuc 
122*11be35a1SLionel Sambuc 	if (write(fds[1], "x", 1) != -1 || errno != EPIPE)
123*11be35a1SLionel Sambuc 		atf_tc_fail_nonfatal("expected EPIPE but write(2) succeeded");
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc 	ATF_REQUIRE(close(fds[1]) == 0);
126*11be35a1SLionel Sambuc 
127*11be35a1SLionel Sambuc 	if (fail != false)
128*11be35a1SLionel Sambuc 		atf_tc_fail_nonfatal("SIGPIPE was not raised");
129*11be35a1SLionel Sambuc }
130*11be35a1SLionel Sambuc 
131*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(write_pos);
ATF_TC_HEAD(write_pos,tc)132*11be35a1SLionel Sambuc ATF_TC_HEAD(write_pos, tc)
133*11be35a1SLionel Sambuc {
134*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks that write(2) "
135*11be35a1SLionel Sambuc 	    "updates the file position");
136*11be35a1SLionel Sambuc }
137*11be35a1SLionel Sambuc 
ATF_TC_BODY(write_pos,tc)138*11be35a1SLionel Sambuc ATF_TC_BODY(write_pos, tc)
139*11be35a1SLionel Sambuc {
140*11be35a1SLionel Sambuc 	const size_t n = 123;
141*11be35a1SLionel Sambuc 	size_t i;
142*11be35a1SLionel Sambuc 	int fd;
143*11be35a1SLionel Sambuc 
144*11be35a1SLionel Sambuc 	fd = open(path, O_RDWR | O_CREAT);
145*11be35a1SLionel Sambuc 	ATF_REQUIRE(fd >= 0);
146*11be35a1SLionel Sambuc 
147*11be35a1SLionel Sambuc 	for (i = 0; i < n; i++) {
148*11be35a1SLionel Sambuc 		ATF_REQUIRE(write(fd, "x", 1) == 1);
149*11be35a1SLionel Sambuc 		ATF_REQUIRE(lseek(fd, 0, SEEK_CUR) == (off_t)(i + 1));
150*11be35a1SLionel Sambuc 	}
151*11be35a1SLionel Sambuc 
152*11be35a1SLionel Sambuc 	ATF_REQUIRE(close(fd) == 0);
153*11be35a1SLionel Sambuc 	ATF_REQUIRE(unlink(path) == 0);
154*11be35a1SLionel Sambuc }
155*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(write_pos,tc)156*11be35a1SLionel Sambuc ATF_TC_CLEANUP(write_pos, tc)
157*11be35a1SLionel Sambuc {
158*11be35a1SLionel Sambuc 	(void)unlink(path);
159*11be35a1SLionel Sambuc }
160*11be35a1SLionel Sambuc 
161*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(write_ret);
ATF_TC_HEAD(write_ret,tc)162*11be35a1SLionel Sambuc ATF_TC_HEAD(write_ret, tc)
163*11be35a1SLionel Sambuc {
164*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks return values from write(2)");
165*11be35a1SLionel Sambuc }
166*11be35a1SLionel Sambuc 
ATF_TC_BODY(write_ret,tc)167*11be35a1SLionel Sambuc ATF_TC_BODY(write_ret, tc)
168*11be35a1SLionel Sambuc {
169*11be35a1SLionel Sambuc 	const size_t n = 99;
170*11be35a1SLionel Sambuc 	char buf[123];
171*11be35a1SLionel Sambuc 	size_t i, j;
172*11be35a1SLionel Sambuc 	int fd;
173*11be35a1SLionel Sambuc 
174*11be35a1SLionel Sambuc 	fd = open(path, O_WRONLY | O_CREAT);
175*11be35a1SLionel Sambuc 	ATF_REQUIRE(fd >= 0);
176*11be35a1SLionel Sambuc 
177*11be35a1SLionel Sambuc 	(void)memset(buf, 'x', sizeof(buf));
178*11be35a1SLionel Sambuc 
179*11be35a1SLionel Sambuc 	for (i = j = 0; i < n; i++)
180*11be35a1SLionel Sambuc 		j += write(fd, buf, sizeof(buf));
181*11be35a1SLionel Sambuc 
182*11be35a1SLionel Sambuc 	if (j != n * 123)
183*11be35a1SLionel Sambuc 		atf_tc_fail("inconsistent return values from write(2)");
184*11be35a1SLionel Sambuc 
185*11be35a1SLionel Sambuc 	(void)close(fd);
186*11be35a1SLionel Sambuc 	(void)unlink(path);
187*11be35a1SLionel Sambuc }
188*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(write_ret,tc)189*11be35a1SLionel Sambuc ATF_TC_CLEANUP(write_ret, tc)
190*11be35a1SLionel Sambuc {
191*11be35a1SLionel Sambuc 	(void)unlink(path);
192*11be35a1SLionel Sambuc }
193*11be35a1SLionel Sambuc 
194*11be35a1SLionel Sambuc ATF_TC(writev_iovmax);
ATF_TC_HEAD(writev_iovmax,tc)195*11be35a1SLionel Sambuc ATF_TC_HEAD(writev_iovmax, tc)
196*11be35a1SLionel Sambuc {
197*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "timeout", "10");
198*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
199*11be35a1SLionel Sambuc 	    "Checks that file descriptor is properly FILE_UNUSE()d "
200*11be35a1SLionel Sambuc 	    "when iovcnt is greater than IOV_MAX");
201*11be35a1SLionel Sambuc }
202*11be35a1SLionel Sambuc 
ATF_TC_BODY(writev_iovmax,tc)203*11be35a1SLionel Sambuc ATF_TC_BODY(writev_iovmax, tc)
204*11be35a1SLionel Sambuc {
205*11be35a1SLionel Sambuc 	ssize_t retval;
206*11be35a1SLionel Sambuc 
207*11be35a1SLionel Sambuc 	(void)printf("Calling writev(2, NULL, IOV_MAX + 1)...\n");
208*11be35a1SLionel Sambuc 
209*11be35a1SLionel Sambuc 	errno = 0;
210*11be35a1SLionel Sambuc 	retval = writev(2, NULL, IOV_MAX + 1);
211*11be35a1SLionel Sambuc 
212*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
213*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ_MSG(errno, EINVAL, "got: %s", strerror(errno));
214*11be35a1SLionel Sambuc }
215*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)216*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
217*11be35a1SLionel Sambuc {
218*11be35a1SLionel Sambuc 
219*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, write_err);
220*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, write_pipe);
221*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, write_pos);
222*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, write_ret);
223*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, writev_iovmax);
224*11be35a1SLionel Sambuc 
225*11be35a1SLionel Sambuc 	return atf_no_error();
226*11be35a1SLionel Sambuc }
227