xref: /minix3/tests/sys/netatalk/t_print.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
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 "netatalk/at_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 at_addr ia;
40*0a6a1f1dSLionel Sambuc 	const char *str;
41*0a6a1f1dSLionel Sambuc 	int len;
42*0a6a1f1dSLionel Sambuc } tst[] = {
43*0a6a1f1dSLionel Sambuc 	{
44*0a6a1f1dSLionel Sambuc 		{ 0, 0 },
45*0a6a1f1dSLionel Sambuc 		"0.0",
46*0a6a1f1dSLionel Sambuc 		3,
47*0a6a1f1dSLionel Sambuc 	},
48*0a6a1f1dSLionel Sambuc 	{
49*0a6a1f1dSLionel Sambuc 		{ htons(3), 255 },
50*0a6a1f1dSLionel Sambuc 		"3.255",
51*0a6a1f1dSLionel Sambuc 		5,
52*0a6a1f1dSLionel Sambuc 	},
53*0a6a1f1dSLionel Sambuc };
54*0a6a1f1dSLionel Sambuc 
55*0a6a1f1dSLionel Sambuc 
56*0a6a1f1dSLionel Sambuc ATF_TC(at_print);
ATF_TC_HEAD(at_print,tc)57*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(at_print, tc)
58*0a6a1f1dSLionel Sambuc {
59*0a6a1f1dSLionel Sambuc 
60*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "printing of struct at_addr");
61*0a6a1f1dSLionel Sambuc }
62*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(at_print,tc)63*0a6a1f1dSLionel Sambuc ATF_TC_BODY(at_print, tc)
64*0a6a1f1dSLionel Sambuc {
65*0a6a1f1dSLionel Sambuc 	char buf[ATALK_ADDRSTRLEN];
66*0a6a1f1dSLionel Sambuc 	int r;
67*0a6a1f1dSLionel Sambuc 	size_t l = sizeof(buf);
68*0a6a1f1dSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc 	for (size_t i = 0; i < __arraycount(tst); i++) {
70*0a6a1f1dSLionel Sambuc 		r = at_print(buf, l, &tst[i].ia);
71*0a6a1f1dSLionel Sambuc 		ATF_REQUIRE_STREQ(buf, tst[i].str);
72*0a6a1f1dSLionel Sambuc 		ATF_REQUIRE_EQ(r, tst[i].len);
73*0a6a1f1dSLionel Sambuc 	}
74*0a6a1f1dSLionel Sambuc 
75*0a6a1f1dSLionel Sambuc 	l = 4;
76*0a6a1f1dSLionel Sambuc 	for (size_t i = 0; i < __arraycount(tst); i++) {
77*0a6a1f1dSLionel Sambuc 		r = at_print(buf, l, &tst[i].ia);
78*0a6a1f1dSLionel Sambuc 		ATF_CHECK(strncmp(buf, tst[i].str, l - 1) == 0);
79*0a6a1f1dSLionel Sambuc 		if (r > (int)l)
80*0a6a1f1dSLionel Sambuc 			ATF_REQUIRE_EQ(buf[l - 1], '\0');
81*0a6a1f1dSLionel Sambuc 		ATF_REQUIRE_EQ(r, tst[i].len);
82*0a6a1f1dSLionel Sambuc 	}
83*0a6a1f1dSLionel Sambuc }
84*0a6a1f1dSLionel Sambuc 
85*0a6a1f1dSLionel Sambuc ATF_TC(sat_print);
ATF_TC_HEAD(sat_print,tc)86*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(sat_print, tc)
87*0a6a1f1dSLionel Sambuc {
88*0a6a1f1dSLionel Sambuc 
89*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "printing of sockaddr_at");
90*0a6a1f1dSLionel Sambuc }
91*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(sat_print,tc)92*0a6a1f1dSLionel Sambuc ATF_TC_BODY(sat_print, tc)
93*0a6a1f1dSLionel Sambuc {
94*0a6a1f1dSLionel Sambuc 	char buf[1024];
95*0a6a1f1dSLionel Sambuc 	char res[1024];
96*0a6a1f1dSLionel Sambuc 	int r, e;
97*0a6a1f1dSLionel Sambuc 	size_t l = sizeof(buf);
98*0a6a1f1dSLionel Sambuc 	struct sockaddr_at sat;
99*0a6a1f1dSLionel Sambuc 
100*0a6a1f1dSLionel Sambuc 	memset(&sat, 0, sizeof(sat));
101*0a6a1f1dSLionel Sambuc 	for (size_t i = 0; i < __arraycount(tst); i++) {
102*0a6a1f1dSLionel Sambuc 		sat.sat_addr = tst[i].ia;
103*0a6a1f1dSLionel Sambuc 		sat.sat_port = (uint8_t)i;
104*0a6a1f1dSLionel Sambuc 		r = sat_print(buf, l, &sat);
105*0a6a1f1dSLionel Sambuc 		if (i == 0)
106*0a6a1f1dSLionel Sambuc 			e = snprintf(res, sizeof(res), "%s", tst[i].str);
107*0a6a1f1dSLionel Sambuc 		else
108*0a6a1f1dSLionel Sambuc 			e = snprintf(res, sizeof(res), "%s:%zu", tst[i].str, i);
109*0a6a1f1dSLionel Sambuc 
110*0a6a1f1dSLionel Sambuc 		ATF_REQUIRE_STREQ(buf, res);
111*0a6a1f1dSLionel Sambuc 		ATF_REQUIRE_EQ(r, e);
112*0a6a1f1dSLionel Sambuc 	}
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc 	l = 8;
115*0a6a1f1dSLionel Sambuc 	for (size_t i = 0; i < __arraycount(tst); i++) {
116*0a6a1f1dSLionel Sambuc 		sat.sat_addr = tst[i].ia;
117*0a6a1f1dSLionel Sambuc 		sat.sat_port = (uint8_t)i;
118*0a6a1f1dSLionel Sambuc 		r = sat_print(buf, l, &sat);
119*0a6a1f1dSLionel Sambuc 		if (i == 0)
120*0a6a1f1dSLionel Sambuc 			e = snprintf(res, l, "%s", tst[i].str);
121*0a6a1f1dSLionel Sambuc 		else
122*0a6a1f1dSLionel Sambuc 			e = snprintf(res, l, "%s:%zu", tst[i].str, i);
123*0a6a1f1dSLionel Sambuc 
124*0a6a1f1dSLionel Sambuc 		ATF_REQUIRE_STREQ(buf, res);
125*0a6a1f1dSLionel Sambuc 		ATF_REQUIRE_EQ(r, e);
126*0a6a1f1dSLionel Sambuc 	}
127*0a6a1f1dSLionel Sambuc }
128*0a6a1f1dSLionel Sambuc 
ATF_TP_ADD_TCS(tp)129*0a6a1f1dSLionel Sambuc ATF_TP_ADD_TCS(tp)
130*0a6a1f1dSLionel Sambuc {
131*0a6a1f1dSLionel Sambuc 
132*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, at_print);
133*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, sat_print);
134*0a6a1f1dSLionel Sambuc 	return atf_no_error();
135*0a6a1f1dSLionel Sambuc }
136