Use TestCase.get_credentials() where possible.
[Samba.git] / source4 / torture / ldb / ldb.c
blob23f060b9b1c7ec44206097d9bb986fb3dcd4062d
1 /*
2 Unix SMB/CIFS implementation.
4 Test LDB attribute functions
6 Copyright (C) Andrew Bartlet <abartlet@samba.org> 2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "lib/events/events.h"
24 #include "lib/ldb/include/ldb.h"
25 #include "lib/ldb/include/ldb_errors.h"
26 #include "lib/ldb-samba/ldif_handlers.h"
27 #include "ldb_wrap.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "param/param.h"
30 #include "torture/smbtorture.h"
31 #include "torture/local/proto.h"
33 static const char *sid = "S-1-5-21-4177067393-1453636373-93818737";
34 static const char *hex_sid = "01040000000000051500000081fdf8f815bba456718f9705";
35 static const char *guid = "975ac5fa-35d9-431d-b86a-845bcd34fff9";
36 static const char *guid2 = "{975ac5fa-35d9-431d-b86a-845bcd34fff9}";
37 static const char *hex_guid = "fac55a97d9351d43b86a845bcd34fff9";
39 static const char *prefix_map_newline = "2:1.2.840.113556.1.2\n5:2.16.840.1.101.2.2.3";
40 static const char *prefix_map_semi = "2:1.2.840.113556.1.2;5:2.16.840.1.101.2.2.3";
42 static bool torture_ldb_attrs(struct torture_context *torture)
44 TALLOC_CTX *mem_ctx = talloc_new(torture);
45 struct ldb_context *ldb;
46 const struct ldb_schema_attribute *attr;
47 struct ldb_val string_sid_blob, binary_sid_blob;
48 struct ldb_val string_guid_blob, string_guid_blob2, binary_guid_blob;
49 struct ldb_val string_prefix_map_newline_blob, string_prefix_map_semi_blob, string_prefix_map_blob;
50 struct ldb_val prefix_map_blob;
52 DATA_BLOB sid_blob = strhex_to_data_blob(mem_ctx, hex_sid);
53 DATA_BLOB guid_blob = strhex_to_data_blob(mem_ctx, hex_guid);
55 torture_assert(torture,
56 ldb = ldb_init(mem_ctx, torture->ev),
57 "Failed to init ldb");
59 torture_assert_int_equal(torture,
60 ldb_register_samba_handlers(ldb), LDB_SUCCESS,
61 "Failed to register Samba handlers");
63 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
65 /* Test SID behaviour */
66 torture_assert(torture, attr = ldb_schema_attribute_by_name(ldb, "objectSid"),
67 "Failed to get objectSid schema attribute");
69 string_sid_blob = data_blob_string_const(sid);
71 torture_assert_int_equal(torture,
72 attr->syntax->ldif_read_fn(ldb, mem_ctx,
73 &string_sid_blob, &binary_sid_blob), 0,
74 "Failed to parse string SID");
76 torture_assert_data_blob_equal(torture, binary_sid_blob, sid_blob,
77 "Read SID into blob form failed");
79 torture_assert_int_equal(torture,
80 attr->syntax->ldif_read_fn(ldb, mem_ctx,
81 &sid_blob, &binary_sid_blob), -1,
82 "Should have failed to parse binary SID");
84 torture_assert_int_equal(torture,
85 attr->syntax->ldif_write_fn(ldb, mem_ctx, &binary_sid_blob, &string_sid_blob), 0,
86 "Failed to parse binary SID");
88 torture_assert_data_blob_equal(torture,
89 string_sid_blob, data_blob_string_const(sid),
90 "Write SID into string form failed");
92 torture_assert_int_equal(torture,
93 attr->syntax->comparison_fn(ldb, mem_ctx, &binary_sid_blob, &string_sid_blob), 0,
94 "Failed to compare binary and string SID");
96 torture_assert_int_equal(torture,
97 attr->syntax->comparison_fn(ldb, mem_ctx, &string_sid_blob, &binary_sid_blob), 0,
98 "Failed to compare string and binary binary SID");
100 torture_assert_int_equal(torture,
101 attr->syntax->comparison_fn(ldb, mem_ctx, &string_sid_blob, &string_sid_blob), 0,
102 "Failed to compare string and string SID");
104 torture_assert_int_equal(torture,
105 attr->syntax->comparison_fn(ldb, mem_ctx, &binary_sid_blob, &binary_sid_blob), 0,
106 "Failed to compare binary and binary SID");
108 torture_assert(torture, attr->syntax->comparison_fn(ldb, mem_ctx, &guid_blob, &binary_sid_blob) != 0,
109 "Failed to distinguish binary GUID and binary SID");
112 /* Test GUID behaviour */
113 torture_assert(torture, attr = ldb_schema_attribute_by_name(ldb, "objectGUID"),
114 "Failed to get objectGUID schema attribute");
116 string_guid_blob = data_blob_string_const(guid);
118 torture_assert_int_equal(torture,
119 attr->syntax->ldif_read_fn(ldb, mem_ctx,
120 &string_guid_blob, &binary_guid_blob), 0,
121 "Failed to parse string GUID");
123 torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob,
124 "Read GUID into blob form failed");
126 string_guid_blob2 = data_blob_string_const(guid2);
128 torture_assert_int_equal(torture,
129 attr->syntax->ldif_read_fn(ldb, mem_ctx,
130 &string_guid_blob2, &binary_guid_blob), 0,
131 "Failed to parse string GUID");
133 torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob,
134 "Read GUID into blob form failed");
136 torture_assert_int_equal(torture,
137 attr->syntax->ldif_read_fn(ldb, mem_ctx,
138 &guid_blob, &binary_guid_blob), 0,
139 "Failed to parse binary GUID");
141 torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob,
142 "Read GUID into blob form failed");
144 torture_assert_int_equal(torture,
145 attr->syntax->ldif_write_fn(ldb, mem_ctx, &binary_guid_blob, &string_guid_blob), 0,
146 "Failed to print binary GUID as string");
148 torture_assert_data_blob_equal(torture, string_sid_blob, data_blob_string_const(sid),
149 "Write SID into string form failed");
151 torture_assert_int_equal(torture,
152 attr->syntax->comparison_fn(ldb, mem_ctx, &binary_guid_blob, &string_guid_blob), 0,
153 "Failed to compare binary and string GUID");
155 torture_assert_int_equal(torture,
156 attr->syntax->comparison_fn(ldb, mem_ctx, &string_guid_blob, &binary_guid_blob), 0,
157 "Failed to compare string and binary binary GUID");
159 torture_assert_int_equal(torture,
160 attr->syntax->comparison_fn(ldb, mem_ctx, &string_guid_blob, &string_guid_blob), 0,
161 "Failed to compare string and string GUID");
163 torture_assert_int_equal(torture,
164 attr->syntax->comparison_fn(ldb, mem_ctx, &binary_guid_blob, &binary_guid_blob), 0,
165 "Failed to compare binary and binary GUID");
167 string_prefix_map_newline_blob = data_blob_string_const(prefix_map_newline);
169 string_prefix_map_semi_blob = data_blob_string_const(prefix_map_semi);
171 /* Test prefixMap behaviour */
172 torture_assert(torture, attr = ldb_schema_attribute_by_name(ldb, "prefixMap"),
173 "Failed to get prefixMap schema attribute");
175 torture_assert_int_equal(torture,
176 attr->syntax->comparison_fn(ldb, mem_ctx, &string_prefix_map_newline_blob, &string_prefix_map_semi_blob), 0,
177 "Failed to compare prefixMap with newlines and prefixMap with semicolons");
179 torture_assert_int_equal(torture,
180 attr->syntax->ldif_read_fn(ldb, mem_ctx, &string_prefix_map_newline_blob, &prefix_map_blob), 0,
181 "Failed to read prefixMap with newlines");
182 torture_assert_int_equal(torture,
183 attr->syntax->comparison_fn(ldb, mem_ctx, &string_prefix_map_newline_blob, &prefix_map_blob), 0,
184 "Failed to compare prefixMap with newlines and prefixMap binary");
186 torture_assert_int_equal(torture,
187 attr->syntax->ldif_write_fn(ldb, mem_ctx, &prefix_map_blob, &string_prefix_map_blob), 0,
188 "Failed to write prefixMap");
189 torture_assert_int_equal(torture,
190 attr->syntax->comparison_fn(ldb, mem_ctx, &string_prefix_map_blob, &prefix_map_blob), 0,
191 "Failed to compare prefixMap ldif write and prefixMap binary");
193 torture_assert_data_blob_equal(torture, string_prefix_map_blob, string_prefix_map_semi_blob,
194 "Failed to compare prefixMap ldif write and prefixMap binary");
198 talloc_free(mem_ctx);
199 return true;
202 static bool torture_ldb_dn_attrs(struct torture_context *torture)
204 TALLOC_CTX *mem_ctx = talloc_new(torture);
205 struct ldb_context *ldb;
206 const struct ldb_dn_extended_syntax *attr;
207 struct ldb_val string_sid_blob, binary_sid_blob;
208 struct ldb_val string_guid_blob, binary_guid_blob;
209 struct ldb_val hex_sid_blob, hex_guid_blob;
211 DATA_BLOB sid_blob = strhex_to_data_blob(mem_ctx, hex_sid);
212 DATA_BLOB guid_blob = strhex_to_data_blob(mem_ctx, hex_guid);
214 torture_assert(torture,
215 ldb = ldb_init(mem_ctx, torture->ev),
216 "Failed to init ldb");
218 torture_assert_int_equal(torture,
219 ldb_register_samba_handlers(ldb), LDB_SUCCESS,
220 "Failed to register Samba handlers");
222 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
224 /* Test SID behaviour */
225 torture_assert(torture, attr = ldb_dn_extended_syntax_by_name(ldb, "SID"),
226 "Failed to get SID DN syntax");
228 string_sid_blob = data_blob_string_const(sid);
230 torture_assert_int_equal(torture,
231 attr->read_fn(ldb, mem_ctx,
232 &string_sid_blob, &binary_sid_blob), 0,
233 "Failed to parse string SID");
235 torture_assert_data_blob_equal(torture, binary_sid_blob, sid_blob,
236 "Read SID into blob form failed");
238 hex_sid_blob = data_blob_string_const(hex_sid);
240 torture_assert_int_equal(torture,
241 attr->read_fn(ldb, mem_ctx,
242 &hex_sid_blob, &binary_sid_blob), 0,
243 "Failed to parse HEX SID");
245 torture_assert_data_blob_equal(torture, binary_sid_blob, sid_blob,
246 "Read SID into blob form failed");
248 torture_assert_int_equal(torture,
249 attr->read_fn(ldb, mem_ctx,
250 &sid_blob, &binary_sid_blob), -1,
251 "Should have failed to parse binary SID");
253 torture_assert_int_equal(torture,
254 attr->write_hex_fn(ldb, mem_ctx, &sid_blob, &hex_sid_blob), 0,
255 "Failed to parse binary SID");
257 torture_assert_data_blob_equal(torture,
258 hex_sid_blob, data_blob_string_const(hex_sid),
259 "Write SID into HEX string form failed");
261 torture_assert_int_equal(torture,
262 attr->write_clear_fn(ldb, mem_ctx, &sid_blob, &string_sid_blob), 0,
263 "Failed to parse binary SID");
265 torture_assert_data_blob_equal(torture,
266 string_sid_blob, data_blob_string_const(sid),
267 "Write SID into clear string form failed");
270 /* Test GUID behaviour */
271 torture_assert(torture, attr = ldb_dn_extended_syntax_by_name(ldb, "GUID"),
272 "Failed to get GUID DN syntax");
274 string_guid_blob = data_blob_string_const(guid);
276 torture_assert_int_equal(torture,
277 attr->read_fn(ldb, mem_ctx,
278 &string_guid_blob, &binary_guid_blob), 0,
279 "Failed to parse string GUID");
281 torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob,
282 "Read GUID into blob form failed");
284 hex_guid_blob = data_blob_string_const(hex_guid);
286 torture_assert_int_equal(torture,
287 attr->read_fn(ldb, mem_ctx,
288 &hex_guid_blob, &binary_guid_blob), 0,
289 "Failed to parse HEX GUID");
291 torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob,
292 "Read GUID into blob form failed");
294 torture_assert_int_equal(torture,
295 attr->read_fn(ldb, mem_ctx,
296 &guid_blob, &binary_guid_blob), -1,
297 "Should have failed to parse binary GUID");
299 torture_assert_int_equal(torture,
300 attr->write_hex_fn(ldb, mem_ctx, &guid_blob, &hex_guid_blob), 0,
301 "Failed to parse binary GUID");
303 torture_assert_data_blob_equal(torture,
304 hex_guid_blob, data_blob_string_const(hex_guid),
305 "Write GUID into HEX string form failed");
307 torture_assert_int_equal(torture,
308 attr->write_clear_fn(ldb, mem_ctx, &guid_blob, &string_guid_blob), 0,
309 "Failed to parse binary GUID");
311 torture_assert_data_blob_equal(torture,
312 string_guid_blob, data_blob_string_const(guid),
313 "Write GUID into clear string form failed");
317 talloc_free(mem_ctx);
318 return true;
321 static bool torture_ldb_dn_extended(struct torture_context *torture)
323 TALLOC_CTX *mem_ctx = talloc_new(torture);
324 struct ldb_context *ldb;
325 struct ldb_dn *dn, *dn2;
327 DATA_BLOB sid_blob = strhex_to_data_blob(mem_ctx, hex_sid);
328 DATA_BLOB guid_blob = strhex_to_data_blob(mem_ctx, hex_guid);
330 const char *dn_str = "cn=admin,cn=users,dc=samba,dc=org";
332 torture_assert(torture,
333 ldb = ldb_init(mem_ctx, torture->ev),
334 "Failed to init ldb");
336 torture_assert_int_equal(torture,
337 ldb_register_samba_handlers(ldb), LDB_SUCCESS,
338 "Failed to register Samba handlers");
340 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
342 /* Check behaviour of a normal DN */
343 torture_assert(torture,
344 dn = ldb_dn_new(mem_ctx, ldb, dn_str),
345 "Failed to create a 'normal' DN");
347 torture_assert(torture,
348 ldb_dn_validate(dn),
349 "Failed to validate 'normal' DN");
351 torture_assert(torture, ldb_dn_has_extended(dn) == false,
352 "Should not find plain DN to be 'extended'");
354 torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") == NULL,
355 "Should not find an SID on plain DN");
357 torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") == NULL,
358 "Should not find an GUID on plain DN");
360 torture_assert(torture, ldb_dn_get_extended_component(dn, "WKGUID") == NULL,
361 "Should not find an WKGUID on plain DN");
363 /* Now make an extended DN */
364 torture_assert(torture,
365 dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>;<SID=%s>;%s",
366 guid, sid, dn_str),
367 "Failed to create an 'extended' DN");
369 torture_assert(torture,
370 dn2 = ldb_dn_copy(mem_ctx, dn),
371 "Failed to copy the 'extended' DN");
372 talloc_free(dn);
373 dn = dn2;
375 torture_assert(torture,
376 ldb_dn_validate(dn),
377 "Failed to validate 'extended' DN");
379 torture_assert(torture, ldb_dn_has_extended(dn) == true,
380 "Should find extended DN to be 'extended'");
382 torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") != NULL,
383 "Should find an SID on extended DN");
385 torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") != NULL,
386 "Should find an GUID on extended DN");
388 torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "SID"), sid_blob,
389 "Extended DN SID incorect");
391 torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "GUID"), guid_blob,
392 "Extended DN GUID incorect");
394 torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), dn_str,
395 "linearized DN incorrect");
397 torture_assert_str_equal(torture, ldb_dn_get_casefold(dn), strupper_talloc(mem_ctx, dn_str),
398 "casefolded DN incorrect");
400 torture_assert_str_equal(torture, ldb_dn_get_component_name(dn, 0), "cn",
401 "componet zero incorrect");
403 torture_assert_data_blob_equal(torture, *ldb_dn_get_component_val(dn, 0), data_blob_string_const("admin"),
404 "componet zero incorrect");
406 torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 1),
407 talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s",
408 guid, sid, dn_str),
409 "Clear extended linearized DN incorrect");
411 torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0),
412 talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s",
413 hex_guid, hex_sid, dn_str),
414 "HEX extended linearized DN incorrect");
416 torture_assert(torture, ldb_dn_remove_child_components(dn, 1) == true,
417 "Failed to remove DN child");
419 torture_assert(torture, ldb_dn_has_extended(dn) == false,
420 "Extended DN flag should be cleared after child element removal");
422 torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") == NULL,
423 "Should not find an SID on DN");
425 torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") == NULL,
426 "Should not find an GUID on DN");
429 /* TODO: test setting these in the other order, and ensure it still comes out 'GUID first' */
430 torture_assert_int_equal(torture, ldb_dn_set_extended_component(dn, "GUID", &guid_blob), 0,
431 "Failed to set a GUID on DN");
433 torture_assert_int_equal(torture, ldb_dn_set_extended_component(dn, "SID", &sid_blob), 0,
434 "Failed to set a SID on DN");
436 torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "SID"), sid_blob,
437 "Extended DN SID incorect");
439 torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "GUID"), guid_blob,
440 "Extended DN GUID incorect");
442 torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "cn=users,dc=samba,dc=org",
443 "linearized DN incorrect");
445 torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 1),
446 talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s",
447 guid, sid, "cn=users,dc=samba,dc=org"),
448 "Clear extended linearized DN incorrect");
450 torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0),
451 talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s",
452 hex_guid, hex_sid, "cn=users,dc=samba,dc=org"),
453 "HEX extended linearized DN incorrect");
455 /* Now check a 'just GUID' DN (clear format) */
456 torture_assert(torture,
457 dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>",
458 guid),
459 "Failed to create an 'extended' DN");
461 torture_assert(torture,
462 ldb_dn_validate(dn),
463 "Failed to validate 'extended' DN");
465 torture_assert(torture, ldb_dn_has_extended(dn) == true,
466 "Should find extended DN to be 'extended'");
468 torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") == NULL,
469 "Should not find an SID on this DN");
471 torture_assert_int_equal(torture, ldb_dn_get_comp_num(dn), 0,
472 "Should not find an 'normal' componet on this DN");
474 torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") != NULL,
475 "Should find an GUID on this DN");
477 torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "GUID"), guid_blob,
478 "Extended DN GUID incorect");
480 torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "",
481 "linearized DN incorrect");
483 torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 1),
484 talloc_asprintf(mem_ctx, "<GUID=%s>",
485 guid),
486 "Clear extended linearized DN incorrect");
488 torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0),
489 talloc_asprintf(mem_ctx, "<GUID=%s>",
490 hex_guid),
491 "HEX extended linearized DN incorrect");
493 /* Now check a 'just GUID' DN (HEX format) */
494 torture_assert(torture,
495 dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>",
496 hex_guid),
497 "Failed to create an 'extended' DN");
499 torture_assert(torture,
500 ldb_dn_validate(dn),
501 "Failed to validate 'extended' DN");
503 torture_assert(torture, ldb_dn_has_extended(dn) == true,
504 "Should find extended DN to be 'extended'");
506 torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") == NULL,
507 "Should not find an SID on this DN");
509 torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") != NULL,
510 "Should find an GUID on this DN");
512 torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "GUID"), guid_blob,
513 "Extended DN GUID incorect");
515 torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "",
516 "linearized DN incorrect");
518 /* Now check a 'just SID' DN (clear format) */
519 torture_assert(torture,
520 dn = ldb_dn_new_fmt(mem_ctx, ldb, "<SID=%s>",
521 sid),
522 "Failed to create an 'extended' DN");
524 torture_assert(torture,
525 ldb_dn_validate(dn),
526 "Failed to validate 'extended' DN");
528 torture_assert(torture, ldb_dn_has_extended(dn) == true,
529 "Should find extended DN to be 'extended'");
531 torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") == NULL,
532 "Should not find an SID on this DN");
534 torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") != NULL,
535 "Should find an SID on this DN");
537 torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "SID"), sid_blob,
538 "Extended DN SID incorect");
540 torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "",
541 "linearized DN incorrect");
543 torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 1),
544 talloc_asprintf(mem_ctx, "<SID=%s>",
545 sid),
546 "Clear extended linearized DN incorrect");
548 torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0),
549 talloc_asprintf(mem_ctx, "<SID=%s>",
550 hex_sid),
551 "HEX extended linearized DN incorrect");
553 /* Now check a 'just SID' DN (HEX format) */
554 torture_assert(torture,
555 dn = ldb_dn_new_fmt(mem_ctx, ldb, "<SID=%s>",
556 hex_sid),
557 "Failed to create an 'extended' DN");
559 torture_assert(torture,
560 ldb_dn_validate(dn),
561 "Failed to validate 'extended' DN");
563 torture_assert(torture, ldb_dn_has_extended(dn) == true,
564 "Should find extended DN to be 'extended'");
566 torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") == NULL,
567 "Should not find an SID on this DN");
569 torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") != NULL,
570 "Should find an SID on this DN");
572 torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "SID"), sid_blob,
573 "Extended DN SID incorect");
575 torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "",
576 "linearized DN incorrect");
578 talloc_free(mem_ctx);
579 return true;
583 static bool torture_ldb_dn(struct torture_context *torture)
585 TALLOC_CTX *mem_ctx = talloc_new(torture);
586 struct ldb_context *ldb;
587 struct ldb_dn *dn;
588 struct ldb_dn *child_dn;
589 struct ldb_dn *typo_dn;
590 struct ldb_dn *special_dn;
591 struct ldb_val val;
593 torture_assert(torture,
594 ldb = ldb_init(mem_ctx, torture->ev),
595 "Failed to init ldb");
597 torture_assert_int_equal(torture,
598 ldb_register_samba_handlers(ldb), LDB_SUCCESS,
599 "Failed to register Samba handlers");
601 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
603 /* Check behaviour of a normal DN */
604 torture_assert(torture,
605 dn = ldb_dn_new(mem_ctx, ldb, NULL),
606 "Failed to create a NULL DN");
608 torture_assert(torture,
609 ldb_dn_validate(dn),
610 "Failed to validate NULL DN");
612 torture_assert(torture,
613 ldb_dn_add_base_fmt(dn, "dc=org"),
614 "Failed to add base DN");
616 torture_assert(torture,
617 ldb_dn_add_child_fmt(dn, "dc=samba"),
618 "Failed to add base DN");
620 torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "dc=samba,dc=org",
621 "linearized DN incorrect");
623 torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0), "dc=samba,dc=org",
624 "extended linearized DN incorrect");
626 /* Check child DN comparisons */
627 torture_assert(torture,
628 child_dn = ldb_dn_new(mem_ctx, ldb, "CN=users,DC=SAMBA,DC=org"),
629 "Failed to create child DN");
631 torture_assert(torture,
632 ldb_dn_compare(dn, child_dn) != 0,
633 "Comparison on dc=samba,dc=org and CN=users,DC=SAMBA,DC=org should != 0");
635 torture_assert(torture,
636 ldb_dn_compare_base(child_dn, dn) != 0,
637 "Base Comparison of CN=users,DC=SAMBA,DC=org and dc=samba,dc=org should != 0");
639 torture_assert(torture,
640 ldb_dn_compare_base(dn, child_dn) == 0,
641 "Base Comparison on dc=samba,dc=org and CN=users,DC=SAMBA,DC=org should == 0");
643 /* Check comparisons with a truncated DN */
644 torture_assert(torture,
645 typo_dn = ldb_dn_new(mem_ctx, ldb, "c=samba,dc=org"),
646 "Failed to create 'typo' DN");
648 torture_assert(torture,
649 ldb_dn_compare(dn, typo_dn) != 0,
650 "Comparison on dc=samba,dc=org and c=samba,dc=org should != 0");
652 torture_assert(torture,
653 ldb_dn_compare_base(typo_dn, dn) != 0,
654 "Base Comparison of c=samba,dc=org and dc=samba,dc=org should != 0");
656 torture_assert(torture,
657 ldb_dn_compare_base(dn, typo_dn) != 0,
658 "Base Comparison on dc=samba,dc=org and c=samba,dc=org should != 0");
660 /* Check comparisons with a special DN */
661 torture_assert(torture,
662 special_dn = ldb_dn_new(mem_ctx, ldb, "@special_dn"),
663 "Failed to create 'special' DN");
665 torture_assert(torture,
666 ldb_dn_compare(dn, special_dn) != 0,
667 "Comparison on dc=samba,dc=org and @special_dn should != 0");
669 torture_assert(torture,
670 ldb_dn_compare_base(special_dn, dn) > 0,
671 "Base Comparison of @special_dn and dc=samba,dc=org should > 0");
673 torture_assert(torture,
674 ldb_dn_compare_base(dn, special_dn) < 0,
675 "Base Comparison on dc=samba,dc=org and @special_dn should < 0");
677 /* Check DN based on MS-ADTS:3.1.1.5.1.2 Naming Constraints*/
678 torture_assert(torture,
679 dn = ldb_dn_new(mem_ctx, ldb, "CN=New\nLine,DC=SAMBA,DC=org"),
680 "Failed to create a DN with 0xA in it");
682 /* this is a warning until we work out how the DEL: CNs work */
683 if (ldb_dn_validate(dn) != false) {
684 torture_warning(torture,
685 "should have failed to validate a DN with 0xA in it");
688 /* Escaped comma */
689 torture_assert(torture,
690 dn = ldb_dn_new(mem_ctx, ldb, "CN=A\\,comma,DC=SAMBA,DC=org"),
691 "Failed to create a DN with an escaped comma in it");
694 val = data_blob_const("CN=Zer\0,DC=SAMBA,DC=org", 23);
695 torture_assert(torture,
696 NULL == ldb_dn_from_ldb_val(mem_ctx, ldb, &val),
697 "should fail to create a DN with 0x0 in it");
699 talloc_free(mem_ctx);
700 return true;
703 static bool torture_ldb_dn_invalid_extended(struct torture_context *torture)
705 TALLOC_CTX *mem_ctx = talloc_new(torture);
706 struct ldb_context *ldb;
707 struct ldb_dn *dn;
709 const char *dn_str = "cn=admin,cn=users,dc=samba,dc=org";
711 torture_assert(torture,
712 ldb = ldb_init(mem_ctx, torture->ev),
713 "Failed to init ldb");
715 torture_assert_int_equal(torture,
716 ldb_register_samba_handlers(ldb), LDB_SUCCESS,
717 "Failed to register Samba handlers");
719 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
721 /* Check behaviour of a normal DN */
722 torture_assert(torture,
723 dn = ldb_dn_new(mem_ctx, ldb, "samba,dc=org"),
724 "Failed to create a 'normal' invalid DN");
726 torture_assert(torture,
727 ldb_dn_validate(dn) == false,
728 "should have failed to validate 'normal' invalid DN");
730 /* Now make an extended DN */
731 torture_assert(torture,
732 dn = ldb_dn_new_fmt(mem_ctx, ldb, "<PID=%s>;%s",
733 sid, dn_str),
734 "Failed to create an invalid 'extended' DN");
736 torture_assert(torture,
737 ldb_dn_validate(dn) == false,
738 "should have failed to validate 'extended' DN");
740 torture_assert(torture,
741 dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>%s",
742 sid, dn_str),
743 "Failed to create an invalid 'extended' DN");
745 torture_assert(torture,
746 ldb_dn_validate(dn) == false,
747 "should have failed to validate 'extended' DN");
749 torture_assert(torture,
750 dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>;",
751 sid),
752 "Failed to create an invalid 'extended' DN");
754 torture_assert(torture,
755 ldb_dn_validate(dn) == false,
756 "should have failed to validate 'extended' DN");
758 torture_assert(torture,
759 dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>;",
760 hex_sid),
761 "Failed to create an invalid 'extended' DN");
763 torture_assert(torture,
764 ldb_dn_validate(dn) == false,
765 "should have failed to validate 'extended' DN");
767 torture_assert(torture,
768 dn = ldb_dn_new_fmt(mem_ctx, ldb, "<SID=%s>;",
769 hex_guid),
770 "Failed to create an invalid 'extended' DN");
772 torture_assert(torture,
773 ldb_dn_validate(dn) == false,
774 "should have failed to validate 'extended' DN");
776 torture_assert(torture,
777 dn = ldb_dn_new_fmt(mem_ctx, ldb, "<SID=%s>;",
778 guid),
779 "Failed to create an invalid 'extended' DN");
781 torture_assert(torture,
782 ldb_dn_validate(dn) == false,
783 "should have failed to validate 'extended' DN");
785 torture_assert(torture,
786 dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=>"),
787 "Failed to create an invalid 'extended' DN");
789 torture_assert(torture,
790 ldb_dn_validate(dn) == false,
791 "should have failed to validate 'extended' DN");
793 return true;
796 struct torture_suite *torture_ldb(TALLOC_CTX *mem_ctx)
798 struct torture_suite *suite = torture_suite_create(mem_ctx, "ldb");
800 if (suite == NULL) {
801 return NULL;
804 torture_suite_add_simple_test(suite, "attrs", torture_ldb_attrs);
805 torture_suite_add_simple_test(suite, "dn-attrs", torture_ldb_dn_attrs);
806 torture_suite_add_simple_test(suite, "dn-extended", torture_ldb_dn_extended);
807 torture_suite_add_simple_test(suite, "dn-invalid-extended", torture_ldb_dn_invalid_extended);
808 torture_suite_add_simple_test(suite, "dn", torture_ldb_dn);
810 suite->description = talloc_strdup(suite, "LDB (samba-specific behaviour) tests");
812 return suite;