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