From 3e52c974bc09db78dca5ecfd6511566bb06200ee Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Sun, 28 Jan 2007 13:32:41 +0000 Subject: [PATCH] Empty buffer before writing, added packet dump function --- tests/dns.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/dns.c b/tests/dns.c index 7242792..9596e85 100644 --- a/tests/dns.c +++ b/tests/dns.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -29,6 +30,8 @@ #include "encoding.h" #include "test.h" +static void dump_packet(char *, size_t); + static char queryPacket[] = "\x05\x39\x01\x00\x00\x01\x00\x00\x00\x00\x00\x01\x32\x41\x4A\x42\x43" "\x55\x59\x54\x43\x50\x45\x42\x39\x47\x51\x39\x4C\x54\x45\x42\x55\x58" @@ -118,6 +121,7 @@ START_TEST(test_encode_query) len = sizeof(buf); memset(&buf, 0, sizeof(buf)); + memset(&resolv, 0, sizeof(resolv)); memset(&q, 0, sizeof(struct query)); q.type = T_NULL; q.id = 1337; @@ -133,6 +137,11 @@ START_TEST(test_encode_query) ret = dns_encode(buf, len, &q, QR_QUERY, resolv, strlen(resolv)); len = sizeof(queryPacket) - 1; // Skip extra null character + if (strncmp(queryPacket, buf, sizeof(queryPacket)) || ret != len) { + printf("\n"); + dump_packet(queryPacket, len); + dump_packet(buf, ret); + } fail_unless(strncmp(queryPacket, buf, sizeof(queryPacket)) == 0, "Did not compile expected packet"); fail_unless(ret == len, va_str("Bad packet length: %d, expected %d", ret, len)); } @@ -196,6 +205,25 @@ START_TEST(test_decode_response) } END_TEST +static void +dump_packet(char *buf, size_t len) +{ + int pos; + + for (pos = 0; pos < len; pos++) { + printf("\\x%02X", (unsigned char) buf[pos]); + } + printf("\n"); + for (pos = 0; pos < len; pos++) { + if (isalnum((unsigned char) buf[pos])) { + printf(" %c ", (unsigned char) buf[pos]); + } else { + printf(" "); + } + } + printf("\n"); +} + TCase * test_dns_create_tests() {