No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / test_addr.c
blob5783d5799d68887f7bd2d4a71f979c1c076232c3
1 /*
2 * Copyright (c) 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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"
34 #include <err.h>
36 __RCSID("$Heimdal: test_addr.c 15036 2005-04-30 15:19:58Z lha $"
37 "$NetBSD$");
39 static void
40 print_addr(krb5_context context, const char *addr)
42 krb5_addresses addresses;
43 krb5_error_code ret;
44 char buf[38];
45 char buf2[1000];
46 size_t len;
47 int i;
49 ret = krb5_parse_address(context, addr, &addresses);
50 if (ret)
51 krb5_err(context, 1, ret, "krb5_parse_address");
53 if (addresses.len < 1)
54 krb5_err(context, 1, ret, "too few addresses");
56 for (i = 0; i < addresses.len; i++) {
57 krb5_print_address(&addresses.val[i], buf, sizeof(buf), &len);
58 #if 0
59 printf("addr %d: %s (%d/%d)\n", i, buf, (int)len, (int)strlen(buf));
60 #endif
61 if (strlen(buf) > sizeof(buf))
62 abort();
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 abort();
71 krb5_free_addresses(context, &addresses);
75 static void
76 truncated_addr(krb5_context context, const char *addr,
77 size_t truncate_len, size_t outlen)
79 krb5_addresses addresses;
80 krb5_error_code ret;
81 char *buf;
82 size_t len;
84 buf = ecalloc(1, outlen + 1);
86 ret = krb5_parse_address(context, addr, &addresses);
87 if (ret)
88 krb5_err(context, 1, ret, "krb5_parse_address");
90 if (addresses.len != 1)
91 krb5_err(context, 1, ret, "addresses should be one");
93 krb5_print_address(&addresses.val[0], buf, truncate_len, &len);
95 #if 0
96 printf("addr %s (%d/%d)\n", buf, (int)len, (int)strlen(buf));
97 #endif
99 if (truncate_len > strlen(buf) + 1)
100 abort();
101 if (outlen != len)
102 abort();
104 krb5_print_address(&addresses.val[0], buf, outlen + 1, &len);
106 #if 0
107 printf("addr %s (%d/%d)\n", buf, (int)len, (int)strlen(buf));
108 #endif
110 if (len != outlen)
111 abort();
112 if (strlen(buf) != len)
113 abort();
115 krb5_free_addresses(context, &addresses);
116 free(buf);
119 static void
120 check_truncation(krb5_context context, const char *addr)
122 int i, len = strlen(addr);
124 for (i = 0; i < len; i++)
125 truncated_addr(context, addr, i, len);
128 static void
129 match_addr(krb5_context context, const char *range_addr,
130 const char *one_addr, int match)
132 krb5_addresses range, one;
133 krb5_error_code ret;
135 ret = krb5_parse_address(context, range_addr, &range);
136 if (ret)
137 krb5_err(context, 1, ret, "krb5_parse_address");
139 if (range.len != 1)
140 krb5_err(context, 1, ret, "wrong num of addresses");
142 ret = krb5_parse_address(context, one_addr, &one);
143 if (ret)
144 krb5_err(context, 1, ret, "krb5_parse_address");
146 if (one.len != 1)
147 krb5_err(context, 1, ret, "wrong num of addresses");
149 if (krb5_address_order(context, &range.val[0], &one.val[0]) == 0) {
150 if (!match)
151 krb5_errx(context, 1, "match when one shouldn't be");
152 } else {
153 if (match)
154 krb5_errx(context, 1, "no match when one should be");
157 krb5_free_addresses(context, &range);
158 krb5_free_addresses(context, &one);
162 main(int argc, char **argv)
164 krb5_context context;
165 krb5_error_code ret;
167 setprogname(argv[0]);
169 ret = krb5_init_context(&context);
170 if (ret)
171 errx (1, "krb5_init_context failed: %d", ret);
173 print_addr(context, "RANGE:127.0.0.0/8");
174 print_addr(context, "RANGE:127.0.0.0/24");
175 print_addr(context, "RANGE:IPv4:127.0.0.0-IPv4:127.0.0.255");
176 print_addr(context, "RANGE:130.237.237.4/29");
177 #ifdef HAVE_IPV6
178 print_addr(context, "RANGE:fe80::209:6bff:fea0:e522/64");
179 print_addr(context, "RANGE:IPv6:fe80::209:6bff:fea0:e522/64");
180 print_addr(context, "RANGE:IPv6:fe80::-IPv6:fe80::ffff:ffff:ffff:ffff");
181 print_addr(context, "RANGE:fe80::-fe80::ffff:ffff:ffff:ffff");
182 #endif
184 check_truncation(context, "IPv4:127.0.0.0");
185 check_truncation(context, "RANGE:IPv4:127.0.0.0-IPv4:127.0.0.255");
186 #ifdef HAVE_IPV6
187 check_truncation(context, "IPv6:::1");
188 check_truncation(context, "IPv6:fe80::ffff:ffff:ffff:ffff");
189 #endif
191 match_addr(context, "RANGE:127.0.0.0/8", "inet:127.0.0.0", 1);
192 match_addr(context, "RANGE:127.0.0.0/8", "inet:127.255.255.255", 1);
193 match_addr(context, "RANGE:127.0.0.0/8", "inet:128.0.0.0", 0);
195 match_addr(context, "RANGE:130.237.237.8/29", "inet:130.237.237.7", 0);
196 match_addr(context, "RANGE:130.237.237.8/29", "inet:130.237.237.8", 1);
197 match_addr(context, "RANGE:130.237.237.8/29", "inet:130.237.237.15", 1);
198 match_addr(context, "RANGE:130.237.237.8/29", "inet:130.237.237.16", 0);
200 krb5_free_context(context);
202 return 0;