t_bind.c revision 1.1 1 1.1 martin /* $NetBSD: t_bind.c,v 1.1 2015/04/05 06:36:52 martin 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.1 martin atf_tc_set_md_var(tc, "descr", "Checks that bindinging 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.1 martin memset(&addr, 0, sizeof(addr));
54 1.1 martin /* use a sin_family != the socket type */
55 1.1 martin addr.sin_port = htons(8000);
56 1.1 martin addr.sin_addr.s_addr = INADDR_ANY;
57 1.1 martin
58 1.1 martin int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
59 1.1 martin ATF_REQUIRE(sock != -1);
60 1.1 martin
61 1.1 martin /* should fail but currently doesn't */
62 1.1 martin ATF_REQUIRE(-1 == bind(sock, (struct sockaddr *)&addr, sizeof(addr)));
63 1.1 martin ATF_REQUIRE(EINVAL == errno);
64 1.1 martin
65 1.1 martin close(sock);
66 1.1 martin }
67 1.1 martin
68 1.1 martin ATF_TP_ADD_TCS(tp)
69 1.1 martin {
70 1.1 martin
71 1.1 martin ATF_TP_ADD_TC(tp, bind_foreign_family);
72 1.1 martin
73 1.1 martin return atf_no_error();
74 1.1 martin }
75