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