2 * Tests exercising the ldb parse operations.
4 * Copyright (C) Catalyst.NET Ltd 2017
5 * Copyright (C) Michael Hanselmann 2019
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.
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.
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/>.
28 #include "../include/ldb.h"
30 struct test_ctx
{ uint8_t dummy
; };
32 static int setup(void **state
)
36 ctx
= talloc_zero(NULL
, struct test_ctx
);
44 static int teardown(void **state
)
46 struct test_ctx
*ctx
=
47 talloc_get_type_abort(*state
, struct test_ctx
);
54 static void test_roundtrip(TALLOC_CTX
*mem_ctx
, const char *filter
, const char *expected
)
56 struct ldb_parse_tree
*tree
;
59 assert_non_null(filter
);
60 assert_non_null(expected
);
62 tree
= ldb_parse_tree(mem_ctx
, filter
);
63 assert_non_null(tree
);
65 serialized
= ldb_filter_from_tree(mem_ctx
, tree
);
66 assert_non_null(serialized
);
68 assert_string_equal(serialized
, expected
);
71 static void test_parse_filtertype(void **state
)
73 struct test_ctx
*ctx
=
74 talloc_get_type_abort(*state
, struct test_ctx
);
76 test_roundtrip(ctx
, "", "(|(objectClass=*)(distinguishedName=*))");
77 test_roundtrip(ctx
, "a=value", "(a=value)");
78 test_roundtrip(ctx
, "(|(foo=bar)(baz=hello))", "(|(foo=bar)(baz=hello))");
79 test_roundtrip(ctx
, " ", "(|(objectClass=*)(distinguishedName=*))");
83 * Test that a nested query with 128 levels of nesting is accepted
85 static void test_nested_filter_eq_limit(void **state
)
87 struct test_ctx
*ctx
=
88 talloc_get_type_abort(*state
, struct test_ctx
);
93 const char *nested_query
= ""
94 "(|(!(|(&(|(|(|(|(|(|(|(|(|(|(|(|"
95 "(|(!(|(&(|(|(|(|(|(|(!(|(!(|(|(|"
96 "(|(!(|(&(|(|(&(|(|(|(|(|(!(!(!(|"
97 "(|(!(|(&(|(|(|(|(|(|(|(|(|(|(|(|"
98 "(|(!(|(&(|(|(|(!(|(|(&(|(|(|(|(|"
99 "(|(!(|(&(|(|(&(|(|(|(|(|(&(&(|(|"
100 "(|(!(|(&(|(|(|(|(|(|(!(|(|(|(|(|"
101 "(|(!(|(&(|(|(!(|(|(|(|(|(|(|(|(|"
112 struct ldb_parse_tree
*tree
= ldb_parse_tree(ctx
, nested_query
);
114 assert_non_null(tree
);
116 * Check that we get the same query back
118 test_roundtrip(ctx
, nested_query
, nested_query
);
122 * Test that a nested query with 129 levels of nesting is rejected.
124 static void test_nested_filter_gt_limit(void **state
)
126 struct test_ctx
*ctx
=
127 talloc_get_type_abort(*state
, struct test_ctx
);
132 const char *nested_query
= ""
133 "(|(!(|(|(&(|(|(|(|(&(|(|(|(|(|(|"
134 "(|(!(|(|(&(|(|(|(|(|(|(|(|(|(|(|"
135 "(|(!(|(|(&(|(|(!(|(|(|(|(!(|(|(|"
136 "(|(!(|(|(&(|(|(|(|(|(|(|(|(|(|(|"
137 "(|(!(|(|(&(|(|(|(!(&(|(|(|(|(|(|"
138 "(|(!(|(|(&(|(|(|(|(|(|(|(|(|(|(|"
139 "(|(!(|(|(&(|(|(|(|(|(|(|(|(|(|(|"
140 "(|(!(|(|(&(|(|(|(|(|(|(|(|(&(|(|"
153 struct ldb_parse_tree
*tree
= ldb_parse_tree(ctx
, nested_query
);
158 int main(int argc
, const char **argv
)
160 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
),
169 cmocka_set_message_output(CM_OUTPUT_SUBUNIT
);
171 return cmocka_run_group_tests(tests
, NULL
, NULL
);