1 /* $NetBSD: t_write.c,v 1.2 2011/10/19 16:19:30 jruoho Exp $ */
4 * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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 $");
35 #include <sys/syslimits.h>
46 static void sighandler(int);
48 static bool fail
= false;
49 static const char *path
= "write";
57 ATF_TC_WITH_CLEANUP(write_err
);
58 ATF_TC_HEAD(write_err
, tc
)
60 atf_tc_set_md_var(tc
, "descr", "Checks errors from write(2)");
63 ATF_TC_BODY(write_err
, tc
)
65 char rbuf
[3] = { 'a', 'b', 'c' };
66 char wbuf
[3] = { 'x', 'y', 'z' };
70 ATF_REQUIRE_ERRNO(EBADF
, write(-1, wbuf
, sizeof(wbuf
)) == -1);
72 fd
= open(path
, O_RDWR
| O_CREAT
);
77 ATF_REQUIRE_ERRNO(0, write(fd
, wbuf
, 3) == 3);
80 ATF_REQUIRE_ERRNO(EINVAL
, write(fd
, wbuf
, SIZE_MAX
) == -1);
83 ATF_REQUIRE_ERRNO(EFAULT
, write(fd
, (void *)-1, 1) == -1);
86 * Check that the above bogus write(2)
87 * calls did not corrupt the file.
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);
98 ATF_TC_CLEANUP(write_err
, tc
)
104 ATF_TC_HEAD(write_pipe
, tc
)
106 atf_tc_set_md_var(tc
, "descr", "Checks for EPIPE from write(2)");
109 ATF_TC_BODY(write_pipe
, tc
)
113 ATF_REQUIRE(pipe(fds
) == 0);
114 ATF_REQUIRE(signal(SIGPIPE
, sighandler
) == 0);
116 ATF_REQUIRE(write(fds
[1], "x", 1) != -1);
117 ATF_REQUIRE(close(fds
[0]) == 0);
122 if (write(fds
[1], "x", 1) != -1 || errno
!= EPIPE
)
123 atf_tc_fail_nonfatal("expected EPIPE but write(2) succeeded");
125 ATF_REQUIRE(close(fds
[1]) == 0);
128 atf_tc_fail_nonfatal("SIGPIPE was not raised");
131 ATF_TC_WITH_CLEANUP(write_pos
);
132 ATF_TC_HEAD(write_pos
, tc
)
134 atf_tc_set_md_var(tc
, "descr", "Checks that write(2) "
135 "updates the file position");
138 ATF_TC_BODY(write_pos
, tc
)
140 const size_t n
= 123;
144 fd
= open(path
, O_RDWR
| O_CREAT
);
145 ATF_REQUIRE(fd
>= 0);
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));
152 ATF_REQUIRE(close(fd
) == 0);
153 ATF_REQUIRE(unlink(path
) == 0);
156 ATF_TC_CLEANUP(write_pos
, tc
)
161 ATF_TC_WITH_CLEANUP(write_ret
);
162 ATF_TC_HEAD(write_ret
, tc
)
164 atf_tc_set_md_var(tc
, "descr", "Checks return values from write(2)");
167 ATF_TC_BODY(write_ret
, tc
)
174 fd
= open(path
, O_WRONLY
| O_CREAT
);
175 ATF_REQUIRE(fd
>= 0);
177 (void)memset(buf
, 'x', sizeof(buf
));
179 for (i
= j
= 0; i
< n
; i
++)
180 j
+= write(fd
, buf
, sizeof(buf
));
183 atf_tc_fail("inconsistent return values from write(2)");
189 ATF_TC_CLEANUP(write_ret
, tc
)
194 ATF_TC(writev_iovmax
);
195 ATF_TC_HEAD(writev_iovmax
, tc
)
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");
203 ATF_TC_BODY(writev_iovmax
, tc
)
207 (void)printf("Calling writev(2, NULL, IOV_MAX + 1)...\n");
210 retval
= writev(2, NULL
, IOV_MAX
+ 1);
212 ATF_REQUIRE_EQ_MSG(retval
, -1, "got: %zd", retval
);
213 ATF_REQUIRE_EQ_MSG(errno
, EINVAL
, "got: %s", strerror(errno
));
219 ATF_TP_ADD_TC(tp
, write_err
);
220 ATF_TP_ADD_TC(tp
, write_pipe
);
221 ATF_TP_ADD_TC(tp
, write_pos
);
222 ATF_TP_ADD_TC(tp
, write_ret
);
223 ATF_TP_ADD_TC(tp
, writev_iovmax
);
225 return atf_no_error();