summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/sys/t_write.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/regress/lib/libc/sys/t_write.c282
1 files changed, 0 insertions, 282 deletions
diff --git a/src/regress/lib/libc/sys/t_write.c b/src/regress/lib/libc/sys/t_write.c
deleted file mode 100644
index 8d01ebfba8..0000000000
--- a/src/regress/lib/libc/sys/t_write.c
+++ /dev/null
@@ -1,282 +0,0 @@
1/* $OpenBSD: t_write.c,v 1.4 2021/12/13 16:56:48 deraadt Exp $ */
2/* $NetBSD: t_write.c,v 1.7 2019/07/16 17:29:18 martin Exp $ */
3
4/*-
5 * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "macros.h"
31
32#include <sys/uio.h>
33#include <sys/mman.h>
34
35#include "atf-c.h"
36#include <errno.h>
37#include <fcntl.h>
38#include <signal.h>
39#include <limits.h>
40#include <stdio.h>
41#include <stdint.h>
42#include <string.h>
43#include <unistd.h>
44#include <paths.h>
45
46static void sighandler(int);
47
48static bool fail = false;
49static const char *path = "write";
50
51static void
52sighandler(int signo __unused)
53{
54 fail = false;
55}
56
57ATF_TC_WITH_CLEANUP(write_err);
58ATF_TC_HEAD(write_err, tc)
59{
60 atf_tc_set_md_var(tc, "descr", "Checks errors from write(2)");
61}
62
63ATF_TC_BODY(write_err, tc)
64{
65 char rbuf[3] = { 'a', 'b', 'c' };
66 char wbuf[3] = { 'x', 'y', 'z' };
67 int fd;
68
69 errno = 0;
70 ATF_REQUIRE_ERRNO(EBADF, write(-1, wbuf, sizeof(wbuf)) == -1);
71
72 fd = open(path, O_RDWR | O_CREAT, 0600);
73
74 if (fd >= 0) {
75
76 errno = 0;
77 ATF_REQUIRE_ERRNO(0, write(fd, wbuf, 3) == 3);
78
79 errno = 0;
80 ATF_REQUIRE_ERRNO(EINVAL, write(fd, wbuf, SIZE_MAX) == -1);
81
82 errno = 0;
83 ATF_REQUIRE_ERRNO(EFAULT, write(fd, (void *)-1, 1) == -1);
84
85 /*
86 * Check that the above bogus write(2)
87 * calls did not corrupt the file.
88 */
89 ATF_REQUIRE(lseek(fd, 0, SEEK_SET) == 0);
90 ATF_REQUIRE(read(fd, rbuf, 3) == 3);
91 ATF_REQUIRE(memcmp(rbuf, wbuf, 3) == 0);
92
93 (void)close(fd);
94 (void)unlink(path);
95 }
96}
97
98ATF_TC_CLEANUP(write_err, tc)
99{
100 (void)unlink(path);
101}
102
103ATF_TC(write_pipe);
104ATF_TC_HEAD(write_pipe, tc)
105{
106 atf_tc_set_md_var(tc, "descr", "Checks for EPIPE from write(2)");
107}
108
109ATF_TC_BODY(write_pipe, tc)
110{
111 int fds[2];
112
113 ATF_REQUIRE(pipe(fds) == 0);
114 ATF_REQUIRE(signal(SIGPIPE, sighandler) == 0);
115
116 ATF_REQUIRE(write(fds[1], "x", 1) != -1);
117 ATF_REQUIRE(close(fds[0]) == 0);
118
119 errno = 0;
120 fail = true;
121
122 if (write(fds[1], "x", 1) != -1 || errno != EPIPE)
123 atf_tc_fail_nonfatal("expected EPIPE but write(2) succeeded");
124
125 ATF_REQUIRE(close(fds[1]) == 0);
126
127 if (fail != false)
128 atf_tc_fail_nonfatal("SIGPIPE was not raised");
129}
130
131ATF_TC_WITH_CLEANUP(write_pos);
132ATF_TC_HEAD(write_pos, tc)
133{
134 atf_tc_set_md_var(tc, "descr", "Checks that write(2) "
135 "updates the file position");
136}
137
138ATF_TC_BODY(write_pos, tc)
139{
140 const size_t n = 123;
141 size_t i;
142 int fd;
143
144 fd = open(path, O_RDWR | O_CREAT, 0600);
145 ATF_REQUIRE(fd >= 0);
146
147 for (i = 0; i < n; i++) {
148 ATF_REQUIRE(write(fd, "x", 1) == 1);
149 ATF_REQUIRE(lseek(fd, 0, SEEK_CUR) == (off_t)(i + 1));
150 }
151
152 ATF_REQUIRE(close(fd) == 0);
153 ATF_REQUIRE(unlink(path) == 0);
154}
155
156ATF_TC_CLEANUP(write_pos, tc)
157{
158 (void)unlink(path);
159}
160
161ATF_TC_WITH_CLEANUP(write_ret);
162ATF_TC_HEAD(write_ret, tc)
163{
164 atf_tc_set_md_var(tc, "descr", "Checks return values from write(2)");
165}
166
167ATF_TC_BODY(write_ret, tc)
168{
169 const size_t n = 99;
170 char buf[123];
171 size_t i, j;
172 int fd;
173
174 fd = open(path, O_WRONLY | O_CREAT, 0600);
175 ATF_REQUIRE(fd >= 0);
176
177 (void)memset(buf, 'x', sizeof(buf));
178
179 for (i = j = 0; i < n; i++)
180 j += write(fd, buf, sizeof(buf));
181
182 if (j != n * 123)
183 atf_tc_fail("inconsistent return values from write(2)");
184
185 (void)close(fd);
186 (void)unlink(path);
187}
188
189ATF_TC_CLEANUP(write_ret, tc)
190{
191 (void)unlink(path);
192}
193
194ATF_TC(writev_iovmax);
195ATF_TC_HEAD(writev_iovmax, tc)
196{
197 atf_tc_set_md_var(tc, "timeout", "10");
198 atf_tc_set_md_var(tc, "descr",
199 "Checks that file descriptor is properly FILE_UNUSE()d "
200 "when iovcnt is greater than IOV_MAX");
201}
202
203ATF_TC_BODY(writev_iovmax, tc)
204{
205 ssize_t retval;
206
207 (void)printf("Calling writev(2, NULL, IOV_MAX + 1)...\n");
208
209 errno = 0;
210 retval = writev(2, NULL, IOV_MAX + 1);
211
212 ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
213 ATF_REQUIRE_EQ_MSG(errno, EINVAL, "got: %s", strerror(errno));
214}
215
216ATF_TC(write_fault);
217
218ATF_TC_HEAD(write_fault, tc)
219{
220 atf_tc_set_md_var(tc, "descr",
221 "Check that writing to non-permitted space returns EFAULT");
222}
223
224#define SIZE 8192
225
226ATF_TC_BODY(write_fault, tc)
227{
228 int fd[2];
229 ATF_REQUIRE(pipe(fd) != -1);
230 // Can't use /dev/null cause it doesn't access the buffer.
231
232 void *map = mmap(NULL, SIZE, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
233 ATF_REQUIRE(map != MAP_FAILED);
234
235 ssize_t retval = write(fd[1], map, SIZE);
236
237 ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
238 ATF_REQUIRE_EQ_MSG(errno, EFAULT, "got: %s", strerror(errno));
239
240 munmap(map, SIZE);
241 close(fd[0]);
242 close(fd[1]);
243}
244
245ATF_TC(read_fault);
246
247ATF_TC_HEAD(read_fault, tc)
248{
249 atf_tc_set_md_var(tc, "descr",
250 "Check that reading from non-permitted space returns EFAULT");
251}
252
253ATF_TC_BODY(read_fault, tc)
254{
255 int fd = open(_PATH_DEVZERO, O_RDONLY);
256 ATF_REQUIRE(fd != -1);
257
258 void *map = mmap(NULL, SIZE, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
259 ATF_REQUIRE(map != MAP_FAILED);
260
261 ssize_t retval = read(fd, map, SIZE);
262
263 ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
264 ATF_REQUIRE_EQ_MSG(errno, EFAULT, "got: %s", strerror(errno));
265
266 munmap(map, SIZE);
267 close(fd);
268}
269
270ATF_TP_ADD_TCS(tp)
271{
272
273 ATF_TP_ADD_TC(tp, write_err);
274 ATF_TP_ADD_TC(tp, write_pipe);
275 ATF_TP_ADD_TC(tp, write_pos);
276 ATF_TP_ADD_TC(tp, write_ret);
277 ATF_TP_ADD_TC(tp, writev_iovmax);
278 ATF_TP_ADD_TC(tp, write_fault);
279 ATF_TP_ADD_TC(tp, read_fault);
280
281 return atf_no_error();
282}