]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/sockets/unix_seqpacket/unix_seqpacket.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / regression / sockets / unix_seqpacket / unix_seqpacket.c
1 /*-
2  * Copyright (c) 2009 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * This software was developed at the University of Cambridge Computer
6  * Laboratory with support from a grant from Google, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/socket.h>
34 #include <sys/un.h>
35
36 #include <err.h>
37 #include <errno.h>
38 #include <limits.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #define FAILERR(str)    err(-1, "%s: %s", __func__, str)
44 #define FAILERRX(str)   errx(-1, "%s: %s", __func__, str)
45
46 static void
47 test_socket(void)
48 {
49         int s;
50
51         s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
52         if (s < 0)
53                 FAILERR("socket");
54         (void)close(s);
55 }
56
57 static void
58 test_socketpair(void)
59 {
60         int sv[2];
61
62         if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sv) < 0)
63                 FAILERR("socketpair");
64         (void)close(sv[0]);
65         (void)close(sv[1]);
66 }
67
68 static void
69 test_listen_unbound(void)
70 {
71         int s;
72
73         s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
74         if (s < 0)
75                 FAILERR("socket");
76         if (listen(s, -1) == 0)
77                 FAILERRX("listen");
78         (void)close(s);
79 }
80
81 static void
82 test_bind(void)
83 {
84         struct sockaddr_un sun;
85         char path[PATH_MAX];
86         int s;
87
88         snprintf(path, sizeof(path), "/tmp/lds.XXXXXXXXX");
89         if (mktemp(path) == NULL)
90                 FAILERR("mktemp");
91         s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
92         if (s < 0)
93                 FAILERR("socket");
94         bzero(&sun, sizeof(sun));
95         sun.sun_family = AF_LOCAL;
96         sun.sun_len = sizeof(sun);
97         strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
98         if (bind(s, (struct sockaddr *)&sun, sizeof(sun)) < 0)
99                 FAILERR("bind");
100         close(s);
101         (void)unlink(path);
102 }
103
104 static void
105 test_listen_bound(void)
106 {
107         struct sockaddr_un sun;
108         char path[PATH_MAX];
109         int s;
110
111         snprintf(path, sizeof(path), "/tmp/lds.XXXXXXXXX");
112         if (mktemp(path) == NULL)
113                 FAILERR("mktemp");
114         s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
115         if (s < 0)
116                 FAILERR("socket");
117         bzero(&sun, sizeof(sun));
118         sun.sun_family = AF_LOCAL;
119         sun.sun_len = sizeof(sun);
120         strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
121         if (bind(s, (struct sockaddr *)&sun, sizeof(sun)) < 0)
122                 FAILERR("bind");
123         if (listen(s, -1)) {
124                 (void)unlink(path);
125                 FAILERR("bind");
126         }
127         close(s);
128         (void)unlink(path);
129 }
130
131 int
132 main(int argc, char *argv[])
133 {
134
135         test_socket();
136         test_socketpair();
137         test_listen_unbound();
138         test_bind();
139         test_listen_bound();
140 }