2 * Copyright (c) 2004 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/module.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
42 #define ACCF_NAME "dataready"
45 * A number of small tests to confirm that attaching ACCF_DATA accept filters
46 * to inet4 ports works as expected. We test:
48 * - That no accept filter is attached on a newly created socket.
49 * - That bind() has no affect on the accept filter state.
50 * - That we can't attach an accept filter to a socket that isn't in the
52 * - That after we fail to attach the filter, querying the kernel shows no
54 * - That we can attach an accept filter to a socket that is in the listen
56 * - That once an accept filter is attached, we can query to make sure it is
58 * - That once an accept filter is attached, we can remove it and query to
59 * make sure it is removed.
64 struct accept_filter_arg afa;
65 struct sockaddr_in sin;
69 /* XXX: PLAIN_TEST_REQUIRE_MODULE "backport" for stable/9 */
70 const char *_mod_name = "accf_data";
72 if (modfind(_mod_name) == -1) {
73 printf("1..0 # SKIP - module %s could not be resolved: %s\n",
74 _mod_name, strerror(errno));
77 /* XXX: PLAIN_TEST_REQUIRE_MODULE for stable/9 */
82 * Step 0. Open socket().
84 lso = socket(PF_INET, SOCK_STREAM, 0);
86 errx(-1, "not ok 1 - socket: %s", strerror(errno));
87 printf("ok 1 - socket\n");
90 * Step 1. After socket(). Should return EINVAL, since no accept
91 * filter should be attached.
93 bzero(&afa, sizeof(afa));
95 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
97 errx(-1, "not ok 2 - getsockopt() after socket() succeeded");
99 errx(-1, "not ok 2 - getsockopt() after socket() failed with "
100 "%d (%s)", errno, strerror(errno));
101 printf("ok 2 - getsockopt\n");
104 * Step 2. Bind(). Ideally this will succeed.
106 bzero(&sin, sizeof(sin));
107 sin.sin_len = sizeof(sin);
108 sin.sin_family = AF_INET;
109 sin.sin_port = htons(8080);
110 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
111 if (bind(lso, (struct sockaddr *)&sin, sizeof(sin)) < 0)
112 errx(-1, "not ok 3 - bind %s", strerror(errno));
113 printf("ok 3 - bind\n");
116 * Step 3: After bind(). getsockopt() should return EINVAL, since no
117 * accept filter should be attached.
120 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
122 errx(-1, "not ok 4 - getsockopt() after bind() succeeded");
124 errx(-1, "not ok 4 - getsockopt() after bind() failed with %d (%s)",
125 errno, strerror(errno));
126 printf("ok 4 - getsockopt\n");
129 * Step 4: Setsockopt() before listen(). Should fail, since it's not
130 * yet a listen() socket.
132 bzero(&afa, sizeof(afa));
133 strncpy(afa.af_name, ACCF_NAME, sizeof(afa.af_name));
134 ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
136 errx(-1, "not ok 5 - setsockopt() before listen() succeeded");
137 printf("ok 5 - setsockopt\n");
140 * Step 5: Getsockopt() after pre-listen() setsockopt(). Should
141 * fail with EINVAL, since setsockopt() should have failed.
144 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
146 errx(-1, "not ok 6 - getsockopt() after pre-listen() setsockopt() "
149 errx(-1, "not ok 6 - pre-listen() getsockopt() failed with %d (%s)",
150 errno, strerror(errno));
151 printf("ok 6 - getsockopt\n");
156 if (listen(lso, -1) < 0)
157 errx(-1, "not ok 7 - listen: %s", strerror(errno));
158 printf("ok 7 - listen\n");
161 * Step 7: Getsockopt() after listen(). Should fail with EINVAL,
162 * since we have not installed accept filter yet.
165 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
167 errx(-1, "not ok 8 - getsockopt() after listen() but before "
168 "setsockopt() succeeded");
170 errx(-1, "not ok 8 - getsockopt() after listen() but before "
171 "setsockopt() failed with %d (%s)", errno, strerror(errno));
172 printf("ok 8 - getsockopt\n");
175 * Step 8: After listen(). This call to setsockopt() should succeed.
177 bzero(&afa, sizeof(afa));
178 strncpy(afa.af_name, ACCF_NAME, sizeof(afa.af_name));
179 ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
181 errx(-1, "not ok 9 - setsockopt() after listen() failed with %d "
182 "(%s)", errno, strerror(errno));
183 printf("ok 9 - setsockopt\n");
186 * Step 9: After setsockopt(). Should succeed and identify
189 bzero(&afa, sizeof(afa));
191 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
193 errx(-1, "not ok 10 - getsockopt() after listen() setsockopt() "
194 "failed with %d (%s)", errno, strerror(errno));
195 if (len != sizeof(afa))
196 errx(-1, "not ok 10 - getsockopt() after setsockopet() after "
197 "listen() returned wrong size (got %d expected %zd)", len,
199 if (strcmp(afa.af_name, ACCF_NAME) != 0)
200 errx(-1, "not ok 10 - getsockopt() after setsockopt() after "
201 "listen() mismatch (got %s expected %s)", afa.af_name,
203 printf("ok 10 - getsockopt\n");
206 * Step 10: Remove accept filter. After removing the accept filter
207 * getsockopt() should fail with EINVAL.
209 ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0);
211 errx(-1, "not ok 11 - setsockopt() after listen() "
212 "failed with %d (%s)", errno, strerror(errno));
213 bzero(&afa, sizeof(afa));
215 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
217 errx(-1, "not ok 11 - getsockopt() after removing "
218 "the accept filter returns valid accept filter %s",
221 errx(-1, "not ok 11 - getsockopt() after removing the accept"
222 "filter failed with %d (%s)", errno, strerror(errno));
223 printf("ok 11 - setsockopt\n");