summaryrefslogtreecommitdiff
path: root/tests/t_accesslist.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/t_accesslist.c')
-rw-r--r--tests/t_accesslist.c122
1 files changed, 122 insertions, 0 deletions
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
18static 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
25static void case_empty(void) {
26 test_case("an empty buffer yields nothing");
27 CHECK_INT(accesslist_parse_hashes(dest, 8, "", 0), 0);
28}
29
30static 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
37static 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
44static 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
51static 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
58static 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
65static 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
72static 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
79static 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
86static 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
95static 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
110void 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}