ldb: Avoid read beyond buffer
[metze/samba/wip.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
31 {
32 };
33
34 static int setup(void **state)
35 {
36         struct test_ctx *ctx;
37
38         ctx = talloc_zero(NULL, struct test_ctx);
39         assert_non_null(ctx);
40
41         *state = ctx;
42
43         return 0;
44 }
45
46 static int teardown(void **state)
47 {
48         struct test_ctx *ctx =
49                 talloc_get_type_abort(*state, struct test_ctx);
50
51         talloc_free(ctx);
52
53         return 0;
54 }
55
56 static void test_roundtrip(TALLOC_CTX *mem_ctx, const char *filter, const char *expected)
57 {
58         struct ldb_parse_tree *tree;
59         char *serialized;
60
61         assert_non_null(filter);
62         assert_non_null(expected);
63
64         tree = ldb_parse_tree(mem_ctx, filter);
65         assert_non_null(tree);
66
67         serialized = ldb_filter_from_tree(mem_ctx, tree);
68         assert_non_null(serialized);
69
70         assert_string_equal(serialized, expected);
71 }
72
73 static void test_parse_filtertype(void **state)
74 {
75         struct test_ctx *ctx =
76                 talloc_get_type_abort(*state, struct test_ctx);
77
78         test_roundtrip(ctx, "", "(|(objectClass=*)(distinguishedName=*))");
79         test_roundtrip(ctx, "a=value", "(a=value)");
80         test_roundtrip(ctx, "(|(foo=bar)(baz=hello))", "(|(foo=bar)(baz=hello))");
81         test_roundtrip(ctx, " ", "(|(objectClass=*)(distinguishedName=*))");
82 }
83
84 int main(int argc, const char **argv)
85 {
86         const struct CMUnitTest tests[] = {
87                 cmocka_unit_test_setup_teardown(test_parse_filtertype, setup, teardown),
88         };
89
90         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
91
92         return cmocka_run_group_tests(tests, NULL, NULL);
93 }