1303980Sngie/*	$NetBSD: t_bind.c,v 1.3 2015/04/05 23:28:10 rtr Exp $	*/
2303980Sngie/*
3303980Sngie * Copyright (c) 2015 The NetBSD Foundation, Inc.
4303980Sngie * All rights reserved.
5303980Sngie *
6303980Sngie * Redistribution and use in source and binary forms, with or without
7303980Sngie * modification, are permitted provided that the following conditions
8303980Sngie * are met:
9303980Sngie * 1. Redistributions of source code must retain the above copyright
10303980Sngie *    notice, this list of conditions and the following disclaimer.
11303980Sngie * 2. Redistributions in binary form must reproduce the above copyright
12303980Sngie *    notice, this list of conditions and the following disclaimer in the
13303980Sngie *    documentation and/or other materials provided with the distribution.
14303980Sngie *
15303980Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
16303980Sngie * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17303980Sngie * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18303980Sngie * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19303980Sngie * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
20303980Sngie * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21303980Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22303980Sngie * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23303980Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24303980Sngie * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25303980Sngie * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26303980Sngie * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27303980Sngie */
28303980Sngie
29303980Sngie#include <errno.h>
30303980Sngie#include <stdlib.h>
31303980Sngie#include <string.h>
32303980Sngie
33303980Sngie#include <unistd.h>
34303980Sngie
35303980Sngie#include <sys/socket.h>
36303980Sngie#include <arpa/inet.h>
37303980Sngie#include <netinet/in.h>
38303980Sngie
39303980Sngie#include <atf-c.h>
40303980Sngie
41303980SngieATF_TC(bind_foreign_family);
42303980Sngie
43303980SngieATF_TC_HEAD(bind_foreign_family, tc)
44303980Sngie{
45303980Sngie	atf_tc_set_md_var(tc, "descr", "Checks that binding a socket "
46303980Sngie	    "with a different address family fails");
47303980Sngie}
48303980Sngie
49303980SngieATF_TC_BODY(bind_foreign_family, tc)
50303980Sngie{
51303980Sngie	struct sockaddr_in addr;
52303980Sngie
53303980Sngie	/* addr.sin_family = AF_UNSPEC = 0 */
54303980Sngie	memset(&addr, 0, sizeof(addr));
55303980Sngie
56303980Sngie	/*
57303980Sngie	 * it is not necessary to initialize sin_{addr,port} since
58303980Sngie	 * those structure members shall not be accessed if bind
59303980Sngie	 * fails correctly.
60303980Sngie	 */
61303980Sngie
62303980Sngie	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
63303980Sngie	ATF_REQUIRE(sock != -1);
64303980Sngie
65303980Sngie	/* should fail but currently doesn't */
66303980Sngie	ATF_REQUIRE(-1 == bind(sock, (struct sockaddr *)&addr, sizeof(addr)));
67303980Sngie	ATF_REQUIRE(EAFNOSUPPORT == errno);
68303980Sngie
69303980Sngie	close(sock);
70303980Sngie}
71303980Sngie
72303980SngieATF_TP_ADD_TCS(tp)
73303980Sngie{
74303980Sngie
75303980Sngie	ATF_TP_ADD_TC(tp, bind_foreign_family);
76303980Sngie
77303980Sngie	return atf_no_error();
78303980Sngie}
79