xref: /freebsd/contrib/netbsd-tests/sys/netinet6/t_print.c (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1 /*	$NetBSD: t_print.c,v 1.2 2014/12/03 13:10:49 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2014 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_print.c,v 1.2 2014/12/03 13:10:49 christos Exp $");
33 
34 #include "netinet6/in6_print.c"
35 #include "netinet/in_print.c"
36 
37 #include <atf-c.h>
38 
39 static const struct {
40 	struct in6_addr ia;
41 	const char *str;
42 	int len;
43 } tst[] = {
44 	{
45 		IN6ADDR_ANY_INIT,
46 		"::",
47 		2,
48 	},
49 	{
50 		IN6ADDR_LOOPBACK_INIT,
51 		"::1",
52 		3,
53 	},
54 	{
55 		IN6ADDR_NODELOCAL_ALLNODES_INIT,
56 		"ff01::1",
57 		7,
58 	},
59 	{
60 		{{{ 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
61 		    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }}},
62 		"1020:3040:5060:7080:102:304:506:708",
63 		35,
64 	},
65 	{
66 		{{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 		    0x00, 0x00, 0xff, 0xff, 0x88, 0x44, 0x22, 0x11 }}},
68 		"::ffff:136.68.34.17",
69 		19,
70 	},
71 };
72 
73 
74 ATF_TC(in6_print);
75 ATF_TC_HEAD(in6_print, tc)
76 {
77 
78 	atf_tc_set_md_var(tc, "descr", "printing of struct in6_addr");
79 }
80 
81 ATF_TC_BODY(in6_print, tc)
82 {
83 	char buf[INET6_ADDRSTRLEN];
84 	int r;
85 	size_t l = sizeof(buf);
86 
87 	for (size_t i = 0; i < __arraycount(tst); i++) {
88 		r = in6_print(buf, l, &tst[i].ia);
89 		ATF_REQUIRE_STREQ(buf, tst[i].str);
90 		ATF_REQUIRE_EQ(r, tst[i].len);
91 	}
92 
93 	l = 12;
94 	for (size_t i = 0; i < __arraycount(tst); i++) {
95 		r = in6_print(buf, l, &tst[i].ia);
96 		ATF_CHECK(strncmp(buf, tst[i].str, l - 1) == 0);
97 		if (r > (int)l)
98 			ATF_REQUIRE_EQ(buf[l - 1], '\0');
99 		ATF_REQUIRE_EQ(r, tst[i].len);
100 	}
101 }
102 
103 ATF_TC(sin6_print);
104 ATF_TC_HEAD(sin6_print, tc)
105 {
106 
107 	atf_tc_set_md_var(tc, "descr", "printing of sockaddr_in6");
108 }
109 
110 ATF_TC_BODY(sin6_print, tc)
111 {
112 	char buf[1024];
113 	char res[1024];
114 	int r, e;
115 	size_t l = sizeof(buf);
116 	struct sockaddr_in6 sin6;
117 	memset(&sin6, 0, sizeof(sin6));
118 
119 	for (size_t i = 0; i < __arraycount(tst); i++) {
120 		sin6.sin6_addr = tst[i].ia;
121 		sin6.sin6_port = (in_port_t)htons(i);
122 		r = sin6_print(buf, l, &sin6);
123 		if (i == 0)
124 			e = snprintf(res, sizeof(res), "%s", tst[i].str);
125 		else
126 			e = snprintf(res, sizeof(res), "[%s]:%zu",
127 			    tst[i].str, i);
128 
129 		ATF_REQUIRE_STREQ(buf, res);
130 		ATF_REQUIRE_EQ(r, e);
131 	}
132 
133 	l = 14;
134 	for (size_t i = 0; i < __arraycount(tst); i++) {
135 		sin6.sin6_addr = tst[i].ia;
136 		sin6.sin6_port = (in_port_t)htons(i);
137 		r = sin6_print(buf, l, &sin6);
138 		if (i == 0)
139 			e = snprintf(res, l, "%s", tst[i].str);
140 		else
141 			e = snprintf(res, l, "[%s]:%zu", tst[i].str, i);
142 
143 		ATF_REQUIRE_STREQ(buf, res);
144 		ATF_REQUIRE_EQ(r, e);
145 	}
146 }
147 
148 ATF_TP_ADD_TCS(tp)
149 {
150 
151 	ATF_TP_ADD_TC(tp, in6_print);
152 	ATF_TP_ADD_TC(tp, sin6_print);
153 	return atf_no_error();
154 }
155