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