iodine/test.c

177 lines
4.1 KiB
C
Raw Normal View History

2006-08-25 00:02:52 +03: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-25 01:04:27 +03:00
#include <netinet/in.h>
2006-08-25 00:02:52 +03:00
#include <sys/stat.h>
2006-08-25 01:04:27 +03:00
#include <arpa/nameser.h>
2006-08-27 23:30:40 +03:00
#ifdef DARWIN
#include <arpa/nameser8_compat.h>
#endif
2006-08-25 00:02:52 +03:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2006-08-25 01:04:27 +03:00
#include <assert.h>
2006-08-25 00:02:52 +03:00
#include "structs.h"
#include "dns.h"
#include "read.h"
2006-08-25 01:04:27 +03:00
static void
test_readputshort()
2006-08-25 00:02:52 +03:00
{
short tshort;
short putted;
2006-08-25 01:04:27 +03:00
short temps;
2006-08-25 00:02:52 +03:00
char buf[4];
2006-08-25 01:04:27 +03:00
short *s;
2006-08-25 00:02:52 +03:00
char* p;
2006-08-25 01:04:27 +03:00
int i;
2006-08-25 00:02:52 +03:00
2006-08-25 00:12:04 +03:00
printf(" * Testing read/putshort... ");
2006-08-25 00:02:52 +03: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-25 01:04:27 +03:00
}
static void
test_readputlong()
{
char buf[4];
long putint;
long tempi;
long tint;
long *l;
char* p;
int i;
2006-08-25 00:02:52 +03:00
2006-08-25 00:12:04 +03:00
printf(" * Testing read/putlong... ");
2006-08-25 00:02:52 +03: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-25 01:04:27 +03:00
}
2006-08-25 01:21:32 +03:00
2006-08-25 01:04:27 +03: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-25 01:21:32 +03:00
char longname[] =
"AA\x81\x80\x00\x01\x00\x00\x00\x00\x00\x00"
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
"\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA"
2006-08-25 01:56:19 +03: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-25 01:04:27 +03: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-25 01:21:32 +03:00
bzero(buf, sizeof(buf));
data = longname + sizeof(HEADER);
buf[256] = '\a';
rv = readname(longname, &data, buf, 256);
assert(buf[256] == '\a');
2006-08-25 01:04:27 +03:00
2006-08-25 01:56:19 +03:00
bzero(buf, sizeof(buf));
data = onejump + sizeof(HEADER);
rv = readname(onejump, &data, buf, 256);
assert(rv == 9);
2006-08-25 01:04:27 +03:00
printf("OK\n");
}
int
main()
{
printf("** iodine test suite\n");
2006-08-25 00:02:52 +03:00
2006-08-25 01:04:27 +03:00
test_readputshort();
test_readputlong();
test_readname();
2006-08-25 00:02:52 +03:00
2006-08-25 00:12:04 +03:00
printf("** All went well :)\n");
2006-08-25 00:02:52 +03:00
return 0;
}