From 00459ad59b6cd7ea645685c20c38b227ce37a3d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Rolim?= Date: Tue, 6 Oct 2020 19:11:40 -0300 Subject: [PATCH] Simplify test.c error counting. Use a global variable instead of copy pasting the rv logic multiple times. Added advantage of counting the amount of errors. --- tests.c | 85 ++++++++++++++++++++++++++------------------------------- 1 file changed, 39 insertions(+), 46 deletions(-) diff --git a/tests.c b/tests.c index ed151e9..c1c41dd 100644 --- a/tests.c +++ b/tests.c @@ -8,53 +8,45 @@ #include "mmap_file.h" #include "gemini.h" -static int compare_strings(const char *expected, const char *result, const char *function) -{ - int rv = 0; +int rv = 0; +static void compare_strings(const char *expected, const char *result, const char *function) +{ printf("%s(): ", function); if (result == NULL || strcmp(expected, result)) { - rv = 1; + rv += 1; puts("failure"); printf(" expected: %s\n got: %s\n", expected, result); } else { puts("success"); } - - return rv; } -static int compare_arrays(const uint8_t *expected, const uint8_t *result, size_t len, const char *function) +static void compare_arrays(const uint8_t *expected, const uint8_t *result, size_t len, const char *function) { - int rv = 0; - printf("%s(): ", function); if (memcmp(expected, result, len)) { - rv = 1; + rv += 1; puts("failure"); printf(" expected: %s\n got: %s\n", print_hex(expected, len, false), print_hex(result, len, false)); } else { puts("success"); } - - return rv; } int main() { - int rv = 0; - { /* formats.c */ uint8_t buf[] = {0x12, 0x02, 0x12, 0x4c, 0xa8}; const char *expected = "1202124ca8"; const char *result = print_hex(buf, sizeof buf, false); - rv = compare_strings(expected, result, "print_hex") ? 1 : rv; + compare_strings(expected, result, "print_hex"); const char *hex = "0403124574"; uint8_t buf_expected[] = {0x04, 0x03, 0x12, 0x45, 0x74}; uint8_t buf_result[sizeof buf_expected]; int err = decode_hex(hex, buf_result, sizeof buf_result); - rv = compare_arrays(buf_expected, buf_result, sizeof buf_result, "decode_hex") ? 1 : rv; + compare_arrays(buf_expected, buf_result, sizeof buf_result, "decode_hex"); assert(err == 0); } @@ -64,10 +56,10 @@ int main() //char scheme[4096], clean[4096], path[4096], port[16]; char *scheme = NULL, *clean = NULL, *path = NULL, *port = NULL; int portn = clean_up_link(dirty, &scheme, &clean, &path, &port); - rv = compare_strings("https://", scheme, "clean_up_link") ? 1 : rv; - 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; + compare_strings("https://", scheme, "clean_up_link"); + compare_strings("hello.com", clean, "clean_up_link"); + compare_strings("/ash", path, "clean_up_link"); + compare_strings("443", port, "clean_up_link"); assert(portn == HTTPS_PORT); free(scheme); scheme = NULL; @@ -77,10 +69,10 @@ int main() dirty = "http://hello.com"; portn = clean_up_link(dirty, &scheme, &clean, &path, &port); - rv = compare_strings("http://", scheme, "clean_up_link") ? 1 : rv; - 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; + compare_strings("http://", scheme, "clean_up_link"); + compare_strings("hello.com", clean, "clean_up_link"); + compare_strings("/", path, "clean_up_link"); + compare_strings("80", port, "clean_up_link"); assert(portn == HTTP_PORT); free(scheme); scheme = NULL; @@ -90,10 +82,10 @@ int main() dirty = "hello.com"; portn = clean_up_link(dirty, &scheme, &clean, &path, &port); - rv = compare_strings("http://", scheme, "clean_up_link") ? 1 : rv; - 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; + compare_strings("http://", scheme, "clean_up_link"); + compare_strings("hello.com", clean, "clean_up_link"); + compare_strings("/", path, "clean_up_link"); + compare_strings("80", port, "clean_up_link"); assert(portn == HTTP_PORT); free(scheme); scheme = NULL; @@ -103,18 +95,18 @@ int main() dirty = "https://bsd.ac/paste.html#sieqaqk_73fe_df51"; portn = clean_up_link(dirty, &scheme, &clean, &path, &port); - rv = compare_strings("https://", scheme, "clean_up_link") ? 1 : rv; - rv = compare_strings("bsd.ac", clean, "clean_up_link") ? 1 : rv; - rv = compare_strings("/paste.html#sieqaqk_73fe_df51", path, "clean_up_link") ? 1 : rv; - rv = compare_strings("443", port, "clean_up_link") ? 1 : rv; + compare_strings("https://", scheme, "clean_up_link"); + compare_strings("bsd.ac", clean, "clean_up_link"); + compare_strings("/paste.html#sieqaqk_73fe_df51", path, "clean_up_link"); + compare_strings("443", port, "clean_up_link"); assert(portn == HTTPS_PORT); uint8_t key_exc[KEY_LEN] = {0x73, 0xfe}; uint8_t iv_exc[IV_LEN] = {0xdf, 0x51}; uint8_t *key, *iv; int err = get_encryption_params(path, &key, &iv); - rv = compare_strings("/sieqaqk", path, "get_encryption_params") ? 1 : rv; - rv = compare_arrays(key_exc, key, KEY_LEN, "get_encryption_params") ? 1 : rv; - rv = compare_arrays(iv_exc, iv, IV_LEN, "get_encryption_params") ? 1 : rv; + compare_strings("/sieqaqk", path, "get_encryption_params"); + compare_arrays(key_exc, key, KEY_LEN, "get_encryption_params"); + compare_arrays(iv_exc, iv, IV_LEN, "get_encryption_params"); assert(err == 0); } @@ -131,7 +123,7 @@ int main() assert(f.offset == written); RESET_MMAP(f); read_from_mmap(&f, tmp, write_size); - rv = compare_arrays(data, f.data, write_size, "{write_into,read_from}_mmap") ? 1 : rv; + compare_arrays(data, f.data, write_size, "{write_into,read_from}_mmap"); } { @@ -139,36 +131,37 @@ int main() const char *gmi_file = "=> aha/ Aha"; struct gemini_link_node *head = NULL; int n = get_links_from_gmi(gmi_file, &head); - rv = compare_strings("aha/", head->path, "get_links_from_gmi") ? 1 : rv; - rv = compare_strings("Aha", head->name, "get_links_from_gmi") ? 1 : rv; + compare_strings("aha/", head->path, "get_links_from_gmi"); + compare_strings("Aha", head->name, "get_links_from_gmi"); assert(n == 1); gmi_file = "=> two-a Two spaces\r\n=> three\tThree"; head = NULL; n = get_links_from_gmi(gmi_file, &head); - rv = compare_strings("two-a", head->path, "get_links_from_gmi") ? 1 : rv; - rv = compare_strings("Two spaces", head->name, "get_links_from_gmi") ? 1 : rv; - rv = compare_strings("three", head->next->path, "get_links_from_gmi") ? 1 : rv; - rv = compare_strings("Three", head->next->name, "get_links_from_gmi") ? 1 : rv; + compare_strings("two-a", head->path, "get_links_from_gmi"); + compare_strings("Two spaces", head->name, "get_links_from_gmi"); + compare_strings("three", head->next->path, "get_links_from_gmi"); + compare_strings("Three", head->next->name, "get_links_from_gmi"); assert(n == 2); const char *root = "/hello"; const char *add = "piper"; char *path = walk_gemini_path(root, add); - rv = compare_strings("/hello/piper", path, "walk_gemini_path") ? 1 : rv; + compare_strings("/hello/piper", path, "walk_gemini_path"); root = "/hello/"; add = "/piper/hi/../../a"; path = walk_gemini_path(root, add); - rv = compare_strings("/hello/a", path, "walk_gemini_path") ? 1 : rv; + compare_strings("/hello/a", path, "walk_gemini_path"); root = "/hello/"; add = "/piper/hi/"; path = walk_gemini_path(root, add); - rv = compare_strings("/hello/piper/hi/", path, "walk_gemini_path") ? 1 : rv; + compare_strings("/hello/piper/hi/", path, "walk_gemini_path"); root = "/hello"; add = "../../../../.."; path = walk_gemini_path(root, add); - rv = compare_strings("/", path, "walk_gemini_path") ? 1 : rv; + compare_strings("/", path, "walk_gemini_path"); } + printf("Total errors: %d\n", rv); return rv; }