2006-12-16 00:29:24 +00:00
|
|
|
/*
|
2014-06-01 06:46:42 +00:00
|
|
|
* Copyright (c) 2006-2014 Erik Ekman <yarrick@kryo.se>,
|
|
|
|
* 2006-2009 Bjorn Andersson <flex@kryo.se>
|
2006-12-16 00:29:24 +00:00
|
|
|
*
|
2014-08-07 19:14:10 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2006-12-16 00:29:24 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-04-24 22:09:24 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2006-12-16 00:29:24 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#ifdef DARWIN
|
2014-04-22 20:24:11 +00:00
|
|
|
#define BIND_8_COMPAT
|
|
|
|
#include <arpa/nameser_compat.h>
|
2006-12-16 00:29:24 +00:00
|
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <check.h>
|
|
|
|
|
2007-02-04 15:28:24 +00:00
|
|
|
#include "common.h"
|
2006-12-16 00:29:24 +00:00
|
|
|
#include "encoding.h"
|
|
|
|
#include "dns.h"
|
|
|
|
#include "read.h"
|
|
|
|
#include "test.h"
|
2008-07-12 20:57:30 +00:00
|
|
|
|
2006-12-16 00:29:24 +00:00
|
|
|
START_TEST(test_read_putshort)
|
|
|
|
{
|
2007-01-28 01:07:51 +00:00
|
|
|
unsigned short k;
|
|
|
|
unsigned short l;
|
2006-12-16 00:29:24 +00:00
|
|
|
char* p;
|
|
|
|
int i;
|
|
|
|
|
2007-01-28 03:05:43 +00:00
|
|
|
for (i = 0; i < 65536; i++) {
|
2007-01-28 01:07:51 +00:00
|
|
|
p = (char*)&k;
|
|
|
|
putshort(&p, i);
|
2008-07-12 20:57:30 +00:00
|
|
|
fail_unless(ntohs(k) == i,
|
|
|
|
"Bad value on putshort for %d: %d != %d",
|
|
|
|
i, ntohs(k), i);
|
2007-01-28 02:50:59 +00:00
|
|
|
|
2007-01-28 01:07:51 +00:00
|
|
|
p = (char*)&k;
|
2014-06-09 17:47:44 +00:00
|
|
|
readshort(NULL, &p, &l);
|
2007-01-28 01:07:51 +00:00
|
|
|
fail_unless(l == i,
|
2008-07-12 20:57:30 +00:00
|
|
|
"Bad value on readshort for %d: %d != %d",
|
|
|
|
i, l, i);
|
2006-12-16 00:29:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_read_putlong)
|
|
|
|
{
|
2007-01-28 02:52:19 +00:00
|
|
|
uint32_t k;
|
|
|
|
uint32_t l;
|
2006-12-16 00:29:24 +00:00
|
|
|
char* p;
|
|
|
|
int i;
|
2007-01-28 02:52:19 +00:00
|
|
|
int j;
|
2006-12-16 00:29:24 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 32; i++) {
|
2007-01-28 02:52:19 +00:00
|
|
|
p = (char*)&k;
|
|
|
|
j = 0xf << i;
|
2006-12-16 00:29:24 +00:00
|
|
|
|
2007-01-28 02:52:19 +00:00
|
|
|
putlong(&p, j);
|
2006-12-16 00:29:24 +00:00
|
|
|
|
2008-07-12 20:57:30 +00:00
|
|
|
fail_unless(ntohl(k) == j,
|
|
|
|
"Bad value on putlong for %d: %d != %d", i, ntohl(j), j);
|
|
|
|
|
2007-01-28 02:52:19 +00:00
|
|
|
p = (char*)&k;
|
|
|
|
readlong(NULL, &p, &l);
|
2006-12-16 00:29:24 +00:00
|
|
|
|
2007-01-28 02:52:19 +00:00
|
|
|
fail_unless(l == j,
|
2008-07-12 20:57:30 +00:00
|
|
|
"Bad value on readlong for %d: %d != %d", i, l, j);
|
2006-12-16 00:29:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2009-02-10 18:10:40 +00:00
|
|
|
START_TEST(test_read_name_empty_loop)
|
2006-12-16 00:29:24 +00:00
|
|
|
{
|
2007-02-11 14:07:26 +00:00
|
|
|
unsigned char emptyloop[] = {
|
2006-12-16 00:29:24 +00:00
|
|
|
'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2008-07-12 20:57:30 +00:00
|
|
|
0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01 };
|
2009-02-10 18:10:40 +00:00
|
|
|
char buf[1024];
|
|
|
|
char *data;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
data = (char*) emptyloop + sizeof(HEADER);
|
|
|
|
buf[1023] = 'A';
|
|
|
|
rv = readname((char *) emptyloop, sizeof(emptyloop), &data, buf, 1023);
|
2014-01-29 20:22:58 +00:00
|
|
|
fail_unless(rv == 0);
|
2009-02-10 18:10:40 +00:00
|
|
|
fail_unless(buf[1023] == 'A');
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_read_name_inf_loop)
|
|
|
|
{
|
2007-02-11 14:07:26 +00:00
|
|
|
unsigned char infloop[] = {
|
2006-12-16 00:29:24 +00:00
|
|
|
'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2008-07-12 20:57:30 +00:00
|
|
|
0x01, 'A', 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01 };
|
2009-02-10 18:10:40 +00:00
|
|
|
char buf[1024];
|
|
|
|
char *data;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
data = (char*) infloop + sizeof(HEADER);
|
|
|
|
buf[4] = '\a';
|
|
|
|
rv = readname((char*) infloop, sizeof(infloop), &data, buf, 4);
|
2014-01-29 20:22:58 +00:00
|
|
|
fail_unless(rv == 3);
|
2009-02-10 18:10:40 +00:00
|
|
|
fail_unless(buf[4] == '\a');
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_read_name_longname)
|
|
|
|
{
|
2008-07-12 20:57:30 +00:00
|
|
|
unsigned char longname[] =
|
2006-12-16 00:29:24 +00:00
|
|
|
"AA\x81\x80\x00\x01\x00\x00\x00\x00\x00\x00"
|
|
|
|
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
|
|
|
|
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
|
|
|
|
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
|
|
|
|
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
|
|
|
|
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
|
|
|
|
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
|
|
|
|
"\x00\x00\x01\x00\x01";
|
|
|
|
char buf[1024];
|
|
|
|
char *data;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2007-02-11 14:07:26 +00:00
|
|
|
data = (char*) longname + sizeof(HEADER);
|
2006-12-16 00:29:24 +00:00
|
|
|
buf[256] = '\a';
|
2007-02-11 14:07:26 +00:00
|
|
|
rv = readname((char*) longname, sizeof(longname), &data, buf, 256);
|
2014-01-29 20:22:58 +00:00
|
|
|
fail_unless(rv == 256);
|
2009-02-10 18:10:40 +00:00
|
|
|
fail_unless(buf[256] == '\a');
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_read_name_onejump)
|
|
|
|
{
|
|
|
|
unsigned char onejump[] =
|
|
|
|
"AA\x81\x80\x00\x01\x00\x00\x00\x00\x00\x00"
|
|
|
|
"\x02hh\xc0\x15\x00\x01\x00\x01\x05zBCDE\x00";
|
|
|
|
char buf[1024];
|
|
|
|
char *data;
|
|
|
|
int rv;
|
2006-12-16 00:29:24 +00:00
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2007-02-11 14:07:26 +00:00
|
|
|
data = (char*) onejump + sizeof(HEADER);
|
|
|
|
rv = readname((char*) onejump, sizeof(onejump), &data, buf, 256);
|
2009-02-10 18:10:40 +00:00
|
|
|
fail_unless(rv == 9);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_read_name_badjump_start)
|
|
|
|
{
|
|
|
|
unsigned char badjump[] = {
|
|
|
|
'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0xfe, 0xcc, 0x00, 0x01, 0x00, 0x01 };
|
|
|
|
unsigned char *jumper;
|
|
|
|
char buf[1024];
|
|
|
|
char *data;
|
|
|
|
int rv;
|
2008-07-12 20:57:30 +00:00
|
|
|
|
2009-02-10 18:10:40 +00:00
|
|
|
/* This test uses malloc to cause segfault if jump is executed */
|
2006-12-16 00:29:24 +00:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
jumper = malloc(sizeof(badjump));
|
|
|
|
if (jumper) {
|
|
|
|
memcpy(jumper, badjump, sizeof(badjump));
|
2007-02-11 14:07:26 +00:00
|
|
|
data = (char*) jumper + sizeof(HEADER);
|
|
|
|
rv = readname((char*) jumper, sizeof(badjump), &data, buf, 256);
|
2006-12-16 00:29:24 +00:00
|
|
|
|
2009-02-10 18:10:40 +00:00
|
|
|
fail_unless(rv == 0);
|
|
|
|
fail_unless(buf[0] == 0);
|
2006-12-16 00:29:24 +00:00
|
|
|
}
|
|
|
|
free(jumper);
|
2009-02-10 18:10:40 +00:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_read_name_badjump_second)
|
|
|
|
{
|
|
|
|
unsigned char badjump2[] = {
|
|
|
|
'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x02, 'B', 'A', 0xfe, 0xcc, 0x00, 0x01, 0x00, 0x01 };
|
|
|
|
unsigned char *jumper;
|
|
|
|
char buf[1024];
|
|
|
|
char *data;
|
|
|
|
int rv;
|
2008-07-12 20:57:30 +00:00
|
|
|
|
2009-02-10 18:10:40 +00:00
|
|
|
/* This test uses malloc to cause segfault if jump is executed */
|
2006-12-16 00:29:24 +00:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
jumper = malloc(sizeof(badjump2));
|
|
|
|
if (jumper) {
|
|
|
|
memcpy(jumper, badjump2, sizeof(badjump2));
|
2007-02-11 14:07:26 +00:00
|
|
|
data = (char*) jumper + sizeof(HEADER);
|
|
|
|
rv = readname((char*) jumper, sizeof(badjump2), &data, buf, 256);
|
2006-12-16 00:29:24 +00:00
|
|
|
|
2009-02-10 18:10:40 +00:00
|
|
|
fail_unless(rv == 4);
|
2008-07-12 20:57:30 +00:00
|
|
|
fail_unless(strcmp("BA.", buf) == 0,
|
|
|
|
"buf is not BA: %s", buf);
|
2006-12-16 00:29:24 +00:00
|
|
|
}
|
|
|
|
free(jumper);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2007-02-11 01:46:45 +00:00
|
|
|
START_TEST(test_putname)
|
|
|
|
{
|
|
|
|
char out[] = "\x06" "BADGER\x06" "BADGER\x04" "KRYO\x02" "SE\x00";
|
|
|
|
char buf[256];
|
|
|
|
char *domain = "BADGER.BADGER.KRYO.SE";
|
|
|
|
char *b;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(buf, 0, 256);
|
|
|
|
b = buf;
|
|
|
|
ret = putname(&b, 256, domain);
|
|
|
|
|
2009-02-10 18:10:40 +00:00
|
|
|
fail_unless(ret == strlen(domain) + 1);
|
2007-02-11 01:46:45 +00:00
|
|
|
fail_unless(strncmp(buf, out, ret) == 0, "Happy flow failed");
|
|
|
|
}
|
|
|
|
END_TEST
|
2008-07-12 20:57:30 +00:00
|
|
|
|
2007-02-11 01:46:45 +00:00
|
|
|
START_TEST(test_putname_nodot)
|
|
|
|
{
|
|
|
|
char buf[256];
|
2008-07-12 20:57:30 +00:00
|
|
|
char *nodot =
|
2007-02-11 01:46:45 +00:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
char *b;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(buf, 0, 256);
|
|
|
|
b = buf;
|
|
|
|
ret = putname(&b, 256, nodot);
|
|
|
|
|
2009-02-10 18:10:40 +00:00
|
|
|
fail_unless(ret == -1);
|
|
|
|
fail_unless(b == buf);
|
2007-02-11 01:46:45 +00:00
|
|
|
}
|
|
|
|
END_TEST
|
2008-07-12 20:57:30 +00:00
|
|
|
|
2007-02-11 01:46:45 +00:00
|
|
|
START_TEST(test_putname_toolong)
|
|
|
|
{
|
|
|
|
char buf[256];
|
2008-07-12 20:57:30 +00:00
|
|
|
char *toolong =
|
2007-02-11 01:46:45 +00:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ."
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ."
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ."
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ."
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ."
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ.";
|
|
|
|
char *b;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(buf, 0, 256);
|
|
|
|
b = buf;
|
|
|
|
ret = putname(&b, 256, toolong);
|
|
|
|
|
2009-02-10 18:10:40 +00:00
|
|
|
fail_unless(ret == -1);
|
|
|
|
fail_unless(b == buf);
|
2007-02-11 01:46:45 +00:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
|
2006-12-16 00:29:24 +00:00
|
|
|
TCase *
|
|
|
|
test_read_create_tests()
|
|
|
|
{
|
|
|
|
TCase *tc;
|
|
|
|
|
|
|
|
tc = tcase_create("Read");
|
2007-01-28 02:50:59 +00:00
|
|
|
tcase_set_timeout(tc, 60);
|
2006-12-16 00:29:24 +00:00
|
|
|
tcase_add_test(tc, test_read_putshort);
|
|
|
|
tcase_add_test(tc, test_read_putlong);
|
2009-02-10 18:10:40 +00:00
|
|
|
tcase_add_test(tc, test_read_name_empty_loop);
|
|
|
|
tcase_add_test(tc, test_read_name_inf_loop);
|
|
|
|
tcase_add_test(tc, test_read_name_longname);
|
|
|
|
tcase_add_test(tc, test_read_name_onejump);
|
|
|
|
tcase_add_test(tc, test_read_name_badjump_start);
|
|
|
|
tcase_add_test(tc, test_read_name_badjump_second);
|
2007-02-11 01:46:45 +00:00
|
|
|
tcase_add_test(tc, test_putname);
|
|
|
|
tcase_add_test(tc, test_putname_nodot);
|
|
|
|
tcase_add_test(tc, test_putname_toolong);
|
2006-12-16 00:29:24 +00:00
|
|
|
|
|
|
|
return tc;
|
|
|
|
}
|