2 * Copyright (c) 2005 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/socket.h>
41 * Simple regression test to exercise some error cases relating to the use of
42 * bind() and connect() on UNIX domain sockets. In particular, make sure
43 * that when two sockets rendezvous using the file system name space, they
44 * get the expected success/failure cases.
47 * - Check that the resulting file mode/owner are right.
48 * - Do the same tests with UNIX domain sockets.
49 * - Check the results of getsockaddr() and getpeeraddr().
52 #define SOCK_NAME_ONE "socket.1"
53 #define SOCK_NAME_TWO "socket.2"
55 #define UNWIND_MAX 1024
59 char u_path[PATH_MAX];
60 } unwind_list[UNWIND_MAX];
63 push_path(const char *path)
66 if (unwind_len >= UNWIND_MAX)
67 err(-1, "push_path: one path too many (%s)", path);
69 strlcpy(unwind_list[unwind_len].u_path, path, PATH_MAX);
78 for (i = unwind_len - 1; i >= 0; i--) {
79 unlink(unwind_list[i].u_path);
80 rmdir(unwind_list[i].u_path);
85 bind_test(const char *directory_path)
87 char socket_path[PATH_MAX];
88 struct sockaddr_un sun;
91 sock1 = socket(PF_UNIX, SOCK_STREAM, 0);
93 warn("bind_test: socket(PF_UNIX, SOCK_STREAM, 0)");
97 if (snprintf(socket_path, sizeof(socket_path), "%s/%s",
98 directory_path, SOCK_NAME_ONE) >= PATH_MAX) {
99 warn("bind_test: snprintf(socket_path)");
104 bzero(&sun, sizeof(sun));
105 sun.sun_len = sizeof(sun);
106 sun.sun_family = AF_UNIX;
107 if (snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", socket_path)
108 >= sizeof(sun.sun_path)) {
109 warn("bind_test: snprintf(sun.sun_path)");
114 if (bind(sock1, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
115 warn("bind_test: bind(sun) #1");
120 push_path(socket_path);
123 * Once a STREAM UNIX domain socket has been bound, it can't be
124 * rebound. Expected error is EINVAL.
126 if (bind(sock1, (struct sockaddr *)&sun, sizeof(sun)) == 0) {
127 warnx("bind_test: bind(sun) #2 succeeded");
131 if (errno != EINVAL) {
132 warn("bind_test: bind(sun) #2");
137 sock2 = socket(PF_UNIX, SOCK_STREAM, 0);
139 warn("bind_test: socket(PF_UNIX, SOCK_STREAM, 0)");
145 * Since a socket is already bound to the pathname, it can't be bound
146 * to a second socket. Expected error is EADDRINUSE.
148 if (bind(sock2, (struct sockaddr *)&sun, sizeof(sun)) == 0) {
149 warnx("bind_test: bind(sun) #3 succeeded");
154 if (errno != EADDRINUSE) {
155 warn("bind_test: bind(sun) #2");
164 * The socket bound to the pathname has been closed, but the pathname
165 * can't be reused without first being unlinked. Expected error is
168 if (bind(sock2, (struct sockaddr *)&sun, sizeof(sun)) == 0) {
169 warnx("bind_test: bind(sun) #4 succeeded");
173 if (errno != EADDRINUSE) {
174 warn("bind_test: bind(sun) #4");
182 * The pathname is now free, so the socket should be able to bind to
185 if (bind(sock2, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
186 warn("bind_test: bind(sun) #5");
196 connect_test(const char *directory_path)
198 char socket_path[PATH_MAX];
199 struct sockaddr_un sun;
202 sock1 = socket(PF_UNIX, SOCK_STREAM, 0);
204 warn("connect_test: socket(PF_UNIX, SOCK_STREAM, 0)");
208 if (snprintf(socket_path, sizeof(socket_path), "%s/%s",
209 directory_path, SOCK_NAME_TWO) >= PATH_MAX) {
210 warn("connect_test: snprintf(socket_path)");
215 bzero(&sun, sizeof(sun));
216 sun.sun_len = sizeof(sun);
217 sun.sun_family = AF_UNIX;
218 if (snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", socket_path)
219 >= sizeof(sun.sun_path)) {
220 warn("connect_test: snprintf(sun.sun_path)");
226 * Try connecting to a path that doesn't yet exist. Should fail with
229 if (connect(sock1, (struct sockaddr *)&sun, sizeof(sun)) == 0) {
230 warnx("connect_test: connect(sun) #1 succeeded");
234 if (errno != ENOENT) {
235 warn("connect_test: connect(sun) #1");
240 if (bind(sock1, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
241 warn("connect_test: bind(sun) #1");
246 if (listen(sock1, 3) < 0) {
247 warn("connect_test: listen(sock1)");
252 push_path(socket_path);
254 sock2 = socket(PF_UNIX, SOCK_STREAM, 0);
256 warn("socket(PF_UNIX, SOCK_STREAM, 0)");
262 * Do a simple connect and make sure that works.
264 if (connect(sock2, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
265 warn("connect(sun) #2");
274 sock2 = socket(PF_UNIX, SOCK_STREAM, 0);
276 warn("socket(PF_UNIX, SOCK_STREAM, 0)");
281 * Confirm that once the listen socket is closed, we get a
282 * connection refused (ECONNREFUSED) when attempting to connect to
285 if (connect(sock2, (struct sockaddr *)&sun, sizeof(sun)) == 0) {
286 warnx("connect(sun) #3 succeeded");
290 if (errno != ECONNREFUSED) {
291 warn("connect(sun) #3");
301 main(int argc, char *argv[])
303 char directory_path[PATH_MAX];
306 strlcpy(directory_path, "/tmp/unix_bind.XXXXXXX", PATH_MAX);
307 if (mkdtemp(directory_path) == NULL)
309 push_path(directory_path);
311 error = bind_test(directory_path);
314 error = connect_test(directory_path);