xref: /netbsd-src/tests/lib/libc/sys/t_bind.c (revision cefecbaa6b5e7792ef4ad9cece8b0893813878bc)
1*cefecbaaSrtr /*	$NetBSD: t_bind.c,v 1.3 2015/04/05 23:28:10 rtr Exp $	*/
24839e0f0Smartin /*
34839e0f0Smartin  * Copyright (c) 2015 The NetBSD Foundation, Inc.
44839e0f0Smartin  * All rights reserved.
54839e0f0Smartin  *
64839e0f0Smartin  * Redistribution and use in source and binary forms, with or without
74839e0f0Smartin  * modification, are permitted provided that the following conditions
84839e0f0Smartin  * are met:
94839e0f0Smartin  * 1. Redistributions of source code must retain the above copyright
104839e0f0Smartin  *    notice, this list of conditions and the following disclaimer.
114839e0f0Smartin  * 2. Redistributions in binary form must reproduce the above copyright
124839e0f0Smartin  *    notice, this list of conditions and the following disclaimer in the
134839e0f0Smartin  *    documentation and/or other materials provided with the distribution.
144839e0f0Smartin  *
154839e0f0Smartin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
164839e0f0Smartin  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
174839e0f0Smartin  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
184839e0f0Smartin  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
194839e0f0Smartin  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
204839e0f0Smartin  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214839e0f0Smartin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
224839e0f0Smartin  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
234839e0f0Smartin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
244839e0f0Smartin  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
254839e0f0Smartin  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
264839e0f0Smartin  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274839e0f0Smartin  */
284839e0f0Smartin 
294839e0f0Smartin #include <errno.h>
304839e0f0Smartin #include <stdlib.h>
314839e0f0Smartin #include <string.h>
324839e0f0Smartin 
334839e0f0Smartin #include <unistd.h>
344839e0f0Smartin 
354839e0f0Smartin #include <sys/socket.h>
364839e0f0Smartin #include <arpa/inet.h>
374839e0f0Smartin #include <netinet/in.h>
384839e0f0Smartin 
394839e0f0Smartin #include <atf-c.h>
404839e0f0Smartin 
414839e0f0Smartin ATF_TC(bind_foreign_family);
424839e0f0Smartin 
ATF_TC_HEAD(bind_foreign_family,tc)434839e0f0Smartin ATF_TC_HEAD(bind_foreign_family, tc)
444839e0f0Smartin {
45*cefecbaaSrtr 	atf_tc_set_md_var(tc, "descr", "Checks that binding a socket "
464839e0f0Smartin 	    "with a different address family fails");
474839e0f0Smartin }
484839e0f0Smartin 
ATF_TC_BODY(bind_foreign_family,tc)494839e0f0Smartin ATF_TC_BODY(bind_foreign_family, tc)
504839e0f0Smartin {
514839e0f0Smartin 	struct sockaddr_in addr;
524839e0f0Smartin 
53a954fb58Srtr 	/* addr.sin_family = AF_UNSPEC = 0 */
544839e0f0Smartin 	memset(&addr, 0, sizeof(addr));
55a954fb58Srtr 
56a954fb58Srtr 	/*
57a954fb58Srtr 	 * it is not necessary to initialize sin_{addr,port} since
58a954fb58Srtr 	 * those structure members shall not be accessed if bind
59a954fb58Srtr 	 * fails correctly.
60a954fb58Srtr 	 */
614839e0f0Smartin 
624839e0f0Smartin 	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
634839e0f0Smartin 	ATF_REQUIRE(sock != -1);
644839e0f0Smartin 
654839e0f0Smartin 	/* should fail but currently doesn't */
664839e0f0Smartin 	ATF_REQUIRE(-1 == bind(sock, (struct sockaddr *)&addr, sizeof(addr)));
67a954fb58Srtr 	ATF_REQUIRE(EAFNOSUPPORT == errno);
684839e0f0Smartin 
694839e0f0Smartin 	close(sock);
704839e0f0Smartin }
714839e0f0Smartin 
ATF_TP_ADD_TCS(tp)724839e0f0Smartin ATF_TP_ADD_TCS(tp)
734839e0f0Smartin {
744839e0f0Smartin 
754839e0f0Smartin 	ATF_TP_ADD_TC(tp, bind_foreign_family);
764839e0f0Smartin 
774839e0f0Smartin 	return atf_no_error();
784839e0f0Smartin }
79