summaryrefslogtreecommitdiff
path: root/libcli/ldap/tests/ldap_message_test.c
blob: c5aacd4bc6b1296fc3f5fb4e116515e0d77663e3 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Unit tests for ldap_message.
 *
 *  Copyright (C) Catalyst.NET Ltd 2020
 *
 * 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/>.
 *
 */

/*
 * from cmocka.c:
 * These headers or their equivalents should be included prior to
 * including
 * this header file.
 *
 * #include <stdarg.h>
 * #include <stddef.h>
 * #include <setjmp.h>
 *
 * This allows test applications to use custom definitions of C standard
 * library functions and types.
 *
 */
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include "lib/util/attr.h"
#include "includes.h"
#include "lib/util/asn1.h"
#include "libcli/ldap/ldap_message.h"
#include "libcli/ldap/ldap_proto.h"

/*
 * declare the internal cmocka cm_print so we can output messages in
 * sub unit format
 */
void cm_print_error(const char * const format, ...);
/*
 * helper function and macro to compare an ldap error code constant with the
 * coresponding nt_status code
 */
#define NT_STATUS_LDAP_V(code) (0xF2000000 | code)
static void _assert_ldap_status_equal(
	int a,
	NTSTATUS b,
	const char * const file,
	const int line)
{
	_assert_int_equal(NT_STATUS_LDAP_V(a), NT_STATUS_V(b), file, line);
}

#define assert_ldap_status_equal(a, b) \
	_assert_ldap_status_equal((a), (b), __FILE__, __LINE__)

/*
 * helper function and macro to assert there were no errors in the last
 * file operation
 */
static void _assert_not_ferror(
	FILE *f,
	const char * const file,
	const int line)
{
	if (f == NULL || ferror(f)) {
		cm_print_error("ferror (%d) %s\n", errno, strerror(errno));
		_fail(file, line);
	}
}

#define assert_not_ferror(f) \
	_assert_not_ferror((f), __FILE__, __LINE__)

struct test_ctx {
};

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

	test_ctx = talloc_zero(NULL, struct test_ctx);
	*state = test_ctx;
	return 0;
}

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

	TALLOC_FREE(test_ctx);
	return 0;
}

/*
 * Test that an empty request is handled correctly
 */
static void test_empty_input(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(
		*state,
		struct test_ctx);
	struct asn1_data *asn1;
	struct ldap_message *ldap_msg;
	NTSTATUS status;
	uint8_t buf[0];
	size_t len = 0;
	struct ldap_request_limits limits = {
		.max_search_size = 256000,
	};


	asn1 = asn1_init(test_ctx, ASN1_MAX_TREE_DEPTH);
	assert_non_null(asn1);

	asn1_load_nocopy(asn1, buf, len);

	ldap_msg = talloc(test_ctx, struct ldap_message);
	assert_non_null(ldap_msg);

	status = ldap_decode(
		asn1, &limits, samba_ldap_control_handlers(), ldap_msg);
	assert_ldap_status_equal(LDAP_PROTOCOL_ERROR, status);
}

/*
 * Check that a request is rejected it it's recursion depth exceeds
 * the maximum value specified. This test uses a very deeply nested query,
 * 10,000 or clauses.
 *
 */
static void test_recursion_depth_large(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(
		*state,
		struct test_ctx);
	struct asn1_data *asn1;
	struct ldap_message *ldap_msg;
	NTSTATUS status;
	FILE *f = NULL;
	uint8_t *buffer = NULL;
	const size_t BUFF_SIZE = 1048576;
	size_t len;
	struct ldap_request_limits limits = {
		.max_search_size = 256000,
	};


	/*
	 * Load a test data file containg 10,000 or clauses in encoded as
	 * an ASN.1 packet.
	 */
	buffer = talloc_zero_array(test_ctx, uint8_t, BUFF_SIZE);
	f = fopen("./libcli/ldap/tests/data/10000-or.dat", "r");
	assert_not_ferror(f);
	len = fread(buffer, sizeof(uint8_t), BUFF_SIZE, f);
	assert_not_ferror(f);
	assert_true(len > 0);

	asn1 = asn1_init(test_ctx, ASN1_MAX_TREE_DEPTH);
	assert_non_null(asn1);
	asn1_load_nocopy(asn1, buffer, len);

	ldap_msg = talloc(test_ctx, struct ldap_message);
	assert_non_null(ldap_msg);

	status = ldap_decode(
		asn1, &limits, samba_ldap_control_handlers(), ldap_msg);
	assert_ldap_status_equal(LDAP_PROTOCOL_ERROR, status);
}

/*
 * Check that a request is not rejected it it's recursion depth equals the
 * maximum value
 */
static void test_recursion_depth_equals_max(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(
		*state,
		struct test_ctx);
	struct asn1_data *asn1;
	struct ldap_message *ldap_msg;
	NTSTATUS status;
	FILE *f = NULL;
	uint8_t *buffer = NULL;
	const size_t BUFF_SIZE = 1048576;
	size_t len;
	struct ldap_request_limits limits = {
		.max_search_size = 256000,
	};


	buffer = talloc_zero_array(test_ctx, uint8_t, BUFF_SIZE);
	f = fopen("./libcli/ldap/tests/data/ldap-recursive.dat", "r");
	assert_not_ferror(f);
	len = fread(buffer, sizeof(uint8_t), BUFF_SIZE, f);
	assert_not_ferror(f);
	assert_true(len > 0);

	asn1 = asn1_init(test_ctx, 4);
	assert_non_null(asn1);
	asn1_load_nocopy(asn1, buffer, len);

	ldap_msg = talloc(test_ctx, struct ldap_message);
	assert_non_null(ldap_msg);

	status = ldap_decode(
		asn1, &limits, samba_ldap_control_handlers(), ldap_msg);
	assert_true(NT_STATUS_IS_OK(status));
}

/*
 * Check that a request is rejected it it's recursion depth is greater than the
 * maximum value
 */
static void test_recursion_depth_greater_than_max(void **state)
{
	struct test_ctx *test_ctx = talloc_get_type_abort(
		*state,
		struct test_ctx);
	struct asn1_data *asn1;
	struct ldap_message *ldap_msg;
	NTSTATUS status;
	FILE *f = NULL;
	uint8_t *buffer = NULL;
	const size_t BUFF_SIZE = 1048576;
	size_t len;
	struct ldap_request_limits limits = {
		.max_search_size = 256000,
	};


	buffer = talloc_zero_array(test_ctx, uint8_t, BUFF_SIZE);
	f = fopen("./libcli/ldap/tests/data/ldap-recursive.dat", "r");
	assert_not_ferror(f);
	len = fread(buffer, sizeof(uint8_t), BUFF_SIZE, f);
	assert_not_ferror(f);
	assert_true(len > 0);

	asn1 = asn1_init(test_ctx, 3);
	assert_non_null(asn1);
	asn1_load_nocopy(asn1, buffer, len);

	ldap_msg = talloc(test_ctx, struct ldap_message);
	assert_non_null(ldap_msg);

	status = ldap_decode(
		asn1, &limits, samba_ldap_control_handlers(), ldap_msg);
	assert_ldap_status_equal(LDAP_PROTOCOL_ERROR, status);
}

int main(_UNUSED_ int argc, _UNUSED_ const char **argv)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test_setup_teardown(
			test_empty_input,
			setup,
			teardown),
		cmocka_unit_test_setup_teardown(
			test_recursion_depth_large,
			setup,
			teardown),
		cmocka_unit_test_setup_teardown(
			test_recursion_depth_equals_max,
			setup,
			teardown),
		cmocka_unit_test_setup_teardown(
			test_recursion_depth_greater_than_max,
			setup,
			teardown),
	};

	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
	return cmocka_run_group_tests(tests, NULL, NULL);
}