1157602Srwatson/*-
2157602Srwatson * Copyright (c) 2006 Robert N. M. Watson
3157602Srwatson * All rights reserved.
4157602Srwatson *
5157602Srwatson * Redistribution and use in source and binary forms, with or without
6157602Srwatson * modification, are permitted provided that the following conditions
7157602Srwatson * are met:
8157602Srwatson * 1. Redistributions of source code must retain the above copyright
9157602Srwatson *    notice, this list of conditions and the following disclaimer.
10157602Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11157602Srwatson *    notice, this list of conditions and the following disclaimer in the
12157602Srwatson *    documentation and/or other materials provided with the distribution.
13157602Srwatson *
14157602Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15157602Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16157602Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17157602Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18157602Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19157602Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20157602Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21157602Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22157602Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23157602Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24157602Srwatson * SUCH DAMAGE.
25157602Srwatson *
26157602Srwatson * $FreeBSD$
27157602Srwatson */
28157602Srwatson
29157602Srwatson/*
30157602Srwatson * Simple UNIX domain socket regression test: create and tear down various
31157602Srwatson * supported and unsupported socket types.
32157602Srwatson */
33157602Srwatson
34157602Srwatson#include <sys/types.h>
35157602Srwatson#include <sys/socket.h>
36157602Srwatson#include <sys/un.h>
37157602Srwatson
38157602Srwatson#include <err.h>
39157602Srwatson#include <errno.h>
40157602Srwatson#include <unistd.h>
41157602Srwatson
42157602Srwatsonint
43281974Sngiemain(void)
44157602Srwatson{
45157602Srwatson	int sock, socks[2];
46157602Srwatson
47157602Srwatson	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
48157602Srwatson	if (sock < 0)
49157602Srwatson		err(-1, "socket(PF_LOCAL, SOCK_STREAM, 0)");
50157602Srwatson	close(sock);
51157602Srwatson
52157602Srwatson	sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
53157602Srwatson	if (sock < 0)
54157602Srwatson		err(-1, "socket(PF_LOCAL, SOCK_DGRAM, 0)");
55157602Srwatson	close(sock);
56157602Srwatson
57157602Srwatson	sock = socket(PF_LOCAL, SOCK_RAW, 0);
58157602Srwatson	if (sock >= 0) {
59157602Srwatson		close(sock);
60157602Srwatson		errx(-1, "socket(PF_LOCAL, SOCK_RAW, 0) returned %d", sock);
61157602Srwatson	}
62294641Sngie	if (errno != EPROTOTYPE)
63157602Srwatson		err(-1, "socket(PF_LOCAL, SOCK_RAW, 0)");
64157602Srwatson
65157602Srwatson	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, socks) < 0)
66157602Srwatson		err(-1, "socketpair(PF_LOCAL, SOCK_STREAM, 0, socks)");
67157602Srwatson	if (socks[0] < 0)
68157602Srwatson		errx(-1, "socketpair(PF_LOCAL, SOCK_STREAM, 0, socks) [0] < 0");
69157602Srwatson	if (socks[1] < 0)
70157602Srwatson		errx(-1, "socketpair(PF_LOCAL, SOCK_STREAM, 0, socks) [1] < 1");
71157602Srwatson	close(socks[0]);
72157602Srwatson	close(socks[1]);
73157602Srwatson
74157602Srwatson	if (socketpair(PF_LOCAL, SOCK_DGRAM, 0, socks) < 0)
75157602Srwatson		err(-1, "socketpair(PF_LOCAL, SOCK_DGRAM, 0, socks)");
76157602Srwatson	if (socks[0] < 0)
77157602Srwatson		errx(-1, "socketpair(PF_LOCAL, SOCK_DGRAM, 0, socks) [0] < 0");
78157602Srwatson	if (socks[1] < 0)
79157602Srwatson		errx(-1, "socketpair(PF_LOCAL, SOCK_DGRAM, 0, socks) [1] < 1");
80157602Srwatson	close(socks[0]);
81157602Srwatson	close(socks[1]);
82157602Srwatson
83157602Srwatson	return (0);
84157602Srwatson}
85