xref: /freebsd/contrib/netbsd-tests/sys/netinet/t_print.c (revision 25ecdc7d52770caf1c9b44b5ec11f468f6b636f3)
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 "netinet/in_print.c"
35 
36 #include <atf-c.h>
37 
38 static const struct {
39 	struct in_addr ia;
40 	const char *str;
41 	int len;
42 } tst[] = {
43 	{
44 		{	.s_addr = ntohl(INADDR_LOOPBACK)	},
45 		"127.0.0.1",
46 		9,
47 	},
48 	{
49 		{	.s_addr = ntohl(INADDR_ANY)		},
50 		"0.0.0.0",
51 		7,
52 	},
53 	{
54 		{	.s_addr = ntohl(IN_CLASSC_NET)		},
55 		"255.255.255.0",
56 		13,
57 	},
58 	{
59 		{	.s_addr = ntohl(INADDR_ALLHOSTS_GROUP)	},
60 		"224.0.0.1",
61 		9,
62 	},
63 };
64 
65 
66 ATF_TC(in_print);
67 ATF_TC_HEAD(in_print, tc)
68 {
69 
70 	atf_tc_set_md_var(tc, "descr", "printing of struct in_addr");
71 }
72 
73 ATF_TC_BODY(in_print, tc)
74 {
75 	char buf[INET_ADDRSTRLEN];
76 	int r;
77 	size_t l = sizeof(buf);
78 
79 	for (size_t i = 0; i < __arraycount(tst); i++) {
80 		r = in_print(buf, l, &tst[i].ia);
81 		ATF_REQUIRE_STREQ(buf, tst[i].str);
82 		ATF_REQUIRE_EQ(r, tst[i].len);
83 	}
84 
85 	l = 8;
86 	for (size_t i = 0; i < __arraycount(tst); i++) {
87 		r = in_print(buf, l, &tst[i].ia);
88 		ATF_CHECK(strncmp(buf, tst[i].str, l - 1) == 0);
89 		ATF_REQUIRE_EQ(buf[l - 1], '\0');
90 		ATF_REQUIRE_EQ(r, tst[i].len);
91 	}
92 }
93 
94 ATF_TC(sin_print);
95 ATF_TC_HEAD(sin_print, tc)
96 {
97 
98 	atf_tc_set_md_var(tc, "descr", "printing of sockaddr_in");
99 }
100 
101 ATF_TC_BODY(sin_print, tc)
102 {
103 	char buf[1024];
104 	char res[1024];
105 	int r, e;
106 	size_t l = sizeof(buf);
107 	struct sockaddr_in sin;
108 	memset(&sin, 0, sizeof(sin));
109 
110 	for (size_t i = 0; i < __arraycount(tst); i++) {
111 		sin.sin_addr = tst[i].ia;
112 		sin.sin_port = (in_port_t)htons(i);
113 		r = sin_print(buf, l, &sin);
114 		if (i == 0)
115 			e = snprintf(res, sizeof(res), "%s", tst[i].str);
116 		else
117 			e = snprintf(res, sizeof(res), "%s:%zu", tst[i].str, i);
118 
119 		ATF_REQUIRE_STREQ(buf, res);
120 		ATF_REQUIRE_EQ(r, e);
121 	}
122 
123 	l = 14;
124 	for (size_t i = 0; i < __arraycount(tst); i++) {
125 		sin.sin_addr = tst[i].ia;
126 		sin.sin_port = (in_port_t)htons(i);
127 		r = sin_print(buf, l, &sin);
128 		if (i == 0)
129 			e = snprintf(res, l, "%s", tst[i].str);
130 		else
131 			e = snprintf(res, l, "%s:%zu", tst[i].str, i);
132 
133 		ATF_REQUIRE_STREQ(buf, res);
134 		ATF_REQUIRE_EQ(r, e);
135 	}
136 }
137 
138 ATF_TP_ADD_TCS(tp)
139 {
140 
141 	ATF_TP_ADD_TC(tp, in_print);
142 	ATF_TP_ADD_TC(tp, sin_print);
143 	return atf_no_error();
144 }
145