Empty buffer before writing, added packet dump function

This commit is contained in:
Erik Ekman 2007-01-28 13:32:41 +00:00
parent a78b85fbf2
commit 3e52c974bc

View File

@ -18,6 +18,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -29,6 +30,8 @@
#include "encoding.h" #include "encoding.h"
#include "test.h" #include "test.h"
static void dump_packet(char *, size_t);
static char queryPacket[] = static char queryPacket[] =
"\x05\x39\x01\x00\x00\x01\x00\x00\x00\x00\x00\x01\x32\x41\x4A\x42\x43" "\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" "\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); len = sizeof(buf);
memset(&buf, 0, sizeof(buf)); memset(&buf, 0, sizeof(buf));
memset(&resolv, 0, sizeof(resolv));
memset(&q, 0, sizeof(struct query)); memset(&q, 0, sizeof(struct query));
q.type = T_NULL; q.type = T_NULL;
q.id = 1337; q.id = 1337;
@ -133,6 +137,11 @@ START_TEST(test_encode_query)
ret = dns_encode(buf, len, &q, QR_QUERY, resolv, strlen(resolv)); ret = dns_encode(buf, len, &q, QR_QUERY, resolv, strlen(resolv));
len = sizeof(queryPacket) - 1; // Skip extra null character 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(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)); 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 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 * TCase *
test_dns_create_tests() test_dns_create_tests()
{ {