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