xref: /freebsd/tools/regression/sockets/fstat/fstat.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1cb824561SRobert Watson /*-
2cb824561SRobert Watson  * Copyright (c) 2008 Robert N. M. Watson
3cb824561SRobert Watson  * All rights reserved.
4cb824561SRobert Watson  *
5cb824561SRobert Watson  * Redistribution and use in source and binary forms, with or without
6cb824561SRobert Watson  * modification, are permitted provided that the following conditions
7cb824561SRobert Watson  * are met:
8cb824561SRobert Watson  * 1. Redistributions of source code must retain the above copyright
9cb824561SRobert Watson  *    notice, this list of conditions and the following disclaimer.
10cb824561SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
11cb824561SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
12cb824561SRobert Watson  *    documentation and/or other materials provided with the distribution.
13cb824561SRobert Watson  *
14cb824561SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15cb824561SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16cb824561SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17cb824561SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18cb824561SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19cb824561SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20cb824561SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21cb824561SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22cb824561SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23cb824561SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24cb824561SRobert Watson  * SUCH DAMAGE.
25cb824561SRobert Watson  */
26cb824561SRobert Watson 
27cb824561SRobert Watson #include <sys/types.h>
28cb824561SRobert Watson #include <sys/socket.h>
29cb824561SRobert Watson #include <sys/stat.h>
30cb824561SRobert Watson 
31cb824561SRobert Watson #include <err.h>
32cb824561SRobert Watson #include <unistd.h>
33cb824561SRobert Watson 
34cb824561SRobert Watson /*
35cb824561SRobert Watson  * Basic test to make sure that fstat(2) returns success on various socket
36cb824561SRobert Watson  * types.  In the future we should also validate the fields, confirming
37cb824561SRobert Watson  * expected results such as the effect of shutdown(2) on permissions, etc.
38cb824561SRobert Watson  */
39cb824561SRobert Watson 
40cb824561SRobert Watson static void
dotest(int domain,int type,int protocol)41cb824561SRobert Watson dotest(int domain, int type, int protocol)
42cb824561SRobert Watson {
43cb824561SRobert Watson 	struct stat sb;
44cb824561SRobert Watson 	int sock;
45cb824561SRobert Watson 
46cb824561SRobert Watson 	sock = socket(domain, type, protocol);
47cb824561SRobert Watson 	if (sock < 0)
48cb824561SRobert Watson 		err(-1, "socket(%d, %d, %d)", domain, type, protocol);
49cb824561SRobert Watson 
50cb824561SRobert Watson 	if (fstat(sock, &sb) < 0)
51cb824561SRobert Watson 		err(-1, "fstat on socket(%d, %d, %d)", domain, type,
52cb824561SRobert Watson 		    protocol);
53cb824561SRobert Watson 
54cb824561SRobert Watson 	close(sock);
55cb824561SRobert Watson }
56cb824561SRobert Watson 
57cb824561SRobert Watson int
main(void)58*cc469a3bSEnji Cooper main(void)
59cb824561SRobert Watson {
60cb824561SRobert Watson 
61cb824561SRobert Watson 	dotest(PF_INET, SOCK_DGRAM, 0);
62cb824561SRobert Watson 	dotest(PF_INET, SOCK_STREAM, 0);
63cb824561SRobert Watson 	dotest(PF_INET6, SOCK_DGRAM, 0);
64cb824561SRobert Watson 	dotest(PF_INET6, SOCK_STREAM, 0);
65cb824561SRobert Watson 	dotest(PF_LOCAL, SOCK_DGRAM, 0);
66cb824561SRobert Watson 	dotest(PF_LOCAL, SOCK_STREAM, 0);
67cb824561SRobert Watson 
68cb824561SRobert Watson 	return (0);
69cb824561SRobert Watson }
70