xref: /netbsd-src/tests/net/sys/t_rfc6056.c (revision 9ddb6ab554e70fb9bbd90c3d96b812bc57755a14)
1 /* $NetBSD: t_rfc6056.c,v 1.2 2011/11/05 19:01:42 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_rfc6056.c,v 1.2 2011/11/05 19:01:42 jruoho Exp $");
33 
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netinet/udp.h>
38 #include <arpa/inet.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <stdlib.h>
45 #include <netdb.h>
46 #include <err.h>
47 
48 #include <atf-c.h>
49 
50 static void
51 test(const char *hostname, const char *service, int family, int al)
52 {
53 	static const char hello[] = "hello\n";
54 	int s, error;
55 	struct sockaddr_storage ss;
56 	struct addrinfo hints, *res;
57 
58 	memset(&hints, 0, sizeof(hints));
59 	hints.ai_family = family;
60 	hints.ai_socktype = SOCK_DGRAM;
61 
62 	error = getaddrinfo(hostname, service, &hints, &res);
63 	if (error)
64 		errx(EXIT_FAILURE, "Cannot get address for %s (%s)",
65 		    hostname, gai_strerror(error));
66 
67 	s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
68 	if (s == -1)
69 		err(EXIT_FAILURE, "socket");
70 
71 	if (setsockopt(s, IPPROTO_UDP, UDP_RFC6056ALGO, &al, sizeof(al)) == -1)
72 		err(EXIT_FAILURE, "setsockopt");
73 
74 	memset(&ss, 0, sizeof(ss));
75 	ss.ss_len = res->ai_addrlen;
76 	ss.ss_family = res->ai_family;
77 
78 	if (bind(s, (struct sockaddr *)&ss, ss.ss_len) == -1)
79 		err(EXIT_FAILURE, "bind");
80 
81 	if (sendto(s, hello, sizeof(hello) - 1, 0,
82 	    res->ai_addr, res->ai_addrlen) == -1)
83 		err(EXIT_FAILURE, "sendto");
84 
85 	if (close(s) == -1)
86 		err(EXIT_FAILURE, "close");
87 
88 	s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
89 	if (s == -1)
90 		err(EXIT_FAILURE, "socket");
91 
92 	if (setsockopt(s, IPPROTO_UDP, UDP_RFC6056ALGO, &al, sizeof(al)) == -1)
93 		err(EXIT_FAILURE, "setsockopt");
94 
95 	if (connect(s, res->ai_addr, res->ai_addrlen) == -1)
96 		err(EXIT_FAILURE, "connect");
97 
98 	if (send(s, hello, sizeof(hello) - 1, 0) == -1)
99 		err(EXIT_FAILURE, "send");
100 
101 	if (close(s) == -1)
102 		err(EXIT_FAILURE, "close");
103 
104 	freeaddrinfo(res);
105 }
106 
107 ATF_TC(inet4);
108 ATF_TC_HEAD(inet4, tc)
109 {
110 	atf_tc_set_md_var(tc, "descr", "Checks random port allocation "
111 	    "for ipv4");
112 }
113 
114 ATF_TC_BODY(inet4, tc)
115 {
116 	for (int i = 0; i < 6; i++)
117 		test("localhost", "http", AF_INET, i);
118 }
119 
120 ATF_TC(inet6);
121 ATF_TC_HEAD(inet6, tc)
122 {
123 	atf_tc_set_md_var(tc, "descr", "Checks random port allocation "
124 	    "for ipv6");
125 }
126 
127 ATF_TC_BODY(inet6, tc)
128 {
129 	for (int i = 0; i < 6; i++)
130 		test("localhost", "http", AF_INET6, i);
131 }
132 
133 ATF_TP_ADD_TCS(tp)
134 {
135         ATF_TP_ADD_TC(tp, inet4);
136         ATF_TP_ADD_TC(tp, inet6);
137 
138 	return atf_no_error();
139 }
140