c2977bcb70217cef0e2d7d61ab4bacf5009264e4
[metze/samba/wip.git] / source4 / torture / ldb / ldb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Test LDB attribute functions
5
6    Copyright (C) Andrew Bartlet <abartlet@samba.org> 2008
7    
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.
12    
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.
17    
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/>.
20 */
21
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/ldb/proto.h"
32
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";
38
39 static bool torture_ldb_attrs(struct torture_context *torture)
40 {
41         TALLOC_CTX *mem_ctx = talloc_new(torture);
42         struct ldb_context *ldb;
43         const struct ldb_schema_attribute *attr;
44         struct ldb_val string_sid_blob, binary_sid_blob;
45         struct ldb_val string_guid_blob, string_guid_blob2, binary_guid_blob;
46
47         DATA_BLOB sid_blob = strhex_to_data_blob(mem_ctx, hex_sid);
48         DATA_BLOB guid_blob = strhex_to_data_blob(mem_ctx, hex_guid);
49
50         torture_assert(torture, 
51                        ldb = ldb_init(mem_ctx, torture->ev),
52                        "Failed to init ldb");
53
54         torture_assert_int_equal(torture, 
55                                  ldb_register_samba_handlers(ldb), 0, 
56                                  "Failed to register Samba handlers");
57
58         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
59
60         /* Test SID behaviour */
61         torture_assert(torture, attr = ldb_schema_attribute_by_name(ldb, "objectSid"), 
62                        "Failed to get objectSid schema attribute");
63         
64         string_sid_blob = data_blob_string_const(sid);
65
66         torture_assert_int_equal(torture, 
67                                  attr->syntax->ldif_read_fn(ldb, mem_ctx, 
68                                                             &string_sid_blob, &binary_sid_blob), 0,
69                                  "Failed to parse string SID");
70         
71         torture_assert_data_blob_equal(torture, binary_sid_blob, sid_blob, 
72                                        "Read SID into blob form failed");
73         
74         torture_assert_int_equal(torture, 
75                                  attr->syntax->ldif_read_fn(ldb, mem_ctx, 
76                                                             &sid_blob, &binary_sid_blob), -1,
77                                  "Should have failed to parse binary SID");
78         
79         torture_assert_int_equal(torture, 
80                                  attr->syntax->ldif_write_fn(ldb, mem_ctx, &binary_sid_blob, &string_sid_blob), 0,
81                                  "Failed to parse binary SID");
82         
83         torture_assert_data_blob_equal(torture, 
84                                        string_sid_blob, data_blob_string_const(sid),
85                                        "Write SID into string form failed");
86         
87         torture_assert_int_equal(torture, 
88                                  attr->syntax->comparison_fn(ldb, mem_ctx, &binary_sid_blob, &string_sid_blob), 0,
89                                  "Failed to compare binary and string SID");
90         
91         torture_assert_int_equal(torture, 
92                                  attr->syntax->comparison_fn(ldb, mem_ctx, &string_sid_blob, &binary_sid_blob), 0,
93                                  "Failed to compare string and binary binary SID");
94         
95         torture_assert_int_equal(torture, 
96                                  attr->syntax->comparison_fn(ldb, mem_ctx, &string_sid_blob, &string_sid_blob), 0,
97                                  "Failed to compare string and string SID");
98         
99         torture_assert_int_equal(torture, 
100                                  attr->syntax->comparison_fn(ldb, mem_ctx, &binary_sid_blob, &binary_sid_blob), 0,
101                                  "Failed to compare binary and binary SID");
102         
103         torture_assert(torture, attr->syntax->comparison_fn(ldb, mem_ctx, &guid_blob, &binary_sid_blob) != 0,
104                        "Failed to distinguish binary GUID and binary SID");
105
106
107         /* Test GUID behaviour */
108         torture_assert(torture, attr = ldb_schema_attribute_by_name(ldb, "objectGUID"), 
109                        "Failed to get objectGUID schema attribute");
110         
111         string_guid_blob = data_blob_string_const(guid);
112
113         torture_assert_int_equal(torture, 
114                                  attr->syntax->ldif_read_fn(ldb, mem_ctx, 
115                                                             &string_guid_blob, &binary_guid_blob), 0,
116                                  "Failed to parse string GUID");
117         
118         torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob, 
119                                        "Read GUID into blob form failed");
120         
121         string_guid_blob2 = data_blob_string_const(guid2);
122         
123         torture_assert_int_equal(torture, 
124                                  attr->syntax->ldif_read_fn(ldb, mem_ctx, 
125                                                             &string_guid_blob2, &binary_guid_blob), 0,
126                                  "Failed to parse string GUID");
127         
128         torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob, 
129                                        "Read GUID into blob form failed");
130         
131         torture_assert_int_equal(torture, 
132                                  attr->syntax->ldif_read_fn(ldb, mem_ctx, 
133                                                             &guid_blob, &binary_guid_blob), 0,
134                                  "Failed to parse binary GUID");
135         
136         torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob, 
137                                        "Read GUID into blob form failed");
138         
139         torture_assert_int_equal(torture, 
140                                  attr->syntax->ldif_write_fn(ldb, mem_ctx, &binary_guid_blob, &string_guid_blob), 0,
141                                  "Failed to print binary GUID as string");
142
143         torture_assert_data_blob_equal(torture, string_sid_blob, data_blob_string_const(sid),
144                                        "Write SID into string form failed");
145         
146         torture_assert_int_equal(torture, 
147                                  attr->syntax->comparison_fn(ldb, mem_ctx, &binary_guid_blob, &string_guid_blob), 0,
148                                  "Failed to compare binary and string GUID");
149         
150         torture_assert_int_equal(torture, 
151                                  attr->syntax->comparison_fn(ldb, mem_ctx, &string_guid_blob, &binary_guid_blob), 0,
152                                  "Failed to compare string and binary binary GUID");
153         
154         torture_assert_int_equal(torture, 
155                                  attr->syntax->comparison_fn(ldb, mem_ctx, &string_guid_blob, &string_guid_blob), 0,
156                                  "Failed to compare string and string GUID");
157         
158         torture_assert_int_equal(torture, 
159                                  attr->syntax->comparison_fn(ldb, mem_ctx, &binary_guid_blob, &binary_guid_blob), 0,
160                                  "Failed to compare binary and binary GUID");
161         
162         
163         
164         talloc_free(mem_ctx);
165         return true;
166 }
167
168 static bool torture_ldb_dn_attrs(struct torture_context *torture)
169 {
170         TALLOC_CTX *mem_ctx = talloc_new(torture);
171         struct ldb_context *ldb;
172         const struct ldb_dn_extended_syntax *attr;
173         struct ldb_val string_sid_blob, binary_sid_blob;
174         struct ldb_val string_guid_blob, binary_guid_blob;
175         struct ldb_val hex_sid_blob, hex_guid_blob;
176
177         DATA_BLOB sid_blob = strhex_to_data_blob(mem_ctx, hex_sid);
178         DATA_BLOB guid_blob = strhex_to_data_blob(mem_ctx, hex_guid);
179
180         torture_assert(torture, 
181                        ldb = ldb_init(mem_ctx, torture->ev),
182                        "Failed to init ldb");
183
184         torture_assert_int_equal(torture, 
185                                  ldb_register_samba_handlers(ldb), 0, 
186                                  "Failed to register Samba handlers");
187
188         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
189
190         /* Test SID behaviour */
191         torture_assert(torture, attr = ldb_dn_extended_syntax_by_name(ldb, "SID"), 
192                        "Failed to get SID DN syntax");
193         
194         string_sid_blob = data_blob_string_const(sid);
195
196         torture_assert_int_equal(torture, 
197                                  attr->read_fn(ldb, mem_ctx, 
198                                                &string_sid_blob, &binary_sid_blob), 0,
199                                  "Failed to parse string SID");
200         
201         torture_assert_data_blob_equal(torture, binary_sid_blob, sid_blob, 
202                                        "Read SID into blob form failed");
203
204         hex_sid_blob = data_blob_string_const(hex_sid);
205         
206         torture_assert_int_equal(torture, 
207                                  attr->read_fn(ldb, mem_ctx, 
208                                                &hex_sid_blob, &binary_sid_blob), 0,
209                                  "Failed to parse HEX SID");
210         
211         torture_assert_data_blob_equal(torture, binary_sid_blob, sid_blob, 
212                                        "Read SID into blob form failed");
213         
214         torture_assert_int_equal(torture, 
215                                  attr->read_fn(ldb, mem_ctx, 
216                                                &sid_blob, &binary_sid_blob), -1,
217                                  "Should have failed to parse binary SID");
218         
219         torture_assert_int_equal(torture, 
220                                  attr->write_hex_fn(ldb, mem_ctx, &sid_blob, &hex_sid_blob), 0,
221                                  "Failed to parse binary SID");
222         
223         torture_assert_data_blob_equal(torture, 
224                                        hex_sid_blob, data_blob_string_const(hex_sid),
225                                        "Write SID into HEX string form failed");
226         
227         torture_assert_int_equal(torture, 
228                                  attr->write_clear_fn(ldb, mem_ctx, &sid_blob, &string_sid_blob), 0,
229                                  "Failed to parse binary SID");
230         
231         torture_assert_data_blob_equal(torture, 
232                                        string_sid_blob, data_blob_string_const(sid),
233                                        "Write SID into clear string form failed");
234         
235
236         /* Test GUID behaviour */
237         torture_assert(torture, attr = ldb_dn_extended_syntax_by_name(ldb, "GUID"), 
238                        "Failed to get GUID DN syntax");
239         
240         string_guid_blob = data_blob_string_const(guid);
241
242         torture_assert_int_equal(torture, 
243                                  attr->read_fn(ldb, mem_ctx, 
244                                                &string_guid_blob, &binary_guid_blob), 0,
245                                  "Failed to parse string GUID");
246         
247         torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob, 
248                                        "Read GUID into blob form failed");
249         
250         hex_guid_blob = data_blob_string_const(hex_guid);
251         
252         torture_assert_int_equal(torture, 
253                                  attr->read_fn(ldb, mem_ctx, 
254                                                &hex_guid_blob, &binary_guid_blob), 0,
255                                  "Failed to parse HEX GUID");
256         
257         torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob, 
258                                        "Read GUID into blob form failed");
259         
260         torture_assert_int_equal(torture, 
261                                  attr->read_fn(ldb, mem_ctx, 
262                                                &guid_blob, &binary_guid_blob), -1,
263                                  "Should have failed to parse binary GUID");
264         
265         torture_assert_int_equal(torture, 
266                                  attr->write_hex_fn(ldb, mem_ctx, &guid_blob, &hex_guid_blob), 0,
267                                  "Failed to parse binary GUID");
268         
269         torture_assert_data_blob_equal(torture, 
270                                        hex_guid_blob, data_blob_string_const(hex_guid),
271                                        "Write GUID into HEX string form failed");
272         
273         torture_assert_int_equal(torture, 
274                                  attr->write_clear_fn(ldb, mem_ctx, &guid_blob, &string_guid_blob), 0,
275                                  "Failed to parse binary GUID");
276         
277         torture_assert_data_blob_equal(torture, 
278                                        string_guid_blob, data_blob_string_const(guid),
279                                        "Write GUID into clear string form failed");
280         
281
282
283         talloc_free(mem_ctx);
284         return true;
285 }
286
287 static bool torture_ldb_dn_extended(struct torture_context *torture)
288 {
289         TALLOC_CTX *mem_ctx = talloc_new(torture);
290         struct ldb_context *ldb;
291         struct ldb_dn *dn, *dn2;
292
293         DATA_BLOB sid_blob = strhex_to_data_blob(mem_ctx, hex_sid);
294         DATA_BLOB guid_blob = strhex_to_data_blob(mem_ctx, hex_guid);
295
296         const char *dn_str = "cn=admin,cn=users,dc=samba,dc=org";
297
298         torture_assert(torture, 
299                        ldb = ldb_init(mem_ctx, torture->ev),
300                        "Failed to init ldb");
301
302         torture_assert_int_equal(torture, 
303                                  ldb_register_samba_handlers(ldb), 0, 
304                                  "Failed to register Samba handlers");
305
306         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
307
308         /* Check behaviour of a normal DN */
309         torture_assert(torture, 
310                        dn = ldb_dn_new(mem_ctx, ldb, dn_str), 
311                        "Failed to create a 'normal' DN");
312
313         torture_assert(torture, 
314                        ldb_dn_validate(dn),
315                        "Failed to validate 'normal' DN");
316
317         torture_assert(torture, ldb_dn_has_extended(dn) == false, 
318                        "Should not find plain DN to be 'extended'");
319
320         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") == NULL, 
321                        "Should not find an SID on plain DN");
322
323         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") == NULL, 
324                        "Should not find an GUID on plain DN");
325         
326         torture_assert(torture, ldb_dn_get_extended_component(dn, "WKGUID") == NULL, 
327                        "Should not find an WKGUID on plain DN");
328         
329         /* Now make an extended DN */
330         torture_assert(torture, 
331                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>;<SID=%s>;%s",
332                                            guid, sid, dn_str), 
333                        "Failed to create an 'extended' DN");
334
335         torture_assert(torture, 
336                        dn2 = ldb_dn_copy(mem_ctx, dn), 
337                        "Failed to copy the 'extended' DN");
338         talloc_free(dn);
339         dn = dn2;
340
341         torture_assert(torture, 
342                        ldb_dn_validate(dn),
343                        "Failed to validate 'extended' DN");
344
345         torture_assert(torture, ldb_dn_has_extended(dn) == true, 
346                        "Should find extended DN to be 'extended'");
347
348         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") != NULL, 
349                        "Should find an SID on extended DN");
350
351         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") != NULL, 
352                        "Should find an GUID on extended DN");
353         
354         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "SID"), sid_blob, 
355                                        "Extended DN SID incorect");
356
357         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "GUID"), guid_blob, 
358                                        "Extended DN GUID incorect");
359
360         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), dn_str, 
361                                  "linearized DN incorrect");
362
363         torture_assert_str_equal(torture, ldb_dn_get_casefold(dn), strupper_talloc(mem_ctx, dn_str), 
364                                  "casefolded DN incorrect");
365
366         torture_assert_str_equal(torture, ldb_dn_get_component_name(dn, 0), "cn", 
367                                  "componet zero incorrect");
368
369         torture_assert_data_blob_equal(torture, *ldb_dn_get_component_val(dn, 0), data_blob_string_const("admin"), 
370                                  "componet zero incorrect");
371
372         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 1),
373                                  talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s", 
374                                                  guid, sid, dn_str),
375                                  "Clear extended linearized DN incorrect");
376
377         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0),
378                                  talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s", 
379                                                  hex_guid, hex_sid, dn_str),
380                                  "HEX extended linearized DN incorrect");
381
382         torture_assert(torture, ldb_dn_remove_child_components(dn, 1) == true,
383                                  "Failed to remove DN child");
384                        
385         torture_assert(torture, ldb_dn_has_extended(dn) == false, 
386                        "Extended DN flag should be cleared after child element removal");
387         
388         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") == NULL, 
389                        "Should not find an SID on DN");
390
391         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") == NULL, 
392                        "Should not find an GUID on DN");
393
394
395         /* TODO:  test setting these in the other order, and ensure it still comes out 'GUID first' */
396         torture_assert_int_equal(torture, ldb_dn_set_extended_component(dn, "GUID", &guid_blob), 0, 
397                        "Failed to set a GUID on DN");
398         
399         torture_assert_int_equal(torture, ldb_dn_set_extended_component(dn, "SID", &sid_blob), 0, 
400                        "Failed to set a SID on DN");
401
402         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "SID"), sid_blob, 
403                                        "Extended DN SID incorect");
404
405         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "GUID"), guid_blob, 
406                                        "Extended DN GUID incorect");
407
408         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "cn=users,dc=samba,dc=org", 
409                                  "linearized DN incorrect");
410
411         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 1),
412                                  talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s", 
413                                                  guid, sid, "cn=users,dc=samba,dc=org"),
414                                  "Clear extended linearized DN incorrect");
415
416         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0),
417                                  talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s", 
418                                                  hex_guid, hex_sid, "cn=users,dc=samba,dc=org"),
419                                  "HEX extended linearized DN incorrect");
420
421         /* Now check a 'just GUID' DN (clear format) */
422         torture_assert(torture, 
423                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>",
424                                            guid), 
425                        "Failed to create an 'extended' DN");
426
427         torture_assert(torture, 
428                        ldb_dn_validate(dn),
429                        "Failed to validate 'extended' DN");
430
431         torture_assert(torture, ldb_dn_has_extended(dn) == true, 
432                        "Should find extended DN to be 'extended'");
433
434         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") == NULL, 
435                        "Should not find an SID on this DN");
436
437         torture_assert_int_equal(torture, ldb_dn_get_comp_num(dn), 0, 
438                        "Should not find an 'normal' componet on this DN");
439
440         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") != NULL, 
441                        "Should find an GUID on this DN");
442         
443         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "GUID"), guid_blob, 
444                                        "Extended DN GUID incorect");
445
446         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "", 
447                                  "linearized DN incorrect");
448
449         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 1),
450                                  talloc_asprintf(mem_ctx, "<GUID=%s>", 
451                                                  guid),
452                                  "Clear extended linearized DN incorrect");
453
454         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0),
455                                  talloc_asprintf(mem_ctx, "<GUID=%s>", 
456                                                  hex_guid),
457                                  "HEX extended linearized DN incorrect");
458
459         /* Now check a 'just GUID' DN (HEX format) */
460         torture_assert(torture, 
461                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>",
462                                            hex_guid), 
463                        "Failed to create an 'extended' DN");
464
465         torture_assert(torture, 
466                        ldb_dn_validate(dn),
467                        "Failed to validate 'extended' DN");
468
469         torture_assert(torture, ldb_dn_has_extended(dn) == true, 
470                        "Should find extended DN to be 'extended'");
471
472         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") == NULL, 
473                        "Should not find an SID on this DN");
474
475         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") != NULL, 
476                        "Should find an GUID on this DN");
477         
478         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "GUID"), guid_blob, 
479                                        "Extended DN GUID incorect");
480
481         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "", 
482                                  "linearized DN incorrect");
483
484         /* Now check a 'just SID' DN (clear format) */
485         torture_assert(torture, 
486                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<SID=%s>",
487                                            sid), 
488                        "Failed to create an 'extended' DN");
489
490         torture_assert(torture, 
491                        ldb_dn_validate(dn),
492                        "Failed to validate 'extended' DN");
493
494         torture_assert(torture, ldb_dn_has_extended(dn) == true, 
495                        "Should find extended DN to be 'extended'");
496
497         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") == NULL, 
498                        "Should not find an SID on this DN");
499
500         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") != NULL, 
501                        "Should find an SID on this DN");
502         
503         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "SID"), sid_blob, 
504                                        "Extended DN SID incorect");
505
506         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "", 
507                                  "linearized DN incorrect");
508
509         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 1),
510                                  talloc_asprintf(mem_ctx, "<SID=%s>", 
511                                                  sid),
512                                  "Clear extended linearized DN incorrect");
513
514         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0),
515                                  talloc_asprintf(mem_ctx, "<SID=%s>", 
516                                                  hex_sid),
517                                  "HEX extended linearized DN incorrect");
518
519         /* Now check a 'just SID' DN (HEX format) */
520         torture_assert(torture, 
521                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<SID=%s>",
522                                            hex_sid), 
523                        "Failed to create an 'extended' DN");
524
525         torture_assert(torture, 
526                        ldb_dn_validate(dn),
527                        "Failed to validate 'extended' DN");
528
529         torture_assert(torture, ldb_dn_has_extended(dn) == true, 
530                        "Should find extended DN to be 'extended'");
531
532         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") == NULL, 
533                        "Should not find an SID on this DN");
534
535         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") != NULL, 
536                        "Should find an SID on this DN");
537         
538         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "SID"), sid_blob, 
539                                        "Extended DN SID incorect");
540
541         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "", 
542                                  "linearized DN incorrect");
543
544         talloc_free(mem_ctx);
545         return true;
546 }
547
548
549 static bool torture_ldb_dn(struct torture_context *torture)
550 {
551         TALLOC_CTX *mem_ctx = talloc_new(torture);
552         struct ldb_context *ldb;
553         struct ldb_dn *dn;
554         struct ldb_dn *child_dn;
555         struct ldb_dn *typo_dn;
556
557         torture_assert(torture, 
558                        ldb = ldb_init(mem_ctx, torture->ev),
559                        "Failed to init ldb");
560
561         torture_assert_int_equal(torture, 
562                                  ldb_register_samba_handlers(ldb), 0, 
563                                  "Failed to register Samba handlers");
564
565         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
566
567         /* Check behaviour of a normal DN */
568         torture_assert(torture, 
569                        dn = ldb_dn_new(mem_ctx, ldb, NULL), 
570                        "Failed to create a NULL DN");
571
572         torture_assert(torture, 
573                        ldb_dn_validate(dn),
574                        "Failed to validate NULL DN");
575
576         torture_assert(torture, 
577                        ldb_dn_add_base_fmt(dn, "dc=org"), 
578                        "Failed to add base DN");
579
580         torture_assert(torture, 
581                        ldb_dn_add_child_fmt(dn, "dc=samba"), 
582                        "Failed to add base DN");
583
584         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "dc=samba,dc=org", 
585                                  "linearized DN incorrect");
586
587         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0), "dc=samba,dc=org", 
588                                  "extended linearized DN incorrect");
589
590         /* Check child DN comparisons */
591         torture_assert(torture, 
592                        child_dn = ldb_dn_new(mem_ctx, ldb, "CN=users,DC=SAMBA,DC=org"), 
593                        "Failed to create child DN");
594
595         torture_assert(torture, 
596                        ldb_dn_compare(dn, child_dn) != 0,
597                        "Comparison on dc=samba,dc=org and CN=users,DC=SAMBA,DC=org should != 0");
598
599         torture_assert(torture, 
600                        ldb_dn_compare_base(child_dn, dn) != 0,
601                        "Base Comparison of CN=users,DC=SAMBA,DC=org and dc=samba,dc=org should != 0");
602
603         torture_assert(torture, 
604                        ldb_dn_compare_base(dn, child_dn) == 0,
605                        "Base Comparison on dc=samba,dc=org and CN=users,DC=SAMBA,DC=org should == 0");
606
607         /* Check comparisons with a truncated DN */
608         torture_assert(torture, 
609                        typo_dn = ldb_dn_new(mem_ctx, ldb, "c=samba,dc=org"), 
610                        "Failed to create 'typo' DN");
611
612         torture_assert(torture, 
613                        ldb_dn_compare(dn, typo_dn) != 0,
614                        "Comparison on dc=samba,dc=org and c=samba,dc=org should != 0");
615
616         torture_assert(torture, 
617                        ldb_dn_compare_base(typo_dn, dn) != 0,
618                        "Base Comparison of c=samba,dc=org and dc=samba,dc=org should != 0");
619
620         torture_assert(torture, 
621                        ldb_dn_compare_base(dn, typo_dn) != 0,
622                        "Base Comparison on dc=samba,dc=org and c=samba,dc=org should != 0");
623
624         talloc_free(mem_ctx);
625         return true;
626 }
627
628 static bool torture_ldb_dn_invalid_extended(struct torture_context *torture)
629 {
630         TALLOC_CTX *mem_ctx = talloc_new(torture);
631         struct ldb_context *ldb;
632         struct ldb_dn *dn;
633
634         const char *dn_str = "cn=admin,cn=users,dc=samba,dc=org";
635
636         torture_assert(torture, 
637                        ldb = ldb_init(mem_ctx, torture->ev),
638                        "Failed to init ldb");
639
640         torture_assert_int_equal(torture, 
641                                  ldb_register_samba_handlers(ldb), 0, 
642                                  "Failed to register Samba handlers");
643
644         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
645
646         /* Check behaviour of a normal DN */
647         torture_assert(torture, 
648                        dn = ldb_dn_new(mem_ctx, ldb, "samba,dc=org"), 
649                        "Failed to create a 'normal' invalid DN");
650
651         torture_assert(torture, 
652                        ldb_dn_validate(dn) == false,
653                        "should have failed to validate 'normal' invalid DN");
654
655         /* Now make an extended DN */
656         torture_assert(torture, 
657                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<PID=%s>;%s",
658                                            sid, dn_str), 
659                        "Failed to create an invalid 'extended' DN");
660
661         torture_assert(torture, 
662                        ldb_dn_validate(dn) == false,
663                        "should have failed to validate 'extended' DN");
664
665         torture_assert(torture, 
666                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>%s",
667                                            sid, dn_str), 
668                        "Failed to create an invalid 'extended' DN");
669
670         torture_assert(torture, 
671                        ldb_dn_validate(dn) == false,
672                        "should have failed to validate 'extended' DN");
673
674         torture_assert(torture, 
675                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>;",
676                                            sid), 
677                        "Failed to create an invalid 'extended' DN");
678
679         torture_assert(torture, 
680                        ldb_dn_validate(dn) == false,
681                        "should have failed to validate 'extended' DN");
682
683         torture_assert(torture, 
684                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>;",
685                                            hex_sid), 
686                        "Failed to create an invalid 'extended' DN");
687
688         torture_assert(torture, 
689                        ldb_dn_validate(dn) == false,
690                        "should have failed to validate 'extended' DN");
691
692         torture_assert(torture, 
693                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<SID=%s>;",
694                                            hex_guid), 
695                        "Failed to create an invalid 'extended' DN");
696
697         torture_assert(torture, 
698                        ldb_dn_validate(dn) == false,
699                        "should have failed to validate 'extended' DN");
700
701         torture_assert(torture, 
702                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<SID=%s>;",
703                                            guid), 
704                        "Failed to create an invalid 'extended' DN");
705
706         torture_assert(torture, 
707                        ldb_dn_validate(dn) == false,
708                        "should have failed to validate 'extended' DN");
709
710         torture_assert(torture, 
711                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=>"), 
712                        "Failed to create an invalid 'extended' DN");
713
714         torture_assert(torture, 
715                        ldb_dn_validate(dn) == false,
716                        "should have failed to validate 'extended' DN");
717
718         return true;
719 }
720
721 NTSTATUS torture_ldb_init(void)
722 {
723         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "LDB");
724         torture_suite_add_simple_test(suite, "ATTRS", torture_ldb_attrs);
725         torture_suite_add_simple_test(suite, "DN-ATTRS", torture_ldb_dn_attrs);
726         torture_suite_add_simple_test(suite, "DN-EXTENDED", torture_ldb_dn_extended);
727         torture_suite_add_simple_test(suite, "DN-INVALID-EXTENDED", torture_ldb_dn_invalid_extended);
728         torture_suite_add_simple_test(suite, "DN", torture_ldb_dn);
729
730         suite->description = talloc_strdup(suite, "LDB (samba-specific behaviour) tests");
731
732         torture_register_suite(suite);
733
734         return NT_STATUS_OK;
735 }