1313535Sngie/*	$NetBSD: t_print.c,v 1.2 2016/08/27 11:30: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>
32313535Sngie__RCSID("$NetBSD: t_print.c,v 1.2 2016/08/27 11:30:49 christos Exp $");
33303980Sngie
34303980Sngie#include "net/dl_print.c"
35303980Sngie
36303980Sngie#include <atf-c.h>
37303980Sngie
38303980Sngiestatic const struct {
39303980Sngie	struct dl_addr ia;
40303980Sngie	const char *str;
41303980Sngie	int len;
42303980Sngie} tst[] = {
43303980Sngie	{
44303980Sngie		{
45303980Sngie			.dl_type = 6,
46303980Sngie			.dl_nlen = 0,
47303980Sngie			.dl_alen = 6,
48303980Sngie			.dl_slen = 0,
49303980Sngie			.dl_data = {
50303980Sngie			    (char)0x01, (char)0xa2, (char)0x03,
51303980Sngie			    (char)0xc4, (char)0x05, (char)0xf6,
52303980Sngie			},
53303980Sngie		},
54303980Sngie		"/6#01:a2:03:c4:05:f6",
55303980Sngie		20,
56303980Sngie	},
57303980Sngie	{
58303980Sngie		{
59303980Sngie			.dl_type = 24,
60303980Sngie			.dl_nlen = 3,
61303980Sngie			.dl_alen = 6,
62303980Sngie			.dl_slen = 0,
63303980Sngie			.dl_data = {
64303980Sngie			    'l', 'o', '0',
65303980Sngie			    (char)0x11, (char)0x22, (char)0x33,
66303980Sngie			    (char)0x44, (char)0x55, (char)0x66,
67303980Sngie			},
68303980Sngie		},
69303980Sngie		"lo0/24#11:22:33:44:55:66",
70303980Sngie		24,
71303980Sngie	},
72303980Sngie	{
73303980Sngie		{
74303980Sngie			.dl_type = 24,
75303980Sngie			.dl_nlen = 7,
76303980Sngie			.dl_alen = 1,
77303980Sngie			.dl_slen = 0,
78303980Sngie			.dl_data = {
79303980Sngie			    'n', 'p', 'f', 'l', 'o', 'g', '0', (char)0xa5,
80303980Sngie			},
81303980Sngie		},
82303980Sngie		"npflog0/24#a5",
83303980Sngie		13,
84303980Sngie	},
85303980Sngie	{
86303980Sngie		{
87303980Sngie			.dl_type = 0,
88303980Sngie			.dl_nlen = 0,
89303980Sngie			.dl_alen = 0,
90303980Sngie			.dl_slen = 0,
91303980Sngie			.dl_data = {
92303980Sngie			    '\0'
93303980Sngie			},
94303980Sngie		},
95303980Sngie		"/0#",
96303980Sngie		3,
97303980Sngie	},
98303980Sngie};
99303980Sngie
100303980Sngie
101303980SngieATF_TC(dl_print);
102303980SngieATF_TC_HEAD(dl_print, tc)
103303980Sngie{
104303980Sngie
105303980Sngie	atf_tc_set_md_var(tc, "descr", "printing of link address");
106303980Sngie}
107303980Sngie
108303980SngieATF_TC_BODY(dl_print, tc)
109303980Sngie{
110303980Sngie	char buf[LINK_ADDRSTRLEN];
111303980Sngie	int r;
112303980Sngie	size_t l = sizeof(buf);
113303980Sngie
114303980Sngie	for (size_t i = 0; i < __arraycount(tst); i++) {
115303980Sngie		r = dl_print(buf, l, &tst[i].ia);
116303980Sngie		ATF_REQUIRE_STREQ(buf, tst[i].str);
117303980Sngie		ATF_REQUIRE_EQ(r, tst[i].len);
118303980Sngie	}
119303980Sngie
120303980Sngie	l = 4;
121303980Sngie	for (size_t i = 0; i < __arraycount(tst); i++) {
122303980Sngie		r = dl_print(buf, l, &tst[i].ia);
123303980Sngie		ATF_CHECK(strncmp(buf, tst[i].str, l - 1) == 0);
124303980Sngie		if (r > (int)l)
125303980Sngie			ATF_REQUIRE_EQ(buf[l - 1], '\0');
126303980Sngie		ATF_REQUIRE_EQ(r, tst[i].len);
127303980Sngie	}
128303980Sngie}
129303980Sngie
130303980SngieATF_TC(sdl_print);
131303980SngieATF_TC_HEAD(sdl_print, tc)
132303980Sngie{
133303980Sngie
134303980Sngie	atf_tc_set_md_var(tc, "descr", "printing of sockaddr_dl");
135303980Sngie}
136303980Sngie
137303980SngieATF_TC_BODY(sdl_print, tc)
138303980Sngie{
139303980Sngie	char buf[1024];
140303980Sngie	char res[1024];
141303980Sngie	int r, e;
142303980Sngie	size_t l = sizeof(buf);
143303980Sngie	struct sockaddr_dl sdl;
144303980Sngie
145303980Sngie	memset(&sdl, 0, sizeof(sdl));
146303980Sngie	for (size_t i = 0; i < __arraycount(tst); i++) {
147303980Sngie		memcpy(&sdl.sdl_addr, &tst[i].ia, sizeof(sdl.sdl_addr));
148303980Sngie		sdl.sdl_index = (uint16_t)i;
149303980Sngie		r = sdl_print(buf, l, &sdl);
150313535Sngie		if (i == 3)
151313535Sngie			e = snprintf(res, l, "link#%zu", i);
152313535Sngie		else
153313535Sngie			e = snprintf(res, l, "[%s]:%zu", tst[i].str, i);
154303980Sngie		ATF_REQUIRE_STREQ(buf, res);
155303980Sngie		ATF_REQUIRE_EQ(r, e);
156303980Sngie	}
157303980Sngie
158303980Sngie	l = 8;
159303980Sngie	for (size_t i = 0; i < __arraycount(tst); i++) {
160303980Sngie		memcpy(&sdl.sdl_addr, &tst[i].ia, sizeof(sdl.sdl_addr));
161303980Sngie		sdl.sdl_index = (uint16_t)i;
162303980Sngie		r = sdl_print(buf, l, &sdl);
163313535Sngie		if (i == 3)
164313535Sngie			e = snprintf(res, l, "link#%zu", i);
165313535Sngie		else
166313535Sngie			e = snprintf(res, l, "[%s]:%zu", tst[i].str, i);
167303980Sngie		ATF_REQUIRE_STREQ(buf, res);
168303980Sngie		ATF_REQUIRE_EQ(r, e);
169303980Sngie	}
170303980Sngie}
171303980Sngie
172303980SngieATF_TP_ADD_TCS(tp)
173303980Sngie{
174303980Sngie
175303980Sngie	ATF_TP_ADD_TC(tp, dl_print);
176303980Sngie	ATF_TP_ADD_TC(tp, sdl_print);
177303980Sngie	return atf_no_error();
178303980Sngie}
179