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/socket.h>
39 * Open, then close a set of UNIX domain socket pairs for datagram and
42 * Confirm that we can't open INET datagram or stream socket pairs.
44 * More tests should be added, including confirming that sending on either
45 * endpoint results in data at the other, that the right kind of socket was
46 * created (stream vs. datagram), and that message boundaries fall in the
50 main(int argc, char *argv[])
56 * UNIX domain socket pair, datagram.
58 if (socketpair(PF_UNIX, SOCK_DGRAM, 0, sv) != 0) {
59 fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM): %s\n",
61 fprintf(stderr, "FAIL\n");
64 if (close(sv[0]) != 0) {
65 fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM) close 0: %s\n",
67 fprintf(stderr, "FAIL\n");
70 if (close(sv[1]) != 0) {
71 fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM) close 1: %s\n",
73 fprintf(stderr, "FAIL\n");
78 * UNIX domain socket pair, stream.
80 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) != 0) {
81 fprintf(stderr, "socketpair(PF_UNIX, SOCK_STREAM): %s\n",
83 fprintf(stderr, "FAIL\n");
86 if (close(sv[0]) != 0) {
87 fprintf(stderr, "socketpair(PF_UNIX, SOCK_STREAM) close 0: %s\n",
89 fprintf(stderr, "FAIL\n");
92 if (close(sv[1]) != 0) {
93 fprintf(stderr, "socketpair(PF_UNIX, SOCK_STREAM) close 1: "
94 "%s\n", strerror(errno));
95 fprintf(stderr, "FAIL\n");
100 * Confirm that PF_INET datagram socket pair creation fails.
102 if (socketpair(PF_INET, SOCK_DGRAM, 0, sv) == 0) {
103 fprintf(stderr, "socketpair(PF_INET, SOCK_DGRAM): opened\n");
104 fprintf(stderr, "FAIL\n");
107 if (errno != EOPNOTSUPP) {
108 fprintf(stderr, "socketpair(PF_INET, SOCK_DGRAM): %s\n",
110 fprintf(stderr, "FAIL\n");
114 * Confirm that PF_INET stream socket pair creation fails.
116 if (socketpair(PF_INET, SOCK_STREAM, 0, sv) == 0) {
117 fprintf(stderr, "socketpair(PF_INET, SOCK_STREAM): opened\n");
118 fprintf(stderr, "FAIL\n");
121 if (errno != EOPNOTSUPP) {
122 fprintf(stderr, "socketpair(PF_INET, SOCK_STREAM): %s\n",
124 fprintf(stderr, "FAIL\n");
128 * Check for sequential fd allocation, and give up early if not.
130 fd1 = dup(STDIN_FILENO);
131 fd2 = dup(STDIN_FILENO);
132 if (fd2 != fd1 + 1) {
133 fprintf(stderr, "Non-sequential fd allocation\n");
134 fprintf(stderr, "FAIL\n");
138 /* Allocate a socketpair using a bad destination address. */
139 if (socketpair(PF_UNIX, SOCK_DGRAM, 0, NULL) == 0) {
140 fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM, NULL): opened\n");
141 fprintf(stderr, "FAIL\n");
144 if (errno != EFAULT) {
145 fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM, NULL): %s\n",
147 fprintf(stderr, "FAIL\n");
151 /* Allocate a file descriptor and make sure it's fd2+1. */
152 fd3 = dup(STDIN_FILENO);
153 if (fd3 != fd2 + 1) {
154 fprintf(stderr, "socketpair(..., NULL) allocated descriptors\n");
155 fprintf(stderr, "FAIL\n");
159 fprintf(stderr, "PASS\n");