iodine/test.c

174 lines
4.0 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-24 21:02:52 +00:00
#include <stdio.h>
#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
#include "structs.h"
#include "dns.h"
#include "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];
long putint;
long tempi;
long tint;
long *l;
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(int));
if (putint != tempi) {
printf("Bad value on putlong for %d\n", i);
exit(2);
}
l = &tempi;
memcpy(buf, l, sizeof(int));
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";
2006-08-24 22:04:27 +00:00
char buf[1024];
char *data;
int rv;
printf(" * Testing readname... ");
fflush(stdout);
bzero(buf, sizeof(buf));
data = emptyloop + sizeof(HEADER);
buf[1023] = 'A';
rv = readname(emptyloop, &data, buf, 1023);
assert(buf[1023] == 'A');
bzero(buf, sizeof(buf));
data = infloop + sizeof(HEADER);
buf[4] = '\a';
rv = readname(infloop, &data, buf, 4);
assert(buf[4] == '\a');
2006-08-24 22:21:32 +00:00
bzero(buf, sizeof(buf));
data = longname + sizeof(HEADER);
buf[256] = '\a';
rv = readname(longname, &data, buf, 256);
assert(buf[256] == '\a');
2006-08-24 22:04:27 +00:00
2006-08-24 22:56:19 +00:00
bzero(buf, sizeof(buf));
data = onejump + sizeof(HEADER);
rv = readname(onejump, &data, buf, 256);
assert(rv == 9);
2006-08-24 22:04:27 +00:00
printf("OK\n");
}
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-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;
}