2007-01-03 05:42:32 -05:00
|
|
|
/*
|
|
|
|
|
* testcode/unitmain.c - unit test main program for unbound.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2007, NLnet Labs. All rights reserved.
|
|
|
|
|
*
|
2007-01-26 06:57:09 -05:00
|
|
|
* This software is open source.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
*
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* Neither the name of the NLNET LABS nor the names of its contributors may
|
|
|
|
|
* be used to endorse or promote products derived from this software without
|
|
|
|
|
* specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
|
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2007-01-03 05:42:32 -05:00
|
|
|
*
|
|
|
|
|
*/
|
2007-01-03 09:55:21 -05:00
|
|
|
/**
|
|
|
|
|
* \file
|
|
|
|
|
* Unit test main program. Calls all the other unit tests.
|
|
|
|
|
* Exits with code 1 on a failure. 0 if all unit tests are successfull.
|
|
|
|
|
*/
|
2007-01-03 05:42:32 -05:00
|
|
|
|
|
|
|
|
#include "config.h"
|
2007-02-05 09:57:16 -05:00
|
|
|
#include "util/log.h"
|
2007-03-14 10:30:30 -04:00
|
|
|
#include "testcode/unitmain.h"
|
2007-02-05 09:57:16 -05:00
|
|
|
|
|
|
|
|
/** number of tests done */
|
|
|
|
|
int testcount = 0;
|
|
|
|
|
|
2007-03-09 08:37:57 -05:00
|
|
|
#include "util/alloc.h"
|
|
|
|
|
/** test alloc code */
|
|
|
|
|
static void
|
|
|
|
|
alloc_test() {
|
|
|
|
|
alloc_special_t *t1, *t2;
|
|
|
|
|
struct alloc_cache major, minor1, minor2;
|
|
|
|
|
int i;
|
|
|
|
|
|
2007-04-16 11:21:50 -04:00
|
|
|
alloc_init(&major, NULL, 0);
|
|
|
|
|
alloc_init(&minor1, &major, 0);
|
|
|
|
|
alloc_init(&minor2, &major, 1);
|
2007-03-09 08:37:57 -05:00
|
|
|
|
|
|
|
|
t1 = alloc_special_obtain(&minor1);
|
|
|
|
|
alloc_clear(&minor1);
|
|
|
|
|
|
|
|
|
|
alloc_special_release(&minor2, t1);
|
|
|
|
|
t2 = alloc_special_obtain(&minor2);
|
|
|
|
|
unit_assert( t1 == t2 ); /* reused */
|
|
|
|
|
alloc_special_release(&minor2, t1);
|
|
|
|
|
|
|
|
|
|
for(i=0; i<100; i++) {
|
|
|
|
|
t1 = alloc_special_obtain(&minor1);
|
|
|
|
|
alloc_special_release(&minor2, t1);
|
|
|
|
|
}
|
|
|
|
|
if(0) {
|
|
|
|
|
alloc_stats(&minor1);
|
|
|
|
|
alloc_stats(&minor2);
|
|
|
|
|
alloc_stats(&major);
|
|
|
|
|
}
|
|
|
|
|
/* reuse happened */
|
|
|
|
|
unit_assert(minor1.num_quar + minor2.num_quar + major.num_quar == 11);
|
|
|
|
|
|
|
|
|
|
alloc_clear(&minor1);
|
|
|
|
|
alloc_clear(&minor2);
|
|
|
|
|
unit_assert(major.num_quar == 11);
|
|
|
|
|
alloc_clear(&major);
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-14 09:02:50 -05:00
|
|
|
#include "util/net_help.h"
|
2007-02-06 11:26:19 -05:00
|
|
|
/** test net code */
|
|
|
|
|
static void
|
|
|
|
|
net_test()
|
2007-02-05 09:57:16 -05:00
|
|
|
{
|
|
|
|
|
unit_assert( str_is_ip6("::") );
|
|
|
|
|
unit_assert( str_is_ip6("::1") );
|
|
|
|
|
unit_assert( str_is_ip6("2001:7b8:206:1:240:f4ff:fe37:8810") );
|
|
|
|
|
unit_assert( str_is_ip6("fe80::240:f4ff:fe37:8810") );
|
|
|
|
|
unit_assert( !str_is_ip6("0.0.0.0") );
|
|
|
|
|
unit_assert( !str_is_ip6("213.154.224.12") );
|
|
|
|
|
unit_assert( !str_is_ip6("213.154.224.255") );
|
|
|
|
|
unit_assert( !str_is_ip6("255.255.255.0") );
|
2007-03-26 06:33:41 -04:00
|
|
|
unit_assert( is_pow2(0) );
|
|
|
|
|
unit_assert( is_pow2(1) );
|
|
|
|
|
unit_assert( is_pow2(2) );
|
|
|
|
|
unit_assert( is_pow2(4) );
|
|
|
|
|
unit_assert( is_pow2(8) );
|
|
|
|
|
unit_assert( is_pow2(16) );
|
|
|
|
|
unit_assert( is_pow2(1024) );
|
|
|
|
|
unit_assert( is_pow2(1024*1024) );
|
|
|
|
|
unit_assert( is_pow2(1024*1024*1024) );
|
|
|
|
|
unit_assert( !is_pow2(3) );
|
|
|
|
|
unit_assert( !is_pow2(5) );
|
|
|
|
|
unit_assert( !is_pow2(6) );
|
|
|
|
|
unit_assert( !is_pow2(7) );
|
|
|
|
|
unit_assert( !is_pow2(9) );
|
|
|
|
|
unit_assert( !is_pow2(10) );
|
|
|
|
|
unit_assert( !is_pow2(11) );
|
|
|
|
|
unit_assert( !is_pow2(17) );
|
|
|
|
|
unit_assert( !is_pow2(23) );
|
|
|
|
|
unit_assert( !is_pow2(257) );
|
|
|
|
|
unit_assert( !is_pow2(259) );
|
2007-02-05 09:57:16 -05:00
|
|
|
}
|
2007-01-03 05:42:32 -05:00
|
|
|
|
2007-05-14 09:22:02 -04:00
|
|
|
#include "util/rtt.h"
|
|
|
|
|
/** test RTT code */
|
|
|
|
|
static void
|
|
|
|
|
rtt_test()
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
struct rtt_info r;
|
|
|
|
|
rtt_init(&r);
|
|
|
|
|
/* initial value sensible */
|
|
|
|
|
unit_assert( rtt_timeout(&r) == 3000 );
|
|
|
|
|
rtt_lost(&r);
|
|
|
|
|
unit_assert( rtt_timeout(&r) == 6000 );
|
|
|
|
|
rtt_lost(&r);
|
|
|
|
|
unit_assert( rtt_timeout(&r) == 12000 );
|
|
|
|
|
rtt_update(&r, 4000);
|
|
|
|
|
unit_assert( rtt_timeout(&r) >= 5000 );
|
|
|
|
|
rtt_lost(&r);
|
|
|
|
|
for(i=0; i<100; i++) {
|
|
|
|
|
rtt_lost(&r);
|
|
|
|
|
unit_assert( rtt_timeout(&r) > RTT_MIN_TIMEOUT-1);
|
|
|
|
|
unit_assert( rtt_timeout(&r) < RTT_MAX_TIMEOUT+1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-15 09:21:10 -04:00
|
|
|
#include "services/cache/infra.h"
|
|
|
|
|
#include "util/config_file.h"
|
|
|
|
|
/** test host cache */
|
|
|
|
|
static void
|
|
|
|
|
infra_test()
|
|
|
|
|
{
|
|
|
|
|
int one = 1;
|
|
|
|
|
uint8_t* zone = (uint8_t*)"\007example\003com\000";
|
|
|
|
|
size_t zonelen = 13;
|
|
|
|
|
struct slabhash* slab;
|
|
|
|
|
struct config_file* cfg = config_create();
|
|
|
|
|
time_t now = 0;
|
|
|
|
|
int vs, to;
|
|
|
|
|
struct infra_host_key* k;
|
|
|
|
|
struct infra_host_data* d;
|
|
|
|
|
|
|
|
|
|
slab = infra_create(cfg);
|
|
|
|
|
unit_assert( infra_host(slab, (struct sockaddr_storage*)&one,
|
|
|
|
|
sizeof(int), now, &vs, &to) );
|
|
|
|
|
unit_assert( vs == 0 && to == 3000 );
|
|
|
|
|
|
|
|
|
|
unit_assert( infra_rtt_update(slab, (struct sockaddr_storage*)&one,
|
|
|
|
|
sizeof(int), -1, now) );
|
|
|
|
|
unit_assert( infra_host(slab, (struct sockaddr_storage*)&one,
|
|
|
|
|
sizeof(int), now, &vs, &to) );
|
|
|
|
|
unit_assert( vs == 0 && to == 6000 );
|
|
|
|
|
|
|
|
|
|
unit_assert( infra_edns_update(slab, (struct sockaddr_storage*)&one,
|
|
|
|
|
sizeof(int), -1, now) );
|
|
|
|
|
unit_assert( infra_host(slab, (struct sockaddr_storage*)&one,
|
|
|
|
|
sizeof(int), now, &vs, &to) );
|
|
|
|
|
unit_assert( vs == -1 && to == 6000 );
|
|
|
|
|
|
|
|
|
|
now += HOST_TTL + 10;
|
|
|
|
|
unit_assert( infra_host(slab, (struct sockaddr_storage*)&one,
|
|
|
|
|
sizeof(int), now, &vs, &to) );
|
|
|
|
|
unit_assert( vs == 0 && to == 3000 );
|
|
|
|
|
|
|
|
|
|
unit_assert( infra_set_lame(slab, (struct sockaddr_storage*)&one,
|
|
|
|
|
sizeof(int), zone, zonelen, now) );
|
|
|
|
|
unit_assert( (d=infra_lookup_host(slab, (struct sockaddr_storage*)&one,
|
|
|
|
|
sizeof(int), 0, now, &k)) );
|
|
|
|
|
unit_assert( d->ttl == now+HOST_TTL );
|
|
|
|
|
unit_assert( d->edns_version == 0 );
|
|
|
|
|
unit_assert( infra_lookup_lame(d, zone, zonelen, now) );
|
|
|
|
|
unit_assert( !infra_lookup_lame(d, zone, zonelen,
|
|
|
|
|
now+HOST_LAME_TTL+10) );
|
|
|
|
|
unit_assert( !infra_lookup_lame(d, (uint8_t*)"\000", 1, now) );
|
|
|
|
|
lock_rw_unlock(&k->entry.lock);
|
|
|
|
|
|
|
|
|
|
infra_delete(slab);
|
|
|
|
|
config_delete(cfg);
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-03 09:55:21 -05:00
|
|
|
/**
|
|
|
|
|
* Main unit test program. Setup, teardown and report errors.
|
|
|
|
|
* @param argc: arg count.
|
|
|
|
|
* @param argv: array of commandline arguments.
|
|
|
|
|
*/
|
2007-02-06 11:26:19 -05:00
|
|
|
int
|
|
|
|
|
main(int argc, char* argv[])
|
2007-01-03 05:42:32 -05:00
|
|
|
{
|
2007-02-23 08:38:54 -05:00
|
|
|
log_init(NULL);
|
2007-01-03 05:42:32 -05:00
|
|
|
if(argc != 1) {
|
|
|
|
|
printf("usage: %s\n", argv[0]);
|
|
|
|
|
printf("\tperforms unit tests.\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
printf("Start of %s unit test.\n", PACKAGE_STRING);
|
2007-03-14 10:30:30 -04:00
|
|
|
checklock_start();
|
2007-02-05 09:57:16 -05:00
|
|
|
net_test();
|
2007-05-02 05:35:19 -04:00
|
|
|
dname_test();
|
2007-05-14 09:22:02 -04:00
|
|
|
rtt_test();
|
2007-03-09 08:37:57 -05:00
|
|
|
alloc_test();
|
2007-03-14 10:30:30 -04:00
|
|
|
lruhash_test();
|
2007-03-23 11:17:11 -04:00
|
|
|
slabhash_test();
|
2007-05-15 09:21:10 -04:00
|
|
|
infra_test();
|
2007-04-17 10:03:33 -04:00
|
|
|
msgparse_test();
|
2007-03-14 10:30:30 -04:00
|
|
|
checklock_stop();
|
2007-04-25 07:36:51 -04:00
|
|
|
printf("%d checks ok.\n", testcount);
|
2007-01-03 05:42:32 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|