t_empty.c revision 1.1 1 1.1 thorpej /* $NetBSD: t_empty.c,v 1.1 2021/10/23 01:28:33 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej *
16 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.1 thorpej */
28 1.1 thorpej
29 1.1 thorpej #include <sys/cdefs.h>
30 1.1 thorpej __RCSID("$NetBSD: t_empty.c,v 1.1 2021/10/23 01:28:33 thorpej Exp $");
31 1.1 thorpej
32 1.1 thorpej #include <sys/event.h>
33 1.1 thorpej #include <sys/socket.h>
34 1.1 thorpej #include <sys/time.h>
35 1.1 thorpej #include <sys/types.h>
36 1.1 thorpej
37 1.1 thorpej #include <netinet/in.h>
38 1.1 thorpej
39 1.1 thorpej #include <err.h>
40 1.1 thorpej #include <errno.h>
41 1.1 thorpej #include <fcntl.h>
42 1.1 thorpej #include <stdbool.h>
43 1.1 thorpej #include <stdio.h>
44 1.1 thorpej #include <stdlib.h>
45 1.1 thorpej #include <unistd.h>
46 1.1 thorpej
47 1.1 thorpej #include <atf-c.h>
48 1.1 thorpej
49 1.1 thorpej static void
50 1.1 thorpej test_empty(int readfd, int writefd, bool is_tcp)
51 1.1 thorpej {
52 1.1 thorpej struct timespec ts = { 0, 0 };
53 1.1 thorpej struct kevent event;
54 1.1 thorpej int kq, error, sndbufsize;
55 1.1 thorpej char buf[1024] = { 0 };
56 1.1 thorpej ssize_t rv;
57 1.1 thorpej
58 1.1 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
59 1.1 thorpej
60 1.1 thorpej EV_SET(&event, writefd, EVFILT_EMPTY, EV_ADD, 0, 0, NULL);
61 1.1 thorpej ATF_REQUIRE(kevent(kq, &event, 1, NULL, 0, NULL) == 0);
62 1.1 thorpej
63 1.1 thorpej /* Check that EMPTY is true. */
64 1.1 thorpej memset(&event, 0, sizeof(event));
65 1.1 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, &event, 1, &ts) == 1);
66 1.1 thorpej ATF_REQUIRE(event.ident == (uintptr_t)writefd);
67 1.1 thorpej ATF_REQUIRE(event.filter == EVFILT_EMPTY);
68 1.1 thorpej
69 1.1 thorpej if (is_tcp) {
70 1.1 thorpej /*
71 1.1 thorpej * Get the write socket buffer size so that we can set
72 1.1 thorpej * the read socket buffer size to something larger
73 1.1 thorpej * later on.
74 1.1 thorpej */
75 1.1 thorpej socklen_t slen = sizeof(sndbufsize);
76 1.1 thorpej ATF_REQUIRE(getsockopt(writefd, SOL_SOCKET,
77 1.1 thorpej SO_SNDBUF, &sndbufsize, &slen) == 0);
78 1.1 thorpej
79 1.1 thorpej /*
80 1.1 thorpej * Set the receive buffer size to 1, slamming shut
81 1.1 thorpej * the TCP receive window, thus trapping all of the
82 1.1 thorpej * data in the sender's queue.
83 1.1 thorpej */
84 1.1 thorpej int val = 1;
85 1.1 thorpej ATF_REQUIRE(setsockopt(readfd, SOL_SOCKET,
86 1.1 thorpej SO_RCVBUF, &val, sizeof(val)) == 0);
87 1.1 thorpej }
88 1.1 thorpej
89 1.1 thorpej /* Write until the write buffer is full. */
90 1.1 thorpej for (rv = 0; rv != -1;) {
91 1.1 thorpej rv = write(writefd, buf, sizeof(buf));
92 1.1 thorpej error = errno;
93 1.1 thorpej ATF_REQUIRE(rv > 0 || (rv == -1 && error == EAGAIN));
94 1.1 thorpej }
95 1.1 thorpej
96 1.1 thorpej /* Check that EMPTY is false. */
97 1.1 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, &event, 1, &ts) == 0);
98 1.1 thorpej
99 1.1 thorpej if (is_tcp) {
100 1.1 thorpej /*
101 1.1 thorpej * Set the receive buffer size to something larger than
102 1.1 thorpej * the sender's send buffer.
103 1.1 thorpej */
104 1.1 thorpej int val = sndbufsize + 128;
105 1.1 thorpej ATF_REQUIRE(setsockopt(readfd, SOL_SOCKET,
106 1.1 thorpej SO_RCVBUF, &val, sizeof(val)) == 0);
107 1.1 thorpej }
108 1.1 thorpej
109 1.1 thorpej /* Read all of the data that's available. */
110 1.1 thorpej for (rv = 0; rv != -1;) {
111 1.1 thorpej rv = read(readfd, buf, sizeof(buf));
112 1.1 thorpej error = errno;
113 1.1 thorpej ATF_REQUIRE(rv > 0 || (rv == -1 && error == EAGAIN));
114 1.1 thorpej }
115 1.1 thorpej
116 1.1 thorpej /*
117 1.1 thorpej * Check that EMPTY is true. Check a few times (TCP might
118 1.1 thorpej * not drain immediately).
119 1.1 thorpej */
120 1.1 thorpej if (is_tcp) {
121 1.1 thorpej for (rv = 0; rv < 5; rv++) {
122 1.1 thorpej if (kevent(kq, NULL, 0, &event, 1, &ts) == 1) {
123 1.1 thorpej break;
124 1.1 thorpej }
125 1.1 thorpej }
126 1.1 thorpej sleep(1);
127 1.1 thorpej }
128 1.1 thorpej memset(&event, 0, sizeof(event));
129 1.1 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, &event, 1, &ts) == 1);
130 1.1 thorpej ATF_REQUIRE(event.ident == (uintptr_t)writefd);
131 1.1 thorpej ATF_REQUIRE(event.filter == EVFILT_EMPTY);
132 1.1 thorpej }
133 1.1 thorpej
134 1.1 thorpej ATF_TC(sock_tcp);
135 1.1 thorpej ATF_TC_HEAD(sock_tcp, tc)
136 1.1 thorpej {
137 1.1 thorpej atf_tc_set_md_var(tc, "descr",
138 1.1 thorpej "Test EVFILT_EMPTY with TCP sockets.");
139 1.1 thorpej }
140 1.1 thorpej
141 1.1 thorpej ATF_TC_BODY(sock_tcp, tc)
142 1.1 thorpej {
143 1.1 thorpej int readsock, writesock;
144 1.1 thorpej socklen_t slen;
145 1.1 thorpej
146 1.1 thorpej ATF_REQUIRE((readsock =
147 1.1 thorpej socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP)) != -1);
148 1.1 thorpej ATF_REQUIRE((writesock =
149 1.1 thorpej socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP)) != -1);
150 1.1 thorpej
151 1.1 thorpej struct sockaddr_in sin = {
152 1.1 thorpej .sin_len = sizeof(sin),
153 1.1 thorpej .sin_family = AF_INET,
154 1.1 thorpej .sin_port = 0, /* no need to swap 0 */
155 1.1 thorpej .sin_addr = { .s_addr = htonl(INADDR_LOOPBACK) },
156 1.1 thorpej };
157 1.1 thorpej ATF_REQUIRE(bind(readsock, (struct sockaddr *)&sin,
158 1.1 thorpej sizeof(sin)) == 0);
159 1.1 thorpej ATF_REQUIRE(listen(readsock, 1) == 0);
160 1.1 thorpej slen = sizeof(sin);
161 1.1 thorpej ATF_REQUIRE(getsockname(readsock, (struct sockaddr *)&sin, &slen) == 0);
162 1.1 thorpej
163 1.1 thorpej ATF_REQUIRE_ERRNO(EINPROGRESS,
164 1.1 thorpej connect(writesock, (struct sockaddr *)&sin, sizeof(sin)) == -1);
165 1.1 thorpej
166 1.1 thorpej slen = sizeof(sin);
167 1.1 thorpej ATF_REQUIRE((readsock = accept(readsock, (struct sockaddr *)&sin,
168 1.1 thorpej &slen)) != -1);
169 1.1 thorpej
170 1.1 thorpej test_empty(readsock, writesock, true);
171 1.1 thorpej }
172 1.1 thorpej
173 1.1 thorpej ATF_TP_ADD_TCS(tp)
174 1.1 thorpej {
175 1.1 thorpej ATF_TP_ADD_TC(tp, sock_tcp);
176 1.1 thorpej
177 1.1 thorpej return atf_no_error();
178 1.1 thorpej }
179