1303980Sngie/*	$NetBSD: t_print.c,v 1.1 2014/12/02 19:48:21 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>
32303980Sngie__RCSID("$NetBSD: t_print.c,v 1.1 2014/12/02 19:48:21 christos Exp $");
33303980Sngie
34303980Sngie#include "netatalk/at_print.c"
35303980Sngie
36303980Sngie#include <atf-c.h>
37303980Sngie
38303980Sngiestatic const struct {
39303980Sngie	struct at_addr ia;
40303980Sngie	const char *str;
41303980Sngie	int len;
42303980Sngie} tst[] = {
43303980Sngie	{
44303980Sngie		{ 0, 0 },
45303980Sngie		"0.0",
46303980Sngie		3,
47303980Sngie	},
48303980Sngie	{
49303980Sngie		{ htons(3), 255 },
50303980Sngie		"3.255",
51303980Sngie		5,
52303980Sngie	},
53303980Sngie};
54303980Sngie
55303980Sngie
56303980SngieATF_TC(at_print);
57303980SngieATF_TC_HEAD(at_print, tc)
58303980Sngie{
59303980Sngie
60303980Sngie	atf_tc_set_md_var(tc, "descr", "printing of struct at_addr");
61303980Sngie}
62303980Sngie
63303980SngieATF_TC_BODY(at_print, tc)
64303980Sngie{
65303980Sngie	char buf[ATALK_ADDRSTRLEN];
66303980Sngie	int r;
67303980Sngie	size_t l = sizeof(buf);
68303980Sngie
69303980Sngie	for (size_t i = 0; i < __arraycount(tst); i++) {
70303980Sngie		r = at_print(buf, l, &tst[i].ia);
71303980Sngie		ATF_REQUIRE_STREQ(buf, tst[i].str);
72303980Sngie		ATF_REQUIRE_EQ(r, tst[i].len);
73303980Sngie	}
74303980Sngie
75303980Sngie	l = 4;
76303980Sngie	for (size_t i = 0; i < __arraycount(tst); i++) {
77303980Sngie		r = at_print(buf, l, &tst[i].ia);
78303980Sngie		ATF_CHECK(strncmp(buf, tst[i].str, l - 1) == 0);
79303980Sngie		if (r > (int)l)
80303980Sngie			ATF_REQUIRE_EQ(buf[l - 1], '\0');
81303980Sngie		ATF_REQUIRE_EQ(r, tst[i].len);
82303980Sngie	}
83303980Sngie}
84303980Sngie
85303980SngieATF_TC(sat_print);
86303980SngieATF_TC_HEAD(sat_print, tc)
87303980Sngie{
88303980Sngie
89303980Sngie	atf_tc_set_md_var(tc, "descr", "printing of sockaddr_at");
90303980Sngie}
91303980Sngie
92303980SngieATF_TC_BODY(sat_print, tc)
93303980Sngie{
94303980Sngie	char buf[1024];
95303980Sngie	char res[1024];
96303980Sngie	int r, e;
97303980Sngie	size_t l = sizeof(buf);
98303980Sngie	struct sockaddr_at sat;
99303980Sngie
100303980Sngie	memset(&sat, 0, sizeof(sat));
101303980Sngie	for (size_t i = 0; i < __arraycount(tst); i++) {
102303980Sngie		sat.sat_addr = tst[i].ia;
103303980Sngie		sat.sat_port = (uint8_t)i;
104303980Sngie		r = sat_print(buf, l, &sat);
105303980Sngie		if (i == 0)
106303980Sngie			e = snprintf(res, sizeof(res), "%s", tst[i].str);
107303980Sngie		else
108303980Sngie			e = snprintf(res, sizeof(res), "%s:%zu", tst[i].str, i);
109303980Sngie
110303980Sngie		ATF_REQUIRE_STREQ(buf, res);
111303980Sngie		ATF_REQUIRE_EQ(r, e);
112303980Sngie	}
113303980Sngie
114303980Sngie	l = 8;
115303980Sngie	for (size_t i = 0; i < __arraycount(tst); i++) {
116303980Sngie		sat.sat_addr = tst[i].ia;
117303980Sngie		sat.sat_port = (uint8_t)i;
118303980Sngie		r = sat_print(buf, l, &sat);
119303980Sngie		if (i == 0)
120303980Sngie			e = snprintf(res, l, "%s", tst[i].str);
121303980Sngie		else
122303980Sngie			e = snprintf(res, l, "%s:%zu", tst[i].str, i);
123303980Sngie
124303980Sngie		ATF_REQUIRE_STREQ(buf, res);
125303980Sngie		ATF_REQUIRE_EQ(r, e);
126303980Sngie	}
127303980Sngie}
128303980Sngie
129303980SngieATF_TP_ADD_TCS(tp)
130303980Sngie{
131303980Sngie
132303980Sngie	ATF_TP_ADD_TC(tp, at_print);
133303980Sngie	ATF_TP_ADD_TC(tp, sat_print);
134303980Sngie	return atf_no_error();
135303980Sngie}
136