diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/runner.c | 1 | ||||
| -rw-r--r-- | tests/stubs.c | 20 | ||||
| -rw-r--r-- | tests/t_accesslist.c | 122 | ||||
| -rw-r--r-- | tests/test.h | 1 |
4 files changed, 144 insertions, 0 deletions
diff --git a/tests/runner.c b/tests/runner.c index c9385f0..bf4fbde 100644 --- a/tests/runner.c +++ b/tests/runner.c | |||
| @@ -19,6 +19,7 @@ static const struct module modules[] = {{"scan_urlencoded_query", test_scan_urle | |||
| 19 | {"ot_vector", test_vector}, | 19 | {"ot_vector", test_vector}, |
| 20 | {"ot_random", test_random}, | 20 | {"ot_random", test_random}, |
| 21 | {"ot_hex", test_hex}, | 21 | {"ot_hex", test_hex}, |
| 22 | {"ot_accesslist", test_accesslist}, | ||
| 22 | {NULL, NULL}}; | 23 | {NULL, NULL}}; |
| 23 | 24 | ||
| 24 | /* Run every module, or only those whose name contains argv[1] */ | 25 | /* Run every module, or only those whose name contains argv[1] */ |
diff --git a/tests/stubs.c b/tests/stubs.c index ae40306..febdd7b 100644 --- a/tests/stubs.c +++ b/tests/stubs.c | |||
| @@ -12,3 +12,23 @@ | |||
| 12 | 12 | ||
| 13 | /* ot_vector.c calls this from vector_clean_list, which no test drives yet. */ | 13 | /* ot_vector.c calls this from vector_clean_list, which no test drives yet. */ |
| 14 | void free_peerlist(ot_peerlist *peer_list) { (void)peer_list; } | 14 | void free_peerlist(ot_peerlist *peer_list) { (void)peer_list; } |
| 15 | |||
| 16 | /* ot_accesslist.c is built with WANT_ACCESSLIST_BLACK so that the hash parser | ||
| 17 | is reachable. These four belong to accesslist_readfile and the reload | ||
| 18 | worker, neither of which any test drives. */ | ||
| 19 | time_t g_now_seconds; | ||
| 20 | volatile int g_opentracker_running; | ||
| 21 | |||
| 22 | const char *mmap_read(const char *filename, size_t *filesize); | ||
| 23 | const char *mmap_read(const char *filename, size_t *filesize) { | ||
| 24 | (void)filename; | ||
| 25 | *filesize = 0; | ||
| 26 | return NULL; | ||
| 27 | } | ||
| 28 | |||
| 29 | int mmap_unmap(const char *mapped, size_t maplen); | ||
| 30 | int mmap_unmap(const char *mapped, size_t maplen) { | ||
| 31 | (void)mapped; | ||
| 32 | (void)maplen; | ||
| 33 | return 0; | ||
| 34 | } | ||
diff --git a/tests/t_accesslist.c b/tests/t_accesslist.c new file mode 100644 index 0000000..9925acd --- /dev/null +++ b/tests/t_accesslist.c | |||
| @@ -0,0 +1,122 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | /* System */ | ||
| 7 | #include <stdlib.h> | ||
| 8 | #include <string.h> | ||
| 9 | |||
| 10 | /* Opentracker */ | ||
| 11 | #include "ot_accesslist.h" | ||
| 12 | #include "test.h" | ||
| 13 | |||
| 14 | #define A "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" | ||
| 15 | #define B "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" | ||
| 16 | #define C "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" | ||
| 17 | |||
| 18 | static ot_hash dest[8]; | ||
| 19 | |||
| 20 | /* The parser is handed a mapped file, so it must never look at a byte beyond | ||
| 21 | the length it was given. These cases place a hex digit immediately after | ||
| 22 | that length: a parser that reads one byte too far sees it, decides the hash | ||
| 23 | runs on, and silently drops the last entry. */ | ||
| 24 | |||
| 25 | static void case_empty(void) { | ||
| 26 | test_case("an empty buffer yields nothing"); | ||
| 27 | CHECK_INT(accesslist_parse_hashes(dest, 8, "", 0), 0); | ||
| 28 | } | ||
| 29 | |||
| 30 | static void case_single_with_newline(void) { | ||
| 31 | const char buf[] = A "\n"; | ||
| 32 | |||
| 33 | test_case("one hash followed by a newline"); | ||
| 34 | CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 41), 1); | ||
| 35 | } | ||
| 36 | |||
| 37 | static void case_single_without_newline(void) { | ||
| 38 | const char buf[] = A "A"; | ||
| 39 | |||
| 40 | test_case("one hash ending the buffer, with a hex digit just past the end"); | ||
| 41 | CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 40), 1); | ||
| 42 | } | ||
| 43 | |||
| 44 | static void case_several_last_without_newline(void) { | ||
| 45 | const char buf[] = A "\n" B "\n" C "C"; | ||
| 46 | |||
| 47 | test_case("a final hash without a trailing newline still counts"); | ||
| 48 | CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 3 * 40 + 2), 3); | ||
| 49 | } | ||
| 50 | |||
| 51 | static void case_run_on_hash_is_rejected(void) { | ||
| 52 | const char buf[] = A "A\n"; | ||
| 53 | |||
| 54 | test_case("forty one hex digits are not a hash"); | ||
| 55 | CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 42), 0); | ||
| 56 | } | ||
| 57 | |||
| 58 | static void case_junk_lines_skipped(void) { | ||
| 59 | const char buf[] = "# a comment\n" A "\nnot a hash\n" B "\n"; | ||
| 60 | |||
| 61 | test_case("lines that are not hashes are ignored"); | ||
| 62 | CHECK_INT(accesslist_parse_hashes(dest, 8, buf, sizeof(buf) - 1), 2); | ||
| 63 | } | ||
| 64 | |||
| 65 | static void case_trailing_junk_on_line(void) { | ||
| 66 | const char buf[] = A " some comment\n" B "\n"; | ||
| 67 | |||
| 68 | test_case("a hash followed by other text on the same line still counts"); | ||
| 69 | CHECK_INT(accesslist_parse_hashes(dest, 8, buf, sizeof(buf) - 1), 2); | ||
| 70 | } | ||
| 71 | |||
| 72 | static void case_capacity_is_respected(void) { | ||
| 73 | const char buf[] = A "\n" B "\n" C "\n"; | ||
| 74 | |||
| 75 | test_case("no more than capacity hashes are written"); | ||
| 76 | CHECK_INT(accesslist_parse_hashes(dest, 2, buf, sizeof(buf) - 1), 2); | ||
| 77 | } | ||
| 78 | |||
| 79 | static void case_zero_capacity_never_writes(void) { | ||
| 80 | const char buf[] = A "\n"; | ||
| 81 | |||
| 82 | test_case("a zero capacity tolerates a null destination"); | ||
| 83 | CHECK_INT(accesslist_parse_hashes(NULL, 0, buf, 41), 0); | ||
| 84 | } | ||
| 85 | |||
| 86 | static void case_decoded_value(void) { | ||
| 87 | const char buf[] = "000102030405060708090A0B0C0D0E0F10111213\n"; | ||
| 88 | const uint8_t want[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; | ||
| 89 | |||
| 90 | test_case("the hash is decoded, not merely counted"); | ||
| 91 | CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 41), 1); | ||
| 92 | CHECK_MEM(dest[0], want, sizeof(want)); | ||
| 93 | } | ||
| 94 | |||
| 95 | static void case_capacity_bound_holds(void) { | ||
| 96 | /* (len + 1) / 41 is what accesslist_readfile allocates. Pack a buffer as | ||
| 97 | densely as the parser allows and check the bound is not exceeded. */ | ||
| 98 | char buf[41 * 4]; | ||
| 99 | size_t len, capacity, found; | ||
| 100 | |||
| 101 | test_case("the capacity formula bounds what the parser can produce"); | ||
| 102 | memcpy(buf, A "\n" B "\n" C "\n" A, 4 * 40 + 3); | ||
| 103 | len = 4 * 40 + 3; | ||
| 104 | capacity = (len + 1) / 41; | ||
| 105 | found = accesslist_parse_hashes(dest, 8, buf, len); | ||
| 106 | CHECK_INT(found, 4); | ||
| 107 | CHECK(found <= capacity); | ||
| 108 | } | ||
| 109 | |||
| 110 | void test_accesslist(void) { | ||
| 111 | case_empty(); | ||
| 112 | case_single_with_newline(); | ||
| 113 | case_single_without_newline(); | ||
| 114 | case_several_last_without_newline(); | ||
| 115 | case_run_on_hash_is_rejected(); | ||
| 116 | case_junk_lines_skipped(); | ||
| 117 | case_trailing_junk_on_line(); | ||
| 118 | case_capacity_is_respected(); | ||
| 119 | case_zero_capacity_never_writes(); | ||
| 120 | case_decoded_value(); | ||
| 121 | case_capacity_bound_holds(); | ||
| 122 | } | ||
diff --git a/tests/test.h b/tests/test.h index 6af0083..8be624b 100644 --- a/tests/test.h +++ b/tests/test.h | |||
| @@ -81,5 +81,6 @@ void test_iovec(void); | |||
| 81 | void test_vector(void); | 81 | void test_vector(void); |
| 82 | void test_random(void); | 82 | void test_random(void); |
| 83 | void test_hex(void); | 83 | void test_hex(void); |
| 84 | void test_accesslist(void); | ||
| 84 | 85 | ||
| 85 | #endif | 86 | #endif |
