File: table_test.c

package info (click to toggle)
sniproxy 0.6.1%2Bgit20240321-0.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 644 kB
  • sloc: ansic: 5,594; perl: 1,673; sh: 237; makefile: 131
file content (189 lines) | stat: -rw-r--r-- 4,978 bytes parent folder | download | duplicates (5)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "table.h"
#include "backend.h"


static void test_empty_table();
static void test_single_entry_table();
static void append_entry(struct Table *, const char *, const char *);
static void add_new_table(struct Table_head *, const char *, const char **);
static void test_add_table();
static void test_tables_reload();
static int count_tables(const struct Table_head *);


int main() {
    test_empty_table();
    test_single_entry_table();
    test_add_table();
    test_tables_reload();
}

static void
test_empty_table() {
    struct Table *table = new_table();
    assert(table != NULL);

    table_ref_get(table);

    const char *name = "table_name";
    accept_table_arg(table, name);
    assert(table->name != NULL);
    assert(strcmp(name, table->name) == 0);
    assert(name != table->name);

    const char *server_query = "example.com";
    struct LookupResult result = table_lookup_server_address(table,
            server_query, strlen(server_query));
    assert(result.address == NULL);

    table_ref_put(table);
}

static void
append_entry(struct Table *table, const char *pattern, const char *address) {
    struct Backend *backend = new_backend();
    assert(backend != NULL);
    accept_backend_arg(backend, pattern);
    accept_backend_arg(backend, address);
    assert(strcmp(pattern, backend->pattern) == 0);
    assert(pattern != backend->pattern);

    add_backend(&table->backends, backend);
}

static void
test_single_entry_table() {
    struct Table *table = new_table();
    assert(table != NULL);

    table_ref_get(table);

    const char *name = "table_name";
    accept_table_arg(table, name);
    assert(table->name != NULL);
    assert(strcmp(name, table->name) == 0);
    assert(name != table->name);

    append_entry(table, "^example\\.com$", "192.0.2.10");

    init_table(table);

    const char *server_query = "example.com";
    struct LookupResult result = table_lookup_server_address(table,
            server_query, strlen(server_query));
    assert(result.address != NULL);

    table_ref_put(table);
}

static void
add_new_table(struct Table_head *tables, const char *name, const char **entries) {
    struct Table *table = new_table();
    assert(table != NULL);

    accept_table_arg(table, name);
    assert(table->name != NULL);
    assert(table->name != name);
    assert(strcmp(table->name, name) == 0);

    while (entries != NULL && entries[0] != NULL && entries[1] != NULL) {
        append_entry(table, entries[0], entries[1]);
        entries += 2;
    }

    add_table(tables, table);
}

static int
count_tables(const struct Table_head *tables) {
    struct Table *iter;
    int count = 0;

    SLIST_FOREACH(iter, tables, entries)
        count++;

    return count;
}

static void
test_add_table() {
    struct Table_head tables = SLIST_HEAD_INITIALIZER();

    add_new_table(&tables, "foo", (const char *[]){
            "^example\\.com$", "192.0.2.10",
            "^.*$", "*",
            NULL});
    add_new_table(&tables, "bar", (const char *[]){
            "^example\\.net$", "192.0.2.11",
            NULL});
    add_new_table(&tables, "baz", (const char *[]){
            "^example\\.org$", "192.0.2.12",
            NULL});

    struct Table *result = table_lookup(&tables, "baz");
    assert(result != NULL);
    assert(strcmp("baz", result->name) == 0);

    result = table_lookup(&tables, "foo");
    assert(result != NULL);
    assert(strcmp("foo", result->name) == 0);

    result = table_lookup(&tables, "bar");
    assert(result != NULL);
    assert(strcmp("bar", result->name) == 0);

    result = table_lookup(&tables, "qvf");
    assert(result == NULL);

    assert(count_tables(&tables) == 3);

    free_tables(&tables);
}

static void
test_tables_reload() {
    struct Table_head existing = SLIST_HEAD_INITIALIZER();
    struct Table_head new = SLIST_HEAD_INITIALIZER();
    struct Table *table = NULL;

    add_new_table(&existing, "foo", (const char *[]){
            "^example\\.com$", "192.0.2.10",
            "^.*$", "*",
            NULL});
    add_new_table(&existing, "bar", (const char *[]){
            "^example\\.net$", "192.0.2.11",
            NULL});

    add_new_table(&new, "baz", (const char *[]){
            "^example\\.org$", "192.0.2.12",
            NULL});
    add_new_table(&new, "foo", (const char *[]){
            "^.*$", "*",
            NULL});

    struct Table *bar = table_lookup(&existing, "bar");
    assert(bar != NULL);
    table_ref_get(bar);

    reload_tables(&existing, &new);

    assert(count_tables(&new) == 0);
    free_tables(&new);

    assert(count_tables(&existing) == 2);

    table = table_lookup(&existing, "baz");
    assert(table != NULL);
    assert(strcmp("baz", table->name) == 0);

    table = table_lookup(&existing, "foo");
    assert(table != NULL);
    assert(strcmp("foo", table->name) == 0);

    free_tables(&existing);
    table_ref_put(bar);
}