1 /* $NetBSD: t_print.c,v 1.2 2014/12/03 13:10:49 christos Exp $ */
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_print.c,v 1.2 2014/12/03 13:10:49 christos Exp $");
34 #include "netinet/in_print.c"
44 { .s_addr = ntohl(INADDR_LOOPBACK) },
49 { .s_addr = ntohl(INADDR_ANY) },
54 { .s_addr = ntohl(IN_CLASSC_NET) },
59 { .s_addr = ntohl(INADDR_ALLHOSTS_GROUP) },
67 ATF_TC_HEAD(in_print, tc)
70 atf_tc_set_md_var(tc, "descr", "printing of struct in_addr");
73 ATF_TC_BODY(in_print, tc)
75 char buf[INET_ADDRSTRLEN];
77 size_t l = sizeof(buf);
79 for (size_t i = 0; i < __arraycount(tst); i++) {
80 r = in_print(buf, l, &tst[i].ia);
81 ATF_REQUIRE_STREQ(buf, tst[i].str);
82 ATF_REQUIRE_EQ(r, tst[i].len);
86 for (size_t i = 0; i < __arraycount(tst); i++) {
87 r = in_print(buf, l, &tst[i].ia);
88 ATF_CHECK(strncmp(buf, tst[i].str, l - 1) == 0);
89 ATF_REQUIRE_EQ(buf[l - 1], '\0');
90 ATF_REQUIRE_EQ(r, tst[i].len);
95 ATF_TC_HEAD(sin_print, tc)
98 atf_tc_set_md_var(tc, "descr", "printing of sockaddr_in");
101 ATF_TC_BODY(sin_print, tc)
106 size_t l = sizeof(buf);
107 struct sockaddr_in sin;
108 memset(&sin, 0, sizeof(sin));
110 for (size_t i = 0; i < __arraycount(tst); i++) {
111 sin.sin_addr = tst[i].ia;
112 sin.sin_port = (in_port_t)htons(i);
113 r = sin_print(buf, l, &sin);
115 e = snprintf(res, sizeof(res), "%s", tst[i].str);
117 e = snprintf(res, sizeof(res), "%s:%zu", tst[i].str, i);
119 ATF_REQUIRE_STREQ(buf, res);
120 ATF_REQUIRE_EQ(r, e);
124 for (size_t i = 0; i < __arraycount(tst); i++) {
125 sin.sin_addr = tst[i].ia;
126 sin.sin_port = (in_port_t)htons(i);
127 r = sin_print(buf, l, &sin);
129 e = snprintf(res, l, "%s", tst[i].str);
131 e = snprintf(res, l, "%s:%zu", tst[i].str, i);
133 ATF_REQUIRE_STREQ(buf, res);
134 ATF_REQUIRE_EQ(r, e);
141 ATF_TP_ADD_TC(tp, in_print);
142 ATF_TP_ADD_TC(tp, sin_print);
143 return atf_no_error();