]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/regression/sockets/socketpair/socketpair.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / tools / regression / sockets / socketpair / socketpair.c
1 /*-
2  * Copyright (c) 2004 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/types.h>
30 #include <sys/socket.h>
31
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 /*
39  * Open, then close a set of UNIX domain socket pairs for datagram and
40  * stream.
41  *
42  * Confirm that we can't open INET datagram or stream socket pairs.
43  *
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
47  * right places.
48  */
49 int
50 main(int argc, char *argv[])
51 {
52         int sv[2];
53
54         /*
55          * UNIX domain socket pair, datagram.
56          */
57         if (socketpair(PF_UNIX, SOCK_DGRAM, 0, sv) != 0) {
58                 fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM): %s\n",
59                     strerror(errno));
60                 fprintf(stderr, "FAIL\n");
61                 exit(-1);
62         }
63         if (close(sv[0]) != 0) {
64                 fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM) close 0: %s\n",
65                     strerror(errno));
66                 fprintf(stderr, "FAIL\n");
67                 exit(-1);
68         }
69         if (close(sv[1]) != 0) {
70                 fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM) close 1: %s\n",
71                     strerror(errno));
72                 fprintf(stderr, "FAIL\n");
73                 exit(-1);
74         }
75
76         /*
77          * UNIX domain socket pair, stream.
78          */
79         if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) != 0) {
80                 fprintf(stderr, "socketpair(PF_UNIX, SOCK_STREAM): %s\n",
81                     strerror(errno));
82                 fprintf(stderr, "FAIL\n");
83                 exit(-1);
84         }
85         if (close(sv[0]) != 0) {
86                 fprintf(stderr, "socketpair(PF_UNIX, SOCK_STREAM) close 0: %s\n",
87                     strerror(errno));
88                 fprintf(stderr, "FAIL\n");
89                 exit(-1);
90         }
91         if (close(sv[1]) != 0) {
92                 fprintf(stderr, "socketpair(PF_UNIX, SOCK_STREAM) close 1: "
93                     "%s\n", strerror(errno));
94                 fprintf(stderr, "FAIL\n");
95                 exit(-1);
96         }
97
98         /*
99          * Confirm that PF_INET datagram socket pair creation fails.
100          */
101         if (socketpair(PF_INET, SOCK_DGRAM, 0, sv) == 0) {
102                 fprintf(stderr, "socketpair(PF_INET, SOCK_DGRAM): opened\n");
103                 fprintf(stderr, "FAIL\n");
104                 exit(-1);
105         }
106         if (errno != EOPNOTSUPP) {
107                 fprintf(stderr, "socketpair(PF_INET, SOCK_DGRAM): %s\n",
108                     strerror(errno));
109                 fprintf(stderr, "FAIL\n");
110         }
111
112         /*
113          * Confirm that PF_INET stream socket pair creation fails.
114          */
115         if (socketpair(PF_INET, SOCK_STREAM, 0, sv) == 0) {
116                 fprintf(stderr, "socketpair(PF_INET, SOCK_STREAM): opened\n");
117                 fprintf(stderr, "FAIL\n");
118                 exit(-1);
119         }
120         if (errno != EOPNOTSUPP) {
121                 fprintf(stderr, "socketpair(PF_INET, SOCK_STREAM): %s\n",
122                     strerror(errno));
123                 fprintf(stderr, "FAIL\n");
124         }
125
126         fprintf(stderr, "PASS\n");
127         exit(0);
128 }