From d1ffedae43df45d35f358d4fb4e51e6f55dfa16f Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Sun, 28 Jan 2007 05:37:13 +0000 Subject: [PATCH] Added test on dns_encode --- src/dns.c | 3 --- src/dns.h | 3 +++ tests/dns.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/dns.c b/src/dns.c index c5e4974..44f0147 100644 --- a/src/dns.c +++ b/src/dns.c @@ -38,9 +38,6 @@ #include "encoding.h" #include "read.h" -#define QR_QUERY 0 -#define QR_ANSWER 1 - // For FreeBSD #ifndef MIN #define MIN(a,b) ((a)<(b)?(a):(b)) diff --git a/src/dns.h b/src/dns.h index 756d7c8..8e97320 100644 --- a/src/dns.h +++ b/src/dns.h @@ -28,6 +28,7 @@ void dns_send_version(int, int); void dns_login(int, char *, int); int dns_read(int, char *, int); int dns_encode_hostname(const char *, char *, int); +int dns_encode(char *, size_t, struct query *, int, char *, size_t); extern struct sockaddr_in peer; @@ -41,5 +42,7 @@ void dnsd_queuepacket(const char *, const int); int dns_parse_reply(char *, int, char *, int); +#define QR_QUERY 0 +#define QR_ANSWER 1 #endif /* _DNS_H_ */ diff --git a/tests/dns.c b/tests/dns.c index 1a0cc57..5ba7636 100644 --- a/tests/dns.c +++ b/tests/dns.c @@ -83,6 +83,33 @@ START_TEST(test_encode_hostname_toolong) } END_TEST +char queryPacket[] = + "\x05\x39\x01\x00\x00\x01\x00\x00\x00\x00\x00\x01\x05\x73\x69\x6C\x6C" + "\x79\x04\x68\x6F\x73\x74\x02\x6F\x66\x06\x69\x6F\x64\x69\x6E\x65\x04" + "\x63\x6F\x64\x65\x04\x6B\x72\x79\x6F\x02\x73\x65\x00\x00\x0A\x00\x01" + "\x00\x00\x29\x10\x00\x00\x00\x80\x00\x00\x00"; + +START_TEST(test_encode_query) +{ + char buf[512]; + char *host = "silly.host.of.iodine.code.kryo.se"; + struct query q; + int len; + int ret; + + len = sizeof(buf); + memset(&q, 0, sizeof(struct query)); + q.type = T_NULL; + q.id = 1337; + + ret = dns_encode(buf, len, &q, QR_QUERY, host, strlen(host)); + len = sizeof(queryPacket) - 1; // Skip extra null character + + 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)); +} +END_TEST + TCase * test_dns_create_tests() { @@ -92,6 +119,7 @@ test_dns_create_tests() tcase_add_test(tc, test_encode_hostname); tcase_add_test(tc, test_encode_hostname_nodot); tcase_add_test(tc, test_encode_hostname_toolong); + tcase_add_test(tc, test_encode_query); return tc; }