1272343Sngie/* $NetBSD: t_socketpair.c,v 1.1 2011/11/05 18:19:02 jruoho Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Christos Zoulas. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 3. All advertising materials mentioning features or use of this software 19272343Sngie * must display the following acknowledgement: 20272343Sngie * This product includes software developed by the NetBSD 21272343Sngie * Foundation, Inc. and its contributors. 22272343Sngie * 4. Neither the name of The NetBSD Foundation nor the names of its 23272343Sngie * contributors may be used to endorse or promote products derived 24272343Sngie * from this software without specific prior written permission. 25272343Sngie * 26272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36272343Sngie * POSSIBILITY OF SUCH DAMAGE. 37272343Sngie */ 38272343Sngie#include <sys/cdefs.h> 39272343Sngie__RCSID("$NetBSD: t_socketpair.c,v 1.1 2011/11/05 18:19:02 jruoho Exp $"); 40272343Sngie 41272343Sngie#include <atf-c.h> 42272343Sngie#include <fcntl.h> 43272343Sngie#include <unistd.h> 44272343Sngie#include <stdlib.h> 45272343Sngie#include <sys/socket.h> 46272343Sngie#include <sys/un.h> 47272343Sngie#include <errno.h> 48272343Sngie 49272343Sngiestatic void 50272343Sngieconnected(int fd) 51272343Sngie{ 52272343Sngie struct sockaddr_un addr; 53272343Sngie socklen_t len = (socklen_t)sizeof(addr); 54272343Sngie ATF_REQUIRE(getpeername(fd, (struct sockaddr*)(void *)&addr, 55272343Sngie &len) == 0); 56272343Sngie} 57272343Sngie 58272343Sngiestatic void 59272343Sngierun(int flags) 60272343Sngie{ 61272343Sngie int fd[2], i; 62272343Sngie 63272343Sngie while ((i = open("/", O_RDONLY)) < 3) 64272343Sngie ATF_REQUIRE(i != -1); 65272343Sngie 66276478Sngie#ifdef __FreeBSD__ 67276478Sngie closefrom(3); 68276478Sngie#else 69272343Sngie ATF_REQUIRE(fcntl(3, F_CLOSEM) != -1); 70276478Sngie#endif 71272343Sngie 72272343Sngie ATF_REQUIRE(socketpair(AF_UNIX, SOCK_DGRAM | flags, 0, fd) == 0); 73272343Sngie 74272343Sngie ATF_REQUIRE(fd[0] == 3); 75272343Sngie ATF_REQUIRE(fd[1] == 4); 76272343Sngie 77272343Sngie connected(fd[0]); 78272343Sngie connected(fd[1]); 79272343Sngie 80272343Sngie if (flags & SOCK_CLOEXEC) { 81272343Sngie ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOEXEC) != 0); 82272343Sngie ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOEXEC) != 0); 83272343Sngie } else { 84272343Sngie ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOEXEC) == 0); 85272343Sngie ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOEXEC) == 0); 86272343Sngie } 87272343Sngie 88272343Sngie if (flags & SOCK_NONBLOCK) { 89272343Sngie ATF_REQUIRE((fcntl(fd[0], F_GETFL) & O_NONBLOCK) != 0); 90272343Sngie ATF_REQUIRE((fcntl(fd[1], F_GETFL) & O_NONBLOCK) != 0); 91272343Sngie } else { 92272343Sngie ATF_REQUIRE((fcntl(fd[0], F_GETFL) & O_NONBLOCK) == 0); 93272343Sngie ATF_REQUIRE((fcntl(fd[1], F_GETFL) & O_NONBLOCK) == 0); 94272343Sngie } 95272343Sngie 96272343Sngie ATF_REQUIRE(close(fd[0]) != -1); 97272343Sngie ATF_REQUIRE(close(fd[1]) != -1); 98272343Sngie} 99272343Sngie 100272343SngieATF_TC(socketpair_basic); 101272343SngieATF_TC_HEAD(socketpair_basic, tc) 102272343Sngie{ 103272343Sngie atf_tc_set_md_var(tc, "descr", "A basic test of socketpair(2)"); 104272343Sngie} 105272343Sngie 106272343SngieATF_TC_BODY(socketpair_basic, tc) 107272343Sngie{ 108272343Sngie run(0); 109272343Sngie} 110272343Sngie 111272343SngieATF_TC(socketpair_nonblock); 112272343SngieATF_TC_HEAD(socketpair_nonblock, tc) 113272343Sngie{ 114272343Sngie atf_tc_set_md_var(tc, "descr", "A non-blocking test of socketpair(2)"); 115272343Sngie} 116272343Sngie 117272343SngieATF_TC_BODY(socketpair_nonblock, tc) 118272343Sngie{ 119272343Sngie run(SOCK_NONBLOCK); 120272343Sngie} 121272343Sngie 122272343SngieATF_TC(socketpair_cloexec); 123272343SngieATF_TC_HEAD(socketpair_cloexec, tc) 124272343Sngie{ 125272343Sngie atf_tc_set_md_var(tc, "descr", "A close-on-exec of socketpair(2)"); 126272343Sngie} 127272343Sngie 128272343SngieATF_TC_BODY(socketpair_cloexec, tc) 129272343Sngie{ 130272343Sngie run(SOCK_CLOEXEC); 131272343Sngie} 132272343Sngie 133272343SngieATF_TP_ADD_TCS(tp) 134272343Sngie{ 135272343Sngie 136272343Sngie ATF_TP_ADD_TC(tp, socketpair_basic); 137272343Sngie ATF_TP_ADD_TC(tp, socketpair_nonblock); 138272343Sngie ATF_TP_ADD_TC(tp, socketpair_cloexec); 139272343Sngie 140272343Sngie return atf_no_error(); 141272343Sngie} 142