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
29 #include "wine/test.h"
31 #ifndef LDAP_AUTH_SIMPLE
32 #define LDAP_AUTH_SIMPLE 0x80
35 #ifndef LDAP_OPT_SERVER_CONTROLS
36 #define LDAP_OPT_SERVER_CONTROLS 0x0012
39 static void test_ldap_parse_sort_control( LDAP
*ld
)
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
;
53 ret
= ldap_create_sort_controlA( ld
, sortkeys
, 0, &sort
);
54 ok( !ret
, "ldap_create_sort_controlA failed %#lx\n", ret
);
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
);
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
);
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=*";
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");
110 ok( !ret
, "ldap_search_extW failed %#lx\n", ret
);
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
)
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");
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
)
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 )
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" );
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",
167 ok( ret
== LDAP_INVALID_CREDENTIALS
, "ldap_bind_s returned %#lx\n", ret
);
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
);
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
};
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
);
238 static void test_ldap_server_control( void )
240 /* SEQUENCE { INTEGER :: 0x07 } */
241 static char mask_ber
[5] = {0x30,0x03,0x02,0x01,0x07};
245 LDAPControlW
*ctrls
[2], mask
;
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" );
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
);
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
);
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" );
282 static void test_ldap_paged_search(void)
288 LDAPMessage
*res
, *entry
;
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" );
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" );
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" );
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
));
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
);
357 test_ldap_paged_search();
358 test_ldap_server_control();
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
);