| 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 |
#include "tests.h" |
| 28 |
|
| 29 |
static pool *p = NULL; |
| 30 |
|
| 31 |
static void set_up(void) { |
| 32 |
if (p == NULL) { |
| 33 |
p = make_sub_pool(NULL); |
| 34 |
} |
| 35 |
|
| 36 |
init_config(); |
| 37 |
|
| 38 |
if (getenv("TEST_VERBOSE") != NULL) { |
| 39 |
pr_trace_use_stderr(TRUE); |
| 40 |
pr_trace_set_levels("filter", 0, 20); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
static void tear_down(void) { |
| 45 |
if (getenv("TEST_VERBOSE") != NULL) { |
| 46 |
pr_trace_use_stderr(FALSE); |
| 47 |
} |
| 48 |
|
| 49 |
if (p) { |
| 50 |
destroy_pool(p); |
| 51 |
p = NULL; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
START_TEST (filter_parse_flags_test) { |
| 56 |
const char *flags_str = NULL; |
| 57 |
int res; |
| 58 |
|
| 59 |
res = pr_filter_parse_flags(NULL, NULL); |
| 60 |
ck_assert_msg(res < 0, "Failed to handle null arguments"); |
| 61 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 62 |
|
| 63 |
res = pr_filter_parse_flags(p, NULL); |
| 64 |
ck_assert_msg(res < 0, "Failed to handle null arguments"); |
| 65 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 66 |
|
| 67 |
res = pr_filter_parse_flags(NULL, flags_str); |
| 68 |
ck_assert_msg(res < 0, "Failed to handle null arguments"); |
| 69 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 70 |
|
| 71 |
flags_str = "foo"; |
| 72 |
res = pr_filter_parse_flags(p, flags_str); |
| 73 |
ck_assert_msg(res < 0, "Failed to handle badly formatted flags '%s'", |
| 74 |
flags_str); |
| 75 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 76 |
|
| 77 |
flags_str = "[foo]"; |
| 78 |
res = pr_filter_parse_flags(p, flags_str); |
| 79 |
ck_assert_msg(res == 0, "Expected %d, got %d", 0, res); |
| 80 |
|
| 81 |
flags_str = "[NC]"; |
| 82 |
res = pr_filter_parse_flags(p, flags_str); |
| 83 |
ck_assert_msg(res == REG_ICASE, "Expected REG_ICASE (%d), got %d", REG_ICASE, |
| 84 |
res); |
| 85 |
|
| 86 |
flags_str = "[nocase]"; |
| 87 |
res = pr_filter_parse_flags(p, flags_str); |
| 88 |
ck_assert_msg(res == REG_ICASE, "Expected REG_ICASE (%d), got %d", REG_ICASE, |
| 89 |
res); |
| 90 |
} |
| 91 |
END_TEST |
| 92 |
|
| 93 |
START_TEST (filter_allow_path_test) { |
| 94 |
int res; |
| 95 |
config_rec *c; |
| 96 |
pr_regex_t *allow_pre, *deny_pre; |
| 97 |
xaset_t *set = NULL; |
| 98 |
const char *path = NULL; |
| 99 |
|
| 100 |
res = pr_filter_allow_path(NULL, NULL); |
| 101 |
ck_assert_msg(res < 0, "Failed to handle null arguments"); |
| 102 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL"); |
| 103 |
|
| 104 |
mark_point(); |
| 105 |
c = add_config_param_set(&set, "test", 1, "test"); |
| 106 |
ck_assert_msg(c != NULL, "Failed to add config param: %s", strerror(errno)); |
| 107 |
|
| 108 |
path = "/foo/bar"; |
| 109 |
res = pr_filter_allow_path(set, path); |
| 110 |
ck_assert_msg(res == 0, "Failed to allow path '%s' with no configured filters", |
| 111 |
path); |
| 112 |
|
| 113 |
|
| 114 |
deny_pre = pr_regexp_alloc(NULL); |
| 115 |
res = pr_regexp_compile(deny_pre, "/bar$", 0); |
| 116 |
ck_assert_msg(res == 0, "Error compiling deny filter"); |
| 117 |
|
| 118 |
c = add_config_param_set(&set, "PathDenyFilter", 1, deny_pre); |
| 119 |
ck_assert_msg(c != NULL, "Failed to add config param: %s", strerror(errno)); |
| 120 |
|
| 121 |
mark_point(); |
| 122 |
res = pr_filter_allow_path(set, path); |
| 123 |
ck_assert_msg(res == PR_FILTER_ERR_FAILS_DENY_FILTER, |
| 124 |
"Failed to reject path '%s' with matching PathDenyFilter", path); |
| 125 |
|
| 126 |
mark_point(); |
| 127 |
path = "/foo/baz"; |
| 128 |
res = pr_filter_allow_path(set, path); |
| 129 |
ck_assert_msg(res == 0, |
| 130 |
"Failed to allow path '%s' with non-matching PathDenyFilter", path); |
| 131 |
pr_regexp_free(NULL, deny_pre); |
| 132 |
|
| 133 |
|
| 134 |
allow_pre = pr_regexp_alloc(NULL); |
| 135 |
res = pr_regexp_compile(allow_pre, "/baz$", 0); |
| 136 |
ck_assert_msg(res == 0, "Error compiling allow filter"); |
| 137 |
|
| 138 |
c = add_config_param_set(&set, "PathAllowFilter", 1, allow_pre); |
| 139 |
ck_assert_msg(c != NULL, "Failed to add config param: %s", strerror(errno)); |
| 140 |
|
| 141 |
mark_point(); |
| 142 |
path = "/foo/quxx"; |
| 143 |
res = pr_filter_allow_path(set, path); |
| 144 |
ck_assert_msg(res == PR_FILTER_ERR_FAILS_ALLOW_FILTER, |
| 145 |
"Failed to allow path '%s' with matching PathAllowFilter", path); |
| 146 |
pr_regexp_free(NULL, allow_pre); |
| 147 |
} |
| 148 |
END_TEST |
| 149 |
|
| 150 |
Suite *tests_get_filter_suite(void) { |
| 151 |
Suite *suite; |
| 152 |
TCase *testcase; |
| 153 |
|
| 154 |
suite = suite_create("filter"); |
| 155 |
|
| 156 |
testcase = tcase_create("base"); |
| 157 |
|
| 158 |
tcase_add_checked_fixture(testcase, set_up, tear_down); |
| 159 |
|
| 160 |
tcase_add_test(testcase, filter_parse_flags_test); |
| 161 |
tcase_add_test(testcase, filter_allow_path_test); |
| 162 |
|
| 163 |
suite_add_tcase(suite, testcase); |
| 164 |
|
| 165 |
return suite; |
| 166 |
} |