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