xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_bind.c (revision c22165b4f1f5d38b681921797a44b3ba8c13b7e0)
1*640235e2SEnji Cooper /*	$NetBSD: t_bind.c,v 1.3 2015/04/05 23:28:10 rtr Exp $	*/
2*640235e2SEnji Cooper /*
3*640235e2SEnji Cooper  * Copyright (c) 2015 The NetBSD Foundation, Inc.
4*640235e2SEnji Cooper  * All rights reserved.
5*640235e2SEnji Cooper  *
6*640235e2SEnji Cooper  * Redistribution and use in source and binary forms, with or without
7*640235e2SEnji Cooper  * modification, are permitted provided that the following conditions
8*640235e2SEnji Cooper  * are met:
9*640235e2SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
10*640235e2SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
11*640235e2SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
12*640235e2SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
13*640235e2SEnji Cooper  *    documentation and/or other materials provided with the distribution.
14*640235e2SEnji Cooper  *
15*640235e2SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
16*640235e2SEnji Cooper  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17*640235e2SEnji Cooper  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18*640235e2SEnji Cooper  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*640235e2SEnji Cooper  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
20*640235e2SEnji Cooper  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*640235e2SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22*640235e2SEnji Cooper  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*640235e2SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24*640235e2SEnji Cooper  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25*640235e2SEnji Cooper  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26*640235e2SEnji Cooper  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*640235e2SEnji Cooper  */
28*640235e2SEnji Cooper 
29*640235e2SEnji Cooper #include <errno.h>
30*640235e2SEnji Cooper #include <stdlib.h>
31*640235e2SEnji Cooper #include <string.h>
32*640235e2SEnji Cooper 
33*640235e2SEnji Cooper #include <unistd.h>
34*640235e2SEnji Cooper 
35*640235e2SEnji Cooper #include <sys/socket.h>
36*640235e2SEnji Cooper #include <arpa/inet.h>
37*640235e2SEnji Cooper #include <netinet/in.h>
38*640235e2SEnji Cooper 
39*640235e2SEnji Cooper #include <atf-c.h>
40*640235e2SEnji Cooper 
41*640235e2SEnji Cooper ATF_TC(bind_foreign_family);
42*640235e2SEnji Cooper 
ATF_TC_HEAD(bind_foreign_family,tc)43*640235e2SEnji Cooper ATF_TC_HEAD(bind_foreign_family, tc)
44*640235e2SEnji Cooper {
45*640235e2SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Checks that binding a socket "
46*640235e2SEnji Cooper 	    "with a different address family fails");
47*640235e2SEnji Cooper }
48*640235e2SEnji Cooper 
ATF_TC_BODY(bind_foreign_family,tc)49*640235e2SEnji Cooper ATF_TC_BODY(bind_foreign_family, tc)
50*640235e2SEnji Cooper {
51*640235e2SEnji Cooper 	struct sockaddr_in addr;
52*640235e2SEnji Cooper 
53*640235e2SEnji Cooper 	/* addr.sin_family = AF_UNSPEC = 0 */
54*640235e2SEnji Cooper 	memset(&addr, 0, sizeof(addr));
55*640235e2SEnji Cooper 
56*640235e2SEnji Cooper 	/*
57*640235e2SEnji Cooper 	 * it is not necessary to initialize sin_{addr,port} since
58*640235e2SEnji Cooper 	 * those structure members shall not be accessed if bind
59*640235e2SEnji Cooper 	 * fails correctly.
60*640235e2SEnji Cooper 	 */
61*640235e2SEnji Cooper 
62*640235e2SEnji Cooper 	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
63*640235e2SEnji Cooper 	ATF_REQUIRE(sock != -1);
64*640235e2SEnji Cooper 
65*640235e2SEnji Cooper 	/* should fail but currently doesn't */
66*640235e2SEnji Cooper 	ATF_REQUIRE(-1 == bind(sock, (struct sockaddr *)&addr, sizeof(addr)));
67*640235e2SEnji Cooper 	ATF_REQUIRE(EAFNOSUPPORT == errno);
68*640235e2SEnji Cooper 
69*640235e2SEnji Cooper 	close(sock);
70*640235e2SEnji Cooper }
71*640235e2SEnji Cooper 
ATF_TP_ADD_TCS(tp)72*640235e2SEnji Cooper ATF_TP_ADD_TCS(tp)
73*640235e2SEnji Cooper {
74*640235e2SEnji Cooper 
75*640235e2SEnji Cooper 	ATF_TP_ADD_TC(tp, bind_foreign_family);
76*640235e2SEnji Cooper 
77*640235e2SEnji Cooper 	return atf_no_error();
78*640235e2SEnji Cooper }
79