smbd: Ensure we grant owner sid in check_parent_access_fsp()
[samba.git] / lib / ldb / tests / ldb_parse_test.c
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 static int setup(void **state)
33 {
34         struct test_ctx *ctx;
35
36         ctx = talloc_zero(NULL, struct test_ctx);
37         assert_non_null(ctx);
38
39         *state = ctx;
40
41         return 0;
42 }
43
44 static int teardown(void **state)
45 {
46         struct test_ctx *ctx =
47                 talloc_get_type_abort(*state, struct test_ctx);
48
49         talloc_free(ctx);
50
51         return 0;
52 }
53
54 static void test_roundtrip(TALLOC_CTX *mem_ctx, const char *filter, const char *expected)
55 {
56         struct ldb_parse_tree *tree;
57         char *serialized;
58
59         assert_non_null(filter);
60         assert_non_null(expected);
61
62         tree = ldb_parse_tree(mem_ctx, filter);
63         assert_non_null(tree);
64
65         serialized = ldb_filter_from_tree(mem_ctx, tree);
66         assert_non_null(serialized);
67
68         assert_string_equal(serialized, expected);
69 }
70
71 static void test_parse_filtertype(void **state)
72 {
73         struct test_ctx *ctx =
74                 talloc_get_type_abort(*state, struct test_ctx);
75
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=*))");
80 }
81
82 /*
83  * Test that a nested query with 128 levels of nesting is accepted
84  */
85 static void test_nested_filter_eq_limit(void **state)
86 {
87         struct test_ctx *ctx =
88                 talloc_get_type_abort(*state, struct test_ctx);
89
90         /*
91          * 128 nested clauses
92          */
93         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         struct ldb_parse_tree *tree = ldb_parse_tree(ctx, nested_query);
113
114         assert_non_null(tree);
115         /*
116          * Check that we get the same query back
117          */
118         test_roundtrip(ctx, nested_query, nested_query);
119 }
120
121 /*
122  * Test that a nested query with 129 levels of nesting is rejected.
123  */
124 static void test_nested_filter_gt_limit(void **state)
125 {
126         struct test_ctx *ctx =
127                 talloc_get_type_abort(*state, struct test_ctx);
128
129         /*
130          * 129 nested clauses
131          */
132         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         struct ldb_parse_tree *tree = ldb_parse_tree(ctx, nested_query);
154
155         assert_null(tree);
156 }
157
158 int main(int argc, const char **argv)
159 {
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),
167         };
168
169         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
170
171         return cmocka_run_group_tests(tests, NULL, NULL);
172 }