wldap32: Handle null DN or null message in ldap_add* and add tests.
[wine.git] / dlls / wldap32 / tests / parse.c
blob788af6f921c66dac837aee05342d7484cbc1ab24
1 /*
2 * test parsing functions
4 * Copyright 2008 Hans Leidekker for CodeWeavers
5 * Copyright 2020 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library 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 GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <windef.h>
25 #include <winbase.h>
26 #include <winldap.h>
27 #include <winber.h>
29 #include "wine/test.h"
31 #ifndef LDAP_AUTH_SIMPLE
32 #define LDAP_AUTH_SIMPLE 0x80
33 #endif
35 #ifndef LDAP_OPT_SERVER_CONTROLS
36 #define LDAP_OPT_SERVER_CONTROLS 0x0012
37 #endif
39 static void test_ldap_parse_sort_control( LDAP *ld )
41 ULONG ret, result;
42 LDAPSortKeyA *sortkeys[2], key;
43 LDAPControlA *sort, *ctrls[2], **server_ctrls;
44 LDAPMessage *res = NULL;
45 struct l_timeval timeout;
47 key.sk_attrtype = (char *)"ou";
48 key.sk_matchruleoid = NULL;
49 key.sk_reverseorder = FALSE;
51 sortkeys[0] = &key;
52 sortkeys[1] = NULL;
53 ret = ldap_create_sort_controlA( ld, sortkeys, 0, &sort );
54 ok( !ret, "ldap_create_sort_controlA failed %#lx\n", ret );
56 ctrls[0] = sort;
57 ctrls[1] = NULL;
58 timeout.tv_sec = 20;
59 timeout.tv_usec = 0;
60 ret = ldap_search_ext_sA( ld, (char *)"dc=debian,dc=org", LDAP_SCOPE_ONELEVEL, (char *)"(uid=*)", NULL, 0,
61 ctrls, NULL, &timeout, 10, &res );
62 if (ret == LDAP_SERVER_DOWN || ret == LDAP_TIMEOUT)
64 skip("test server can't be reached\n");
65 ldap_control_freeA( sort );
66 return;
68 ok( !ret, "ldap_search_ext_sA failed %#lx\n", ret );
69 ok( res != NULL, "expected res != NULL\n" );
71 ret = ldap_parse_resultA( NULL, NULL, NULL, NULL, NULL, NULL, &server_ctrls, 0 );
72 ok( ret == LDAP_PARAM_ERROR, "ldap_parse_resultA should fail, got %#lx\n", ret );
73 ret = ldap_parse_resultA( NULL, res, NULL, NULL, NULL, NULL, &server_ctrls, 0 );
74 ok( ret == LDAP_PARAM_ERROR, "ldap_parse_resultA should fail, got %#lx\n", ret );
75 ret = ldap_parse_resultA( ld, NULL, NULL, NULL, NULL, NULL, &server_ctrls, 0 );
76 ok( ret == LDAP_NO_RESULTS_RETURNED, "ldap_parse_resultA should fail, got %#lx\n", ret );
77 result = ~0u;
78 ret = ldap_parse_resultA( ld, res, &result, NULL, NULL, NULL, &server_ctrls, 1 );
79 ok( !ret, "ldap_parse_resultA failed %#lx\n", ret );
80 ok( !result, "got %#lx expected 0\n", result );
82 ret = ldap_parse_sort_controlA( NULL, NULL, NULL, NULL );
83 ok( ret == LDAP_PARAM_ERROR, "ldap_parse_sort_controlA failed %#lx\n", ret );
85 ret = ldap_parse_sort_controlA( ld, NULL, NULL, NULL );
86 ok( ret == LDAP_CONTROL_NOT_FOUND, "ldap_parse_sort_controlA failed %#lx\n", ret );
88 ret = ldap_parse_sort_controlA( ld, NULL, &result, NULL );
89 ok( ret == LDAP_CONTROL_NOT_FOUND, "ldap_parse_sort_controlA failed %#lx\n", ret );
91 ret = ldap_parse_sort_controlA( ld, server_ctrls, &result, NULL );
92 ok( ret == LDAP_CONTROL_NOT_FOUND, "ldap_parse_sort_controlA failed %#lx\n", ret );
94 ldap_control_freeA( sort );
95 ldap_controls_freeA( server_ctrls );
98 static void test_ldap_search_extW( LDAP *ld )
100 ULONG ret, message, timelimit;
101 WCHAR base[] = L"", filter[] = L"ou=*";
103 timelimit = 20;
104 ret = ldap_search_extW( ld, base, LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, timelimit, 0, &message );
105 if (ret == LDAP_SERVER_DOWN || ret == LDAP_UNAVAILABLE)
107 skip("test server can't be reached\n");
108 return;
110 ok( !ret, "ldap_search_extW failed %#lx\n", ret );
112 timelimit = 0;
113 ret = ldap_search_extW( ld, base, LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, timelimit, 0, &message );
114 ok( !ret, "ldap_search_extW failed %#lx\n", ret );
117 static void test_ldap_set_optionW( LDAP *ld )
119 ULONG ret, oldvalue;
121 ret = ldap_get_optionW( ld, LDAP_OPT_REFERRALS, &oldvalue );
122 if (ret == LDAP_SERVER_DOWN || ret == LDAP_UNAVAILABLE)
124 skip("test server can't be reached\n");
125 return;
128 ret = ldap_set_optionW( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
129 ok( !ret, "ldap_set_optionW failed %#lx\n", ret );
131 ret = ldap_set_optionW( ld, LDAP_OPT_REFERRALS, (void *)&oldvalue );
132 ok( !ret, "ldap_set_optionW failed %#lx\n", ret );
135 static void test_ldap_get_optionW( LDAP *ld )
137 ULONG ret, version;
139 ret = ldap_get_optionW( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
140 ok( !ret, "ldap_get_optionW failed %#lx\n", ret );
141 ok( version == LDAP_VERSION3, "got %lu\n", version );
144 static void test_ldap_bind_sA( void )
146 LDAP *ld;
147 ULONG ret;
148 int version;
150 ld = ldap_sslinitA( (char *)"db.debian.org", 636, 1 );
151 ok( ld != NULL, "ldap_sslinit failed\n" );
153 version = LDAP_VERSION3;
154 ret = ldap_set_optionW( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
155 if (ret == LDAP_SERVER_DOWN || ret == LDAP_UNAVAILABLE)
157 skip( "test server can't be reached\n" );
158 ldap_unbind( ld );
159 return;
162 ret = ldap_connect( ld, NULL );
163 ok( !ret, "ldap_connect failed %#lx\n", ret );
165 ret = ldap_bind_sA( ld, (char *)"uid=winetest,ou=users,dc=debian,dc=org", (char *)"winetest",
166 LDAP_AUTH_SIMPLE );
167 ok( ret == LDAP_INVALID_CREDENTIALS, "ldap_bind_s returned %#lx\n", ret );
168 ldap_unbind( ld );
170 ld = ldap_sslinitA( (char *)"db.debian.org", 389, 0 );
171 ok( ld != NULL, "ldap_sslinit failed\n" );
173 ret = ldap_connect( ld, NULL );
174 ok( !ret, "ldap_connect failed %#lx\n", ret );
175 ldap_unbind( ld );
178 static void test_ldap_add(void)
180 char *one_empty_string[] = { (char *)"", NULL };
181 LDAPModA empty_equals_empty = { 0, (char *)"", { one_empty_string } };
182 LDAPModA *attrs[] = { &empty_equals_empty, NULL };
183 LDAP *ld;
184 ULONG ret, num;
186 ld = ldap_initA( (char *)"db.debian.org", 389 );
187 ok( ld != NULL, "ldap_init failed\n" );
189 ret = ldap_addA( NULL, NULL, NULL );
190 ok( ret == (ULONG)-1, "ldap_addA should fail, got %#lx\n", ret );
191 ret = ldap_addA( NULL, (char *)"", attrs );
192 ok( ret == (ULONG)-1, "ldap_addA should fail, got %#lx\n", ret );
193 ret = ldap_addA( ld, NULL, attrs );
194 ok( ret != (ULONG)-1, "ldap_addA should succeed, got %#lx\n", ret );
195 ret = ldap_addA( ld, (char *)"", NULL );
196 ok( ret != (ULONG)-1, "ldap_addA should succeed, got %#lx\n", ret );
197 ret = ldap_addA( ld, (char *)"", attrs );
198 ok( ret != (ULONG)-1, "ldap_addA should succeed, got %#lx\n", ret );
200 ret = ldap_add_sA( NULL, NULL, NULL );
201 ok( ret == LDAP_PARAM_ERROR, "ldap_add_sA should fail, got %#lx\n", ret );
202 ret = ldap_add_sA( NULL, (char *)"", attrs );
203 ok( ret == LDAP_PARAM_ERROR, "ldap_add_sA should fail, got %#lx\n", ret );
204 ret = ldap_add_sA( ld, NULL, attrs );
205 ok( ret == LDAP_ALREADY_EXISTS, "ldap_add_sA should fail, got %#lx\n", ret );
206 ret = ldap_add_sA( ld, (char *)"", NULL );
207 ok( ret == LDAP_PROTOCOL_ERROR, "ldap_add_sA should fail, got %#lx\n", ret );
208 ret = ldap_add_sA( ld, (char *)"", attrs );
209 ok( ret == LDAP_ALREADY_EXISTS, "ldap_add_sA should fail, got %#lx\n", ret );
211 ret = ldap_add_extA( NULL, NULL, NULL, NULL, NULL, NULL );
212 ok( ret == LDAP_PARAM_ERROR, "ldap_add_extA should fail, got %#lx\n", ret );
213 ret = ldap_add_extA( NULL, (char *)"", attrs, NULL, NULL, &num );
214 ok( ret == LDAP_PARAM_ERROR, "ldap_add_extA should fail, got %#lx\n", ret );
215 ret = ldap_add_extA( ld, NULL, attrs, NULL, NULL, &num );
216 ok( !ret, "ldap_add_extA should succeed, got %#lx\n", ret );
217 ret = ldap_add_extA( ld, (char *)"", NULL, NULL, NULL, &num );
218 ok( !ret, "ldap_add_extA should succeed, got %#lx\n", ret );
219 ret = ldap_add_extA( ld, (char *)"", attrs, NULL, NULL, NULL );
220 ok( ret == LDAP_PARAM_ERROR, "ldap_add_extA should fail, got %#lx\n", ret );
221 ret = ldap_add_extA( ld, (char *)"", attrs, NULL, NULL, &num );
222 ok( !ret, "ldap_add_extA should succeed, got %#lx\n", ret );
224 ret = ldap_add_ext_sA( NULL, NULL, NULL, NULL, NULL );
225 ok( ret == LDAP_PARAM_ERROR, "ldap_add_ext_sA should fail, got %#lx\n", ret );
226 ret = ldap_add_ext_sA( NULL, (char *)"", attrs, NULL, NULL );
227 ok( ret == LDAP_PARAM_ERROR, "ldap_add_ext_sA should fail, got %#lx\n", ret );
228 ret = ldap_add_ext_sA( ld, NULL, attrs, NULL, NULL );
229 ok( ret == LDAP_ALREADY_EXISTS, "ldap_add_ext_sA should fail, got %#lx\n", ret );
230 ret = ldap_add_ext_sA( ld, (char *)"", NULL, NULL, NULL );
231 ok( ret == LDAP_PROTOCOL_ERROR, "ldap_add_ext_sA should fail, got %#lx\n", ret );
232 ret = ldap_add_ext_sA( ld, (char *)"", attrs, NULL, NULL );
233 ok( ret == LDAP_ALREADY_EXISTS, "ldap_add_ext_sA should fail, got %#lx\n", ret );
235 ldap_unbind( ld );
238 static void test_ldap_server_control( void )
240 /* SEQUENCE { INTEGER :: 0x07 } */
241 static char mask_ber[5] = {0x30,0x03,0x02,0x01,0x07};
242 LDAP *ld;
243 ULONG ret;
244 int version;
245 LDAPControlW *ctrls[2], mask;
246 LDAPMessage *res;
248 ld = ldap_initA( (char *)"db.debian.org", 389 );
249 ok( ld != NULL, "ldap_init failed\n" );
251 version = LDAP_VERSION3;
252 ret = ldap_set_optionW( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
253 if (ret == LDAP_SERVER_DOWN || ret == LDAP_UNAVAILABLE)
255 skip( "test server can't be reached\n" );
256 ldap_unbind( ld );
257 return;
260 ret = ldap_connect( ld, NULL );
261 ok( !ret, "ldap_connect failed %#lx\n", ret );
263 /* test setting a not supported server control */
264 mask.ldctl_oid = (WCHAR *)L"1.2.840.113556.1.4.801";
265 mask.ldctl_iscritical = TRUE;
266 mask.ldctl_value.bv_val = mask_ber;
267 mask.ldctl_value.bv_len = sizeof(mask_ber);
268 ctrls[0] = &mask;
269 ctrls[1] = NULL;
270 ret = ldap_set_optionW(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
271 ok( ret == LDAP_PARAM_ERROR, "ldap_set_optionW should fail: %#lx\n", ret );
273 res = NULL;
274 ret = ldap_search_sA( ld, (char *)"dc=debian,dc=org", LDAP_SCOPE_BASE, (char *)"(objectclass=*)", NULL, FALSE, &res );
275 ok( !ret, "ldap_search_sA failed %#lx\n", ret );
276 ok( res != NULL, "expected res != NULL\n" );
278 ldap_msgfree( res );
279 ldap_unbind( ld );
282 static void test_ldap_paged_search(void)
284 LDAP *ld;
285 ULONG ret, count;
286 int version;
287 LDAPSearch *search;
288 LDAPMessage *res, *entry;
289 BerElement *ber;
290 WCHAR *attr;
292 ld = ldap_initA( (char *)"db.debian.org", 389 );
293 ok( ld != NULL, "ldap_init failed\n" );
295 version = LDAP_VERSION3;
296 ret = ldap_set_optionW( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
297 if (ret == LDAP_SERVER_DOWN || ret == LDAP_UNAVAILABLE)
299 skip( "test server can't be reached\n" );
300 ldap_unbind( ld );
301 return;
304 search = ldap_search_init_pageW( ld, (WCHAR *)L"", LDAP_SCOPE_BASE, (WCHAR *)L"(objectclass=*)",
305 NULL, FALSE, NULL, NULL, 0, 0, NULL);
306 ok( search != NULL, "ldap_search_init_page failed\n" );
308 count = 0xdeadbeef;
309 res = NULL;
310 ret = ldap_get_next_page_s( ld, search, NULL, 1, &count, &res );
311 if (ret == LDAP_SERVER_DOWN || ret == LDAP_UNAVAILABLE)
313 skip( "test server can't be reached\n" );
314 ldap_unbind( ld );
315 return;
317 ok( !ret, "ldap_get_next_page_s failed %#lx\n", ret );
318 ok( res != NULL, "expected res != NULL\n" );
319 ok( count == 0, "got %lu\n", count );
321 count = ldap_count_entries( NULL, NULL );
322 ok( count == 0, "got %lu\n", count );
323 count = ldap_count_entries( ld, NULL );
324 ok( count == 0, "got %lu\n", count );
325 count = ldap_count_entries( NULL, res );
326 todo_wine ok( count == 1, "got %lu\n", count );
327 count = ldap_count_entries( ld, res );
328 ok( count == 1, "got %lu\n", count );
330 entry = ldap_first_entry( ld, res);
331 ok( res != NULL, "expected entry != NULL\n" );
333 attr = ldap_first_attributeW( ld, entry, &ber );
334 ok( !wcscmp( attr, L"objectClass" ), "got %s\n", wine_dbgstr_w( attr ) );
335 ldap_memfreeW( attr );
336 attr = ldap_next_attributeW( ld, entry, ber );
337 ok( !attr, "got %s\n", wine_dbgstr_w( attr ));
339 ber_free(ber, 0);
340 ldap_msgfree( res );
342 count = 0xdeadbeef;
343 res = (void *)0xdeadbeef;
344 ret = ldap_get_next_page_s( ld, search, NULL, 1, &count, &res );
345 ok( ret == LDAP_NO_RESULTS_RETURNED, "got %#lx\n", ret );
346 ok( !res, "expected res == NULL\n" );
347 ok( count == 0, "got %lu\n", count );
349 ldap_search_abandon_page( ld, search );
350 ldap_unbind( ld );
353 START_TEST (parse)
355 LDAP *ld;
357 test_ldap_paged_search();
358 test_ldap_server_control();
359 test_ldap_bind_sA();
360 test_ldap_add();
362 ld = ldap_initA( (char *)"db.debian.org", 389 );
363 ok( ld != NULL, "ldap_init failed\n" );
365 test_ldap_parse_sort_control( ld );
366 test_ldap_search_extW( ld );
367 test_ldap_get_optionW( ld );
368 test_ldap_set_optionW( ld );
369 ldap_unbind( ld );