Add tests for clean_up_links.

Clean up makefile.
This commit is contained in:
Érico Rolim 2020-09-12 23:43:03 -03:00
parent 5b7ec3ead5
commit 95f4c0192b
2 changed files with 40 additions and 12 deletions

View File

@ -1,11 +1,12 @@
CFLAGS = -O2 -g -pipe -Wall -Wextra CFLAGS = -O2 -g -pipe -Wall -Wextra
LDLIBS = -lbearssl -lsbearssl -lskarnet LDLIBS = -lbearssl -lsbearssl -lskarnet
LDFLAGS = -Wl,--as-needed
FINAL = purr FINAL = purr
OBJS = purr.o socket.o urls.o files.o comm.o formats.o encrypt.o OBJS = purr.o socket.o urls.o files.o comm.o formats.o encrypt.o
TEST = tests TEST = tests
TOBJS = tests.o formats.o TOBJS = tests.o formats.o urls.o
all: $(FINAL) all: $(FINAL)
@ -19,4 +20,4 @@ tests: $(TOBJS)
$(OBJS): purr.h $(OBJS): purr.h
clean: clean:
rm -f $(FINAL) $(OBJS) rm -f $(FINAL) $(OBJS) $(TEST) $(TOBJS)

47
tests.c
View File

@ -1,8 +1,25 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h>
#include "purr.h" #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 main()
{ {
int rv = 0; int rv = 0;
@ -10,17 +27,27 @@ int main()
{ {
/* formats.c */ /* formats.c */
uint8_t buf[] = {0x12, 0x02, 0x12, 0x4c, 0xa8}; uint8_t buf[] = {0x12, 0x02, 0x12, 0x4c, 0xa8};
char *expected = "1202124ca8"; const char *expected = "1202124ca8";
char *result = print_hex(buf, sizeof buf, false); 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)) { /* urls.c */
puts("failure"); const char *dirty = "https://hello.com/ash";
printf("expected: %s\ngot: %s\n", expected, result); char clean[4096], path[4096], port[16];
rv = 1; int portn = clean_up_link(dirty, clean, path, port);
} else { rv = compare_strings("hello.com", clean, "clean_up_link") ? 1 : rv;
puts("success"); 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; return rv;