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