xref: /netbsd-src/external/bsd/unbound/dist/testcode/unitmain.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /*
2  * testcode/unitmain.c - unit test main program for unbound.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 /**
37  * \file
38  * Unit test main program. Calls all the other unit tests.
39  * Exits with code 1 on a failure. 0 if all unit tests are successful.
40  */
41 
42 #include "config.h"
43 #ifdef HAVE_OPENSSL_ERR_H
44 #include <openssl/err.h>
45 #endif
46 
47 #ifdef HAVE_OPENSSL_RAND_H
48 #include <openssl/rand.h>
49 #endif
50 
51 #ifdef HAVE_OPENSSL_CONF_H
52 #include <openssl/conf.h>
53 #endif
54 
55 #ifdef HAVE_OPENSSL_ENGINE_H
56 #include <openssl/engine.h>
57 #endif
58 
59 #ifdef HAVE_NSS
60 /* nss3 */
61 #include "nss.h"
62 #endif
63 
64 #include "sldns/rrdef.h"
65 #include "sldns/keyraw.h"
66 #include "util/log.h"
67 #include "testcode/unitmain.h"
68 
69 /** number of tests done */
70 int testcount = 0;
71 
72 #include "util/alloc.h"
73 /** test alloc code */
74 static void
75 alloc_test(void) {
76 	alloc_special_t *t1, *t2;
77 	struct alloc_cache major, minor1, minor2;
78 	int i;
79 
80 	unit_show_feature("alloc_special_obtain");
81 	alloc_init(&major, NULL, 0);
82 	alloc_init(&minor1, &major, 0);
83 	alloc_init(&minor2, &major, 1);
84 
85 	t1 = alloc_special_obtain(&minor1);
86 	alloc_clear(&minor1);
87 
88 	alloc_special_release(&minor2, t1);
89 	t2 = alloc_special_obtain(&minor2);
90 	unit_assert( t1 == t2 ); /* reused */
91 	alloc_special_release(&minor2, t1);
92 
93 	for(i=0; i<100; i++) {
94 		t1 = alloc_special_obtain(&minor1);
95 		alloc_special_release(&minor2, t1);
96 	}
97 	if(0) {
98 		alloc_stats(&minor1);
99 		alloc_stats(&minor2);
100 		alloc_stats(&major);
101 	}
102 	/* reuse happened */
103 	unit_assert(minor1.num_quar + minor2.num_quar + major.num_quar == 11);
104 
105 	alloc_clear(&minor1);
106 	alloc_clear(&minor2);
107 	unit_assert(major.num_quar == 11);
108 	alloc_clear(&major);
109 }
110 
111 #include "util/net_help.h"
112 /** test net code */
113 static void
114 net_test(void)
115 {
116 	const char* t4[] = {"\000\000\000\000",
117 		"\200\000\000\000",
118 		"\300\000\000\000",
119 		"\340\000\000\000",
120 		"\360\000\000\000",
121 		"\370\000\000\000",
122 		"\374\000\000\000",
123 		"\376\000\000\000",
124 		"\377\000\000\000",
125 		"\377\200\000\000",
126 		"\377\300\000\000",
127 		"\377\340\000\000",
128 		"\377\360\000\000",
129 		"\377\370\000\000",
130 		"\377\374\000\000",
131 		"\377\376\000\000",
132 		"\377\377\000\000",
133 		"\377\377\200\000",
134 		"\377\377\300\000",
135 		"\377\377\340\000",
136 		"\377\377\360\000",
137 		"\377\377\370\000",
138 		"\377\377\374\000",
139 		"\377\377\376\000",
140 		"\377\377\377\000",
141 		"\377\377\377\200",
142 		"\377\377\377\300",
143 		"\377\377\377\340",
144 		"\377\377\377\360",
145 		"\377\377\377\370",
146 		"\377\377\377\374",
147 		"\377\377\377\376",
148 		"\377\377\377\377",
149 		"\377\377\377\377",
150 		"\377\377\377\377",
151 	};
152 	unit_show_func("util/net_help.c", "str_is_ip6");
153 	unit_assert( str_is_ip6("::") );
154 	unit_assert( str_is_ip6("::1") );
155 	unit_assert( str_is_ip6("2001:7b8:206:1:240:f4ff:fe37:8810") );
156 	unit_assert( str_is_ip6("fe80::240:f4ff:fe37:8810") );
157 	unit_assert( !str_is_ip6("0.0.0.0") );
158 	unit_assert( !str_is_ip6("213.154.224.12") );
159 	unit_assert( !str_is_ip6("213.154.224.255") );
160 	unit_assert( !str_is_ip6("255.255.255.0") );
161 	unit_show_func("util/net_help.c", "is_pow2");
162 	unit_assert( is_pow2(0) );
163 	unit_assert( is_pow2(1) );
164 	unit_assert( is_pow2(2) );
165 	unit_assert( is_pow2(4) );
166 	unit_assert( is_pow2(8) );
167 	unit_assert( is_pow2(16) );
168 	unit_assert( is_pow2(1024) );
169 	unit_assert( is_pow2(1024*1024) );
170 	unit_assert( is_pow2(1024*1024*1024) );
171 	unit_assert( !is_pow2(3) );
172 	unit_assert( !is_pow2(5) );
173 	unit_assert( !is_pow2(6) );
174 	unit_assert( !is_pow2(7) );
175 	unit_assert( !is_pow2(9) );
176 	unit_assert( !is_pow2(10) );
177 	unit_assert( !is_pow2(11) );
178 	unit_assert( !is_pow2(17) );
179 	unit_assert( !is_pow2(23) );
180 	unit_assert( !is_pow2(257) );
181 	unit_assert( !is_pow2(259) );
182 
183 	/* test addr_mask */
184 	unit_show_func("util/net_help.c", "addr_mask");
185 	if(1) {
186 		struct sockaddr_in a4;
187 		struct sockaddr_in6 a6;
188 		socklen_t l4 = (socklen_t)sizeof(a4);
189 		socklen_t l6 = (socklen_t)sizeof(a6);
190 		int i;
191 		a4.sin_family = AF_INET;
192 		a6.sin6_family = AF_INET6;
193 		for(i=0; i<35; i++) {
194 			/* address 255.255.255.255 */
195 			memcpy(&a4.sin_addr, "\377\377\377\377", 4);
196 			addr_mask((struct sockaddr_storage*)&a4, l4, i);
197 			unit_assert(memcmp(&a4.sin_addr, t4[i], 4) == 0);
198 		}
199 		memcpy(&a6.sin6_addr, "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377", 16);
200 		addr_mask((struct sockaddr_storage*)&a6, l6, 128);
201 		unit_assert(memcmp(&a6.sin6_addr, "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377", 16) == 0);
202 		addr_mask((struct sockaddr_storage*)&a6, l6, 122);
203 		unit_assert(memcmp(&a6.sin6_addr, "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\300", 16) == 0);
204 		addr_mask((struct sockaddr_storage*)&a6, l6, 120);
205 		unit_assert(memcmp(&a6.sin6_addr, "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\000", 16) == 0);
206 		addr_mask((struct sockaddr_storage*)&a6, l6, 64);
207 		unit_assert(memcmp(&a6.sin6_addr, "\377\377\377\377\377\377\377\377\000\000\000\000\000\000\000\000", 16) == 0);
208 		addr_mask((struct sockaddr_storage*)&a6, l6, 0);
209 		unit_assert(memcmp(&a6.sin6_addr, "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000", 16) == 0);
210 	}
211 
212 	/* test addr_in_common */
213 	unit_show_func("util/net_help.c", "addr_in_common");
214 	if(1) {
215 		struct sockaddr_in a4, b4;
216 		struct sockaddr_in6 a6, b6;
217 		socklen_t l4 = (socklen_t)sizeof(a4);
218 		socklen_t l6 = (socklen_t)sizeof(a6);
219 		int i;
220 		a4.sin_family = AF_INET;
221 		b4.sin_family = AF_INET;
222 		a6.sin6_family = AF_INET6;
223 		b6.sin6_family = AF_INET6;
224 		memcpy(&a4.sin_addr, "abcd", 4);
225 		memcpy(&b4.sin_addr, "abcd", 4);
226 		unit_assert(addr_in_common((struct sockaddr_storage*)&a4, 32,
227 			(struct sockaddr_storage*)&b4, 32, l4) == 32);
228 		unit_assert(addr_in_common((struct sockaddr_storage*)&a4, 34,
229 			(struct sockaddr_storage*)&b4, 32, l4) == 32);
230 		for(i=0; i<=32; i++) {
231 			unit_assert(addr_in_common(
232 				(struct sockaddr_storage*)&a4, 32,
233 				(struct sockaddr_storage*)&b4, i, l4) == i);
234 			unit_assert(addr_in_common(
235 				(struct sockaddr_storage*)&a4, i,
236 				(struct sockaddr_storage*)&b4, 32, l4) == i);
237 			unit_assert(addr_in_common(
238 				(struct sockaddr_storage*)&a4, i,
239 				(struct sockaddr_storage*)&b4, i, l4) == i);
240 		}
241 		for(i=0; i<=32; i++) {
242 			memcpy(&a4.sin_addr, "\377\377\377\377", 4);
243 			memcpy(&b4.sin_addr, t4[i], 4);
244 			unit_assert(addr_in_common(
245 				(struct sockaddr_storage*)&a4, 32,
246 				(struct sockaddr_storage*)&b4, 32, l4) == i);
247 			unit_assert(addr_in_common(
248 				(struct sockaddr_storage*)&b4, 32,
249 				(struct sockaddr_storage*)&a4, 32, l4) == i);
250 		}
251 		memcpy(&a6.sin6_addr, "abcdefghabcdefgh", 16);
252 		memcpy(&b6.sin6_addr, "abcdefghabcdefgh", 16);
253 		unit_assert(addr_in_common((struct sockaddr_storage*)&a6, 128,
254 			(struct sockaddr_storage*)&b6, 128, l6) == 128);
255 		unit_assert(addr_in_common((struct sockaddr_storage*)&a6, 129,
256 			(struct sockaddr_storage*)&b6, 128, l6) == 128);
257 		for(i=0; i<=128; i++) {
258 			unit_assert(addr_in_common(
259 				(struct sockaddr_storage*)&a6, 128,
260 				(struct sockaddr_storage*)&b6, i, l6) == i);
261 			unit_assert(addr_in_common(
262 				(struct sockaddr_storage*)&a6, i,
263 				(struct sockaddr_storage*)&b6, 128, l6) == i);
264 			unit_assert(addr_in_common(
265 				(struct sockaddr_storage*)&a6, i,
266 				(struct sockaddr_storage*)&b6, i, l6) == i);
267 		}
268 	}
269 	/* test sockaddr_cmp_addr */
270 	unit_show_func("util/net_help.c", "sockaddr_cmp_addr");
271 	if(1) {
272 		struct sockaddr_storage a, b;
273 		socklen_t alen = (socklen_t)sizeof(a);
274 		socklen_t blen = (socklen_t)sizeof(b);
275 		unit_assert(ipstrtoaddr("127.0.0.0", 53, &a, &alen));
276 		unit_assert(ipstrtoaddr("127.255.255.255", 53, &b, &blen));
277 		unit_assert(sockaddr_cmp_addr(&a, alen, &b, blen) < 0);
278 		unit_assert(sockaddr_cmp_addr(&b, blen, &a, alen) > 0);
279 		unit_assert(sockaddr_cmp_addr(&a, alen, &a, alen) == 0);
280 		unit_assert(sockaddr_cmp_addr(&b, blen, &b, blen) == 0);
281 		unit_assert(ipstrtoaddr("192.168.121.5", 53, &a, &alen));
282 		unit_assert(sockaddr_cmp_addr(&a, alen, &b, blen) > 0);
283 		unit_assert(sockaddr_cmp_addr(&b, blen, &a, alen) < 0);
284 		unit_assert(sockaddr_cmp_addr(&a, alen, &a, alen) == 0);
285 		unit_assert(ipstrtoaddr("2001:3578:ffeb::99", 53, &b, &blen));
286 		unit_assert(sockaddr_cmp_addr(&b, blen, &b, blen) == 0);
287 		unit_assert(sockaddr_cmp_addr(&a, alen, &b, blen) < 0);
288 		unit_assert(sockaddr_cmp_addr(&b, blen, &a, alen) > 0);
289 	}
290 	/* test addr_is_ip4mapped */
291 	unit_show_func("util/net_help.c", "addr_is_ip4mapped");
292 	if(1) {
293 		struct sockaddr_storage a;
294 		socklen_t l = (socklen_t)sizeof(a);
295 		unit_assert(ipstrtoaddr("12.13.14.15", 53, &a, &l));
296 		unit_assert(!addr_is_ip4mapped(&a, l));
297 		unit_assert(ipstrtoaddr("fe80::217:31ff:fe91:df", 53, &a, &l));
298 		unit_assert(!addr_is_ip4mapped(&a, l));
299 		unit_assert(ipstrtoaddr("ffff::217:31ff:fe91:df", 53, &a, &l));
300 		unit_assert(!addr_is_ip4mapped(&a, l));
301 		unit_assert(ipstrtoaddr("::ffff:31ff:fe91:df", 53, &a, &l));
302 		unit_assert(!addr_is_ip4mapped(&a, l));
303 		unit_assert(ipstrtoaddr("::fffe:fe91:df", 53, &a, &l));
304 		unit_assert(!addr_is_ip4mapped(&a, l));
305 		unit_assert(ipstrtoaddr("::ffff:127.0.0.1", 53, &a, &l));
306 		unit_assert(addr_is_ip4mapped(&a, l));
307 		unit_assert(ipstrtoaddr("::ffff:127.0.0.2", 53, &a, &l));
308 		unit_assert(addr_is_ip4mapped(&a, l));
309 		unit_assert(ipstrtoaddr("::ffff:192.168.0.2", 53, &a, &l));
310 		unit_assert(addr_is_ip4mapped(&a, l));
311 		unit_assert(ipstrtoaddr("2::ffff:192.168.0.2", 53, &a, &l));
312 		unit_assert(!addr_is_ip4mapped(&a, l));
313 	}
314 	/* test addr_is_any */
315 	unit_show_func("util/net_help.c", "addr_is_any");
316 	if(1) {
317 		struct sockaddr_storage a;
318 		socklen_t l = (socklen_t)sizeof(a);
319 		unit_assert(ipstrtoaddr("0.0.0.0", 53, &a, &l));
320 		unit_assert(addr_is_any(&a, l));
321 		unit_assert(ipstrtoaddr("0.0.0.0", 10053, &a, &l));
322 		unit_assert(addr_is_any(&a, l));
323 		unit_assert(ipstrtoaddr("0.0.0.0", 0, &a, &l));
324 		unit_assert(addr_is_any(&a, l));
325 		unit_assert(ipstrtoaddr("::0", 0, &a, &l));
326 		unit_assert(addr_is_any(&a, l));
327 		unit_assert(ipstrtoaddr("::0", 53, &a, &l));
328 		unit_assert(addr_is_any(&a, l));
329 		unit_assert(ipstrtoaddr("::1", 53, &a, &l));
330 		unit_assert(!addr_is_any(&a, l));
331 		unit_assert(ipstrtoaddr("2001:1667::1", 0, &a, &l));
332 		unit_assert(!addr_is_any(&a, l));
333 		unit_assert(ipstrtoaddr("2001::0", 0, &a, &l));
334 		unit_assert(!addr_is_any(&a, l));
335 		unit_assert(ipstrtoaddr("10.0.0.0", 0, &a, &l));
336 		unit_assert(!addr_is_any(&a, l));
337 		unit_assert(ipstrtoaddr("0.0.0.10", 0, &a, &l));
338 		unit_assert(!addr_is_any(&a, l));
339 		unit_assert(ipstrtoaddr("192.0.2.1", 0, &a, &l));
340 		unit_assert(!addr_is_any(&a, l));
341 	}
342 }
343 
344 #include "util/config_file.h"
345 /** test config_file: cfg_parse_memsize */
346 static void
347 config_memsize_test(void)
348 {
349 	size_t v = 0;
350 	unit_show_func("util/config_file.c", "cfg_parse_memsize");
351 	if(0) {
352 		/* these emit errors */
353 		unit_assert( cfg_parse_memsize("", &v) == 0);
354 		unit_assert( cfg_parse_memsize("bla", &v) == 0);
355 		unit_assert( cfg_parse_memsize("nop", &v) == 0);
356 		unit_assert( cfg_parse_memsize("n0b", &v) == 0);
357 		unit_assert( cfg_parse_memsize("gb", &v) == 0);
358 		unit_assert( cfg_parse_memsize("b", &v) == 0);
359 		unit_assert( cfg_parse_memsize("kb", &v) == 0);
360 		unit_assert( cfg_parse_memsize("kk kb", &v) == 0);
361 	}
362 	unit_assert( cfg_parse_memsize("0", &v) && v==0);
363 	unit_assert( cfg_parse_memsize("1", &v) && v==1);
364 	unit_assert( cfg_parse_memsize("10", &v) && v==10);
365 	unit_assert( cfg_parse_memsize("10b", &v) && v==10);
366 	unit_assert( cfg_parse_memsize("5b", &v) && v==5);
367 	unit_assert( cfg_parse_memsize("1024", &v) && v==1024);
368 	unit_assert( cfg_parse_memsize("1k", &v) && v==1024);
369 	unit_assert( cfg_parse_memsize("1K", &v) && v==1024);
370 	unit_assert( cfg_parse_memsize("1Kb", &v) && v==1024);
371 	unit_assert( cfg_parse_memsize("1kb", &v) && v==1024);
372 	unit_assert( cfg_parse_memsize("1 kb", &v) && v==1024);
373 	unit_assert( cfg_parse_memsize("10 kb", &v) && v==10240);
374 	unit_assert( cfg_parse_memsize("2k", &v) && v==2048);
375 	unit_assert( cfg_parse_memsize("2m", &v) && v==2048*1024);
376 	unit_assert( cfg_parse_memsize("3M", &v) && v==3072*1024);
377 	unit_assert( cfg_parse_memsize("40m", &v) && v==40960*1024);
378 	unit_assert( cfg_parse_memsize("1G", &v) && v==1024*1024*1024);
379 	unit_assert( cfg_parse_memsize("1 Gb", &v) && v==1024*1024*1024);
380 	unit_assert( cfg_parse_memsize("0 Gb", &v) && v==0*1024*1024);
381 }
382 
383 /** test config_file: test tag code */
384 static void
385 config_tag_test(void)
386 {
387 	unit_show_func("util/config_file.c", "taglist_intersect");
388 	unit_assert( taglist_intersect(
389 		(uint8_t*)"\000\000\000", 3, (uint8_t*)"\001\000\001", 3
390 		) == 0);
391 	unit_assert( taglist_intersect(
392 		(uint8_t*)"\000\000\001", 3, (uint8_t*)"\001\000\001", 3
393 		) == 1);
394 	unit_assert( taglist_intersect(
395 		(uint8_t*)"\001\000\000", 3, (uint8_t*)"\001\000\001", 3
396 		) == 1);
397 	unit_assert( taglist_intersect(
398 		(uint8_t*)"\001", 1, (uint8_t*)"\001\000\001", 3
399 		) == 1);
400 	unit_assert( taglist_intersect(
401 		(uint8_t*)"\001\000\001", 3, (uint8_t*)"\001", 1
402 		) == 1);
403 }
404 
405 #include "util/rtt.h"
406 /** test RTT code */
407 static void
408 rtt_test(void)
409 {
410 	int init = 376;
411 	int i;
412 	struct rtt_info r;
413 	unit_show_func("util/rtt.c", "rtt_timeout");
414 	rtt_init(&r);
415 	/* initial value sensible */
416 	unit_assert( rtt_timeout(&r) == init );
417 	rtt_lost(&r, init);
418 	unit_assert( rtt_timeout(&r) == init*2 );
419 	rtt_lost(&r, init*2);
420 	unit_assert( rtt_timeout(&r) == init*4 );
421 	rtt_update(&r, 4000);
422 	unit_assert( rtt_timeout(&r) >= 2000 );
423 	rtt_lost(&r, rtt_timeout(&r) );
424 	for(i=0; i<100; i++) {
425 		rtt_lost(&r, rtt_timeout(&r) );
426 		unit_assert( rtt_timeout(&r) > RTT_MIN_TIMEOUT-1);
427 		unit_assert( rtt_timeout(&r) < RTT_MAX_TIMEOUT+1);
428 	}
429 }
430 
431 #include "services/cache/infra.h"
432 #include "util/config_file.h"
433 
434 /* lookup and get key and data structs easily */
435 static struct infra_data* infra_lookup_host(struct infra_cache* infra,
436 	struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone,
437 	size_t zonelen, int wr, time_t now, struct infra_key** k)
438 {
439 	struct infra_data* d;
440 	struct lruhash_entry* e = infra_lookup_nottl(infra, addr, addrlen,
441 		zone, zonelen, wr);
442 	if(!e) return NULL;
443 	d = (struct infra_data*)e->data;
444 	if(d->ttl < now) {
445 		lock_rw_unlock(&e->lock);
446 		return NULL;
447 	}
448 	*k = (struct infra_key*)e->key;
449 	return d;
450 }
451 
452 /** test host cache */
453 static void
454 infra_test(void)
455 {
456 	struct sockaddr_storage one;
457 	socklen_t onelen;
458 	uint8_t* zone = (uint8_t*)"\007example\003com\000";
459 	size_t zonelen = 13;
460 	struct infra_cache* slab;
461 	struct config_file* cfg = config_create();
462 	time_t now = 0;
463 	uint8_t edns_lame;
464 	int vs, to;
465 	struct infra_key* k;
466 	struct infra_data* d;
467 	int init = 376;
468 
469 	unit_show_feature("infra cache");
470 	unit_assert(ipstrtoaddr("127.0.0.1", 53, &one, &onelen));
471 
472 	slab = infra_create(cfg);
473 	unit_assert( infra_host(slab, &one, onelen, zone, zonelen, now,
474 		&vs, &edns_lame, &to) );
475 	unit_assert( vs == 0 && to == init && edns_lame == 0 );
476 
477 	unit_assert( infra_rtt_update(slab, &one, onelen, zone, zonelen, LDNS_RR_TYPE_A, -1, init, now) );
478 	unit_assert( infra_host(slab, &one, onelen, zone, zonelen,
479 			now, &vs, &edns_lame, &to) );
480 	unit_assert( vs == 0 && to == init*2 && edns_lame == 0 );
481 
482 	unit_assert( infra_edns_update(slab, &one, onelen, zone, zonelen, -1, now) );
483 	unit_assert( infra_host(slab, &one, onelen, zone, zonelen,
484 			now, &vs, &edns_lame, &to) );
485 	unit_assert( vs == -1 && to == init*2  && edns_lame == 1);
486 
487 	now += cfg->host_ttl + 10;
488 	unit_assert( infra_host(slab, &one, onelen, zone, zonelen,
489 			now, &vs, &edns_lame, &to) );
490 	unit_assert( vs == 0 && to == init && edns_lame == 0 );
491 
492 	unit_assert( infra_set_lame(slab, &one, onelen,
493 		zone, zonelen,  now, 0, 0, LDNS_RR_TYPE_A) );
494 	unit_assert( (d=infra_lookup_host(slab, &one, onelen, zone, zonelen, 0, now, &k)) );
495 	unit_assert( d->ttl == now+cfg->host_ttl );
496 	unit_assert( d->edns_version == 0 );
497 	unit_assert(!d->isdnsseclame && !d->rec_lame && d->lame_type_A &&
498 		!d->lame_other);
499 	lock_rw_unlock(&k->entry.lock);
500 
501 	/* test merge of data */
502 	unit_assert( infra_set_lame(slab, &one, onelen,
503 		zone, zonelen,  now, 0, 0, LDNS_RR_TYPE_AAAA) );
504 	unit_assert( (d=infra_lookup_host(slab, &one, onelen, zone, zonelen, 0, now, &k)) );
505 	unit_assert(!d->isdnsseclame && !d->rec_lame && d->lame_type_A &&
506 		d->lame_other);
507 	lock_rw_unlock(&k->entry.lock);
508 
509 	/* test that noEDNS cannot overwrite known-yesEDNS */
510 	now += cfg->host_ttl + 10;
511 	unit_assert( infra_host(slab, &one, onelen, zone, zonelen,
512 			now, &vs, &edns_lame, &to) );
513 	unit_assert( vs == 0 && to == init && edns_lame == 0 );
514 
515 	unit_assert( infra_edns_update(slab, &one, onelen, zone, zonelen, 0, now) );
516 	unit_assert( infra_host(slab, &one, onelen, zone, zonelen,
517 			now, &vs, &edns_lame, &to) );
518 	unit_assert( vs == 0 && to == init && edns_lame == 1 );
519 
520 	unit_assert( infra_edns_update(slab, &one, onelen, zone, zonelen, -1, now) );
521 	unit_assert( infra_host(slab, &one, onelen, zone, zonelen,
522 			now, &vs, &edns_lame, &to) );
523 	unit_assert( vs == 0 && to == init && edns_lame == 1 );
524 
525 	infra_delete(slab);
526 	config_delete(cfg);
527 }
528 
529 #include "util/random.h"
530 /** test randomness */
531 static void
532 rnd_test(void)
533 {
534 	struct ub_randstate* r;
535 	int num = 1000, i;
536 	long int a[1000];
537 	unsigned int seed = (unsigned)time(NULL);
538 	unit_show_feature("ub_random");
539 	printf("ub_random seed is %u\n", seed);
540 	unit_assert( (r = ub_initstate(seed, NULL)) );
541 	for(i=0; i<num; i++) {
542 		a[i] = ub_random(r);
543 		unit_assert(a[i] >= 0);
544 		unit_assert((size_t)a[i] <= (size_t)0x7fffffff);
545 		if(i > 5)
546 			unit_assert(a[i] != a[i-1] || a[i] != a[i-2] ||
547 				a[i] != a[i-3] || a[i] != a[i-4] ||
548 				a[i] != a[i-5] || a[i] != a[i-6]);
549 	}
550 	a[0] = ub_random_max(r, 1);
551 	unit_assert(a[0] >= 0 && a[0] < 1);
552 	a[0] = ub_random_max(r, 10000);
553 	unit_assert(a[0] >= 0 && a[0] < 10000);
554 	for(i=0; i<num; i++) {
555 		a[i] = ub_random_max(r, 10);
556 		unit_assert(a[i] >= 0 && a[i] < 10);
557 	}
558 	ub_randfree(r);
559 }
560 
561 void unit_show_func(const char* file, const char* func)
562 {
563 	printf("test %s:%s\n", file, func);
564 }
565 
566 void unit_show_feature(const char* feature)
567 {
568 	printf("test %s functions\n", feature);
569 }
570 
571 /**
572  * Main unit test program. Setup, teardown and report errors.
573  * @param argc: arg count.
574  * @param argv: array of commandline arguments.
575  * @return program failure if test fails.
576  */
577 int
578 main(int argc, char* argv[])
579 {
580 	log_init(NULL, 0, NULL);
581 	if(argc != 1) {
582 		printf("usage: %s\n", argv[0]);
583 		printf("\tperforms unit tests.\n");
584 		return 1;
585 	}
586 	printf("Start of %s unit test.\n", PACKAGE_STRING);
587 #ifdef HAVE_SSL
588 	ERR_load_crypto_strings();
589 #  ifdef USE_GOST
590 	(void)sldns_key_EVP_load_gost_id();
591 #  endif
592 #elif defined(HAVE_NSS)
593 	if(NSS_NoDB_Init(".") != SECSuccess)
594 		fatal_exit("could not init NSS");
595 #endif /* HAVE_SSL or HAVE_NSS*/
596 	checklock_start();
597 	neg_test();
598 	rnd_test();
599 	verify_test();
600 	net_test();
601 	config_memsize_test();
602 	config_tag_test();
603 	dname_test();
604 	rtt_test();
605 	anchors_test();
606 	alloc_test();
607 	regional_test();
608 	lruhash_test();
609 	slabhash_test();
610 	infra_test();
611 	ldns_test();
612 	msgparse_test();
613 	checklock_stop();
614 	printf("%d checks ok.\n", testcount);
615 #ifdef HAVE_SSL
616 #  if defined(USE_GOST) && defined(HAVE_LDNS_KEY_EVP_UNLOAD_GOST)
617 	sldns_key_EVP_unload_gost();
618 #  endif
619 #  ifdef HAVE_OPENSSL_CONFIG
620 	EVP_cleanup();
621 	ENGINE_cleanup();
622 	CONF_modules_free();
623 #  endif
624 	CRYPTO_cleanup_all_ex_data();
625 	ERR_free_strings();
626 	RAND_cleanup();
627 #elif defined(HAVE_NSS)
628 	if(NSS_Shutdown() != SECSuccess)
629 		fatal_exit("could not shutdown NSS");
630 #endif /* HAVE_SSL or HAVE_NSS */
631 #ifdef HAVE_PTHREAD
632 	/* dlopen frees its thread specific state */
633 	pthread_exit(NULL);
634 #endif
635 	return 0;
636 }
637