File: test_rule.c

package info (click to toggle)
shadowsocks-libev 3.3.6%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,192 kB
  • sloc: ansic: 15,474; sh: 3,518; python: 452; makefile: 25
file content (126 lines) | stat: -rw-r--r-- 2,720 bytes parent folder | download
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
#include <string.h>
#include <stdlib.h>

int verbose = 0;

#include "rule.h"
#include "utils.h"

static void
test_new_rule(void)
{
    rule_t *rule = new_rule();
    assert(rule != NULL);
    assert(rule->pattern == NULL);
    assert(rule->pattern_re == NULL);
    free(rule);
}

static void
test_accept_rule_arg(void)
{
    rule_t *rule = new_rule();
    assert(rule != NULL);

    int ret = accept_rule_arg(rule, "^example\\.com$");
    assert(ret == 1);
    assert(rule->pattern != NULL);
    assert(strcmp(rule->pattern, "^example\\.com$") == 0);

    /* Second call should fail - pattern already set */
    ret = accept_rule_arg(rule, "another");
    assert(ret == -1);
    (void)ret;

    free(rule->pattern);
    free(rule);
}

static void
test_init_rule(void)
{
    rule_t *rule = new_rule();
    accept_rule_arg(rule, "^test.*$");

    int ret = init_rule(rule);
    assert(ret == 1);
    (void)ret;
    assert(rule->pattern_re != NULL);

    if (rule->match_data)
        pcre2_match_data_free(rule->match_data);
    if (rule->pattern_re)
        pcre2_code_free(rule->pattern_re);
    free(rule->pattern);
    free(rule);
}

static void
test_init_rule_invalid(void)
{
    rule_t *rule = new_rule();
    accept_rule_arg(rule, "[invalid");  /* Unclosed bracket */

    int ret = init_rule(rule);
    assert(ret == 0);  /* Should fail */
    (void)ret;

    free(rule->pattern);
    free(rule);
}

static void
test_lookup_rule(void)
{
    struct cork_dllist rules;
    cork_dllist_init(&rules);

    rule_t *rule1 = new_rule();
    accept_rule_arg(rule1, "^google\\.com$");
    init_rule(rule1);
    add_rule(&rules, rule1);

    rule_t *rule2 = new_rule();
    accept_rule_arg(rule2, ".*\\.example\\.com$");
    init_rule(rule2);
    add_rule(&rules, rule2);

    /* Should match rule1 */
    rule_t *found = lookup_rule(&rules, "google.com", 10);
    assert(found == rule1);

    /* Should match rule2 */
    found = lookup_rule(&rules, "sub.example.com", 15);
    assert(found == rule2);

    /* Should not match */
    found = lookup_rule(&rules, "other.net", 9);
    assert(found == NULL);
    (void)found;

    /* Clean up */
    if (rule1->match_data) pcre2_match_data_free(rule1->match_data);
    if (rule1->pattern_re) pcre2_code_free(rule1->pattern_re);
    free(rule1->pattern);
    free(rule1);
    if (rule2->match_data) pcre2_match_data_free(rule2->match_data);
    if (rule2->pattern_re) pcre2_code_free(rule2->pattern_re);
    free(rule2->pattern);
    free(rule2);
}

int
main(void)
{
    test_new_rule();
    test_accept_rule_arg();
    test_init_rule();
    test_init_rule_invalid();
    test_lookup_rule();
    return 0;
}