1 1.2 christos /* $NetBSD: t_print.c,v 1.2 2016/08/27 11:30:49 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.2 christos __RCSID("$NetBSD: t_print.c,v 1.2 2016/08/27 11:30:49 christos Exp $"); 33 1.1 christos 34 1.1 christos #include "net/dl_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 dl_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 { 45 1.1 christos .dl_type = 6, 46 1.1 christos .dl_nlen = 0, 47 1.1 christos .dl_alen = 6, 48 1.1 christos .dl_slen = 0, 49 1.1 christos .dl_data = { 50 1.1 christos (char)0x01, (char)0xa2, (char)0x03, 51 1.1 christos (char)0xc4, (char)0x05, (char)0xf6, 52 1.1 christos }, 53 1.1 christos }, 54 1.1 christos "/6#01:a2:03:c4:05:f6", 55 1.1 christos 20, 56 1.1 christos }, 57 1.1 christos { 58 1.1 christos { 59 1.1 christos .dl_type = 24, 60 1.1 christos .dl_nlen = 3, 61 1.1 christos .dl_alen = 6, 62 1.1 christos .dl_slen = 0, 63 1.1 christos .dl_data = { 64 1.1 christos 'l', 'o', '0', 65 1.1 christos (char)0x11, (char)0x22, (char)0x33, 66 1.1 christos (char)0x44, (char)0x55, (char)0x66, 67 1.1 christos }, 68 1.1 christos }, 69 1.1 christos "lo0/24#11:22:33:44:55:66", 70 1.1 christos 24, 71 1.1 christos }, 72 1.1 christos { 73 1.1 christos { 74 1.1 christos .dl_type = 24, 75 1.1 christos .dl_nlen = 7, 76 1.1 christos .dl_alen = 1, 77 1.1 christos .dl_slen = 0, 78 1.1 christos .dl_data = { 79 1.1 christos 'n', 'p', 'f', 'l', 'o', 'g', '0', (char)0xa5, 80 1.1 christos }, 81 1.1 christos }, 82 1.1 christos "npflog0/24#a5", 83 1.1 christos 13, 84 1.1 christos }, 85 1.1 christos { 86 1.1 christos { 87 1.1 christos .dl_type = 0, 88 1.1 christos .dl_nlen = 0, 89 1.1 christos .dl_alen = 0, 90 1.1 christos .dl_slen = 0, 91 1.1 christos .dl_data = { 92 1.1 christos '\0' 93 1.1 christos }, 94 1.1 christos }, 95 1.1 christos "/0#", 96 1.1 christos 3, 97 1.1 christos }, 98 1.1 christos }; 99 1.1 christos 100 1.1 christos 101 1.1 christos ATF_TC(dl_print); 102 1.1 christos ATF_TC_HEAD(dl_print, tc) 103 1.1 christos { 104 1.1 christos 105 1.1 christos atf_tc_set_md_var(tc, "descr", "printing of link address"); 106 1.1 christos } 107 1.1 christos 108 1.1 christos ATF_TC_BODY(dl_print, tc) 109 1.1 christos { 110 1.1 christos char buf[LINK_ADDRSTRLEN]; 111 1.1 christos int r; 112 1.1 christos size_t l = sizeof(buf); 113 1.1 christos 114 1.1 christos for (size_t i = 0; i < __arraycount(tst); i++) { 115 1.1 christos r = dl_print(buf, l, &tst[i].ia); 116 1.1 christos ATF_REQUIRE_STREQ(buf, tst[i].str); 117 1.1 christos ATF_REQUIRE_EQ(r, tst[i].len); 118 1.1 christos } 119 1.1 christos 120 1.1 christos l = 4; 121 1.1 christos for (size_t i = 0; i < __arraycount(tst); i++) { 122 1.1 christos r = dl_print(buf, l, &tst[i].ia); 123 1.1 christos ATF_CHECK(strncmp(buf, tst[i].str, l - 1) == 0); 124 1.1 christos if (r > (int)l) 125 1.1 christos ATF_REQUIRE_EQ(buf[l - 1], '\0'); 126 1.1 christos ATF_REQUIRE_EQ(r, tst[i].len); 127 1.1 christos } 128 1.1 christos } 129 1.1 christos 130 1.1 christos ATF_TC(sdl_print); 131 1.1 christos ATF_TC_HEAD(sdl_print, tc) 132 1.1 christos { 133 1.1 christos 134 1.1 christos atf_tc_set_md_var(tc, "descr", "printing of sockaddr_dl"); 135 1.1 christos } 136 1.1 christos 137 1.1 christos ATF_TC_BODY(sdl_print, tc) 138 1.1 christos { 139 1.1 christos char buf[1024]; 140 1.1 christos char res[1024]; 141 1.1 christos int r, e; 142 1.1 christos size_t l = sizeof(buf); 143 1.1 christos struct sockaddr_dl sdl; 144 1.1 christos 145 1.1 christos memset(&sdl, 0, sizeof(sdl)); 146 1.1 christos for (size_t i = 0; i < __arraycount(tst); i++) { 147 1.1 christos memcpy(&sdl.sdl_addr, &tst[i].ia, sizeof(sdl.sdl_addr)); 148 1.1 christos sdl.sdl_index = (uint16_t)i; 149 1.1 christos r = sdl_print(buf, l, &sdl); 150 1.2 christos if (i == 3) 151 1.2 christos e = snprintf(res, l, "link#%zu", i); 152 1.2 christos else 153 1.2 christos e = snprintf(res, l, "[%s]:%zu", tst[i].str, i); 154 1.1 christos ATF_REQUIRE_STREQ(buf, res); 155 1.1 christos ATF_REQUIRE_EQ(r, e); 156 1.1 christos } 157 1.1 christos 158 1.1 christos l = 8; 159 1.1 christos for (size_t i = 0; i < __arraycount(tst); i++) { 160 1.1 christos memcpy(&sdl.sdl_addr, &tst[i].ia, sizeof(sdl.sdl_addr)); 161 1.1 christos sdl.sdl_index = (uint16_t)i; 162 1.1 christos r = sdl_print(buf, l, &sdl); 163 1.2 christos if (i == 3) 164 1.2 christos e = snprintf(res, l, "link#%zu", i); 165 1.2 christos else 166 1.2 christos e = snprintf(res, l, "[%s]:%zu", tst[i].str, i); 167 1.1 christos ATF_REQUIRE_STREQ(buf, res); 168 1.1 christos ATF_REQUIRE_EQ(r, e); 169 1.1 christos } 170 1.1 christos } 171 1.1 christos 172 1.1 christos ATF_TP_ADD_TCS(tp) 173 1.1 christos { 174 1.1 christos 175 1.1 christos ATF_TP_ADD_TC(tp, dl_print); 176 1.1 christos ATF_TP_ADD_TC(tp, sdl_print); 177 1.1 christos return atf_no_error(); 178 1.1 christos } 179