2 * Copyright (c) 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
33 #include "krb5_locl.h"
37 print_addr(krb5_context context
, const char *addr
)
39 krb5_addresses addresses
;
46 ret
= krb5_parse_address(context
, addr
, &addresses
);
48 krb5_err(context
, 1, ret
, "krb5_parse_address");
50 if (addresses
.len
< 1)
51 krb5_err(context
, 1, ret
, "too few addresses");
53 for (i
= 0; i
< addresses
.len
; i
++) {
54 krb5_print_address(&addresses
.val
[i
], buf
, sizeof(buf
), &len
);
56 printf("addr %d: %s (%d/%d)\n", i
, buf
, (int)len
, (int)strlen(buf
));
58 if (strlen(buf
) > sizeof(buf
))
60 krb5_print_address(&addresses
.val
[i
], buf2
, sizeof(buf2
), &len
);
62 printf("addr %d: %s (%d/%d)\n", i
, buf2
, (int)len
, (int)strlen(buf2
));
64 if (strlen(buf2
) > sizeof(buf2
))
68 krb5_free_addresses(context
, &addresses
);
73 truncated_addr(krb5_context context
, const char *addr
,
74 size_t truncate_len
, size_t outlen
)
76 krb5_addresses addresses
;
81 buf
= ecalloc(1, outlen
+ 1);
83 ret
= krb5_parse_address(context
, addr
, &addresses
);
85 krb5_err(context
, 1, ret
, "krb5_parse_address");
87 if (addresses
.len
!= 1)
88 krb5_err(context
, 1, ret
, "addresses should be one");
90 krb5_print_address(&addresses
.val
[0], buf
, truncate_len
, &len
);
93 printf("addr %s (%d/%d) should be %d\n", buf
, (int)len
, (int)strlen(buf
), (int)outlen
);
96 if (truncate_len
> strlen(buf
) + 1)
101 krb5_print_address(&addresses
.val
[0], buf
, outlen
+ 1, &len
);
104 printf("addr %s (%d/%d)\n", buf
, (int)len
, (int)strlen(buf
));
109 if (strlen(buf
) != len
)
112 krb5_free_addresses(context
, &addresses
);
117 check_truncation(krb5_context context
, const char *addr
)
119 int i
, len
= strlen(addr
);
121 truncated_addr(context
, addr
, len
, len
);
123 for (i
= 0; i
< len
; i
++)
124 truncated_addr(context
, addr
, i
, len
);
128 match_addr(krb5_context context
, const char *range_addr
,
129 const char *one_addr
, int match
)
131 krb5_addresses range
, one
;
134 ret
= krb5_parse_address(context
, range_addr
, &range
);
136 krb5_err(context
, 1, ret
, "krb5_parse_address");
139 krb5_err(context
, 1, ret
, "wrong num of addresses");
141 ret
= krb5_parse_address(context
, one_addr
, &one
);
143 krb5_err(context
, 1, ret
, "krb5_parse_address");
146 krb5_err(context
, 1, ret
, "wrong num of addresses");
148 if (krb5_address_order(context
, &range
.val
[0], &one
.val
[0]) == 0) {
150 krb5_errx(context
, 1, "match when one shouldn't be");
153 krb5_errx(context
, 1, "no match when one should be");
156 krb5_free_addresses(context
, &range
);
157 krb5_free_addresses(context
, &one
);
162 /* For the truncation tests, calling strcpy_s() or strcat_s() with a
163 size of 0 results in the invalid parameter handler being invoked.
164 For the debug version, the runtime also throws an assert. */
167 inv_param_handler(const wchar_t* expression
,
168 const wchar_t* function
,
173 printf("Invalid parameter handler invoked for: %S in %S(%d) [%S]\n",
174 function
, file
, line
, expression
);
177 static _invalid_parameter_handler _inv_old
= NULL
;
179 #define SET_INVALID_PARAM_HANDLER _inv_old = _set_invalid_parameter_handler(inv_param_handler)
183 #define SET_INVALID_PARAM_HANDLER ((void) 0)
188 main(int argc
, char **argv
)
190 krb5_context context
;
193 SET_INVALID_PARAM_HANDLER
;
195 setprogname(argv
[0]);
197 ret
= krb5_init_context(&context
);
199 errx (1, "krb5_init_context failed: %d", ret
);
201 print_addr(context
, "RANGE:127.0.0.0/8");
202 print_addr(context
, "RANGE:127.0.0.0/24");
203 print_addr(context
, "RANGE:IPv4:127.0.0.0-IPv4:127.0.0.255");
204 print_addr(context
, "RANGE:130.237.237.4/29");
206 print_addr(context
, "RANGE:fe80::209:6bff:fea0:e522/64");
207 print_addr(context
, "RANGE:IPv6:fe80::209:6bff:fea0:e522/64");
208 print_addr(context
, "RANGE:IPv6:fe80::-IPv6:fe80::ffff:ffff:ffff:ffff");
209 print_addr(context
, "RANGE:fe80::-fe80::ffff:ffff:ffff:ffff");
212 check_truncation(context
, "IPv4:127.0.0.0");
213 check_truncation(context
, "RANGE:IPv4:127.0.0.0-IPv4:127.0.0.255");
215 check_truncation(context
, "IPv6:::");
216 check_truncation(context
, "IPv6:::1");
217 check_truncation(context
, "IPv6:fe80:9:c3e::209:6bff:fea0:e522");
218 check_truncation(context
, "IPv6:fe80::209:0:0:0");
219 check_truncation(context
, "IPv6:fe80::ffff:ffff:ffff:ffff");
222 match_addr(context
, "RANGE:127.0.0.0/8", "inet:127.0.0.0", 1);
223 match_addr(context
, "RANGE:127.0.0.0/8", "inet:127.255.255.255", 1);
224 match_addr(context
, "RANGE:127.0.0.0/8", "inet:128.0.0.0", 0);
226 match_addr(context
, "RANGE:130.237.237.8/29", "inet:130.237.237.7", 0);
227 match_addr(context
, "RANGE:130.237.237.8/29", "inet:130.237.237.8", 1);
228 match_addr(context
, "RANGE:130.237.237.8/29", "inet:130.237.237.15", 1);
229 match_addr(context
, "RANGE:130.237.237.8/29", "inet:130.237.237.16", 0);
231 krb5_free_context(context
);