diff --git a/makefile b/makefile index dfafe86..58302b4 100644 --- a/makefile +++ b/makefile @@ -1,11 +1,12 @@ CFLAGS = -O2 -g -pipe -Wall -Wextra LDLIBS = -lbearssl -lsbearssl -lskarnet +LDFLAGS = -Wl,--as-needed FINAL = purr OBJS = purr.o socket.o urls.o files.o comm.o formats.o encrypt.o TEST = tests -TOBJS = tests.o formats.o +TOBJS = tests.o formats.o urls.o all: $(FINAL) @@ -19,4 +20,4 @@ tests: $(TOBJS) $(OBJS): purr.h clean: - rm -f $(FINAL) $(OBJS) + rm -f $(FINAL) $(OBJS) $(TEST) $(TOBJS) diff --git a/tests.c b/tests.c index 7ed3963..f7e3373 100644 --- a/tests.c +++ b/tests.c @@ -1,8 +1,25 @@ #include #include +#include #include "purr.h" +static int compare_strings(const char *expected, const char *result, const char *function) +{ + int rv = 0; + + printf("%s(): ", function); + if (strcmp(expected, result)) { + rv = 1; + puts("failure"); + printf("expected: %s\ngot: %s\n", expected, result); + } else { + puts("success"); + } + + return rv; +} + int main() { int rv = 0; @@ -10,17 +27,27 @@ int main() { /* formats.c */ uint8_t buf[] = {0x12, 0x02, 0x12, 0x4c, 0xa8}; - char *expected = "1202124ca8"; - char *result = print_hex(buf, sizeof buf, false); + const char *expected = "1202124ca8"; + const char *result = print_hex(buf, sizeof buf, false); + rv = compare_strings(expected, result, "print_hex") ? 1 : rv; + } - printf("print_hex(): "); - if (strcmp(expected, result)) { - puts("failure"); - printf("expected: %s\ngot: %s\n", expected, result); - rv = 1; - } else { - puts("success"); - } + { + /* urls.c */ + const char *dirty = "https://hello.com/ash"; + char clean[4096], path[4096], port[16]; + int portn = clean_up_link(dirty, clean, path, port); + rv = compare_strings("hello.com", clean, "clean_up_link") ? 1 : rv; + rv = compare_strings("/ash", path, "clean_up_link") ? 1 : rv; + rv = compare_strings("443", port, "clean_up_link") ? 1 : rv; + assert(portn == HTTPS_PORT); + + dirty = "http://hello.com"; + portn = clean_up_link(dirty, clean, path, port); + rv = compare_strings("hello.com", clean, "clean_up_link") ? 1 : rv; + rv = compare_strings("/", path, "clean_up_link") ? 1 : rv; + rv = compare_strings("80", port, "clean_up_link") ? 1 : rv; + assert(portn == HTTP_PORT); } return rv;