From 9ccbd4bc88a30cd2ae670d2ee36f7d428c36ac87 Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Sat, 16 Dec 2006 00:29:24 +0000 Subject: [PATCH] added tests for putshort, putlong and readname --- tests/Makefile | 4 +- tests/read.c | 196 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/test.c | 3 + tests/test.h | 2 + 4 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 tests/read.c diff --git a/tests/Makefile b/tests/Makefile index 9a73157..3027e61 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,7 +1,7 @@ CC = gcc TEST = test -OBJS = test.o base32.o -SRCOBJS = ../src/base32.o +OBJS = test.o base32.o read.o +SRCOBJS = ../src/base32.o ../src/read.o OS = `uname | tr "a-z" "A-Z"` diff --git a/tests/read.c b/tests/read.c new file mode 100644 index 0000000..2c6fb2c --- /dev/null +++ b/tests/read.c @@ -0,0 +1,196 @@ +/* + * Copyright (c) 2006 Bjorn Andersson , Erik Ekman + * + * 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 +#include +#include +#include +#include +#ifdef DARWIN +#include +#endif +#include +#include +#include +#include +#include + +#include "structs.h" +#include "encoding.h" +#include "dns.h" +#include "read.h" +#include "test.h" + +START_TEST(test_read_putshort) +{ + short tshort; + short putted; + short temps; + char buf[4]; + short *s; + char* p; + int i; + + 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)); + fail_unless(putted == temps, + va_str("Bad value on putshort for %d: %d != %d", + i, putted, temps)); + s = &temps; + memcpy(buf, s, sizeof(short)); + p = buf; + readshort(NULL, &p, &temps); + + fail_unless(temps == tshort, + va_str("Bad value on readshort for %d: %d != %d", + i, temps, tshort)); + } +} +END_TEST + +START_TEST(test_read_putlong) +{ + char buf[4]; + uint32_t putint; + uint32_t tempi; + uint32_t tint; + uint32_t *l; + char* p; + int i; + + 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)); + + fail_unless(putint == tempi, + va_str("Bad value on putlong for %d: %d != %d", + i, putint, tempi)); + + l = &tempi; + memcpy(buf, l, sizeof(uint32_t)); + p = buf; + readlong(NULL, &p, &tempi); + + fail_unless(tempi == tint, + va_str("Bad value on readlong for %d: %d != %d", + i, tempi, tint)); + } +} +END_TEST + +START_TEST(test_read_name) +{ + 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 }; + char longname[] = + "AA\x81\x80\x00\x01\x00\x00\x00\x00\x00\x00" + "\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA" + "\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA" + "\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA" + "\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA" + "\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA" + "\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA" + "\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; + char buf[1024]; + char *data; + int rv; + + memset(buf, 0, sizeof(buf)); + data = emptyloop + sizeof(HEADER); + buf[1023] = 'A'; + rv = readname(emptyloop, sizeof(emptyloop), &data, buf, 1023); + fail_unless(buf[1023] == 'A', NULL); + + memset(buf, 0, sizeof(buf)); + data = infloop + sizeof(HEADER); + buf[4] = '\a'; + rv = readname(infloop, sizeof(infloop), &data, buf, 4); + fail_unless(buf[4] == '\a', NULL); + + memset(buf, 0, sizeof(buf)); + data = longname + sizeof(HEADER); + buf[256] = '\a'; + rv = readname(longname, sizeof(longname), &data, buf, 256); + fail_unless(buf[256] == '\a', NULL); + + memset(buf, 0, sizeof(buf)); + data = onejump + sizeof(HEADER); + rv = readname(onejump, sizeof(onejump), &data, buf, 256); + fail_unless(rv == 9, NULL); + + // These two tests use malloc to cause segfault if jump is executed + 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); + + fail_unless(rv == 0, NULL); + } + free(jumper); + + 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); + + /* XXX: This was 'strcmp("BA"...' changed to 'BA.', make sure this is correct */ + fail_unless(rv == 4, NULL); + fail_unless(strcmp("BA.", buf) == 0, + va_str("buf is not BA: %s", buf)); + } + free(jumper); +} +END_TEST + +TCase * +test_read_create_tests() +{ + TCase *tc; + + tc = tcase_create("Read"); + tcase_add_test(tc, test_read_putshort); + tcase_add_test(tc, test_read_putlong); + tcase_add_test(tc, test_read_name); + + return tc; +} diff --git a/tests/test.c b/tests/test.c index 84d6f19..53abc17 100644 --- a/tests/test.c +++ b/tests/test.c @@ -31,6 +31,9 @@ main() test = test_base32_create_tests(); suite_add_tcase(iodine, test); + test = test_read_create_tests(); + suite_add_tcase(iodine, test); + runner = srunner_create(iodine); srunner_run_all(runner, CK_VERBOSE); failed = srunner_ntests_failed(runner); diff --git a/tests/test.h b/tests/test.h index c5a0708..de7783f 100644 --- a/tests/test.h +++ b/tests/test.h @@ -2,6 +2,8 @@ #define __TEST_H__ TCase *test_base32_create_tests(); +TCase *test_read_create_tests(); + char *va_str(const char *, ...); #endif