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