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