Fix bug#291 and unit test spruced up.

git-svn-id: https://unbound.nlnetlabs.nl/svn/trunk@1948 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
wouter 2010-01-06 10:55:14 +00:00
parent bdf600e08a
commit 6a7022a71d
11 changed files with 96 additions and 6 deletions

View File

@ -1,5 +1,6 @@
6 January 2010: Wouter
- iana portlist updated.
- bug#291: DNS wireformat max is 255. dname_valid allowed 256 length.
1 January 2010: Wouter
- iana portlist updated.

View File

@ -124,6 +124,7 @@ void anchors_test()
{
ldns_buffer* buff = ldns_buffer_new(65800);
struct val_anchors* a;
unit_show_feature("trust anchor store");
unit_assert(a = anchors_create());
ldns_buffer_flip(buff);
test_anchor_empty(a);

View File

@ -64,6 +64,7 @@ dname_to_buf(ldns_buffer* b, const char* str)
static void
dname_test_qdl(ldns_buffer* buff)
{
unit_show_func("util/data/dname.c", "query_dname_len");
unit_assert( query_dname_len(buff) == 0);
unit_assert( query_dname_len(dname_to_buf(buff, ".")) == 1 );
unit_assert( query_dname_len(dname_to_buf(buff, "bla.foo.")) == 9 );
@ -75,6 +76,7 @@ dname_test_qdl(ldns_buffer* buff)
static void
dname_test_qdtl(ldns_buffer* buff)
{
unit_show_func("util/data/dname.c", "query_dname_tolower");
ldns_buffer_write_at(buff, 0, "\012abCDeaBCde\003cOm\000", 16);
query_dname_tolower(ldns_buffer_begin(buff));
unit_assert( memcmp(ldns_buffer_begin(buff),
@ -98,6 +100,7 @@ dname_test_qdtl(ldns_buffer* buff)
static void
dname_test_query_dname_compare()
{
unit_show_func("util/data/dname.c", "query_dname_compare");
unit_assert(query_dname_compare((uint8_t*)"", (uint8_t*)"") == 0);
unit_assert(query_dname_compare((uint8_t*)"\001a",
(uint8_t*)"\001a") == 0);
@ -127,6 +130,7 @@ dname_test_query_dname_compare()
static void
dname_test_count_labels()
{
unit_show_func("util/data/dname.c", "dname_count_labels");
unit_assert(dname_count_labels((uint8_t*)"") == 1);
unit_assert(dname_count_labels((uint8_t*)"\003com") == 2);
unit_assert(dname_count_labels((uint8_t*)"\003org") == 2);
@ -140,6 +144,7 @@ static void
dname_test_count_size_labels()
{
size_t sz = 0;
unit_show_func("util/data/dname.c", "dname_count_size_labels");
unit_assert(dname_count_size_labels((uint8_t*)"", &sz) == 1);
unit_assert(sz == 1);
unit_assert(dname_count_size_labels((uint8_t*)"\003com", &sz) == 2);
@ -159,6 +164,7 @@ dname_test_count_size_labels()
static void
dname_test_pkt_dname_len(ldns_buffer* buff)
{
unit_show_func("util/data/dname.c", "pkt_dname_len");
ldns_buffer_clear(buff);
ldns_buffer_write(buff, "\000", 1);
ldns_buffer_flip(buff);
@ -265,6 +271,7 @@ static void
dname_test_dname_lab_cmp()
{
int ml = 0; /* number of labels that matched exactly */
unit_show_func("util/data/dname.c", "dname_lab_cmp");
/* test for equality succeeds */
unit_assert(dname_lab_cmp((uint8_t*)"", 1, (uint8_t*)"", 1, &ml) == 0);
@ -365,6 +372,7 @@ dname_test_dname_lab_cmp()
static void
dname_test_subdomain()
{
unit_show_func("util/data/dname.c", "dname_subdomain");
unit_assert(dname_subdomain_c(
(uint8_t*)"",
(uint8_t*)""));
@ -401,6 +409,7 @@ dname_test_subdomain()
static void
dname_test_strict_subdomain()
{
unit_show_func("util/data/dname.c", "dname_strict_subdomain");
unit_assert(!dname_strict_subdomain(
(uint8_t*)"", 1,
(uint8_t*)"", 1));
@ -437,6 +446,7 @@ dname_test_strict_subdomain()
static void
dname_test_isroot()
{
unit_show_func("util/data/dname.c", "dname_isroot");
unit_assert(dname_is_root((uint8_t*)"\000"));
unit_assert(!dname_is_root((uint8_t*)"\001a\000"));
unit_assert(!dname_is_root((uint8_t*)"\005abvcd\003com\000"));
@ -452,6 +462,7 @@ dname_test_removelabel()
uint8_t* orig = (uint8_t*)"\007example\003com\000";
uint8_t* n = orig;
size_t l = 13;
unit_show_func("util/data/dname.c", "dname_remove_label");
dname_remove_label(&n, &l);
unit_assert( n == orig+8 );
unit_assert( l == 5 );
@ -467,6 +478,7 @@ dname_test_removelabel()
static void
dname_test_sigcount()
{
unit_show_func("util/data/dname.c", "dname_signame_label_count");
unit_assert(dname_signame_label_count((uint8_t*)"\000") == 0);
unit_assert(dname_signame_label_count((uint8_t*)"\001*\000") == 0);
unit_assert(dname_signame_label_count((uint8_t*)"\003xom\000") == 1);
@ -486,6 +498,7 @@ dname_test_sigcount()
static void
dname_test_iswild()
{
unit_show_func("util/data/dname.c", "dname_iswild");
unit_assert( !dname_is_wild((uint8_t*)"\000") );
unit_assert( dname_is_wild((uint8_t*)"\001*\000") );
unit_assert( !dname_is_wild((uint8_t*)"\003net\000") );
@ -496,6 +509,7 @@ dname_test_iswild()
static void
dname_test_canoncmp()
{
unit_show_func("util/data/dname.c", "dname_canonical_compare");
/* equality */
unit_assert( dname_canonical_compare(
(uint8_t*)"\000",
@ -715,6 +729,7 @@ dname_test_canoncmp()
static void
dname_test_topdomain()
{
unit_show_func("util/data/dname.c", "dname_get_shared_topdomain");
unit_assert( query_dname_compare(
dname_get_shared_topdomain(
(uint8_t*)"",
@ -732,6 +747,52 @@ dname_test_topdomain()
(uint8_t*)"\007example\003com") == 0);
}
/** Test dname_valid */
static void
dname_test_valid()
{
unit_show_func("util/data/dname.c", "dname_valid");
unit_assert( dname_valid(
(uint8_t*)"\003www\007example\003com", 255) == 17);
unit_assert( dname_valid((uint8_t*)"", 255) == 1);
unit_assert( dname_valid( (uint8_t*)
"\020a1cdef5555544444"
"\020a2cdef5555544444"
"\020a3cdef5555544444"
"\020a4cdef5555544444"
"\020a5cdef5555544444"
"\020a6cdef5555544444"
"\020a7cdef5555544444"
"\020a8cdef5555544444"
"\020a9cdef5555544444"
"\020aAcdef5555544444"
"\020aBcdef5555544444"
"\020aCcdef5555544444"
"\020aDcdef5555544444"
"\020aEcdef5555544444" /* 238 up to here */
"\007aabbccd" /* 246 up to here */
"\007example\000" /* 255 to here */
, 255) == 255);
unit_assert( dname_valid( (uint8_t*)
"\020a1cdef5555544444"
"\020a2cdef5555544444"
"\020a3cdef5555544444"
"\020a4cdef5555544444"
"\020a5cdef5555544444"
"\020a6cdef5555544444"
"\020a7cdef5555544444"
"\020a8cdef5555544444"
"\020a9cdef5555544444"
"\020aAcdef5555544444"
"\020aBcdef5555544444"
"\020aCcdef5555544444"
"\020aDcdef5555544444"
"\020aEcdef5555544444" /* 238 up to here */
"\007aabbccd" /* 246 up to here */
"\010exampleX\000" /* 256 to here */
, 4096) == 0);
}
void dname_test()
{
ldns_buffer* buff = ldns_buffer_new(65800);
@ -751,5 +812,6 @@ void dname_test()
dname_test_iswild();
dname_test_canoncmp();
dname_test_topdomain();
dname_test_valid();
ldns_buffer_free(buff);
}

View File

@ -482,7 +482,7 @@ void lruhash_test()
/* start very very small array, so it can do lots of table_grow() */
/* also small in size so that reclaim has to be done quickly. */
struct lruhash* table ;
printf("lruhash test\n");
unit_show_feature("lruhash");
table = lruhash_create(2, 8192,
test_slabhash_sizefunc, test_slabhash_compfunc,
test_slabhash_delkey, test_slabhash_deldata, NULL);

View File

@ -54,6 +54,7 @@ alloc_test() {
struct alloc_cache major, minor1, minor2;
int i;
unit_show_feature("alloc_special_obtain");
alloc_init(&major, NULL, 0);
alloc_init(&minor1, &major, 0);
alloc_init(&minor2, &major, 1);
@ -125,6 +126,7 @@ net_test()
"\377\377\377\377",
"\377\377\377\377",
};
unit_show_func("util/net_help.c", "str_is_ip6");
unit_assert( str_is_ip6("::") );
unit_assert( str_is_ip6("::1") );
unit_assert( str_is_ip6("2001:7b8:206:1:240:f4ff:fe37:8810") );
@ -133,6 +135,7 @@ net_test()
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") );
unit_show_func("util/net_help.c", "is_pow2");
unit_assert( is_pow2(0) );
unit_assert( is_pow2(1) );
unit_assert( is_pow2(2) );
@ -155,6 +158,7 @@ net_test()
unit_assert( !is_pow2(259) );
/* test addr_mask */
unit_show_func("util/net_help.c", "addr_mask");
if(1) {
struct sockaddr_in a4;
struct sockaddr_in6 a6;
@ -183,6 +187,7 @@ net_test()
}
/* test addr_in_common */
unit_show_func("util/net_help.c", "addr_in_common");
if(1) {
struct sockaddr_in a4, b4;
struct sockaddr_in6 a6, b6;
@ -239,6 +244,7 @@ net_test()
}
}
/* test addr_is_ip4mapped */
unit_show_func("util/net_help.c", "addr_is_ip4mapped");
if(1) {
struct sockaddr_storage a;
socklen_t l = (socklen_t)sizeof(a);
@ -269,6 +275,7 @@ static void
config_memsize_test()
{
size_t v = 0;
unit_show_func("util/config_file.c", "cfg_parse_memsize");
if(0) {
/* these emit errors */
unit_assert( cfg_parse_memsize("", &v) == 0);
@ -309,6 +316,7 @@ rtt_test()
int init = 376;
int i;
struct rtt_info r;
unit_show_func("util/rtt.c", "rtt_timeout");
rtt_init(&r);
/* initial value sensible */
unit_assert( rtt_timeout(&r) == init );
@ -346,6 +354,7 @@ infra_test()
int init = 376;
int dlame, rlame, alame, olame;
unit_show_feature("infra cache");
unit_assert(ipstrtoaddr("127.0.0.1", 53, &one, &onelen));
slab = infra_create(cfg);
@ -403,6 +412,7 @@ rnd_test()
struct ub_randstate* r;
int num = 100, i;
long int a[100];
unit_show_feature("ub_random");
unit_assert( (r = ub_initstate((unsigned)time(NULL), NULL)) );
for(i=0; i<num; i++) {
a[i] = ub_random(r);
@ -416,6 +426,16 @@ rnd_test()
ub_randfree(r);
}
void unit_show_func(const char* file, const char* func)
{
printf("test %s:%s\n", file, func);
}
void unit_show_feature(const char* feature)
{
printf("test %s functions\n", feature);
}
/**
* Main unit test program. Setup, teardown and report errors.
* @param argc: arg count.
@ -445,8 +465,8 @@ main(int argc, char* argv[])
net_test();
config_memsize_test();
dname_test();
anchors_test();
rtt_test();
anchors_test();
alloc_test();
lruhash_test();
slabhash_test();

View File

@ -51,6 +51,11 @@ extern int testcount;
#define unit_assert(x) do {testcount++; if(!(x)) { fprintf(stderr, "assertion failure %s:%d\n", __FILE__, __LINE__); exit(1);}} while(0)
#endif
/** we are now testing this function */
void unit_show_func(const char* file, const char* func);
/** we are testing this functionality */
void unit_show_feature(const char* feature);
/** unit test lruhashtable implementation */
void lruhash_test();
/** unit test slabhashtable implementation */

View File

@ -506,7 +506,7 @@ void msgparse_test()
alloc_init(&super_a, NULL, 0);
alloc_init(&alloc, &super_a, 2);
printf("testmsgparse\n");
unit_show_feature("message parse");
simpletest(pkt, &alloc, out);
/* plain hex dumps, like pcat */
testfromfile(pkt, &alloc, out, "testdata/test_packets.1");

View File

@ -530,6 +530,7 @@ void neg_test()
{
struct val_neg_cache* neg;
srandom(48);
unit_show_feature("negative cache");
/* create with defaults */
neg = val_neg_create(NULL, 1500);

View File

@ -361,7 +361,7 @@ void slabhash_test()
/* start very very small array, so it can do lots of table_grow() */
/* also small in size so that reclaim has to be done quickly. */
struct slabhash* table;
printf("slabhash test\n");
unit_show_feature("slabhash");
table = slabhash_create(4, 2, 10400,
test_slabhash_sizefunc, test_slabhash_compfunc,
test_slabhash_delkey, test_slabhash_deldata, NULL);

View File

@ -467,7 +467,7 @@ nsec3_hash_test(const char* fname)
void
verify_test()
{
printf("verify test\n");
unit_show_feature("signature verify");
verifytest_file("testdata/test_signatures.1", "20070818005004");
verifytest_file("testdata/test_signatures.2", "20080414005004");
verifytest_file("testdata/test_signatures.3", "20080416005004");

View File

@ -78,7 +78,7 @@ dname_valid(uint8_t* dname, size_t maxlen)
if(labellen&0xc0)
return 0; /* no compression ptrs allowed */
len += labellen + 1;
if(len > LDNS_MAX_DOMAINLEN)
if(len >= LDNS_MAX_DOMAINLEN)
return 0; /* too long */
if(len > maxlen)
return 0; /* does not fit in memory allocation */