1 /* $OpenBSD: t_socketpair.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */
2 /* $NetBSD: t_socketpair.c,v 1.2 2017/01/13 20:04:52 christos Exp $ */
3
4 /*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Christos Zoulas.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include "macros.h"
41
42 #include "atf-c.h"
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <sys/socket.h>
47 #include <sys/un.h>
48 #include <errno.h>
49
50 static void
connected(int fd)51 connected(int fd)
52 {
53 struct sockaddr_un addr;
54 socklen_t len = (socklen_t)sizeof(addr);
55 ATF_REQUIRE(getpeername(fd, (struct sockaddr*)(void *)&addr,
56 &len) == 0);
57 }
58
59 static void
run(int flags)60 run(int flags)
61 {
62 int fd[2], i;
63
64 while ((i = open("/", O_RDONLY)) < 3)
65 ATF_REQUIRE(i != -1);
66
67 ATF_REQUIRE(closefrom(3) != -1);
68
69 ATF_REQUIRE(socketpair(AF_UNIX, SOCK_DGRAM | flags, 0, fd) == 0);
70
71 ATF_REQUIRE(fd[0] == 3);
72 ATF_REQUIRE(fd[1] == 4);
73
74 connected(fd[0]);
75 connected(fd[1]);
76
77 if (flags & SOCK_CLOEXEC) {
78 ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOEXEC) != 0);
79 ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOEXEC) != 0);
80 } else {
81 ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOEXEC) == 0);
82 ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOEXEC) == 0);
83 }
84
85 if (flags & SOCK_NONBLOCK) {
86 ATF_REQUIRE((fcntl(fd[0], F_GETFL) & O_NONBLOCK) != 0);
87 ATF_REQUIRE((fcntl(fd[1], F_GETFL) & O_NONBLOCK) != 0);
88 } else {
89 ATF_REQUIRE((fcntl(fd[0], F_GETFL) & O_NONBLOCK) == 0);
90 ATF_REQUIRE((fcntl(fd[1], F_GETFL) & O_NONBLOCK) == 0);
91 }
92
93 ATF_REQUIRE(close(fd[0]) != -1);
94 ATF_REQUIRE(close(fd[1]) != -1);
95 }
96
97 ATF_TC(socketpair_basic);
ATF_TC_HEAD(socketpair_basic,tc)98 ATF_TC_HEAD(socketpair_basic, tc)
99 {
100 atf_tc_set_md_var(tc, "descr", "A basic test of socketpair(2)");
101 }
102
ATF_TC_BODY(socketpair_basic,tc)103 ATF_TC_BODY(socketpair_basic, tc)
104 {
105 run(0);
106 }
107
108 ATF_TC(socketpair_nonblock);
ATF_TC_HEAD(socketpair_nonblock,tc)109 ATF_TC_HEAD(socketpair_nonblock, tc)
110 {
111 atf_tc_set_md_var(tc, "descr", "A non-blocking test of socketpair(2)");
112 }
113
ATF_TC_BODY(socketpair_nonblock,tc)114 ATF_TC_BODY(socketpair_nonblock, tc)
115 {
116 run(SOCK_NONBLOCK);
117 }
118
119 ATF_TC(socketpair_cloexec);
ATF_TC_HEAD(socketpair_cloexec,tc)120 ATF_TC_HEAD(socketpair_cloexec, tc)
121 {
122 atf_tc_set_md_var(tc, "descr", "A close-on-exec of socketpair(2)");
123 }
124
ATF_TC_BODY(socketpair_cloexec,tc)125 ATF_TC_BODY(socketpair_cloexec, tc)
126 {
127 run(SOCK_CLOEXEC);
128 }
129
ATF_TP_ADD_TCS(tp)130 ATF_TP_ADD_TCS(tp)
131 {
132
133 ATF_TP_ADD_TC(tp, socketpair_basic);
134 ATF_TP_ADD_TC(tp, socketpair_nonblock);
135 ATF_TP_ADD_TC(tp, socketpair_cloexec);
136
137 return atf_no_error();
138 }
139