1 1.1 elric /* $NetBSD: test_addr.c,v 1.2 2017/01/28 21:31:49 christos Exp $ */ 2 1.1 elric 3 1.1 elric /* 4 1.1 elric * Copyright (c) 2005 Kungliga Tekniska Hgskolan 5 1.1 elric * (Royal Institute of Technology, Stockholm, Sweden). 6 1.1 elric * All rights reserved. 7 1.1 elric * 8 1.1 elric * Redistribution and use in source and binary forms, with or without 9 1.1 elric * modification, are permitted provided that the following conditions 10 1.1 elric * are met: 11 1.1 elric * 12 1.1 elric * 1. Redistributions of source code must retain the above copyright 13 1.1 elric * notice, this list of conditions and the following disclaimer. 14 1.1 elric * 15 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 elric * notice, this list of conditions and the following disclaimer in the 17 1.1 elric * documentation and/or other materials provided with the distribution. 18 1.1 elric * 19 1.1 elric * 3. Neither the name of KTH nor the names of its contributors may be 20 1.1 elric * used to endorse or promote products derived from this software without 21 1.1 elric * specific prior written permission. 22 1.1 elric * 23 1.1 elric * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY 24 1.1 elric * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 1.1 elric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE 27 1.1 elric * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 1.1 elric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 1.1 elric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 30 1.1 elric * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 1.1 elric * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 1.1 elric * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 1.1 elric * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 34 1.1 elric 35 1.1 elric #include "krb5_locl.h" 36 1.1 elric #include <err.h> 37 1.1 elric 38 1.1 elric static void 39 1.1 elric print_addr(krb5_context context, const char *addr) 40 1.1 elric { 41 1.1 elric krb5_addresses addresses; 42 1.1 elric krb5_error_code ret; 43 1.1 elric char buf[38]; 44 1.1 elric char buf2[1000]; 45 1.1 elric size_t len; 46 1.1 elric int i; 47 1.1 elric 48 1.1 elric ret = krb5_parse_address(context, addr, &addresses); 49 1.1 elric if (ret) 50 1.1 elric krb5_err(context, 1, ret, "krb5_parse_address"); 51 1.1 elric 52 1.1 elric if (addresses.len < 1) 53 1.1 elric krb5_err(context, 1, ret, "too few addresses"); 54 1.1 elric 55 1.1 elric for (i = 0; i < addresses.len; i++) { 56 1.1 elric krb5_print_address(&addresses.val[i], buf, sizeof(buf), &len); 57 1.1 elric #if 0 58 1.1 elric printf("addr %d: %s (%d/%d)\n", i, buf, (int)len, (int)strlen(buf)); 59 1.1 elric #endif 60 1.1 elric if (strlen(buf) > sizeof(buf)) 61 1.1 elric krb5_err(context, 1, ret, "len %d larger then buf %d", 62 1.1 elric (int)strlen(buf), (int)sizeof(buf)); 63 1.1 elric krb5_print_address(&addresses.val[i], buf2, sizeof(buf2), &len); 64 1.1 elric #if 0 65 1.1 elric printf("addr %d: %s (%d/%d)\n", i, buf2, (int)len, (int)strlen(buf2)); 66 1.1 elric #endif 67 1.1 elric if (strlen(buf2) > sizeof(buf2)) 68 1.1 elric krb5_err(context, 1, ret, "len %d larger then buf %d", 69 1.1 elric (int)strlen(buf2), (int)sizeof(buf2)); 70 1.1 elric 71 1.1 elric } 72 1.1 elric krb5_free_addresses(context, &addresses); 73 1.1 elric 74 1.1 elric } 75 1.1 elric 76 1.1 elric static void 77 1.1 elric truncated_addr(krb5_context context, const char *addr, 78 1.1 elric size_t truncate_len, size_t outlen) 79 1.1 elric { 80 1.1 elric krb5_addresses addresses; 81 1.1 elric krb5_error_code ret; 82 1.1 elric char *buf; 83 1.1 elric size_t len; 84 1.1 elric 85 1.1 elric buf = ecalloc(1, outlen + 1); 86 1.1 elric 87 1.1 elric ret = krb5_parse_address(context, addr, &addresses); 88 1.1 elric if (ret) 89 1.1 elric krb5_err(context, 1, ret, "krb5_parse_address"); 90 1.1 elric 91 1.1 elric if (addresses.len != 1) 92 1.1 elric krb5_err(context, 1, ret, "addresses should be one"); 93 1.1 elric 94 1.1 elric krb5_print_address(&addresses.val[0], buf, truncate_len, &len); 95 1.1 elric 96 1.1 elric #if 0 97 1.1 elric printf("addr %s (%d/%d) should be %d\n", buf, (int)len, (int)strlen(buf), (int)outlen); 98 1.1 elric #endif 99 1.1 elric 100 1.1 elric if (truncate_len > strlen(buf) + 1) 101 1.1 elric krb5_err(context, 1, ret, "%s truncate_len %d larger then strlen %d source %s", 102 1.1 elric buf, (int)truncate_len, (int)strlen(buf), addr); 103 1.1 elric 104 1.1 elric if (outlen != len) 105 1.1 elric krb5_err(context, 1, ret, "%s: outlen %d != len %d", 106 1.1 elric buf, (int)outlen, (int)strlen(buf)); 107 1.1 elric 108 1.1 elric krb5_print_address(&addresses.val[0], buf, outlen + 1, &len); 109 1.1 elric 110 1.1 elric #if 0 111 1.1 elric printf("addr %s (%d/%d)\n", buf, (int)len, (int)strlen(buf)); 112 1.1 elric #endif 113 1.1 elric 114 1.1 elric if (len != outlen) 115 1.1 elric abort(); 116 1.1 elric if (strlen(buf) != len) 117 1.1 elric abort(); 118 1.1 elric 119 1.1 elric krb5_free_addresses(context, &addresses); 120 1.1 elric free(buf); 121 1.1 elric } 122 1.1 elric 123 1.1 elric static void 124 1.1 elric check_truncation(krb5_context context, const char *addr) 125 1.1 elric { 126 1.1 elric int i, len = strlen(addr); 127 1.1 elric 128 1.1 elric truncated_addr(context, addr, len, len); 129 1.1 elric 130 1.1 elric for (i = 0; i < len; i++) 131 1.1 elric truncated_addr(context, addr, i, len); 132 1.1 elric } 133 1.1 elric 134 1.1 elric static void 135 1.1 elric match_addr(krb5_context context, const char *range_addr, 136 1.1 elric const char *one_addr, int match) 137 1.1 elric { 138 1.1 elric krb5_addresses range, one; 139 1.1 elric krb5_error_code ret; 140 1.1 elric 141 1.1 elric ret = krb5_parse_address(context, range_addr, &range); 142 1.1 elric if (ret) 143 1.1 elric krb5_err(context, 1, ret, "krb5_parse_address"); 144 1.1 elric 145 1.1 elric if (range.len != 1) 146 1.1 elric krb5_err(context, 1, ret, "wrong num of addresses"); 147 1.1 elric 148 1.1 elric ret = krb5_parse_address(context, one_addr, &one); 149 1.1 elric if (ret) 150 1.1 elric krb5_err(context, 1, ret, "krb5_parse_address"); 151 1.1 elric 152 1.1 elric if (one.len != 1) 153 1.1 elric krb5_err(context, 1, ret, "wrong num of addresses"); 154 1.1 elric 155 1.1 elric if (krb5_address_order(context, &range.val[0], &one.val[0]) == 0) { 156 1.1 elric if (!match) 157 1.1 elric krb5_errx(context, 1, "match when one shouldn't be"); 158 1.1 elric } else { 159 1.1 elric if (match) 160 1.1 elric krb5_errx(context, 1, "no match when one should be"); 161 1.1 elric } 162 1.1 elric 163 1.1 elric krb5_free_addresses(context, &range); 164 1.1 elric krb5_free_addresses(context, &one); 165 1.1 elric } 166 1.1 elric 167 1.1 elric #ifdef _MSC_VER 168 1.1 elric 169 1.1 elric /* For the truncation tests, calling strcpy_s() or strcat_s() with a 170 1.1 elric size of 0 results in the invalid parameter handler being invoked. 171 1.1 elric For the debug version, the runtime also throws an assert. */ 172 1.1 elric 173 1.1 elric static void 174 1.1 elric inv_param_handler(const wchar_t* expression, 175 1.1 elric const wchar_t* function, 176 1.1 elric const wchar_t* file, 177 1.1 elric unsigned int line, 178 1.1 elric uintptr_t pReserved) 179 1.1 elric { 180 1.1 elric printf("Invalid parameter handler invoked for: %S in %S(%d) [%S]\n", 181 1.1 elric function, file, line, expression); 182 1.1 elric } 183 1.1 elric 184 1.1 elric static _invalid_parameter_handler _inv_old = NULL; 185 1.1 elric 186 1.1 elric #define SET_INVALID_PARAM_HANDLER _inv_old = _set_invalid_parameter_handler(inv_param_handler) 187 1.1 elric 188 1.1 elric #else 189 1.1 elric 190 1.1 elric #define SET_INVALID_PARAM_HANDLER ((void) 0) 191 1.1 elric 192 1.1 elric #endif 193 1.1 elric 194 1.1 elric int 195 1.1 elric main(int argc, char **argv) 196 1.1 elric { 197 1.1 elric krb5_context context; 198 1.1 elric krb5_error_code ret; 199 1.1 elric 200 1.1 elric SET_INVALID_PARAM_HANDLER; 201 1.1 elric 202 1.1 elric setprogname(argv[0]); 203 1.1 elric 204 1.1 elric ret = krb5_init_context(&context); 205 1.1 elric if (ret) 206 1.1 elric errx (1, "krb5_init_context failed: %d", ret); 207 1.1 elric 208 1.1 elric print_addr(context, "RANGE:127.0.0.0/8"); 209 1.1 elric print_addr(context, "RANGE:127.0.0.0/24"); 210 1.1 elric print_addr(context, "RANGE:IPv4:127.0.0.0-IPv4:127.0.0.255"); 211 1.1 elric print_addr(context, "RANGE:130.237.237.4/29"); 212 1.1 elric #ifdef HAVE_IPV6 213 1.1 elric print_addr(context, "RANGE:2001:db8:1:2:3:4:1428:7ab/64"); 214 1.1 elric print_addr(context, "RANGE:IPv6:fe80::209:6bff:fea0:e522/64"); 215 1.1 elric print_addr(context, "RANGE:IPv6:fe80::-IPv6:fe80::ffff:ffff:ffff:ffff"); 216 1.1 elric print_addr(context, "RANGE:fe80::-fe80::ffff:ffff:ffff:ffff"); 217 1.1 elric #endif 218 1.1 elric 219 1.1 elric check_truncation(context, "IPv4:127.0.0.0"); 220 1.1 elric check_truncation(context, "RANGE:IPv4:127.0.0.0-IPv4:127.0.0.255"); 221 1.1 elric #ifdef HAVE_IPV6 222 1.1 elric check_truncation(context, "IPv6:::"); 223 1.1 elric check_truncation(context, "IPv6:::1"); 224 1.1 elric check_truncation(context, "IPv6:2001:db8:1:2:3:4:1428:7ab"); 225 1.1 elric check_truncation(context, "IPv6:fe80::209:0:0:0"); 226 1.1 elric check_truncation(context, "IPv6:fe80::ffff:ffff:ffff:ffff"); 227 1.1 elric #endif 228 1.1 elric 229 1.1 elric match_addr(context, "RANGE:127.0.0.0/8", "inet:127.0.0.0", 1); 230 1.1 elric match_addr(context, "RANGE:127.0.0.0/8", "inet:127.255.255.255", 1); 231 1.1 elric match_addr(context, "RANGE:127.0.0.0/8", "inet:128.0.0.0", 0); 232 1.1 elric 233 1.1 elric match_addr(context, "RANGE:130.237.237.8/29", "inet:130.237.237.7", 0); 234 1.1 elric match_addr(context, "RANGE:130.237.237.8/29", "inet:130.237.237.8", 1); 235 1.1 elric match_addr(context, "RANGE:130.237.237.8/29", "inet:130.237.237.15", 1); 236 1.1 elric match_addr(context, "RANGE:130.237.237.8/29", "inet:130.237.237.16", 0); 237 1.1 elric 238 1.1 elric krb5_free_context(context); 239 1.1 elric 240 1.1 elric return 0; 241 1.1 elric } 242