1183652Srwatson/*-
2183652Srwatson * Copyright (c) 2008 Robert N. M. Watson
3183652Srwatson * All rights reserved.
4183652Srwatson *
5183652Srwatson * Redistribution and use in source and binary forms, with or without
6183652Srwatson * modification, are permitted provided that the following conditions
7183652Srwatson * are met:
8183652Srwatson * 1. Redistributions of source code must retain the above copyright
9183652Srwatson *    notice, this list of conditions and the following disclaimer.
10183652Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11183652Srwatson *    notice, this list of conditions and the following disclaimer in the
12183652Srwatson *    documentation and/or other materials provided with the distribution.
13183652Srwatson *
14183652Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15183652Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16183652Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17183652Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18183652Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19183652Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20183652Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21183652Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22183652Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23183652Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24183652Srwatson * SUCH DAMAGE.
25183652Srwatson *
26183652Srwatson * $FreeBSD$
27183652Srwatson */
28183652Srwatson
29183652Srwatson#include <sys/types.h>
30183652Srwatson#include <sys/socket.h>
31183652Srwatson#include <sys/stat.h>
32183652Srwatson
33183652Srwatson#include <err.h>
34183652Srwatson#include <unistd.h>
35183652Srwatson
36183652Srwatson/*
37183652Srwatson * Basic test to make sure that fstat(2) returns success on various socket
38183652Srwatson * types.  In the future we should also validate the fields, confirming
39183652Srwatson * expected results such as the effect of shutdown(2) on permissions, etc.
40183652Srwatson */
41183652Srwatson
42183652Srwatsonstatic void
43183652Srwatsondotest(int domain, int type, int protocol)
44183652Srwatson{
45183652Srwatson	struct stat sb;
46183652Srwatson	int sock;
47183652Srwatson
48183652Srwatson	sock = socket(domain, type, protocol);
49183652Srwatson	if (sock < 0)
50183652Srwatson		err(-1, "socket(%d, %d, %d)", domain, type, protocol);
51183652Srwatson
52183652Srwatson	if (fstat(sock, &sb) < 0)
53183652Srwatson		err(-1, "fstat on socket(%d, %d, %d)", domain, type,
54183652Srwatson		    protocol);
55183652Srwatson
56183652Srwatson	close(sock);
57183652Srwatson}
58183652Srwatson
59183652Srwatsonint
60183652Srwatsonmain(int argc, char *argv[])
61183652Srwatson{
62183652Srwatson
63183652Srwatson	dotest(PF_INET, SOCK_DGRAM, 0);
64183652Srwatson	dotest(PF_INET, SOCK_STREAM, 0);
65183652Srwatson	dotest(PF_INET6, SOCK_DGRAM, 0);
66183652Srwatson	dotest(PF_INET6, SOCK_STREAM, 0);
67183652Srwatson	dotest(PF_LOCAL, SOCK_DGRAM, 0);
68183652Srwatson	dotest(PF_LOCAL, SOCK_STREAM, 0);
69183652Srwatson
70183652Srwatson	return (0);
71183652Srwatson}
72