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