summaryrefslogtreecommitdiffstats
path: root/lib/ldb/tests/ldb_parse_test.c
blob: b08c7b7680ca9d3e8362d742dd8260db2a0cc2a9 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Tests exercising the ldb parse operations.
 *
 * Copyright (C) Catalyst.NET Ltd 2017
 * Copyright (C) Michael Hanselmann 2019
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>

#include "../include/ldb.h"

struct test_ctx { uint8_t dummy; };

static int setup(void **state)
{
	struct test_ctx *ctx;

	ctx = talloc_zero(NULL, struct test_ctx);
	assert_non_null(ctx);

	*state = ctx;

	return 0;
}

static int teardown(void **state)
{
	struct test_ctx *ctx =
		talloc_get_type_abort(*state, struct test_ctx);

	talloc_free(ctx);

	return 0;
}

static void test_roundtrip(TALLOC_CTX *mem_ctx, const char *filter, const char *expected)
{
	struct ldb_parse_tree *tree;
	char *serialized;

	assert_non_null(filter);
	assert_non_null(expected);

	tree = ldb_parse_tree(mem_ctx, filter);
	assert_non_null(tree);

	serialized = ldb_filter_from_tree(mem_ctx, tree);
	assert_non_null(serialized);

	assert_string_equal(serialized, expected);
}

static void test_parse_filtertype(void **state)
{
	struct test_ctx *ctx =
		talloc_get_type_abort(*state, struct test_ctx);

	test_roundtrip(ctx, "", "(|(objectClass=*)(distinguishedName=*))");
	test_roundtrip(ctx, "a=value", "(a=value)");
	test_roundtrip(ctx, "(|(foo=bar)(baz=hello))", "(|(foo=bar)(baz=hello))");
	test_roundtrip(ctx, " ", "(|(objectClass=*)(distinguishedName=*))");
}

/*
 * Test that a nested query with 128 levels of nesting is accepted
 */
static void test_nested_filter_eq_limit(void **state)
{
	struct test_ctx *ctx =
		talloc_get_type_abort(*state, struct test_ctx);

	/*
	 * 128 nested clauses
	 */
	const char *nested_query = ""
		"(|(!(|(&(|(|(|(|(|(|(|(|(|(|(|(|"
		"(|(!(|(&(|(|(|(|(|(|(!(|(!(|(|(|"
		"(|(!(|(&(|(|(&(|(|(|(|(|(!(!(!(|"
		"(|(!(|(&(|(|(|(|(|(|(|(|(|(|(|(|"
		"(|(!(|(&(|(|(|(!(|(|(&(|(|(|(|(|"
		"(|(!(|(&(|(|(&(|(|(|(|(|(&(&(|(|"
		"(|(!(|(&(|(|(|(|(|(|(!(|(|(|(|(|"
		"(|(!(|(&(|(|(!(|(|(|(|(|(|(|(|(|"
		"(a=b)"
		"))))))))))))))))"
		"))))))))))))))))"
		"))))))))))))))))"
		"))))))))))))))))"
		"))))))))))))))))"
		"))))))))))))))))"
		"))))))))))))))))"
		"))))))))))))))))";

	struct ldb_parse_tree *tree = ldb_parse_tree(ctx, nested_query);

	assert_non_null(tree);
	/*
	 * Check that we get the same query back
	 */
	test_roundtrip(ctx, nested_query, nested_query);
}

/*
 * Test that a nested query with 129 levels of nesting is rejected.
 */
static void test_nested_filter_gt_limit(void **state)
{
	struct test_ctx *ctx =
		talloc_get_type_abort(*state, struct test_ctx);

	/*
	 * 129 nested clauses
	 */
	const char *nested_query = ""
		"(|(!(|(|(&(|(|(|(|(&(|(|(|(|(|(|"
		"(|(!(|(|(&(|(|(|(|(|(|(|(|(|(|(|"
		"(|(!(|(|(&(|(|(!(|(|(|(|(!(|(|(|"
		"(|(!(|(|(&(|(|(|(|(|(|(|(|(|(|(|"
		"(|(!(|(|(&(|(|(|(!(&(|(|(|(|(|(|"
		"(|(!(|(|(&(|(|(|(|(|(|(|(|(|(|(|"
		"(|(!(|(|(&(|(|(|(|(|(|(|(|(|(|(|"
		"(|(!(|(|(&(|(|(|(|(|(|(|(|(&(|(|"
		"(|"
		"(a=b)"
		")"
		"))))))))))))))))"
		"))))))))))))))))"
		"))))))))))))))))"
		"))))))))))))))))"
		"))))))))))))))))"
		"))))))))))))))))"
		"))))))))))))))))"
		"))))))))))))))))";

	struct ldb_parse_tree *tree = ldb_parse_tree(ctx, nested_query);

	assert_null(tree);
}

int main(int argc, const char **argv)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test_setup_teardown(
			test_parse_filtertype, setup, teardown),
		cmocka_unit_test_setup_teardown(
			test_nested_filter_eq_limit, setup, teardown),
		cmocka_unit_test_setup_teardown(
			test_nested_filter_gt_limit, setup, teardown),
	};

	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);

	return cmocka_run_group_tests(tests, NULL, NULL);
}