2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2022 Gleb Smirnoff <glebius@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/socket.h>
29 #include <netinet/in.h>
36 listensock(struct sockaddr_in *sin)
40 ATF_REQUIRE((l = socket(PF_INET, SOCK_STREAM, 0)) > 0);
41 ATF_REQUIRE(fcntl(l, F_SETFL, O_NONBLOCK) != -1);
42 ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &(socklen_t){1},
44 *sin = (struct sockaddr_in){
45 .sin_len = sizeof(sin),
46 .sin_family = AF_INET,
47 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
49 ATF_REQUIRE(bind(l, (struct sockaddr *)sin, sizeof(*sin)) == 0);
50 ATF_REQUIRE(getsockname(l, (struct sockaddr *)sin,
51 &(socklen_t){ sizeof(*sin) }) == 0);
52 ATF_REQUIRE(listen(l, -1) == 0);
58 clientsock(struct sockaddr_in *sin)
62 ATF_REQUIRE((s = socket(PF_INET, SOCK_STREAM, 0)) > 0);
63 ATF_REQUIRE(connect(s, (struct sockaddr *)sin, sizeof(*sin)) == 0);
69 accfon(int l, struct accept_filter_arg *af)
72 if (setsockopt(l, SOL_SOCKET, SO_ACCEPTFILTER, af, sizeof(*af)) != 0) {
74 atf_tc_skip("Accept filter %s not loaded in kernel",
77 atf_tc_fail("setsockopt(SO_ACCEPTFILTER): %s",
83 * XXX: return from send(2) on a localhost connection doesn't guarantee that
84 * netisr has fully processed and delivered the data to the remote local
85 * socket. Sleep a fraction of second to "guarantee" that it did.
88 usend(int s, const void *msg, size_t len)
92 rv = send(s, msg, len, 0);
97 ATF_TC_WITHOUT_HEAD(data);
100 struct accept_filter_arg afa = {
101 .af_name = "dataready"
103 struct sockaddr_in sin;
106 l = listensock(&sin);
108 s = clientsock(&sin);
109 ATF_REQUIRE(accept(l, NULL, 0) == -1);
110 ATF_REQUIRE(errno == EAGAIN);
111 ATF_REQUIRE(usend(s, "foo", sizeof("foo")) == sizeof("foo"));
112 ATF_REQUIRE((a = accept(l, NULL, 0)) > 0);
115 ATF_TC_WITHOUT_HEAD(http);
116 ATF_TC_BODY(http, tc)
118 struct accept_filter_arg afa = {
119 .af_name = "httpready"
121 struct sockaddr_in sin;
124 l = listensock(&sin);
126 s = clientsock(&sin);
129 ATF_REQUIRE(accept(l, NULL, 0) == -1);
130 ATF_REQUIRE(errno == EAGAIN);
132 /* 2) Data, that doesn't look like HTTP. */
133 ATF_REQUIRE(usend(s, "foo", sizeof("foo")) == sizeof("foo"));
134 ATF_REQUIRE((a = accept(l, NULL, 0)) > 0);
139 #define CHUNK1 "GET / "
140 #define CHUNK2 "HTTP/1.0\r\n\n"
141 #define LEN(c) (sizeof(c) - 1)
143 /* 3) Partial HTTP. */
144 s = clientsock(&sin);
145 ATF_REQUIRE(usend(s, CHUNK1, LEN(CHUNK1)) == LEN(CHUNK1));
146 ATF_REQUIRE(accept(l, NULL, 0) == -1);
147 ATF_REQUIRE(errno == EAGAIN);
149 /* 4) Complete HTTP. */
150 ATF_REQUIRE(usend(s, CHUNK2, LEN(CHUNK2)) == LEN(CHUNK2));
151 ATF_REQUIRE((a = accept(l, NULL, 0)) > 0);
156 ATF_TP_ADD_TC(tp, data);
157 ATF_TP_ADD_TC(tp, http);
159 return (atf_no_error());