Line data Source code
1 : /*
2 : * Tests exercising the ldb parse operations.
3 : *
4 : * Copyright (C) Catalyst.NET Ltd 2017
5 : * Copyright (C) Michael Hanselmann 2019
6 : *
7 : * This program is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation; either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * This program is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License
18 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : *
20 : */
21 :
22 : #include <stdarg.h>
23 : #include <stddef.h>
24 : #include <stdint.h>
25 : #include <setjmp.h>
26 : #include <cmocka.h>
27 :
28 : #include "../include/ldb.h"
29 :
30 : struct test_ctx { uint8_t dummy; };
31 :
32 3 : static int setup(void **state)
33 : {
34 3 : struct test_ctx *ctx;
35 :
36 3 : ctx = talloc_zero(NULL, struct test_ctx);
37 3 : assert_non_null(ctx);
38 :
39 3 : *state = ctx;
40 :
41 3 : return 0;
42 : }
43 :
44 3 : static int teardown(void **state)
45 : {
46 3 : struct test_ctx *ctx =
47 3 : talloc_get_type_abort(*state, struct test_ctx);
48 :
49 3 : talloc_free(ctx);
50 :
51 3 : return 0;
52 : }
53 :
54 5 : static void test_roundtrip(TALLOC_CTX *mem_ctx, const char *filter, const char *expected)
55 : {
56 5 : struct ldb_parse_tree *tree;
57 5 : char *serialized;
58 :
59 5 : assert_non_null(filter);
60 5 : assert_non_null(expected);
61 :
62 5 : tree = ldb_parse_tree(mem_ctx, filter);
63 5 : assert_non_null(tree);
64 :
65 5 : serialized = ldb_filter_from_tree(mem_ctx, tree);
66 5 : assert_non_null(serialized);
67 :
68 5 : assert_string_equal(serialized, expected);
69 5 : }
70 :
71 1 : static void test_parse_filtertype(void **state)
72 : {
73 1 : struct test_ctx *ctx =
74 1 : talloc_get_type_abort(*state, struct test_ctx);
75 :
76 1 : test_roundtrip(ctx, "", "(|(objectClass=*)(distinguishedName=*))");
77 1 : test_roundtrip(ctx, "a=value", "(a=value)");
78 1 : test_roundtrip(ctx, "(|(foo=bar)(baz=hello))", "(|(foo=bar)(baz=hello))");
79 1 : test_roundtrip(ctx, " ", "(|(objectClass=*)(distinguishedName=*))");
80 1 : }
81 :
82 : /*
83 : * Test that a nested query with 128 levels of nesting is accepted
84 : */
85 1 : static void test_nested_filter_eq_limit(void **state)
86 : {
87 1 : struct test_ctx *ctx =
88 1 : talloc_get_type_abort(*state, struct test_ctx);
89 :
90 : /*
91 : * 128 nested clauses
92 : */
93 1 : const char *nested_query = ""
94 : "(|(!(|(&(|(|(|(|(|(|(|(|(|(|(|(|"
95 : "(|(!(|(&(|(|(|(|(|(|(!(|(!(|(|(|"
96 : "(|(!(|(&(|(|(&(|(|(|(|(|(!(!(!(|"
97 : "(|(!(|(&(|(|(|(|(|(|(|(|(|(|(|(|"
98 : "(|(!(|(&(|(|(|(!(|(|(&(|(|(|(|(|"
99 : "(|(!(|(&(|(|(&(|(|(|(|(|(&(&(|(|"
100 : "(|(!(|(&(|(|(|(|(|(|(!(|(|(|(|(|"
101 : "(|(!(|(&(|(|(!(|(|(|(|(|(|(|(|(|"
102 : "(a=b)"
103 : "))))))))))))))))"
104 : "))))))))))))))))"
105 : "))))))))))))))))"
106 : "))))))))))))))))"
107 : "))))))))))))))))"
108 : "))))))))))))))))"
109 : "))))))))))))))))"
110 : "))))))))))))))))";
111 :
112 1 : struct ldb_parse_tree *tree = ldb_parse_tree(ctx, nested_query);
113 :
114 1 : assert_non_null(tree);
115 : /*
116 : * Check that we get the same query back
117 : */
118 1 : test_roundtrip(ctx, nested_query, nested_query);
119 1 : }
120 :
121 : /*
122 : * Test that a nested query with 129 levels of nesting is rejected.
123 : */
124 1 : static void test_nested_filter_gt_limit(void **state)
125 : {
126 1 : struct test_ctx *ctx =
127 1 : talloc_get_type_abort(*state, struct test_ctx);
128 :
129 : /*
130 : * 129 nested clauses
131 : */
132 1 : const char *nested_query = ""
133 : "(|(!(|(|(&(|(|(|(|(&(|(|(|(|(|(|"
134 : "(|(!(|(|(&(|(|(|(|(|(|(|(|(|(|(|"
135 : "(|(!(|(|(&(|(|(!(|(|(|(|(!(|(|(|"
136 : "(|(!(|(|(&(|(|(|(|(|(|(|(|(|(|(|"
137 : "(|(!(|(|(&(|(|(|(!(&(|(|(|(|(|(|"
138 : "(|(!(|(|(&(|(|(|(|(|(|(|(|(|(|(|"
139 : "(|(!(|(|(&(|(|(|(|(|(|(|(|(|(|(|"
140 : "(|(!(|(|(&(|(|(|(|(|(|(|(|(&(|(|"
141 : "(|"
142 : "(a=b)"
143 : ")"
144 : "))))))))))))))))"
145 : "))))))))))))))))"
146 : "))))))))))))))))"
147 : "))))))))))))))))"
148 : "))))))))))))))))"
149 : "))))))))))))))))"
150 : "))))))))))))))))"
151 : "))))))))))))))))";
152 :
153 1 : struct ldb_parse_tree *tree = ldb_parse_tree(ctx, nested_query);
154 :
155 1 : assert_null(tree);
156 1 : }
157 :
158 1 : int main(int argc, const char **argv)
159 : {
160 1 : const struct CMUnitTest tests[] = {
161 : cmocka_unit_test_setup_teardown(
162 : test_parse_filtertype, setup, teardown),
163 : cmocka_unit_test_setup_teardown(
164 : test_nested_filter_eq_limit, setup, teardown),
165 : cmocka_unit_test_setup_teardown(
166 : test_nested_filter_gt_limit, setup, teardown),
167 : };
168 :
169 1 : cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
170 :
171 1 : return cmocka_run_group_tests(tests, NULL, NULL);
172 : }
|