iodine/tests/test.c

263 lines
6.5 KiB
C
Raw Normal View History

2006-08-24 21:02:52 +00:00
/*
* Copyright (c) 2006 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
*
* Permission to use, copy, modify, and distribute this software for any
* 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.
*/
#include <sys/types.h>
#include <sys/socket.h>
2006-08-24 22:04:27 +00:00
#include <netinet/in.h>
2006-08-24 21:02:52 +00:00
#include <sys/stat.h>
2006-08-24 22:04:27 +00:00
#include <arpa/nameser.h>
2006-08-27 20:30:40 +00:00
#ifdef DARWIN
#include <arpa/nameser8_compat.h>
#endif
2006-08-24 21:02:52 +00:00
#include <stdio.h>
#include <stdint.h>
2006-08-24 21:02:52 +00:00
#include <stdlib.h>
#include <string.h>
2006-08-24 22:04:27 +00:00
#include <assert.h>
2006-08-24 21:02:52 +00:00
2006-11-18 13:37:37 +00:00
#include "../src/structs.h"
#include "../src/encoding.h"
#include "../src/dns.h"
#include "../src/read.h"
2006-08-24 22:04:27 +00:00
static void
test_readputshort()
2006-08-24 21:02:52 +00:00
{
short tshort;
short putted;
2006-08-24 22:04:27 +00:00
short temps;
2006-08-24 21:02:52 +00:00
char buf[4];
2006-08-24 22:04:27 +00:00
short *s;
2006-08-24 21:02:52 +00:00
char* p;
2006-08-24 22:04:27 +00:00
int i;
2006-08-24 21:02:52 +00:00
2006-08-24 21:12:04 +00:00
printf(" * Testing read/putshort... ");
2006-08-24 21:02:52 +00:00
fflush(stdout);
for (i = 0; i < 65536; i++) {
tshort = (unsigned short) i;
temps = htons(tshort);
p = buf;
putshort(&p, tshort);
s = &putted;
memcpy(s, buf, sizeof(short));
if (putted != temps) {
printf("Bad value on putshort for %d\n", i);
exit(1);
}
s = &temps;
memcpy(buf, s, sizeof(short));
p = buf;
readshort(NULL, &p, &temps);
if (temps != tshort) {
printf("Bad value on readshort for %d\n", i);
exit(1);
}
}
printf("OK\n");
2006-08-24 22:04:27 +00:00
}
static void
test_readputlong()
{
char buf[4];
uint32_t putint;
uint32_t tempi;
uint32_t tint;
uint32_t *l;
2006-08-24 22:04:27 +00:00
char* p;
int i;
2006-08-24 21:02:52 +00:00
2006-08-24 21:12:04 +00:00
printf(" * Testing read/putlong... ");
2006-08-24 21:02:52 +00:00
fflush(stdout);
for (i = 0; i < 32; i++) {
tint = 0xF << i;
tempi = htonl(tint);
p = buf;
putlong(&p, tint);
l = &putint;
memcpy(l, buf, sizeof(uint32_t));
2006-08-24 21:02:52 +00:00
if (putint != tempi) {
printf("Bad value on putlong for %d\n", i);
exit(2);
}
l = &tempi;
memcpy(buf, l, sizeof(uint32_t));
2006-08-24 21:02:52 +00:00
p = buf;
readlong(NULL, &p, &tempi);
if (tempi != tint) {
printf("Bad value on readlong for %d\n", i);
exit(2);
}
}
printf("OK\n");
2006-08-24 22:04:27 +00:00
}
2006-08-24 22:21:32 +00:00
2006-08-24 22:04:27 +00:00
static void
test_readname()
{
char emptyloop[] = {
'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01 };
char infloop[] = {
'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 'A', 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01 };
2006-08-24 22:21:32 +00:00
char longname[] =
"AA\x81\x80\x00\x01\x00\x00\x00\x00\x00\x00"
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
2006-08-24 22:56:19 +00:00
"\x00\x00\x01\x00\x01";
char onejump[] =
"AA\x81\x80\x00\x01\x00\x00\x00\x00\x00\x00"
"\x02hh\xc0\x15\x00\x01\x00\x01\x05zBCDE\x00";
char badjump[] = {
'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xfe, 0xcc, 0x00, 0x01, 0x00, 0x01 };
char badjump2[] = {
'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 'B', 'A', 0xfe, 0xcc, 0x00, 0x01, 0x00, 0x01 };
char *jumper;
2006-08-24 22:04:27 +00:00
char buf[1024];
char *data;
int rv;
printf(" * Testing readname... ");
fflush(stdout);
2006-11-08 21:45:28 +00:00
memset(buf, 0, sizeof(buf));
2006-08-24 22:04:27 +00:00
data = emptyloop + sizeof(HEADER);
buf[1023] = 'A';
rv = readname(emptyloop, sizeof(emptyloop), &data, buf, 1023);
2006-08-24 22:04:27 +00:00
assert(buf[1023] == 'A');
2006-11-08 21:45:28 +00:00
memset(buf, 0, sizeof(buf));
2006-08-24 22:04:27 +00:00
data = infloop + sizeof(HEADER);
buf[4] = '\a';
rv = readname(infloop, sizeof(infloop), &data, buf, 4);
2006-08-24 22:04:27 +00:00
assert(buf[4] == '\a');
2006-08-24 22:21:32 +00:00
2006-11-08 21:45:28 +00:00
memset(buf, 0, sizeof(buf));
2006-08-24 22:21:32 +00:00
data = longname + sizeof(HEADER);
buf[256] = '\a';
rv = readname(longname, sizeof(longname), &data, buf, 256);
2006-08-24 22:21:32 +00:00
assert(buf[256] == '\a');
2006-08-24 22:04:27 +00:00
2006-11-08 21:45:28 +00:00
memset(buf, 0, sizeof(buf));
2006-08-24 22:56:19 +00:00
data = onejump + sizeof(HEADER);
rv = readname(onejump, sizeof(onejump), &data, buf, 256);
2006-08-24 22:56:19 +00:00
assert(rv == 9);
2006-11-08 20:32:37 +00:00
// These two tests use malloc to cause segfault if jump is executed
2006-11-08 21:45:28 +00:00
memset(buf, 0, sizeof(buf));
jumper = malloc(sizeof(badjump));
if (jumper) {
memcpy(jumper, badjump, sizeof(badjump));
data = jumper + sizeof(HEADER);
rv = readname(jumper, sizeof(badjump), &data, buf, 256);
assert(rv == 0);
}
free(jumper);
2006-11-08 21:45:28 +00:00
memset(buf, 0, sizeof(buf));
jumper = malloc(sizeof(badjump2));
if (jumper) {
memcpy(jumper, badjump2, sizeof(badjump2));
data = jumper + sizeof(HEADER);
rv = readname(jumper, sizeof(badjump2), &data, buf, 256);
assert(rv == 4);
2006-11-08 21:45:28 +00:00
assert(strcmp("BA.", buf) == 0);
}
free(jumper);
2006-08-24 22:56:19 +00:00
2006-08-24 22:04:27 +00:00
printf("OK\n");
}
2006-11-05 11:44:43 +00:00
static void
test_encode_hostname() {
char buf[256];
int len;
int ret;
len = 256;
printf(" * Testing hostname encoding... ");
2006-11-08 20:34:45 +00:00
fflush(stdout);
2006-11-05 11:44:43 +00:00
memset(buf, 0, 256);
ret = dns_encode_hostname( // More than 63 chars between dots
"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"
, buf, len);
assert(ret == -1);
memset(buf, 0, 256);
ret = dns_encode_hostname( // More chars than fits into array
"ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ."
"ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ."
"ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ.ABCDEFGHIJKLMNOPQRSTUVWXYZ."
, buf, len);
assert(ret == -1);
assert(strlen(buf) < len);
printf("OK\n");
}
2006-11-08 21:45:28 +00:00
static void
test_base32() {
char temp[256];
char *start = "HELLOTEST";
char *out = "1HELLOTEST";
char *end;
char *tempend;
int codedlength;
printf(" * Testing base32 encoding... ");
fflush(stdout);
memset(temp, 0, sizeof(temp));
end = malloc(16);
memset(end, 0, 16);
codedlength = encode_data(start, 9, 256, temp, 0);
tempend = temp + strlen(temp);
decode_data(end, 16, temp, tempend);
assert(strcmp(out, end) == 0);
free(end);
printf("OK\n");
}
2006-08-24 22:04:27 +00:00
int
main()
{
printf("** iodine test suite\n");
2006-08-24 21:02:52 +00:00
2006-08-24 22:04:27 +00:00
test_readputshort();
test_readputlong();
test_readname();
2006-11-05 11:44:43 +00:00
test_encode_hostname();
2006-11-08 21:45:28 +00:00
test_base32();
2006-08-24 21:02:52 +00:00
2006-08-24 21:12:04 +00:00
printf("** All went well :)\n");
2006-08-24 21:02:52 +00:00
return 0;
}