Rework query testcase, some dns cleanup

This commit is contained in:
Erik Ekman 2007-01-28 11:52:36 +00:00
parent 89c99f1ddf
commit 1a46808b8a
5 changed files with 61 additions and 13 deletions

View file

@ -62,7 +62,7 @@ static uint16_t pingid;
int int
open_dns(const char *domain, int localport, in_addr_t listen_ip) open_dns(int localport, in_addr_t listen_ip)
{ {
int fd; int fd;
int flag; int flag;
@ -91,15 +91,19 @@ open_dns(const char *domain, int localport, in_addr_t listen_ip)
return -1; return -1;
} }
// Save top domain used
strncpy(topdomain, domain, sizeof(topdomain) - 1);
topdomain[sizeof(topdomain) - 1] = '\0';
printf("Opened UDP socket\n"); printf("Opened UDP socket\n");
return fd; return fd;
} }
void
dns_set_topdomain(const char *domain)
{
strncpy(topdomain, domain, sizeof(topdomain) - 1);
topdomain[sizeof(topdomain) - 1] = '\0';
}
int int
dns_settarget(const char *host) dns_settarget(const char *host)
{ {

View file

@ -17,8 +17,9 @@
#ifndef _DNS_H_ #ifndef _DNS_H_
#define _DNS_H_ #define _DNS_H_
int open_dns(const char *, int, in_addr_t); int open_dns(int, in_addr_t);
int dns_settarget(const char*); int dns_settarget(const char*);
void dns_set_topdomain(const char*);
void close_dns(int); void close_dns(int);
int dns_sending(); int dns_sending();

View file

@ -327,7 +327,8 @@ main(int argc, char **argv)
if ((tun_fd = open_tun(device)) == -1) if ((tun_fd = open_tun(device)) == -1)
goto cleanup1; goto cleanup1;
if ((dns_fd = open_dns(argv[1], 0, INADDR_ANY)) == -1) dns_set_topdomain(argv[1]);
if ((dns_fd = open_dns(0, INADDR_ANY)) == -1)
goto cleanup2; goto cleanup2;
if (dns_settarget(argv[0]) == -1) if (dns_settarget(argv[0]) == -1)
goto cleanup2; goto cleanup2;

View file

@ -375,7 +375,8 @@ main(int argc, char **argv)
goto cleanup0; goto cleanup0;
if (tun_setip(argv[0]) != 0 || tun_setmtu(mtu) != 0) if (tun_setip(argv[0]) != 0 || tun_setmtu(mtu) != 0)
goto cleanup1; goto cleanup1;
if ((dnsd_fd = open_dns(argv[1], port, listen_ip)) == -1) dns_set_topdomain(argv[1]);
if ((dnsd_fd = open_dns(port, listen_ip)) == -1)
goto cleanup2; goto cleanup2;
my_ip = inet_addr(argv[0]); my_ip = inet_addr(argv[0]);

View file

@ -26,13 +26,16 @@
#include "structs.h" #include "structs.h"
#include "dns.h" #include "dns.h"
#include "encoding.h"
#include "test.h" #include "test.h"
static char queryPacket[] = static char queryPacket[] =
"\x05\x39\x01\x00\x00\x01\x00\x00\x00\x00\x00\x01\x05\x73\x69\x6C\x6C" "\x05\x39\x01\x00\x00\x01\x00\x00\x00\x00\x00\x01\x32\x41\x4A\x42\x43"
"\x79\x04\x68\x6F\x73\x74\x02\x6F\x66\x06\x69\x6F\x64\x69\x6E\x65\x04" "\x55\x59\x54\x43\x50\x45\x42\x39\x47\x51\x39\x4C\x54\x45\x42\x55\x58"
"\x63\x6F\x64\x65\x04\x6B\x72\x79\x6F\x02\x73\x65\x00\x00\x0A\x00\x01" "\x47\x49\x44\x55\x4E\x42\x53\x53\x41\x36\x44\x46\x4F\x4E\x39\x43\x41"
"\x00\x00\x29\x10\x00\x00\x00\x80\x00\x00\x00"; "\x5A\x44\x42\x32\x41\x41\x41\x41\x41\x36\x44\x42\x04\x6B\x72\x79\x6F"
"\x02\x73\x65\x00\x00\x0A\x00\x01\x00\x00\x29\x10\x00\x00\x00\x80\x00"
"\x00\x00";
static char answerPacket[] = static char answerPacket[] =
"\x05\x39\x84\x00\x00\x01\x00\x01\x00\x00\x00\x00\x05\x73\x69\x6C\x6C" "\x05\x39\x84\x00\x00\x01\x00\x01\x00\x00\x00\x00\x05\x73\x69\x6C\x6C"
@ -43,6 +46,9 @@ static char answerPacket[] =
"\x20\x62\x65\x20\x64\x65\x6C\x69\x76\x65\x72\x65\x64"; "\x20\x62\x65\x20\x64\x65\x6C\x69\x76\x65\x72\x65\x64";
static char *msgData = "this is the message to be delivered"; static char *msgData = "this is the message to be delivered";
static char *queryData = "HELLO this is the test data";
static char *recData = "AHELLO this is the test data"; // The A flag is added
START_TEST(test_encode_hostname) START_TEST(test_encode_hostname)
{ {
@ -102,17 +108,29 @@ END_TEST
START_TEST(test_encode_query) START_TEST(test_encode_query)
{ {
char buf[512]; char buf[512];
char *host = "silly.host.of.iodine.code.kryo.se"; char resolv[512];
char *host = "kryo.se";
struct query q; struct query q;
char *d;
int len; int len;
int pos;
int ret; int ret;
len = sizeof(buf); len = sizeof(buf);
memset(&buf, 0, sizeof(buf));
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;
d = resolv;
ret = dns_encode(buf, len, &q, QR_QUERY, host, strlen(host)); encode_data(queryData, strlen(queryData), 100, d, 'A');
pos = strlen(resolv);
d += pos;
if (*d != '.') {
*d++ = '.';
}
strncpy(d, host, strlen(host)+1);
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
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");
@ -120,6 +138,26 @@ START_TEST(test_encode_query)
} }
END_TEST END_TEST
START_TEST(test_decode_query)
{
char buf[512];
struct query q;
int len;
int ret;
memset(&q, 0, sizeof(struct query));
memset(&buf, 0, sizeof(buf));
q.id = 0;
len = sizeof(queryPacket) - 1;
dns_set_topdomain("kryo.se");
ret = dns_decode(buf, sizeof(buf), &q, QR_QUERY, queryPacket, len);
fail_unless(strncmp(buf, recData, ret) == 0, "Did not extract expected host: '%s'", buf);
fail_unless(strlen(buf) == strlen(recData), va_str("Bad host length: %d, expected %d", strlen(q.name), strlen(recData)));
}
END_TEST
START_TEST(test_encode_response) START_TEST(test_encode_response)
{ {
char buf[512]; char buf[512];
@ -129,6 +167,7 @@ START_TEST(test_encode_response)
int ret; int ret;
len = sizeof(buf); len = sizeof(buf);
memset(&buf, 0, sizeof(buf));
memset(&q, 0, sizeof(struct query)); memset(&q, 0, sizeof(struct query));
strncpy(q.name, host, strlen(host)); strncpy(q.name, host, strlen(host));
q.type = T_NULL; q.type = T_NULL;
@ -149,6 +188,7 @@ START_TEST(test_decode_response)
int ret; int ret;
len = sizeof(buf); len = sizeof(buf);
memset(&buf, 0, sizeof(buf));
ret = dns_decode(buf, len, NULL, QR_ANSWER, answerPacket, sizeof(answerPacket)-1); ret = dns_decode(buf, len, NULL, QR_ANSWER, answerPacket, sizeof(answerPacket)-1);
fail_unless(strncmp(msgData, buf, sizeof(msgData)) == 0, "Did not extract expected data"); fail_unless(strncmp(msgData, buf, sizeof(msgData)) == 0, "Did not extract expected data");
@ -166,6 +206,7 @@ test_dns_create_tests()
tcase_add_test(tc, test_encode_hostname_nodot); tcase_add_test(tc, test_encode_hostname_nodot);
tcase_add_test(tc, test_encode_hostname_toolong); tcase_add_test(tc, test_encode_hostname_toolong);
tcase_add_test(tc, test_encode_query); tcase_add_test(tc, test_encode_query);
tcase_add_test(tc, test_decode_query);
tcase_add_test(tc, test_encode_response); tcase_add_test(tc, test_encode_response);
tcase_add_test(tc, test_decode_response); tcase_add_test(tc, test_decode_response);