Fixed tests after #6 api changes

This commit is contained in:
Erik Ekman 2007-06-09 16:57:33 +00:00
parent f099a77743
commit 019fb51ee6
4 changed files with 74 additions and 44 deletions

View file

@ -20,14 +20,16 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "encoding.h"
#include "base32.h" #include "base32.h"
#include "test.h" #include "test.h"
struct touple struct tuple
{ {
char *a; char *a;
char *b; char *b;
} testpairs[] = { } testpairs[] = {
{ "iodinetestingtesting", "nfxwi0lomv0gk21unfxgo3dfon0gs1th" },
{ "abc123", "mfrggmjsgm" }, { "abc123", "mfrggmjsgm" },
{ NULL, NULL } { NULL, NULL }
}; };
@ -35,46 +37,39 @@ struct touple
START_TEST(test_base32_encode) START_TEST(test_base32_encode)
{ {
size_t len; size_t len;
char *buf; char buf[4096];
int val; int val;
int i; int i;
len = 0; len = sizeof(buf);
buf = NULL;
for (i = 0; testpairs[i].a != NULL; i++) { for (i = 0; testpairs[i].a != NULL; i++) {
val = base32_encode(&buf, &len, testpairs[i].a, strlen(testpairs[i].a)); val = base32_encode(buf, &len, testpairs[i].a, strlen(testpairs[i].a));
fail_unless(val > 0, strerror(errno)); fail_unless(val > 0, strerror(errno));
fail_unless(buf != NULL, "buf == NULL");
fail_unless(strcmp(buf, testpairs[i].b) == 0, fail_unless(strcmp(buf, testpairs[i].b) == 0,
va_str("'%s' != '%s'", buf, testpairs[i].b)); va_str("'%s' != '%s'", buf, testpairs[i].b));
} }
free(buf);
} }
END_TEST END_TEST
START_TEST(test_base32_decode) START_TEST(test_base32_decode)
{ {
size_t len; size_t len;
void *buf; char buf[4096];
int val; int val;
int i; int i;
len = 0; len = sizeof(buf);
buf = NULL;
for (i = 0; testpairs[i].a != NULL; i++) { for (i = 0; testpairs[i].a != NULL; i++) {
val = base32_decode(&buf, &len, testpairs[i].b); val = base32_decode(buf, &len, testpairs[i].b, strlen(testpairs[i].b));
fail_unless(val > 0, strerror(errno)); fail_unless(val > 0, strerror(errno));
fail_unless(buf != NULL, "buf == NULL"); fail_unless(buf != NULL, "buf == NULL");
fail_unless(strcmp(buf, testpairs[i].a) == 0, fail_unless(strcmp(buf, testpairs[i].a) == 0,
va_str("'%s' != '%s'", buf, testpairs[i].a)); va_str("'%s' != '%s'", buf, testpairs[i].a));
} }
free(buf);
} }
END_TEST END_TEST

View file

@ -28,17 +28,17 @@
#include "common.h" #include "common.h"
#include "dns.h" #include "dns.h"
#include "encoding.h" #include "encoding.h"
#include "base32.h"
#include "test.h" #include "test.h"
static void dump_packet(char *, size_t); 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\x2D\x41\x6A\x62\x63"
"\x55\x59\x54\x43\x50\x45\x42\x39\x47\x51\x39\x4C\x54\x45\x42\x55\x58" "\x75\x79\x74\x63\x70\x65\x62\x30\x67\x71\x30\x6C\x74\x65\x62\x75\x78"
"\x47\x49\x44\x55\x4E\x42\x53\x53\x41\x36\x44\x46\x4F\x4E\x39\x43\x41" "\x67\x69\x64\x75\x6E\x62\x73\x73\x61\x33\x64\x66\x6F\x6E\x30\x63\x61"
"\x5A\x44\x42\x32\x41\x41\x41\x41\x41\x36\x44\x42\x04\x6B\x72\x79\x6F" "\x7A\x64\x62\x6F\x72\x71\x71\x04\x6B\x72\x79\x6F\x02\x73\x65\x00\x00"
"\x02\x73\x65\x00\x00\x0A\x00\x01\x00\x00\x29\x10\x00\x00\x00\x80\x00" "\x0A\x00\x01\x00\x00\x29\x10\x00\x00\x00\x80\x00\x00\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"
@ -51,16 +51,16 @@ static char answerPacket[] =
static char *msgData = "this is the message to be delivered"; static char *msgData = "this is the message to be delivered";
static char *topdomain = "kryo.se"; static char *topdomain = "kryo.se";
static char *queryData = "HELLO this is the test data"; static char *innerData = "HELLO this is the test data";
static char *recData = "AHELLO this is the test data"; /* The A flag is added */
START_TEST(test_encode_query) START_TEST(test_encode_query)
{ {
char buf[512]; char buf[512];
char resolv[512]; char resolv[512];
struct query q; struct query q;
struct encoder *enc;
char *d; char *d;
int len; size_t len;
int ret; int ret;
len = sizeof(buf); len = sizeof(buf);
@ -70,9 +70,10 @@ START_TEST(test_encode_query)
q.type = T_NULL; q.type = T_NULL;
q.id = 1337; q.id = 1337;
d = resolv; d = resolv;
enc = get_base32_encoder();
*d++ = 'A'; *d++ = 'A';
encode_data(queryData, strlen(queryData), 100, d); enc->encode(d, &len, innerData, strlen(innerData));
d = resolv + strlen(resolv); d = resolv + strlen(resolv);
if (*d != '.') { if (*d != '.') {
*d++ = '.'; *d++ = '.';
@ -96,20 +97,22 @@ START_TEST(test_decode_query)
char buf[512]; char buf[512];
char *domain; char *domain;
struct query q; struct query q;
int len; struct encoder *enc;
int ret; size_t len;
memset(&q, 0, sizeof(struct query)); memset(&q, 0, sizeof(struct query));
memset(&buf, 0, sizeof(buf)); memset(&buf, 0, sizeof(buf));
q.id = 0; q.id = 0;
len = sizeof(queryPacket) - 1; len = sizeof(queryPacket) - 1;
enc = get_base32_encoder();
dns_decode(buf, sizeof(buf), &q, QR_QUERY, queryPacket, len); dns_decode(buf, sizeof(buf), &q, QR_QUERY, queryPacket, len);
domain = strstr(q.name, topdomain); domain = strstr(q.name, topdomain);
ret = decode_data(buf, sizeof(buf), q.name, domain); len = sizeof(buf);
unpack_data(buf, len, &(q.name[1]), (int) (domain - q.name) - 1, enc);
fail_unless(strncmp(buf, recData, ret) == 0, "Did not extract expected host: '%s'", buf); fail_unless(strncmp(buf, innerData, strlen(innerData)) == 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))); fail_unless(strlen(buf) == strlen(innerData), va_str("Bad host length: %d, expected %d: '%s'", strlen(buf), strlen(innerData), buf));
} }
END_TEST END_TEST

View file

@ -22,24 +22,54 @@
#include "encoding.h" #include "encoding.h"
#include "test.h" #include "test.h"
START_TEST(test_encoding_base32) struct tuple
{ {
char temp[256]; char *a;
char *start = "HELLOTEST"; char *b;
char *out = "1HELLOTEST"; } dottests[] = {
char end[256]; { "aaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
char *tempend; "aaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.a"},
int codedlength; { "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."},
{ "abc123", "abc123" },
{ NULL, NULL }
};
memset(temp, 0, sizeof(temp)); START_TEST(test_inline_dotify)
memset(end, 0, sizeof(end)); {
unsigned i;
char temp[1024];
char *b;
codedlength = encode_data(start, strlen(start), sizeof(temp) - 1, temp + 1); while (dottests[i].a) {
temp[0] = '1'; memset(temp, 0, sizeof(temp));
tempend = temp + strlen(temp); strcpy(temp, dottests[i].a);
decode_data(end, sizeof(end), temp, tempend); b = temp;
inline_dotify(b, sizeof(temp));
fail_unless(strcmp(out, end) == 0, NULL); fail_unless(strcmp(dottests[i].b, temp) == 0,
va_str("'%s' != '%s'", temp, dottests[i].b));
i++;
}
}
END_TEST
START_TEST(test_inline_undotify)
{
unsigned i;
char temp[1024];
char *b;
while (dottests[i].a) {
memset(temp, 0, sizeof(temp));
strcpy(temp, dottests[i].b);
b = temp;
inline_undotify(b, sizeof(temp));
fail_unless(strcmp(dottests[i].a, temp) == 0,
va_str("'%s' != '%s'", temp, dottests[i].a));
i++;
}
} }
END_TEST END_TEST
@ -49,7 +79,8 @@ test_encoding_create_tests()
TCase *tc; TCase *tc;
tc = tcase_create("Encoding"); tc = tcase_create("Encoding");
tcase_add_test(tc, test_encoding_base32); tcase_add_test(tc, test_inline_dotify);
tcase_add_test(tc, test_inline_undotify);
return tc; return tc;
} }

View file

@ -23,6 +23,7 @@
#include <netinet/in.h> #include <netinet/in.h>
#include "common.h" #include "common.h"
#include "encoding.h"
#include "user.h" #include "user.h"
#include "test.h" #include "test.h"