1bb9fc56ngie/*	$NetBSD: t_print.c,v 1.2 2016/08/27 11:30:49 christos Exp $	*/
2c84c9cangie
3c84c9cangie/*-
4c84c9cangie * Copyright (c) 2014 The NetBSD Foundation, Inc.
5c84c9cangie * All rights reserved.
6c84c9cangie *
7c84c9cangie * This code is derived from software contributed to The NetBSD Foundation
8c84c9cangie * by Christos Zoulas.
9c84c9cangie *
10c84c9cangie * Redistribution and use in source and binary forms, with or without
11c84c9cangie * modification, are permitted provided that the following conditions
12c84c9cangie * are met:
13c84c9cangie * 1. Redistributions of source code must retain the above copyright
14c84c9cangie *    notice, this list of conditions and the following disclaimer.
15c84c9cangie * 2. Redistributions in binary form must reproduce the above copyright
16c84c9cangie *    notice, this list of conditions and the following disclaimer in the
17c84c9cangie *    documentation and/or other materials provided with the distribution.
18c84c9cangie *
19c84c9cangie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20c84c9cangie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21c84c9cangie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22c84c9cangie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23c84c9cangie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24c84c9cangie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25c84c9cangie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26c84c9cangie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27c84c9cangie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28c84c9cangie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29c84c9cangie * POSSIBILITY OF SUCH DAMAGE.
30c84c9cangie */
31c84c9cangie#include <sys/cdefs.h>
32bb9fc56ngie__RCSID("$NetBSD: t_print.c,v 1.2 2016/08/27 11:30:49 christos Exp $");
33c84c9cangie
34c84c9cangie#include "net/dl_print.c"
35c84c9cangie
36c84c9cangie#include <atf-c.h>
37c84c9cangie
38c84c9cangiestatic const struct {
39c84c9cangie	struct dl_addr ia;
40c84c9cangie	const char *str;
41c84c9cangie	int len;
42c84c9cangie} tst[] = {
43c84c9cangie	{
44c84c9cangie		{
45c84c9cangie			.dl_type = 6,
46c84c9cangie			.dl_nlen = 0,
47c84c9cangie			.dl_alen = 6,
48c84c9cangie			.dl_slen = 0,
49c84c9cangie			.dl_data = {
50c84c9cangie			    (char)0x01, (char)0xa2, (char)0x03,
51c84c9cangie			    (char)0xc4, (char)0x05, (char)0xf6,
52c84c9cangie			},
53c84c9cangie		},
54c84c9cangie		"/6#01:a2:03:c4:05:f6",
55c84c9cangie		20,
56c84c9cangie	},
57c84c9cangie	{
58c84c9cangie		{
59c84c9cangie			.dl_type = 24,
60c84c9cangie			.dl_nlen = 3,
61c84c9cangie			.dl_alen = 6,
62c84c9cangie			.dl_slen = 0,
63c84c9cangie			.dl_data = {
64c84c9cangie			    'l', 'o', '0',
65c84c9cangie			    (char)0x11, (char)0x22, (char)0x33,
66c84c9cangie			    (char)0x44, (char)0x55, (char)0x66,
67c84c9cangie			},
68c84c9cangie		},
69c84c9cangie		"lo0/24#11:22:33:44:55:66",
70c84c9cangie		24,
71c84c9cangie	},
72c84c9cangie	{
73c84c9cangie		{
74c84c9cangie			.dl_type = 24,
75c84c9cangie			.dl_nlen = 7,
76c84c9cangie			.dl_alen = 1,
77c84c9cangie			.dl_slen = 0,
78c84c9cangie			.dl_data = {
79c84c9cangie			    'n', 'p', 'f', 'l', 'o', 'g', '0', (char)0xa5,
80c84c9cangie			},
81c84c9cangie		},
82c84c9cangie		"npflog0/24#a5",
83c84c9cangie		13,
84c84c9cangie	},
85c84c9cangie	{
86c84c9cangie		{
87c84c9cangie			.dl_type = 0,
88c84c9cangie			.dl_nlen = 0,
89c84c9cangie			.dl_alen = 0,
90c84c9cangie			.dl_slen = 0,
91c84c9cangie			.dl_data = {
92c84c9cangie			    '\0'
93c84c9cangie			},
94c84c9cangie		},
95c84c9cangie		"/0#",
96c84c9cangie		3,
97c84c9cangie	},
98c84c9cangie};
99c84c9cangie
100c84c9cangie
101c84c9cangieATF_TC(dl_print);
102c84c9cangieATF_TC_HEAD(dl_print, tc)
103c84c9cangie{
104c84c9cangie
105c84c9cangie	atf_tc_set_md_var(tc, "descr", "printing of link address");
106c84c9cangie}
107c84c9cangie
108c84c9cangieATF_TC_BODY(dl_print, tc)
109c84c9cangie{
110c84c9cangie	char buf[LINK_ADDRSTRLEN];
111c84c9cangie	int r;
112c84c9cangie	size_t l = sizeof(buf);
113c84c9cangie
114c84c9cangie	for (size_t i = 0; i < __arraycount(tst); i++) {
115c84c9cangie		r = dl_print(buf, l, &tst[i].ia);
116c84c9cangie		ATF_REQUIRE_STREQ(buf, tst[i].str);
117c84c9cangie		ATF_REQUIRE_EQ(r, tst[i].len);
118c84c9cangie	}
119c84c9cangie
120c84c9cangie	l = 4;
121c84c9cangie	for (size_t i = 0; i < __arraycount(tst); i++) {
122c84c9cangie		r = dl_print(buf, l, &tst[i].ia);
123c84c9cangie		ATF_CHECK(strncmp(buf, tst[i].str, l - 1) == 0);
124c84c9cangie		if (r > (int)l)
125c84c9cangie			ATF_REQUIRE_EQ(buf[l - 1], '\0');
126c84c9cangie		ATF_REQUIRE_EQ(r, tst[i].len);
127c84c9cangie	}
128c84c9cangie}
129c84c9cangie
130c84c9cangieATF_TC(sdl_print);
131c84c9cangieATF_TC_HEAD(sdl_print, tc)
132c84c9cangie{
133c84c9cangie
134c84c9cangie	atf_tc_set_md_var(tc, "descr", "printing of sockaddr_dl");
135c84c9cangie}
136c84c9cangie
137c84c9cangieATF_TC_BODY(sdl_print, tc)
138c84c9cangie{
139c84c9cangie	char buf[1024];
140c84c9cangie	char res[1024];
141c84c9cangie	int r, e;
142c84c9cangie	size_t l = sizeof(buf);
143c84c9cangie	struct sockaddr_dl sdl;
144c84c9cangie
145c84c9cangie	memset(&sdl, 0, sizeof(sdl));
146c84c9cangie	for (size_t i = 0; i < __arraycount(tst); i++) {
147c84c9cangie		memcpy(&sdl.sdl_addr, &tst[i].ia, sizeof(sdl.sdl_addr));
148c84c9cangie		sdl.sdl_index = (uint16_t)i;
149c84c9cangie		r = sdl_print(buf, l, &sdl);
150bb9fc56ngie		if (i == 3)
151bb9fc56ngie			e = snprintf(res, l, "link#%zu", i);
152bb9fc56ngie		else
153bb9fc56ngie			e = snprintf(res, l, "[%s]:%zu", tst[i].str, i);
154c84c9cangie		ATF_REQUIRE_STREQ(buf, res);
155c84c9cangie		ATF_REQUIRE_EQ(r, e);
156c84c9cangie	}
157c84c9cangie
158c84c9cangie	l = 8;
159c84c9cangie	for (size_t i = 0; i < __arraycount(tst); i++) {
160c84c9cangie		memcpy(&sdl.sdl_addr, &tst[i].ia, sizeof(sdl.sdl_addr));
161c84c9cangie		sdl.sdl_index = (uint16_t)i;
162c84c9cangie		r = sdl_print(buf, l, &sdl);
163bb9fc56ngie		if (i == 3)
164bb9fc56ngie			e = snprintf(res, l, "link#%zu", i);
165bb9fc56ngie		else
166bb9fc56ngie			e = snprintf(res, l, "[%s]:%zu", tst[i].str, i);
167c84c9cangie		ATF_REQUIRE_STREQ(buf, res);
168c84c9cangie		ATF_REQUIRE_EQ(r, e);
169c84c9cangie	}
170c84c9cangie}
171c84c9cangie
172c84c9cangieATF_TP_ADD_TCS(tp)
173c84c9cangie{
174c84c9cangie
175c84c9cangie	ATF_TP_ADD_TC(tp, dl_print);
176c84c9cangie	ATF_TP_ADD_TC(tp, sdl_print);
177c84c9cangie	return atf_no_error();
178c84c9cangie}
179