1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
It is considered beerware. Prost. Skol. Cheers or whatever.
$id$ */
/* System */
#include <stdlib.h>
#include <string.h>
/* Opentracker */
#include "ot_accesslist.h"
#include "test.h"
#define A "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
#define B "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
#define C "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
static ot_hash dest[8];
/* The parser is handed a mapped file, so it must never look at a byte beyond
the length it was given. These cases place a hex digit immediately after
that length: a parser that reads one byte too far sees it, decides the hash
runs on, and silently drops the last entry. */
static void case_empty(void) {
test_case("an empty buffer yields nothing");
CHECK_INT(accesslist_parse_hashes(dest, 8, "", 0), 0);
}
static void case_single_with_newline(void) {
const char buf[] = A "\n";
test_case("one hash followed by a newline");
CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 41), 1);
}
static void case_single_without_newline(void) {
const char buf[] = A "A";
test_case("one hash ending the buffer, with a hex digit just past the end");
CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 40), 1);
}
static void case_several_last_without_newline(void) {
const char buf[] = A "\n" B "\n" C "C";
test_case("a final hash without a trailing newline still counts");
CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 3 * 40 + 2), 3);
}
static void case_run_on_hash_is_rejected(void) {
const char buf[] = A "A\n";
test_case("forty one hex digits are not a hash");
CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 42), 0);
}
static void case_junk_lines_skipped(void) {
const char buf[] = "# a comment\n" A "\nnot a hash\n" B "\n";
test_case("lines that are not hashes are ignored");
CHECK_INT(accesslist_parse_hashes(dest, 8, buf, sizeof(buf) - 1), 2);
}
static void case_trailing_junk_on_line(void) {
const char buf[] = A " some comment\n" B "\n";
test_case("a hash followed by other text on the same line still counts");
CHECK_INT(accesslist_parse_hashes(dest, 8, buf, sizeof(buf) - 1), 2);
}
static void case_capacity_is_respected(void) {
const char buf[] = A "\n" B "\n" C "\n";
test_case("no more than capacity hashes are written");
CHECK_INT(accesslist_parse_hashes(dest, 2, buf, sizeof(buf) - 1), 2);
}
static void case_zero_capacity_never_writes(void) {
const char buf[] = A "\n";
test_case("a zero capacity tolerates a null destination");
CHECK_INT(accesslist_parse_hashes(NULL, 0, buf, 41), 0);
}
static void case_decoded_value(void) {
const char buf[] = "000102030405060708090A0B0C0D0E0F10111213\n";
const uint8_t want[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
test_case("the hash is decoded, not merely counted");
CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 41), 1);
CHECK_MEM(dest[0], want, sizeof(want));
}
static void case_capacity_bound_holds(void) {
/* (len + 1) / 41 is what accesslist_readfile allocates. Pack a buffer as
densely as the parser allows and check the bound is not exceeded. */
char buf[41 * 4];
size_t len, capacity, found;
test_case("the capacity formula bounds what the parser can produce");
memcpy(buf, A "\n" B "\n" C "\n" A, 4 * 40 + 3);
len = 4 * 40 + 3;
capacity = (len + 1) / 41;
found = accesslist_parse_hashes(dest, 8, buf, len);
CHECK_INT(found, 4);
CHECK(found <= capacity);
}
void test_accesslist(void) {
case_empty();
case_single_with_newline();
case_single_without_newline();
case_several_last_without_newline();
case_run_on_hash_is_rejected();
case_junk_lines_skipped();
case_trailing_junk_on_line();
case_capacity_is_respected();
case_zero_capacity_never_writes();
case_decoded_value();
case_capacity_bound_holds();
}
|