xref: /freebsd/contrib/netbsd-tests/sys/netatalk/t_print.c (revision 63a938566d524836885917d95bd491aa4400b181)
1 /*	$NetBSD: t_print.c,v 1.1 2014/12/02 19:48:21 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_print.c,v 1.1 2014/12/02 19:48:21 christos Exp $");
33 
34 #include "netatalk/at_print.c"
35 
36 #include <atf-c.h>
37 
38 static const struct {
39 	struct at_addr ia;
40 	const char *str;
41 	int len;
42 } tst[] = {
43 	{
44 		{ 0, 0 },
45 		"0.0",
46 		3,
47 	},
48 	{
49 		{ htons(3), 255 },
50 		"3.255",
51 		5,
52 	},
53 };
54 
55 
56 ATF_TC(at_print);
57 ATF_TC_HEAD(at_print, tc)
58 {
59 
60 	atf_tc_set_md_var(tc, "descr", "printing of struct at_addr");
61 }
62 
63 ATF_TC_BODY(at_print, tc)
64 {
65 	char buf[ATALK_ADDRSTRLEN];
66 	int r;
67 	size_t l = sizeof(buf);
68 
69 	for (size_t i = 0; i < __arraycount(tst); i++) {
70 		r = at_print(buf, l, &tst[i].ia);
71 		ATF_REQUIRE_STREQ(buf, tst[i].str);
72 		ATF_REQUIRE_EQ(r, tst[i].len);
73 	}
74 
75 	l = 4;
76 	for (size_t i = 0; i < __arraycount(tst); i++) {
77 		r = at_print(buf, l, &tst[i].ia);
78 		ATF_CHECK(strncmp(buf, tst[i].str, l - 1) == 0);
79 		if (r > (int)l)
80 			ATF_REQUIRE_EQ(buf[l - 1], '\0');
81 		ATF_REQUIRE_EQ(r, tst[i].len);
82 	}
83 }
84 
85 ATF_TC(sat_print);
86 ATF_TC_HEAD(sat_print, tc)
87 {
88 
89 	atf_tc_set_md_var(tc, "descr", "printing of sockaddr_at");
90 }
91 
92 ATF_TC_BODY(sat_print, tc)
93 {
94 	char buf[1024];
95 	char res[1024];
96 	int r, e;
97 	size_t l = sizeof(buf);
98 	struct sockaddr_at sat;
99 
100 	memset(&sat, 0, sizeof(sat));
101 	for (size_t i = 0; i < __arraycount(tst); i++) {
102 		sat.sat_addr = tst[i].ia;
103 		sat.sat_port = (uint8_t)i;
104 		r = sat_print(buf, l, &sat);
105 		if (i == 0)
106 			e = snprintf(res, sizeof(res), "%s", tst[i].str);
107 		else
108 			e = snprintf(res, sizeof(res), "%s:%zu", tst[i].str, i);
109 
110 		ATF_REQUIRE_STREQ(buf, res);
111 		ATF_REQUIRE_EQ(r, e);
112 	}
113 
114 	l = 8;
115 	for (size_t i = 0; i < __arraycount(tst); i++) {
116 		sat.sat_addr = tst[i].ia;
117 		sat.sat_port = (uint8_t)i;
118 		r = sat_print(buf, l, &sat);
119 		if (i == 0)
120 			e = snprintf(res, l, "%s", tst[i].str);
121 		else
122 			e = snprintf(res, l, "%s:%zu", tst[i].str, i);
123 
124 		ATF_REQUIRE_STREQ(buf, res);
125 		ATF_REQUIRE_EQ(r, e);
126 	}
127 }
128 
129 ATF_TP_ADD_TCS(tp)
130 {
131 
132 	ATF_TP_ADD_TC(tp, at_print);
133 	ATF_TP_ADD_TC(tp, sat_print);
134 	return atf_no_error();
135 }
136