1 /*
2  * Tests exercising the ldb match operations.
3  *
4  *
5  * Copyright (C) Catalyst.NET Ltd 2017
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 /*
23  * from cmocka.c:
24  * These headers or their equivalents should be included prior to
25  * including
26  * this header file.
27  *
28  * #include <stdarg.h>
29  * #include <stddef.h>
30  * #include <setjmp.h>
31  *
32  * This allows test applications to use custom definitions of C standard
33  * library functions and types.
34  */
35 #include <stdarg.h>
36 #include <stddef.h>
37 #include <stdint.h>
38 #include <setjmp.h>
39 #include <cmocka.h>
40 
41 #include "../common/ldb_match.c"
42 
43 #include "../include/ldb.h"
44 
45 struct ldbtest_ctx {
46 	struct tevent_context *ev;
47 	struct ldb_context *ldb;
48 };
49 
ldb_test_canonicalise(struct ldb_context * ldb,void * mem_ctx,const struct ldb_val * in,struct ldb_val * out)50 static int ldb_test_canonicalise(
51 	struct ldb_context *ldb,
52 	void *mem_ctx,
53 	const struct ldb_val *in,
54 	struct ldb_val *out)
55 {
56 	out->length = in->length;
57 	out->data = in->data;
58 	return 0;
59 }
60 
setup(void ** state)61 static int setup(void **state)
62 {
63 	struct ldbtest_ctx *test_ctx;
64 	struct ldb_schema_syntax *syntax = NULL;
65 	int ret;
66 
67 	test_ctx = talloc_zero(NULL, struct ldbtest_ctx);
68 	assert_non_null(test_ctx);
69 
70 	test_ctx->ev = tevent_context_init(test_ctx);
71 	assert_non_null(test_ctx->ev);
72 
73 	test_ctx->ldb = ldb_init(test_ctx, test_ctx->ev);
74 	assert_non_null(test_ctx->ldb);
75 
76 	syntax = talloc_zero(test_ctx, struct ldb_schema_syntax);
77 	assert_non_null(syntax);
78 	syntax->canonicalise_fn = ldb_test_canonicalise;
79 
80 	ret = ldb_schema_attribute_add_with_syntax(
81 	    test_ctx->ldb, "a", LDB_ATTR_FLAG_FIXED, syntax);
82 	assert_int_equal(LDB_SUCCESS, ret);
83 
84 	*state = test_ctx;
85 	return 0;
86 }
87 
teardown(void ** state)88 static int teardown(void **state)
89 {
90 	talloc_free(*state);
91 	return 0;
92 }
93 
94 
95 /*
96  * The wild card pattern "attribute=*" is parsed as an LDB_OP_PRESENT operation
97  * rather than a LDB_OP_????
98  *
99  * This test serves to document that behaviour, and to confirm that
100  * ldb_wildcard_compare handles this case appropriately.
101  */
test_wildcard_match_star(void ** state)102 static void test_wildcard_match_star(void **state)
103 {
104 	struct ldbtest_ctx *ctx = *state;
105 	bool matched = false;
106 	int ret;
107 
108 	uint8_t value[] = "The value.......end";
109 	struct ldb_val val = {
110 		.data   = value,
111 		.length = (sizeof(value))
112 	};
113 	struct ldb_parse_tree *tree = ldb_parse_tree(ctx, "a=*");
114 	assert_non_null(tree);
115 
116 	ret = ldb_wildcard_compare(ctx->ldb, tree, val, &matched);
117 	assert_false(matched);
118 	assert_int_equal(LDB_ERR_INAPPROPRIATE_MATCHING, ret);
119 }
120 
121 /*
122  * Test basic wild card matching
123  *
124  */
test_wildcard_match(void ** state)125 static void test_wildcard_match(void **state)
126 {
127 	struct ldbtest_ctx *ctx = *state;
128 	bool matched = false;
129 
130 	uint8_t value[] = "The value.......end";
131 	struct ldb_val val = {
132 		.data   = value,
133 		.length = (sizeof(value))
134 	};
135 	struct ldb_parse_tree *tree = ldb_parse_tree(ctx, "objectClass=*end");
136 	assert_non_null(tree);
137 
138 	ldb_wildcard_compare(ctx->ldb, tree, val, &matched);
139 	assert_true(matched);
140 }
141 
142 
143 /*
144  * ldb_handler_copy and ldb_val_dup over allocate by one and add a trailing '\0'
145  * to the data, to make them safe to use the C string functions on.
146  *
147  * However testing for the trailing '\0' is not the correct way to test for
148  * the end of a value, the length should be checked instead.
149  */
test_wildcard_match_end_condition(void ** state)150 static void test_wildcard_match_end_condition(void **state)
151 {
152 	struct ldbtest_ctx *ctx = *state;
153 	bool matched = false;
154 
155 	uint8_t value[] = "hellomynameisbobx";
156 	struct ldb_val val = {
157 		.data   = talloc_memdup(NULL, value, sizeof(value)),
158 		.length = (sizeof(value) - 2)
159 	};
160 	struct ldb_parse_tree *tree = ldb_parse_tree(ctx, "a=*hello*mynameis*bob");
161 	assert_non_null(tree);
162 
163 	ldb_wildcard_compare(ctx->ldb, tree, val, &matched);
164 	assert_true(matched);
165 }
166 
167 /*
168  * Note: to run under valgrind use:
169  *       valgrind \
170  *           --suppressions=lib/ldb/tests/ldb_match_test.valgrind \
171  *           bin/ldb_match_test
172  */
main(int argc,const char ** argv)173 int main(int argc, const char **argv)
174 {
175 	const struct CMUnitTest tests[] = {
176 		cmocka_unit_test_setup_teardown(
177 			test_wildcard_match_star,
178 			setup,
179 			teardown),
180 		cmocka_unit_test_setup_teardown(
181 			test_wildcard_match,
182 			setup,
183 			teardown),
184 		cmocka_unit_test_setup_teardown(
185 			test_wildcard_match_end_condition,
186 			setup,
187 			teardown),
188 	};
189 
190 	return cmocka_run_group_tests(tests, NULL, NULL);
191 }
192