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