1303980Sngie/*	$NetBSD: t_print.c,v 1.2 2014/12/03 13:10:49 christos Exp $	*/
2303980Sngie
3303980Sngie/*-
4303980Sngie * Copyright (c) 2014 The NetBSD Foundation, Inc.
5303980Sngie * All rights reserved.
6303980Sngie *
7303980Sngie * This code is derived from software contributed to The NetBSD Foundation
8303980Sngie * by Christos Zoulas.
9303980Sngie *
10303980Sngie * Redistribution and use in source and binary forms, with or without
11303980Sngie * modification, are permitted provided that the following conditions
12303980Sngie * are met:
13303980Sngie * 1. Redistributions of source code must retain the above copyright
14303980Sngie *    notice, this list of conditions and the following disclaimer.
15303980Sngie * 2. Redistributions in binary form must reproduce the above copyright
16303980Sngie *    notice, this list of conditions and the following disclaimer in the
17303980Sngie *    documentation and/or other materials provided with the distribution.
18303980Sngie *
19303980Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20303980Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21303980Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22303980Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23303980Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24303980Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25303980Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26303980Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27303980Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28303980Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29303980Sngie * POSSIBILITY OF SUCH DAMAGE.
30303980Sngie */
31303980Sngie#include <sys/cdefs.h>
32303980Sngie__RCSID("$NetBSD: t_print.c,v 1.2 2014/12/03 13:10:49 christos Exp $");
33303980Sngie
34303980Sngie#include "netinet6/in6_print.c"
35303980Sngie#include "netinet/in_print.c"
36303980Sngie
37303980Sngie#include <atf-c.h>
38303980Sngie
39303980Sngiestatic const struct {
40303980Sngie	struct in6_addr ia;
41303980Sngie	const char *str;
42303980Sngie	int len;
43303980Sngie} tst[] = {
44303980Sngie	{
45303980Sngie		IN6ADDR_ANY_INIT,
46303980Sngie		"::",
47303980Sngie		2,
48303980Sngie	},
49303980Sngie	{
50303980Sngie		IN6ADDR_LOOPBACK_INIT,
51303980Sngie		"::1",
52303980Sngie		3,
53303980Sngie	},
54303980Sngie	{
55303980Sngie		IN6ADDR_NODELOCAL_ALLNODES_INIT,
56303980Sngie		"ff01::1",
57303980Sngie		7,
58303980Sngie	},
59303980Sngie	{
60303980Sngie		{{{ 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
61303980Sngie		    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }}},
62303980Sngie		"1020:3040:5060:7080:102:304:506:708",
63303980Sngie		35,
64303980Sngie	},
65303980Sngie	{
66303980Sngie		{{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67303980Sngie		    0x00, 0x00, 0xff, 0xff, 0x88, 0x44, 0x22, 0x11 }}},
68303980Sngie		"::ffff:136.68.34.17",
69303980Sngie		19,
70303980Sngie	},
71303980Sngie};
72303980Sngie
73303980Sngie
74303980SngieATF_TC(in6_print);
75303980SngieATF_TC_HEAD(in6_print, tc)
76303980Sngie{
77303980Sngie
78303980Sngie	atf_tc_set_md_var(tc, "descr", "printing of struct in6_addr");
79303980Sngie}
80303980Sngie
81303980SngieATF_TC_BODY(in6_print, tc)
82303980Sngie{
83303980Sngie	char buf[INET6_ADDRSTRLEN];
84303980Sngie	int r;
85303980Sngie	size_t l = sizeof(buf);
86303980Sngie
87303980Sngie	for (size_t i = 0; i < __arraycount(tst); i++) {
88303980Sngie		r = in6_print(buf, l, &tst[i].ia);
89303980Sngie		ATF_REQUIRE_STREQ(buf, tst[i].str);
90303980Sngie		ATF_REQUIRE_EQ(r, tst[i].len);
91303980Sngie	}
92303980Sngie
93303980Sngie	l = 12;
94303980Sngie	for (size_t i = 0; i < __arraycount(tst); i++) {
95303980Sngie		r = in6_print(buf, l, &tst[i].ia);
96303980Sngie		ATF_CHECK(strncmp(buf, tst[i].str, l - 1) == 0);
97303980Sngie		if (r > (int)l)
98303980Sngie			ATF_REQUIRE_EQ(buf[l - 1], '\0');
99303980Sngie		ATF_REQUIRE_EQ(r, tst[i].len);
100303980Sngie	}
101303980Sngie}
102303980Sngie
103303980SngieATF_TC(sin6_print);
104303980SngieATF_TC_HEAD(sin6_print, tc)
105303980Sngie{
106303980Sngie
107303980Sngie	atf_tc_set_md_var(tc, "descr", "printing of sockaddr_in6");
108303980Sngie}
109303980Sngie
110303980SngieATF_TC_BODY(sin6_print, tc)
111303980Sngie{
112303980Sngie	char buf[1024];
113303980Sngie	char res[1024];
114303980Sngie	int r, e;
115303980Sngie	size_t l = sizeof(buf);
116303980Sngie	struct sockaddr_in6 sin6;
117303980Sngie	memset(&sin6, 0, sizeof(sin6));
118303980Sngie
119303980Sngie	for (size_t i = 0; i < __arraycount(tst); i++) {
120303980Sngie		sin6.sin6_addr = tst[i].ia;
121303980Sngie		sin6.sin6_port = (in_port_t)htons(i);
122303980Sngie		r = sin6_print(buf, l, &sin6);
123303980Sngie		if (i == 0)
124303980Sngie			e = snprintf(res, sizeof(res), "%s", tst[i].str);
125303980Sngie		else
126303980Sngie			e = snprintf(res, sizeof(res), "[%s]:%zu",
127303980Sngie			    tst[i].str, i);
128303980Sngie
129303980Sngie		ATF_REQUIRE_STREQ(buf, res);
130303980Sngie		ATF_REQUIRE_EQ(r, e);
131303980Sngie	}
132303980Sngie
133303980Sngie	l = 14;
134303980Sngie	for (size_t i = 0; i < __arraycount(tst); i++) {
135303980Sngie		sin6.sin6_addr = tst[i].ia;
136303980Sngie		sin6.sin6_port = (in_port_t)htons(i);
137303980Sngie		r = sin6_print(buf, l, &sin6);
138303980Sngie		if (i == 0)
139303980Sngie			e = snprintf(res, l, "%s", tst[i].str);
140303980Sngie		else
141303980Sngie			e = snprintf(res, l, "[%s]:%zu", tst[i].str, i);
142303980Sngie
143303980Sngie		ATF_REQUIRE_STREQ(buf, res);
144303980Sngie		ATF_REQUIRE_EQ(r, e);
145303980Sngie	}
146303980Sngie}
147303980Sngie
148303980SngieATF_TP_ADD_TCS(tp)
149303980Sngie{
150303980Sngie
151303980Sngie	ATF_TP_ADD_TC(tp, in6_print);
152303980Sngie	ATF_TP_ADD_TC(tp, sin6_print);
153303980Sngie	return atf_no_error();
154303980Sngie}
155