Unit test for parser with lots fo content.

git-svn-id: file:///svn/unbound/trunk@247 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
Wouter Wijngaards 2007-04-17 15:18:24 +00:00
parent 1cb321662d
commit 3507ec2e64
4 changed files with 400063 additions and 13 deletions

View file

@ -7,6 +7,10 @@
* forgot rrset_count addition.
* did & of ptr on stack for memory position calculation.
* dname_pkt_copy forgot to read next label length.
- test from file and fixes
* double frees fixed in error conditions.
* types with less than full rdata allowed by parser.
Some dynamic update packets seem to use it.
16 April 2007: Wouter
- following a small change in LDNS, parsing code calculates the

View file

@ -187,6 +187,22 @@ test_buffers(ldns_buffer* pkt, ldns_buffer* out)
return 0;
}
/** check if unbound formerr equals ldns formerr. */
static void
checkformerr(ldns_buffer* pkt)
{
ldns_pkt* p;
ldns_status status = ldns_buffer2pkt_wire(&p, pkt);
if(0) printf("formerr, ldns parse is: %s\n",
ldns_get_errorstr_by_id(status));
if(status == LDNS_STATUS_OK) {
printf("Formerr, but ldns gives packet:\n");
ldns_pkt_print(stdout, p);
exit(1);
}
unit_assert(status != LDNS_STATUS_OK);
}
/** test a packet */
static void
testpkt(ldns_buffer* pkt, struct alloc_cache* alloc, ldns_buffer* out,
@ -208,16 +224,18 @@ testpkt(ldns_buffer* pkt, struct alloc_cache* alloc, ldns_buffer* out,
memmove(&flags, ldns_buffer_at(pkt, 2), sizeof(flags));
ret = reply_info_parse(pkt, alloc, &qi, &rep);
if(ret != 0) {
printf("exit code %d: %s", ret,
if(0) printf("parse code %d: %s\n", ret,
ldns_lookup_by_id(ldns_rcodes, ret)->name);
unit_assert(ret == 0);
}
sz = reply_info_iov_regen(&qi, rep, id, flags, iov, maxiov,
timenow, region);
unit_assert(sz != 0); /* udp packets should fit in 1024 iov */
write_iov_buffer(out, iov, sz);
test_buffers(pkt, out);
if(ret == LDNS_RCODE_FORMERR)
checkformerr(pkt);
unit_assert(ret != LDNS_RCODE_SERVFAIL);
} else {
sz = reply_info_iov_regen(&qi, rep, id, flags, iov, maxiov,
timenow, region);
unit_assert(sz != 0); /* udp packets should fit in 1024 iov */
write_iov_buffer(out, iov, sz);
test_buffers(pkt, out);
}
query_info_clear(&qi);
reply_info_parsedelete(rep, alloc);
@ -263,6 +281,30 @@ simpletest(ldns_buffer* pkt, struct alloc_cache* alloc, ldns_buffer* out)
" 64 b9 00 04 c0 3a 80 1e ");
}
/** simple test of parsing. */
static void
testfromfile(ldns_buffer* pkt, struct alloc_cache* alloc, ldns_buffer* out,
const char* fname)
{
FILE* in = fopen(fname, "r");
char buf[102400];
int no=0;
if(!in) {
perror("fname");
return;
}
while(fgets(buf, sizeof(buf), in)) {
if(buf[0] == ';') /* comment */
continue;
if(strlen(buf) < 10) /* skip pcat line numbers. */
continue;
if(0) printf("test no %d\n", no);
testpkt(pkt, alloc, out, buf);
no++;
}
fclose(in);
}
void msgparse_test()
{
ldns_buffer* pkt = ldns_buffer_new(65553);
@ -272,7 +314,9 @@ void msgparse_test()
alloc_init(&super_a, NULL, 0);
alloc_init(&alloc, &super_a, 2);
printf("testmsgparse\n");
simpletest(pkt, &alloc, out);
testfromfile(pkt, &alloc, out, "testdata/test_packets.1");
/* cleanup */
alloc_clear(&alloc);

400000
testdata/test_packets.1 vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -171,13 +171,13 @@ calc_size(ldns_buffer* pkt, uint16_t type, struct rr_parse* rr)
if(ldns_buffer_remaining(pkt) < pkt_len)
return 0;
desc = ldns_rr_descript(type);
if(desc->_dname_count > 0) {
if(pkt_len > 0 && desc->_dname_count > 0) {
int count = (int)desc->_dname_count;
int rdf = 0;
size_t len;
size_t oldpos;
/* skip first part. */
while(count) {
while(pkt_len > 0 && count) {
switch(desc->_wireformat[rdf]) {
case LDNS_RDF_TYPE_DNAME:
/* decompress every domain name */
@ -256,13 +256,13 @@ rdata_copy(ldns_buffer* pkt, struct rrset_parse* pset,
return 0;
log_assert((size_t)pkt_len+2 <= rr->size);
desc = ldns_rr_descript(ntohs(pset->type));
if(desc->_dname_count > 0) {
if(pkt_len > 0 && desc->_dname_count > 0) {
int count = (int)desc->_dname_count;
int rdf = 0;
size_t len;
size_t oldpos;
/* decompress dnames. */
while(count) {
while(pkt_len > 0 && count) {
switch(desc->_wireformat[rdf]) {
case LDNS_RDF_TYPE_DNAME:
oldpos = ldns_buffer_position(pkt);
@ -414,6 +414,7 @@ int reply_info_parse(ldns_buffer* pkt, struct alloc_cache* alloc,
struct msg_parse* msg;
int ret;
qinf->qname = NULL;
*rep = NULL;
msg = region_alloc(region, sizeof(*msg));
if(!msg)
@ -433,6 +434,7 @@ int reply_info_parse(ldns_buffer* pkt, struct alloc_cache* alloc,
if((ret = parse_create_msg(pkt, msg, alloc, qinf, rep)) != 0) {
query_info_clear(qinf);
reply_info_parsedelete(*rep, alloc);
*rep = NULL;
region_free_all(region);
region_destroy(region);
return ret;