ldb: Avoid multiple tiny allocations during full DB scan
[sfrench/samba-autobuild/.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 <ldb.h>
25 #include <ldb-samba/ldb_wrap.h>
26 #include <ldb_errors.h>
27 #include <ldb_module.h>
28 #include "lib/ldb-samba/ldif_handlers.h"
29 #include "ldb_wrap.h"
30 #include "dsdb/samdb/samdb.h"
31 #include "param/param.h"
32 #include "torture/smbtorture.h"
33 #include "torture/local/proto.h"
34
35 static const char *sid = "S-1-5-21-4177067393-1453636373-93818737";
36 static const char *hex_sid = "01040000000000051500000081fdf8f815bba456718f9705";
37 static const char *guid = "975ac5fa-35d9-431d-b86a-845bcd34fff9";
38 static const char *guid2 = "{975ac5fa-35d9-431d-b86a-845bcd34fff9}";
39 static const char *hex_guid = "fac55a97d9351d43b86a845bcd34fff9";
40
41 static const char *prefix_map_newline = "2:1.2.840.113556.1.2\n5:2.16.840.1.101.2.2.3";
42 static const char *prefix_map_semi = "2:1.2.840.113556.1.2;5:2.16.840.1.101.2.2.3";
43
44 /**
45  * This is the hex code derived from the tdbdump for
46  * "st/ad_dc/private/sam.ldb.d/DC=ADDC,DC=SAMBA,DC=EXAMPLE,DC=COM.ldb"
47  * key "DN=CN=DDA1D01D-4BD7-4C49-A184-46F9241B560E,CN=OPERATIONS,CN=DOMAINUPDATES,CN=SYSTEM,DC=ADDC,DC=SAMBA,DC=EXAMPLE,DC=COM\00"
48  *   -- adrianc
49  */
50
51 static const uint8_t dda1d01d_bin[] = {
52         0x67, 0x19, 0x01, 0x26, 0x0d, 0x00, 0x00, 0x00, 0x43, 0x4e, 0x3d, 0x64, 0x64, 0x61, 0x31, 0x64,
53         0x30, 0x31, 0x64, 0x2d, 0x34, 0x62, 0x64, 0x37, 0x2d, 0x34, 0x63, 0x34, 0x39, 0x2d, 0x61, 0x31,
54         0x38, 0x34, 0x2d, 0x34, 0x36, 0x66, 0x39, 0x32, 0x34, 0x31, 0x62, 0x35, 0x36, 0x30, 0x65, 0x2c,
55         0x43, 0x4e, 0x3d, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2c, 0x43, 0x4e,
56         0x3d, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x73, 0x2c, 0x43,
57         0x4e, 0x3d, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x44, 0x43, 0x3d, 0x61, 0x64, 0x64, 0x63,
58         0x2c, 0x44, 0x43, 0x3d, 0x73, 0x61, 0x6d, 0x62, 0x61, 0x2c, 0x44, 0x43, 0x3d, 0x65, 0x78, 0x61,
59         0x6d, 0x70, 0x6c, 0x65, 0x2c, 0x44, 0x43, 0x3d, 0x63, 0x6f, 0x6d, 0x00, 0x6f, 0x62, 0x6a, 0x65,
60         0x63, 0x74, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
61         0x74, 0x6f, 0x70, 0x00, 0x09, 0x00, 0x00, 0x00, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65,
62         0x72, 0x00, 0x63, 0x6e, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x64, 0x64, 0x61,
63         0x31, 0x64, 0x30, 0x31, 0x64, 0x2d, 0x34, 0x62, 0x64, 0x37, 0x2d, 0x34, 0x63, 0x34, 0x39, 0x2d,
64         0x61, 0x31, 0x38, 0x34, 0x2d, 0x34, 0x36, 0x66, 0x39, 0x32, 0x34, 0x31, 0x62, 0x35, 0x36, 0x30,
65         0x65, 0x00, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x54, 0x79, 0x70, 0x65, 0x00, 0x01,
66         0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x34, 0x00, 0x77, 0x68, 0x65, 0x6e, 0x43, 0x72, 0x65,
67         0x61, 0x74, 0x65, 0x64, 0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x32, 0x30, 0x31,
68         0x35, 0x30, 0x37, 0x30, 0x38, 0x32, 0x32, 0x34, 0x33, 0x31, 0x30, 0x2e, 0x30, 0x5a, 0x00, 0x77,
69         0x68, 0x65, 0x6e, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x64, 0x00, 0x01, 0x00, 0x00, 0x00, 0x11,
70         0x00, 0x00, 0x00, 0x32, 0x30, 0x31, 0x35, 0x30, 0x37, 0x30, 0x38, 0x32, 0x32, 0x34, 0x33, 0x31,
71         0x30, 0x2e, 0x30, 0x5a, 0x00, 0x75, 0x53, 0x4e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x00,
72         0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x33, 0x34, 0x36, 0x37, 0x00, 0x75, 0x53, 0x4e,
73         0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x64, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
74         0x33, 0x34, 0x36, 0x37, 0x00, 0x73, 0x68, 0x6f, 0x77, 0x49, 0x6e, 0x41, 0x64, 0x76, 0x61, 0x6e,
75         0x63, 0x65, 0x64, 0x56, 0x69, 0x65, 0x77, 0x4f, 0x6e, 0x6c, 0x79, 0x00, 0x01, 0x00, 0x00, 0x00,
76         0x04, 0x00, 0x00, 0x00, 0x54, 0x52, 0x55, 0x45, 0x00, 0x6e, 0x54, 0x53, 0x65, 0x63, 0x75, 0x72,
77         0x69, 0x74, 0x79, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x00, 0x01, 0x00,
78         0x00, 0x00, 0x18, 0x05, 0x00, 0x00, 0x01, 0x00, 0x17, 0x8c, 0x14, 0x00, 0x00, 0x00, 0x30, 0x00,
79         0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01, 0x05, 0x00, 0x00, 0x00, 0x00,
80         0x00, 0x05, 0x15, 0x00, 0x00, 0x00, 0x9a, 0xbd, 0x91, 0x7d, 0xd5, 0xe0, 0x11, 0x3c, 0x6e, 0x5e,
81         0x1a, 0x4b, 0x00, 0x02, 0x00, 0x00, 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x15, 0x00,
82         0x00, 0x00, 0x9a, 0xbd, 0x91, 0x7d, 0xd5, 0xe0, 0x11, 0x3c, 0x6e, 0x5e, 0x1a, 0x4b, 0x00, 0x02,
83         0x00, 0x00, 0x04, 0x00, 0x78, 0x00, 0x02, 0x00, 0x00, 0x00, 0x07, 0x5a, 0x38, 0x00, 0x20, 0x00,
84         0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xbe, 0x3b, 0x0e, 0xf3, 0xf0, 0x9f, 0xd1, 0x11, 0xb6, 0x03,
85         0x00, 0x00, 0xf8, 0x03, 0x67, 0xc1, 0xa5, 0x7a, 0x96, 0xbf, 0xe6, 0x0d, 0xd0, 0x11, 0xa2, 0x85,
86         0x00, 0xaa, 0x00, 0x30, 0x49, 0xe2, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
87         0x00, 0x00, 0x07, 0x5a, 0x38, 0x00, 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xbf, 0x3b,
88         0x0e, 0xf3, 0xf0, 0x9f, 0xd1, 0x11, 0xb6, 0x03, 0x00, 0x00, 0xf8, 0x03, 0x67, 0xc1, 0xa5, 0x7a,
89         0x96, 0xbf, 0xe6, 0x0d, 0xd0, 0x11, 0xa2, 0x85, 0x00, 0xaa, 0x00, 0x30, 0x49, 0xe2, 0x01, 0x01,
90         0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x54, 0x04, 0x17, 0x00,
91         0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0xff, 0x01, 0x0f, 0x00, 0x01, 0x05, 0x00, 0x00, 0x00, 0x00,
92         0x00, 0x05, 0x15, 0x00, 0x00, 0x00, 0x9a, 0xbd, 0x91, 0x7d, 0xd5, 0xe0, 0x11, 0x3c, 0x6e, 0x5e,
93         0x1a, 0x4b, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0xff, 0x01, 0x0f, 0x00, 0x01, 0x01,
94         0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x94, 0x00,
95         0x02, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x0b, 0x00, 0x00, 0x00, 0x05, 0x1a,
96         0x3c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x16, 0x4c, 0xc0, 0x20,
97         0xd0, 0x11, 0xa7, 0x68, 0x00, 0xaa, 0x00, 0x6e, 0x05, 0x29, 0x14, 0xcc, 0x28, 0x48, 0x37, 0x14,
98         0xbc, 0x45, 0x9b, 0x07, 0xad, 0x6f, 0x01, 0x5e, 0x5f, 0x28, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00,
99         0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x05, 0x1a, 0x3c, 0x00, 0x10, 0x00,
100         0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x16, 0x4c, 0xc0, 0x20, 0xd0, 0x11, 0xa7, 0x68,
101         0x00, 0xaa, 0x00, 0x6e, 0x05, 0x29, 0xba, 0x7a, 0x96, 0xbf, 0xe6, 0x0d, 0xd0, 0x11, 0xa2, 0x85,
102         0x00, 0xaa, 0x00, 0x30, 0x49, 0xe2, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
103         0x00, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x05, 0x1a, 0x3c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x03, 0x00,
104         0x00, 0x00, 0x10, 0x20, 0x20, 0x5f, 0xa5, 0x79, 0xd0, 0x11, 0x90, 0x20, 0x00, 0xc0, 0x4f, 0xc2,
105         0xd4, 0xcf, 0x14, 0xcc, 0x28, 0x48, 0x37, 0x14, 0xbc, 0x45, 0x9b, 0x07, 0xad, 0x6f, 0x01, 0x5e,
106         0x5f, 0x28, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x02,
107         0x00, 0x00, 0x05, 0x1a, 0x3c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x10, 0x20,
108         0x20, 0x5f, 0xa5, 0x79, 0xd0, 0x11, 0x90, 0x20, 0x00, 0xc0, 0x4f, 0xc2, 0xd4, 0xcf, 0xba, 0x7a,
109         0x96, 0xbf, 0xe6, 0x0d, 0xd0, 0x11, 0xa2, 0x85, 0x00, 0xaa, 0x00, 0x30, 0x49, 0xe2, 0x01, 0x02,
110         0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x05, 0x1a,
111         0x3c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x40, 0xc2, 0x0a, 0xbc, 0xa9, 0x79,
112         0xd0, 0x11, 0x90, 0x20, 0x00, 0xc0, 0x4f, 0xc2, 0xd4, 0xcf, 0x14, 0xcc, 0x28, 0x48, 0x37, 0x14,
113         0xbc, 0x45, 0x9b, 0x07, 0xad, 0x6f, 0x01, 0x5e, 0x5f, 0x28, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00,
114         0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x05, 0x1a, 0x3c, 0x00, 0x10, 0x00,
115         0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x40, 0xc2, 0x0a, 0xbc, 0xa9, 0x79, 0xd0, 0x11, 0x90, 0x20,
116         0x00, 0xc0, 0x4f, 0xc2, 0xd4, 0xcf, 0xba, 0x7a, 0x96, 0xbf, 0xe6, 0x0d, 0xd0, 0x11, 0xa2, 0x85,
117         0x00, 0xaa, 0x00, 0x30, 0x49, 0xe2, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
118         0x00, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x05, 0x1a, 0x3c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x03, 0x00,
119         0x00, 0x00, 0x42, 0x2f, 0xba, 0x59, 0xa2, 0x79, 0xd0, 0x11, 0x90, 0x20, 0x00, 0xc0, 0x4f, 0xc2,
120         0xd3, 0xcf, 0x14, 0xcc, 0x28, 0x48, 0x37, 0x14, 0xbc, 0x45, 0x9b, 0x07, 0xad, 0x6f, 0x01, 0x5e,
121         0x5f, 0x28, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x02,
122         0x00, 0x00, 0x05, 0x1a, 0x3c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x42, 0x2f,
123         0xba, 0x59, 0xa2, 0x79, 0xd0, 0x11, 0x90, 0x20, 0x00, 0xc0, 0x4f, 0xc2, 0xd3, 0xcf, 0xba, 0x7a,
124         0x96, 0xbf, 0xe6, 0x0d, 0xd0, 0x11, 0xa2, 0x85, 0x00, 0xaa, 0x00, 0x30, 0x49, 0xe2, 0x01, 0x02,
125         0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x05, 0x1a,
126         0x3c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xf8, 0x88, 0x70, 0x03, 0xe1, 0x0a,
127         0xd2, 0x11, 0xb4, 0x22, 0x00, 0xa0, 0xc9, 0x68, 0xf9, 0x39, 0x14, 0xcc, 0x28, 0x48, 0x37, 0x14,
128         0xbc, 0x45, 0x9b, 0x07, 0xad, 0x6f, 0x01, 0x5e, 0x5f, 0x28, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00,
129         0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x05, 0x1a, 0x3c, 0x00, 0x10, 0x00,
130         0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xf8, 0x88, 0x70, 0x03, 0xe1, 0x0a, 0xd2, 0x11, 0xb4, 0x22,
131         0x00, 0xa0, 0xc9, 0x68, 0xf9, 0x39, 0xba, 0x7a, 0x96, 0xbf, 0xe6, 0x0d, 0xd0, 0x11, 0xa2, 0x85,
132         0x00, 0xaa, 0x00, 0x30, 0x49, 0xe2, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
133         0x00, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x05, 0x1a, 0x38, 0x00, 0x10, 0x00, 0x00, 0x00, 0x03, 0x00,
134         0x00, 0x00, 0x6d, 0x9e, 0xc6, 0xb7, 0xc7, 0x2c, 0xd2, 0x11, 0x85, 0x4e, 0x00, 0xa0, 0xc9, 0x83,
135         0xf6, 0x08, 0x86, 0x7a, 0x96, 0xbf, 0xe6, 0x0d, 0xd0, 0x11, 0xa2, 0x85, 0x00, 0xaa, 0x00, 0x30,
136         0x49, 0xe2, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x09, 0x00, 0x00, 0x00, 0x05, 0x1a,
137         0x38, 0x00, 0x10, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x6d, 0x9e, 0xc6, 0xb7, 0xc7, 0x2c,
138         0xd2, 0x11, 0x85, 0x4e, 0x00, 0xa0, 0xc9, 0x83, 0xf6, 0x08, 0x9c, 0x7a, 0x96, 0xbf, 0xe6, 0x0d,
139         0xd0, 0x11, 0xa2, 0x85, 0x00, 0xaa, 0x00, 0x30, 0x49, 0xe2, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
140         0x00, 0x05, 0x09, 0x00, 0x00, 0x00, 0x05, 0x1a, 0x38, 0x00, 0x10, 0x00, 0x00, 0x00, 0x03, 0x00,
141         0x00, 0x00, 0x6d, 0x9e, 0xc6, 0xb7, 0xc7, 0x2c, 0xd2, 0x11, 0x85, 0x4e, 0x00, 0xa0, 0xc9, 0x83,
142         0xf6, 0x08, 0xba, 0x7a, 0x96, 0xbf, 0xe6, 0x0d, 0xd0, 0x11, 0xa2, 0x85, 0x00, 0xaa, 0x00, 0x30,
143         0x49, 0xe2, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x09, 0x00, 0x00, 0x00, 0x05, 0x1a,
144         0x2c, 0x00, 0x94, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x14, 0xcc, 0x28, 0x48, 0x37, 0x14,
145         0xbc, 0x45, 0x9b, 0x07, 0xad, 0x6f, 0x01, 0x5e, 0x5f, 0x28, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00,
146         0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x05, 0x1a, 0x2c, 0x00, 0x94, 0x00,
147         0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x9c, 0x7a, 0x96, 0xbf, 0xe6, 0x0d, 0xd0, 0x11, 0xa2, 0x85,
148         0x00, 0xaa, 0x00, 0x30, 0x49, 0xe2, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
149         0x00, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x05, 0x1a, 0x2c, 0x00, 0x94, 0x00, 0x02, 0x00, 0x02, 0x00,
150         0x00, 0x00, 0xba, 0x7a, 0x96, 0xbf, 0xe6, 0x0d, 0xd0, 0x11, 0xa2, 0x85, 0x00, 0xaa, 0x00, 0x30,
151         0x49, 0xe2, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x02,
152         0x00, 0x00, 0x05, 0x12, 0x28, 0x00, 0x30, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xde, 0x47,
153         0xe6, 0x91, 0x6f, 0xd9, 0x70, 0x4b, 0x95, 0x57, 0xd6, 0x3f, 0xf4, 0xf3, 0xcc, 0xd8, 0x01, 0x01,
154         0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x12, 0x24, 0x00, 0xff, 0x01,
155         0x0f, 0x00, 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x15, 0x00, 0x00, 0x00, 0x9a, 0xbd,
156         0x91, 0x7d, 0xd5, 0xe0, 0x11, 0x3c, 0x6e, 0x5e, 0x1a, 0x4b, 0x07, 0x02, 0x00, 0x00, 0x00, 0x12,
157         0x18, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00,
158         0x00, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x00, 0x12, 0x18, 0x00, 0xbd, 0x01, 0x0f, 0x00, 0x01, 0x02,
159         0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00, 0x20, 0x02, 0x00, 0x00, 0x00, 0x6e,
160         0x61, 0x6d, 0x65, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x64, 0x64, 0x61, 0x31,
161         0x64, 0x30, 0x31, 0x64, 0x2d, 0x34, 0x62, 0x64, 0x37, 0x2d, 0x34, 0x63, 0x34, 0x39, 0x2d, 0x61,
162         0x31, 0x38, 0x34, 0x2d, 0x34, 0x36, 0x66, 0x39, 0x32, 0x34, 0x31, 0x62, 0x35, 0x36, 0x30, 0x65,
163         0x00, 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x47, 0x55, 0x49, 0x44, 0x00, 0x01, 0x00, 0x00, 0x00,
164         0x10, 0x00, 0x00, 0x00, 0x57, 0x93, 0x1e, 0x29, 0x25, 0x49, 0xe5, 0x40, 0x9d, 0x98, 0x36, 0x07,
165         0x11, 0x9e, 0xbd, 0xe5, 0x00, 0x72, 0x65, 0x70, 0x6c, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74,
166         0x79, 0x4d, 0x65, 0x74, 0x61, 0x44, 0x61, 0x74, 0x61, 0x00, 0x01, 0x00, 0x00, 0x00, 0x90, 0x01,
167         0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
168         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7e, 0x38, 0xae, 0x0b, 0x03, 0x00,
169         0x00, 0x00, 0x9d, 0xcd, 0xcd, 0x57, 0xee, 0x58, 0x6e, 0x4e, 0x96, 0x99, 0xcc, 0x7d, 0xe1, 0x96,
170         0xf1, 0x05, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00,
171         0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7e, 0x38, 0xae, 0x0b, 0x03, 0x00,
172         0x00, 0x00, 0x9d, 0xcd, 0xcd, 0x57, 0xee, 0x58, 0x6e, 0x4e, 0x96, 0x99, 0xcc, 0x7d, 0xe1, 0x96,
173         0xf1, 0x05, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00,
174         0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7e, 0x38, 0xae, 0x0b, 0x03, 0x00,
175         0x00, 0x00, 0x9d, 0xcd, 0xcd, 0x57, 0xee, 0x58, 0x6e, 0x4e, 0x96, 0x99, 0xcc, 0x7d, 0xe1, 0x96,
176         0xf1, 0x05, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00,
177         0x00, 0x00, 0xa9, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7e, 0x38, 0xae, 0x0b, 0x03, 0x00,
178         0x00, 0x00, 0x9d, 0xcd, 0xcd, 0x57, 0xee, 0x58, 0x6e, 0x4e, 0x96, 0x99, 0xcc, 0x7d, 0xe1, 0x96,
179         0xf1, 0x05, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00,
180         0x00, 0x00, 0x19, 0x01, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7e, 0x38, 0xae, 0x0b, 0x03, 0x00,
181         0x00, 0x00, 0x9d, 0xcd, 0xcd, 0x57, 0xee, 0x58, 0x6e, 0x4e, 0x96, 0x99, 0xcc, 0x7d, 0xe1, 0x96,
182         0xf1, 0x05, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00,
183         0x00, 0x00, 0x01, 0x00, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7e, 0x38, 0xae, 0x0b, 0x03, 0x00,
184         0x00, 0x00, 0x9d, 0xcd, 0xcd, 0x57, 0xee, 0x58, 0x6e, 0x4e, 0x96, 0x99, 0xcc, 0x7d, 0xe1, 0x96,
185         0xf1, 0x05, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00,
186         0x00, 0x00, 0x0e, 0x03, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7e, 0x38, 0xae, 0x0b, 0x03, 0x00,
187         0x00, 0x00, 0x9d, 0xcd, 0xcd, 0x57, 0xee, 0x58, 0x6e, 0x4e, 0x96, 0x99, 0xcc, 0x7d, 0xe1, 0x96,
188         0xf1, 0x05, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00,
189         0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7e, 0x38, 0xae, 0x0b, 0x03, 0x00,
190         0x00, 0x00, 0x9d, 0xcd, 0xcd, 0x57, 0xee, 0x58, 0x6e, 0x4e, 0x96, 0x99, 0xcc, 0x7d, 0xe1, 0x96,
191         0xf1, 0x05, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00,
192         0x00, 0x00, 0x00, 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x43, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72,
193         0x79, 0x00, 0x01, 0x00, 0x00, 0x00, 0x76, 0x00, 0x00, 0x00, 0x3c, 0x47, 0x55, 0x49, 0x44, 0x3d,
194         0x35, 0x32, 0x34, 0x32, 0x39, 0x30, 0x33, 0x38, 0x2d, 0x65, 0x34, 0x33, 0x35, 0x2d, 0x34, 0x66,
195         0x65, 0x33, 0x2d, 0x39, 0x36, 0x34, 0x65, 0x2d, 0x38, 0x30, 0x64, 0x61, 0x31, 0x35, 0x34, 0x39,
196         0x39, 0x63, 0x39, 0x63, 0x3e, 0x3b, 0x43, 0x4e, 0x3d, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e,
197         0x65, 0x72, 0x2c, 0x43, 0x4e, 0x3d, 0x53, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x2c, 0x43, 0x4e, 0x3d,
198         0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2c, 0x44, 0x43,
199         0x3d, 0x61, 0x64, 0x64, 0x63, 0x2c, 0x44, 0x43, 0x3d, 0x73, 0x61, 0x6d, 0x62, 0x61, 0x2c, 0x44,
200         0x43, 0x3d, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2c, 0x44, 0x43, 0x3d, 0x63, 0x6f, 0x6d,
201         0x00
202 };
203
204 static const char dda1d01d_ldif[] = ""
205 "dn: CN=dda1d01d-4bd7-4c49-a184-46f9241b560e,CN=Operations,CN=DomainUpdates,CN=System,DC=addc,DC=samba,DC=example,DC=com\n"
206 "objectClass: top\n"
207 "objectClass: container\n"
208 "cn: dda1d01d-4bd7-4c49-a184-46f9241b560e\n"
209 "instanceType: 4\n"
210 "whenCreated: 20150708224310.0Z\n"
211 "whenChanged: 20150708224310.0Z\n"
212 "uSNCreated: 3467\n"
213 "uSNChanged: 3467\n"
214 "showInAdvancedViewOnly: TRUE\n"
215 "nTSecurityDescriptor: O:S-1-5-21-2106703258-1007804629-1260019310-512G:S-1-5-2\n"
216 " 1-2106703258-1007804629-1260019310-512D:AI(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;S-\n"
217 " 1-5-21-2106703258-1007804629-1260019310-512)(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;\n"
218 " SY)(A;;RPLCLORC;;;AU)(OA;CIIOID;RP;4c164200-20c0-11d0-a768-00aa006e0529;4828c\n"
219 " c14-1437-45bc-9b07-ad6f015e5f28;RU)(OA;CIIOID;RP;4c164200-20c0-11d0-a768-00aa\n"
220 " 006e0529;bf967aba-0de6-11d0-a285-00aa003049e2;RU)(OA;CIIOID;RP;5f202010-79a5-\n"
221 " 11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)(OA;CIIOID;RP;\n"
222 " 5f202010-79a5-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)\n"
223 " (OA;CIIOID;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad\n"
224 " 6f015e5f28;RU)(OA;CIIOID;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;bf967aba-0de\n"
225 " 6-11d0-a285-00aa003049e2;RU)(OA;CIIOID;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3c\n"
226 " f;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)(OA;CIIOID;RP;59ba2f42-79a2-11d0-90\n"
227 " 20-00c04fc2d3cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)(OA;CIIOID;RP;037088f\n"
228 " 8-0ae1-11d2-b422-00a0c968f939;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)(OA;CII\n"
229 " OID;RP;037088f8-0ae1-11d2-b422-00a0c968f939;bf967aba-0de6-11d0-a285-00aa00304\n"
230 " 9e2;RU)(OA;CIIOID;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a86-0de6-11d0-\n"
231 " a285-00aa003049e2;ED)(OA;CIIOID;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967\n"
232 " a9c-0de6-11d0-a285-00aa003049e2;ED)(OA;CIIOID;RP;b7c69e6d-2cc7-11d2-854e-00a0\n"
233 " c983f608;bf967aba-0de6-11d0-a285-00aa003049e2;ED)(OA;CIIOID;RPLCLORC;;4828cc1\n"
234 " 4-1437-45bc-9b07-ad6f015e5f28;RU)(OA;CIIOID;RPLCLORC;;bf967a9c-0de6-11d0-a285\n"
235 " -00aa003049e2;RU)(OA;CIIOID;RPLCLORC;;bf967aba-0de6-11d0-a285-00aa003049e2;RU\n"
236 " )(OA;CIID;RPWPCR;91e647de-d96f-4b70-9557-d63ff4f3ccd8;;PS)(A;CIID;RPWPCRCCDCL\n"
237 " CLORCWOWDSDDTSW;;;S-1-5-21-2106703258-1007804629-1260019310-519)(A;CIID;LC;;;\n"
238 " RU)(A;CIID;RPWPCRCCLCLORCWOWDSDSW;;;BA)S:AI(OU;CIIOIDSA;WP;f30e3bbe-9ff0-11d1\n"
239 " -b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)(OU;CIIOIDSA;WP;f3\n"
240 " 0e3bbf-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)\n"
241 "name: dda1d01d-4bd7-4c49-a184-46f9241b560e\n"
242 "objectGUID: 291e9357-4925-40e5-9d98-3607119ebde5\n"
243 "replPropertyMetaData:: AQAAAAAAAAAIAAAAAAAAAAAAAAABAAAAfjiuCwMAAACdzc1X7lhuTpa\n"
244 " ZzH3hlvEFiw0AAAAAAACLDQAAAAAAAAEAAgABAAAAfjiuCwMAAACdzc1X7lhuTpaZzH3hlvEFiw0A\n"
245 " AAAAAACLDQAAAAAAAAIAAgABAAAAfjiuCwMAAACdzc1X7lhuTpaZzH3hlvEFiw0AAAAAAACLDQAAA\n"
246 " AAAAKkAAgABAAAAfjiuCwMAAACdzc1X7lhuTpaZzH3hlvEFiw0AAAAAAACLDQAAAAAAABkBAgABAA\n"
247 " AAfjiuCwMAAACdzc1X7lhuTpaZzH3hlvEFiw0AAAAAAACLDQAAAAAAAAEACQABAAAAfjiuCwMAAAC\n"
248 " dzc1X7lhuTpaZzH3hlvEFiw0AAAAAAACLDQAAAAAAAA4DCQABAAAAfjiuCwMAAACdzc1X7lhuTpaZ\n"
249 " zH3hlvEFiw0AAAAAAACLDQAAAAAAAAMAAAABAAAAfjiuCwMAAACdzc1X7lhuTpaZzH3hlvEFiw0AA\n"
250 " AAAAACLDQAAAAAAAA==\n"
251 "objectCategory: <GUID=52429038-e435-4fe3-964e-80da15499c9c>;CN=Container,CN=Sc\n"
252 " hema,CN=Configuration,DC=addc,DC=samba,DC=example,DC=com\n\n";
253
254 static const char *dda1d01d_ldif_reduced = ""
255 "dn: CN=dda1d01d-4bd7-4c49-a184-46f9241b560e,CN=Operations,CN=DomainUpdates,CN=System,DC=addc,DC=samba,DC=example,DC=com\n"
256 "objectClass: top\n"
257 "objectClass: container\n"
258 "instanceType: 4\n"
259 "whenChanged: 20150708224310.0Z\n"
260 "uSNCreated: 3467\n"
261 "showInAdvancedViewOnly: TRUE\n"
262 "name: dda1d01d-4bd7-4c49-a184-46f9241b560e\n\n";
263
264 static bool torture_ldb_attrs(struct torture_context *torture)
265 {
266         TALLOC_CTX *mem_ctx = talloc_new(torture);
267         struct ldb_context *ldb;
268         const struct ldb_schema_attribute *attr;
269         struct ldb_val string_sid_blob, binary_sid_blob;
270         struct ldb_val string_guid_blob, string_guid_blob2, binary_guid_blob;
271         struct ldb_val string_prefix_map_newline_blob, string_prefix_map_semi_blob, string_prefix_map_blob;
272         struct ldb_val prefix_map_blob;
273
274         DATA_BLOB sid_blob = strhex_to_data_blob(mem_ctx, hex_sid);
275         DATA_BLOB guid_blob = strhex_to_data_blob(mem_ctx, hex_guid);
276
277         torture_assert(torture, 
278                        ldb = ldb_init(mem_ctx, torture->ev),
279                        "Failed to init ldb");
280
281         torture_assert_int_equal(torture, 
282                                  ldb_register_samba_handlers(ldb), LDB_SUCCESS,
283                                  "Failed to register Samba handlers");
284
285         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
286
287         /* Test SID behaviour */
288         torture_assert(torture, attr = ldb_schema_attribute_by_name(ldb, "objectSid"), 
289                        "Failed to get objectSid schema attribute");
290         
291         string_sid_blob = data_blob_string_const(sid);
292
293         torture_assert_int_equal(torture, 
294                                  attr->syntax->ldif_read_fn(ldb, mem_ctx, 
295                                                             &string_sid_blob, &binary_sid_blob), 0,
296                                  "Failed to parse string SID");
297         
298         torture_assert_data_blob_equal(torture, binary_sid_blob, sid_blob, 
299                                        "Read SID into blob form failed");
300         
301         torture_assert_int_equal(torture, 
302                                  attr->syntax->ldif_read_fn(ldb, mem_ctx, 
303                                                             &sid_blob, &binary_sid_blob), -1,
304                                  "Should have failed to parse binary SID");
305         
306         torture_assert_int_equal(torture, 
307                                  attr->syntax->ldif_write_fn(ldb, mem_ctx, &binary_sid_blob, &string_sid_blob), 0,
308                                  "Failed to parse binary SID");
309         
310         torture_assert_data_blob_equal(torture, 
311                                        string_sid_blob, data_blob_string_const(sid),
312                                        "Write SID into string form failed");
313         
314         torture_assert_int_equal(torture, 
315                                  attr->syntax->comparison_fn(ldb, mem_ctx, &binary_sid_blob, &string_sid_blob), 0,
316                                  "Failed to compare binary and string SID");
317         
318         torture_assert_int_equal(torture, 
319                                  attr->syntax->comparison_fn(ldb, mem_ctx, &string_sid_blob, &binary_sid_blob), 0,
320                                  "Failed to compare string and binary binary SID");
321         
322         torture_assert_int_equal(torture, 
323                                  attr->syntax->comparison_fn(ldb, mem_ctx, &string_sid_blob, &string_sid_blob), 0,
324                                  "Failed to compare string and string SID");
325         
326         torture_assert_int_equal(torture, 
327                                  attr->syntax->comparison_fn(ldb, mem_ctx, &binary_sid_blob, &binary_sid_blob), 0,
328                                  "Failed to compare binary and binary SID");
329         
330         torture_assert(torture, attr->syntax->comparison_fn(ldb, mem_ctx, &guid_blob, &binary_sid_blob) != 0,
331                        "Failed to distinguish binary GUID and binary SID");
332
333
334         /* Test GUID behaviour */
335         torture_assert(torture, attr = ldb_schema_attribute_by_name(ldb, "objectGUID"), 
336                        "Failed to get objectGUID schema attribute");
337         
338         string_guid_blob = data_blob_string_const(guid);
339
340         torture_assert_int_equal(torture, 
341                                  attr->syntax->ldif_read_fn(ldb, mem_ctx, 
342                                                             &string_guid_blob, &binary_guid_blob), 0,
343                                  "Failed to parse string GUID");
344         
345         torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob, 
346                                        "Read GUID into blob form failed");
347         
348         string_guid_blob2 = data_blob_string_const(guid2);
349         
350         torture_assert_int_equal(torture, 
351                                  attr->syntax->ldif_read_fn(ldb, mem_ctx, 
352                                                             &string_guid_blob2, &binary_guid_blob), 0,
353                                  "Failed to parse string GUID");
354         
355         torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob, 
356                                        "Read GUID into blob form failed");
357         
358         torture_assert_int_equal(torture, 
359                                  attr->syntax->ldif_read_fn(ldb, mem_ctx, 
360                                                             &guid_blob, &binary_guid_blob), 0,
361                                  "Failed to parse binary GUID");
362         
363         torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob, 
364                                        "Read GUID into blob form failed");
365         
366         torture_assert_int_equal(torture, 
367                                  attr->syntax->ldif_write_fn(ldb, mem_ctx, &binary_guid_blob, &string_guid_blob), 0,
368                                  "Failed to print binary GUID as string");
369
370         torture_assert_data_blob_equal(torture, string_sid_blob, data_blob_string_const(sid),
371                                        "Write SID into string form failed");
372         
373         torture_assert_int_equal(torture, 
374                                  attr->syntax->comparison_fn(ldb, mem_ctx, &binary_guid_blob, &string_guid_blob), 0,
375                                  "Failed to compare binary and string GUID");
376         
377         torture_assert_int_equal(torture, 
378                                  attr->syntax->comparison_fn(ldb, mem_ctx, &string_guid_blob, &binary_guid_blob), 0,
379                                  "Failed to compare string and binary binary GUID");
380         
381         torture_assert_int_equal(torture, 
382                                  attr->syntax->comparison_fn(ldb, mem_ctx, &string_guid_blob, &string_guid_blob), 0,
383                                  "Failed to compare string and string GUID");
384         
385         torture_assert_int_equal(torture, 
386                                  attr->syntax->comparison_fn(ldb, mem_ctx, &binary_guid_blob, &binary_guid_blob), 0,
387                                  "Failed to compare binary and binary GUID");
388         
389         string_prefix_map_newline_blob = data_blob_string_const(prefix_map_newline);
390         
391         string_prefix_map_semi_blob = data_blob_string_const(prefix_map_semi);
392         
393         /* Test prefixMap behaviour */
394         torture_assert(torture, attr = ldb_schema_attribute_by_name(ldb, "prefixMap"), 
395                        "Failed to get prefixMap schema attribute");
396         
397         torture_assert_int_equal(torture, 
398                                  attr->syntax->comparison_fn(ldb, mem_ctx, &string_prefix_map_newline_blob, &string_prefix_map_semi_blob), 0,
399                                  "Failed to compare prefixMap with newlines and prefixMap with semicolons");
400         
401         torture_assert_int_equal(torture, 
402                                  attr->syntax->ldif_read_fn(ldb, mem_ctx, &string_prefix_map_newline_blob, &prefix_map_blob), 0,
403                                  "Failed to read prefixMap with newlines");
404         torture_assert_int_equal(torture, 
405                                  attr->syntax->comparison_fn(ldb, mem_ctx, &string_prefix_map_newline_blob, &prefix_map_blob), 0,
406                                  "Failed to compare prefixMap with newlines and prefixMap binary");
407         
408         torture_assert_int_equal(torture, 
409                                  attr->syntax->ldif_write_fn(ldb, mem_ctx, &prefix_map_blob, &string_prefix_map_blob), 0,
410                                  "Failed to write prefixMap");
411         torture_assert_int_equal(torture, 
412                                  attr->syntax->comparison_fn(ldb, mem_ctx, &string_prefix_map_blob, &prefix_map_blob), 0,
413                                  "Failed to compare prefixMap ldif write and prefixMap binary");
414         
415         torture_assert_data_blob_equal(torture, string_prefix_map_blob, string_prefix_map_semi_blob,
416                 "Failed to compare prefixMap ldif write and prefixMap binary");
417         
418
419
420         talloc_free(mem_ctx);
421         return true;
422 }
423
424 static bool torture_ldb_dn_attrs(struct torture_context *torture)
425 {
426         TALLOC_CTX *mem_ctx = talloc_new(torture);
427         struct ldb_context *ldb;
428         const struct ldb_dn_extended_syntax *attr;
429         struct ldb_val string_sid_blob, binary_sid_blob;
430         struct ldb_val string_guid_blob, binary_guid_blob;
431         struct ldb_val hex_sid_blob, hex_guid_blob;
432
433         DATA_BLOB sid_blob = strhex_to_data_blob(mem_ctx, hex_sid);
434         DATA_BLOB guid_blob = strhex_to_data_blob(mem_ctx, hex_guid);
435
436         torture_assert(torture, 
437                        ldb = ldb_init(mem_ctx, torture->ev),
438                        "Failed to init ldb");
439
440         torture_assert_int_equal(torture, 
441                                  ldb_register_samba_handlers(ldb), LDB_SUCCESS,
442                                  "Failed to register Samba handlers");
443
444         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
445
446         /* Test SID behaviour */
447         torture_assert(torture, attr = ldb_dn_extended_syntax_by_name(ldb, "SID"), 
448                        "Failed to get SID DN syntax");
449         
450         string_sid_blob = data_blob_string_const(sid);
451
452         torture_assert_int_equal(torture, 
453                                  attr->read_fn(ldb, mem_ctx, 
454                                                &string_sid_blob, &binary_sid_blob), 0,
455                                  "Failed to parse string SID");
456         
457         torture_assert_data_blob_equal(torture, binary_sid_blob, sid_blob, 
458                                        "Read SID into blob form failed");
459
460         hex_sid_blob = data_blob_string_const(hex_sid);
461         
462         torture_assert_int_equal(torture, 
463                                  attr->read_fn(ldb, mem_ctx, 
464                                                &hex_sid_blob, &binary_sid_blob), 0,
465                                  "Failed to parse HEX SID");
466         
467         torture_assert_data_blob_equal(torture, binary_sid_blob, sid_blob, 
468                                        "Read SID into blob form failed");
469         
470         torture_assert_int_equal(torture, 
471                                  attr->read_fn(ldb, mem_ctx, 
472                                                &sid_blob, &binary_sid_blob), -1,
473                                  "Should have failed to parse binary SID");
474         
475         torture_assert_int_equal(torture, 
476                                  attr->write_hex_fn(ldb, mem_ctx, &sid_blob, &hex_sid_blob), 0,
477                                  "Failed to parse binary SID");
478         
479         torture_assert_data_blob_equal(torture, 
480                                        hex_sid_blob, data_blob_string_const(hex_sid),
481                                        "Write SID into HEX string form failed");
482         
483         torture_assert_int_equal(torture, 
484                                  attr->write_clear_fn(ldb, mem_ctx, &sid_blob, &string_sid_blob), 0,
485                                  "Failed to parse binary SID");
486         
487         torture_assert_data_blob_equal(torture, 
488                                        string_sid_blob, data_blob_string_const(sid),
489                                        "Write SID into clear string form failed");
490         
491
492         /* Test GUID behaviour */
493         torture_assert(torture, attr = ldb_dn_extended_syntax_by_name(ldb, "GUID"), 
494                        "Failed to get GUID DN syntax");
495         
496         string_guid_blob = data_blob_string_const(guid);
497
498         torture_assert_int_equal(torture, 
499                                  attr->read_fn(ldb, mem_ctx, 
500                                                &string_guid_blob, &binary_guid_blob), 0,
501                                  "Failed to parse string GUID");
502         
503         torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob, 
504                                        "Read GUID into blob form failed");
505         
506         hex_guid_blob = data_blob_string_const(hex_guid);
507         
508         torture_assert_int_equal(torture, 
509                                  attr->read_fn(ldb, mem_ctx, 
510                                                &hex_guid_blob, &binary_guid_blob), 0,
511                                  "Failed to parse HEX GUID");
512         
513         torture_assert_data_blob_equal(torture, binary_guid_blob, guid_blob, 
514                                        "Read GUID into blob form failed");
515         
516         torture_assert_int_equal(torture, 
517                                  attr->read_fn(ldb, mem_ctx, 
518                                                &guid_blob, &binary_guid_blob), -1,
519                                  "Should have failed to parse binary GUID");
520         
521         torture_assert_int_equal(torture, 
522                                  attr->write_hex_fn(ldb, mem_ctx, &guid_blob, &hex_guid_blob), 0,
523                                  "Failed to parse binary GUID");
524         
525         torture_assert_data_blob_equal(torture, 
526                                        hex_guid_blob, data_blob_string_const(hex_guid),
527                                        "Write GUID into HEX string form failed");
528         
529         torture_assert_int_equal(torture, 
530                                  attr->write_clear_fn(ldb, mem_ctx, &guid_blob, &string_guid_blob), 0,
531                                  "Failed to parse binary GUID");
532         
533         torture_assert_data_blob_equal(torture, 
534                                        string_guid_blob, data_blob_string_const(guid),
535                                        "Write GUID into clear string form failed");
536         
537
538
539         talloc_free(mem_ctx);
540         return true;
541 }
542
543 static bool torture_ldb_dn_extended(struct torture_context *torture)
544 {
545         TALLOC_CTX *mem_ctx = talloc_new(torture);
546         struct ldb_context *ldb;
547         struct ldb_dn *dn, *dn2;
548
549         DATA_BLOB sid_blob = strhex_to_data_blob(mem_ctx, hex_sid);
550         DATA_BLOB guid_blob = strhex_to_data_blob(mem_ctx, hex_guid);
551
552         const char *dn_str = "cn=admin,cn=users,dc=samba,dc=org";
553
554         torture_assert(torture, 
555                        ldb = ldb_init(mem_ctx, torture->ev),
556                        "Failed to init ldb");
557
558         torture_assert_int_equal(torture, 
559                                  ldb_register_samba_handlers(ldb), LDB_SUCCESS,
560                                  "Failed to register Samba handlers");
561
562         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
563
564         /* Check behaviour of a normal DN */
565         torture_assert(torture, 
566                        dn = ldb_dn_new(mem_ctx, ldb, dn_str), 
567                        "Failed to create a 'normal' DN");
568
569         torture_assert(torture, 
570                        ldb_dn_validate(dn),
571                        "Failed to validate 'normal' DN");
572
573         torture_assert(torture, ldb_dn_has_extended(dn) == false, 
574                        "Should not find plain DN to be 'extended'");
575
576         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") == NULL, 
577                        "Should not find an SID on plain DN");
578
579         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") == NULL, 
580                        "Should not find an GUID on plain DN");
581         
582         torture_assert(torture, ldb_dn_get_extended_component(dn, "WKGUID") == NULL, 
583                        "Should not find an WKGUID on plain DN");
584         
585         /* Now make an extended DN */
586         torture_assert(torture, 
587                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>;<SID=%s>;%s",
588                                            guid, sid, dn_str), 
589                        "Failed to create an 'extended' DN");
590
591         torture_assert(torture, 
592                        dn2 = ldb_dn_copy(mem_ctx, dn), 
593                        "Failed to copy the 'extended' DN");
594         talloc_free(dn);
595         dn = dn2;
596
597         torture_assert(torture, 
598                        ldb_dn_validate(dn),
599                        "Failed to validate 'extended' DN");
600
601         torture_assert(torture, ldb_dn_has_extended(dn) == true, 
602                        "Should find extended DN to be 'extended'");
603
604         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") != NULL, 
605                        "Should find an SID on extended DN");
606
607         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") != NULL, 
608                        "Should find an GUID on extended DN");
609         
610         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "SID"), sid_blob, 
611                                        "Extended DN SID incorect");
612
613         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "GUID"), guid_blob, 
614                                        "Extended DN GUID incorect");
615
616         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), dn_str, 
617                                  "linearized DN incorrect");
618
619         torture_assert_str_equal(torture, ldb_dn_get_casefold(dn), strupper_talloc(mem_ctx, dn_str), 
620                                  "casefolded DN incorrect");
621
622         torture_assert_str_equal(torture, ldb_dn_get_component_name(dn, 0), "cn", 
623                                  "componet zero incorrect");
624
625         torture_assert_data_blob_equal(torture, *ldb_dn_get_component_val(dn, 0), data_blob_string_const("admin"), 
626                                  "componet zero incorrect");
627
628         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 1),
629                                  talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s", 
630                                                  guid, sid, dn_str),
631                                  "Clear extended linearized DN incorrect");
632
633         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0),
634                                  talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s", 
635                                                  hex_guid, hex_sid, dn_str),
636                                  "HEX extended linearized DN incorrect");
637
638         torture_assert(torture, ldb_dn_remove_child_components(dn, 1) == true,
639                                  "Failed to remove DN child");
640                        
641         torture_assert(torture, ldb_dn_has_extended(dn) == false, 
642                        "Extended DN flag should be cleared after child element removal");
643         
644         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") == NULL, 
645                        "Should not find an SID on DN");
646
647         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") == NULL, 
648                        "Should not find an GUID on DN");
649
650
651         /* TODO:  test setting these in the other order, and ensure it still comes out 'GUID first' */
652         torture_assert_int_equal(torture, ldb_dn_set_extended_component(dn, "GUID", &guid_blob), 0, 
653                        "Failed to set a GUID on DN");
654         
655         torture_assert_int_equal(torture, ldb_dn_set_extended_component(dn, "SID", &sid_blob), 0, 
656                        "Failed to set a SID on DN");
657
658         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "SID"), sid_blob, 
659                                        "Extended DN SID incorect");
660
661         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "GUID"), guid_blob, 
662                                        "Extended DN GUID incorect");
663
664         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "cn=users,dc=samba,dc=org", 
665                                  "linearized DN incorrect");
666
667         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 1),
668                                  talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s", 
669                                                  guid, sid, "cn=users,dc=samba,dc=org"),
670                                  "Clear extended linearized DN incorrect");
671
672         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0),
673                                  talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s", 
674                                                  hex_guid, hex_sid, "cn=users,dc=samba,dc=org"),
675                                  "HEX extended linearized DN incorrect");
676
677         /* Now check a 'just GUID' DN (clear format) */
678         torture_assert(torture, 
679                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>",
680                                            guid), 
681                        "Failed to create an 'extended' DN");
682
683         torture_assert(torture, 
684                        ldb_dn_validate(dn),
685                        "Failed to validate 'extended' DN");
686
687         torture_assert(torture, ldb_dn_has_extended(dn) == true, 
688                        "Should find extended DN to be 'extended'");
689
690         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") == NULL, 
691                        "Should not find an SID on this DN");
692
693         torture_assert_int_equal(torture, ldb_dn_get_comp_num(dn), 0, 
694                        "Should not find an 'normal' componet on this DN");
695
696         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") != NULL, 
697                        "Should find an GUID on this DN");
698         
699         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "GUID"), guid_blob, 
700                                        "Extended DN GUID incorect");
701
702         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "", 
703                                  "linearized DN incorrect");
704
705         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 1),
706                                  talloc_asprintf(mem_ctx, "<GUID=%s>", 
707                                                  guid),
708                                  "Clear extended linearized DN incorrect");
709
710         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0),
711                                  talloc_asprintf(mem_ctx, "<GUID=%s>", 
712                                                  hex_guid),
713                                  "HEX extended linearized DN incorrect");
714
715         /* Now check a 'just GUID' DN (HEX format) */
716         torture_assert(torture, 
717                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>",
718                                            hex_guid), 
719                        "Failed to create an 'extended' DN");
720
721         torture_assert(torture, 
722                        ldb_dn_validate(dn),
723                        "Failed to validate 'extended' DN");
724
725         torture_assert(torture, ldb_dn_has_extended(dn) == true, 
726                        "Should find extended DN to be 'extended'");
727
728         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") == NULL, 
729                        "Should not find an SID on this DN");
730
731         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") != NULL, 
732                        "Should find an GUID on this DN");
733         
734         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "GUID"), guid_blob, 
735                                        "Extended DN GUID incorect");
736
737         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "", 
738                                  "linearized DN incorrect");
739
740         /* Now check a 'just SID' DN (clear format) */
741         torture_assert(torture, 
742                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<SID=%s>",
743                                            sid), 
744                        "Failed to create an 'extended' DN");
745
746         torture_assert(torture, 
747                        ldb_dn_validate(dn),
748                        "Failed to validate 'extended' DN");
749
750         torture_assert(torture, ldb_dn_has_extended(dn) == true, 
751                        "Should find extended DN to be 'extended'");
752
753         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") == NULL, 
754                        "Should not find an SID on this DN");
755
756         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") != NULL, 
757                        "Should find an SID on this DN");
758         
759         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "SID"), sid_blob, 
760                                        "Extended DN SID incorect");
761
762         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "", 
763                                  "linearized DN incorrect");
764
765         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 1),
766                                  talloc_asprintf(mem_ctx, "<SID=%s>", 
767                                                  sid),
768                                  "Clear extended linearized DN incorrect");
769
770         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0),
771                                  talloc_asprintf(mem_ctx, "<SID=%s>", 
772                                                  hex_sid),
773                                  "HEX extended linearized DN incorrect");
774
775         /* Now check a 'just SID' DN (HEX format) */
776         torture_assert(torture, 
777                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<SID=%s>",
778                                            hex_sid), 
779                        "Failed to create an 'extended' DN");
780
781         torture_assert(torture, 
782                        ldb_dn_validate(dn),
783                        "Failed to validate 'extended' DN");
784
785         torture_assert(torture, ldb_dn_has_extended(dn) == true, 
786                        "Should find extended DN to be 'extended'");
787
788         torture_assert(torture, ldb_dn_get_extended_component(dn, "GUID") == NULL, 
789                        "Should not find an SID on this DN");
790
791         torture_assert(torture, ldb_dn_get_extended_component(dn, "SID") != NULL, 
792                        "Should find an SID on this DN");
793         
794         torture_assert_data_blob_equal(torture, *ldb_dn_get_extended_component(dn, "SID"), sid_blob, 
795                                        "Extended DN SID incorect");
796
797         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "", 
798                                  "linearized DN incorrect");
799
800         talloc_free(mem_ctx);
801         return true;
802 }
803
804
805 static bool torture_ldb_dn(struct torture_context *torture)
806 {
807         TALLOC_CTX *mem_ctx = talloc_new(torture);
808         struct ldb_context *ldb;
809         struct ldb_dn *dn;
810         struct ldb_dn *child_dn;
811         struct ldb_dn *typo_dn;
812         struct ldb_dn *special_dn;
813         struct ldb_val val;
814
815         torture_assert(torture, 
816                        ldb = ldb_init(mem_ctx, torture->ev),
817                        "Failed to init ldb");
818
819         torture_assert_int_equal(torture, 
820                                  ldb_register_samba_handlers(ldb), LDB_SUCCESS,
821                                  "Failed to register Samba handlers");
822
823         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
824
825         /* Check behaviour of a normal DN */
826         torture_assert(torture, 
827                        dn = ldb_dn_new(mem_ctx, ldb, NULL), 
828                        "Failed to create a NULL DN");
829
830         torture_assert(torture, 
831                        ldb_dn_validate(dn),
832                        "Failed to validate NULL DN");
833
834         torture_assert(torture, 
835                        ldb_dn_add_base_fmt(dn, "dc=org"), 
836                        "Failed to add base DN");
837
838         torture_assert(torture, 
839                        ldb_dn_add_child_fmt(dn, "dc=samba"), 
840                        "Failed to add base DN");
841
842         torture_assert_str_equal(torture, ldb_dn_get_linearized(dn), "dc=samba,dc=org", 
843                                  "linearized DN incorrect");
844
845         torture_assert_str_equal(torture, ldb_dn_get_extended_linearized(mem_ctx, dn, 0), "dc=samba,dc=org", 
846                                  "extended linearized DN incorrect");
847
848         /* Check child DN comparisons */
849         torture_assert(torture, 
850                        child_dn = ldb_dn_new(mem_ctx, ldb, "CN=users,DC=SAMBA,DC=org"), 
851                        "Failed to create child DN");
852
853         torture_assert(torture, 
854                        ldb_dn_compare(dn, child_dn) != 0,
855                        "Comparison on dc=samba,dc=org and CN=users,DC=SAMBA,DC=org should != 0");
856
857         torture_assert(torture, 
858                        ldb_dn_compare_base(child_dn, dn) != 0,
859                        "Base Comparison of CN=users,DC=SAMBA,DC=org and dc=samba,dc=org should != 0");
860
861         torture_assert(torture, 
862                        ldb_dn_compare_base(dn, child_dn) == 0,
863                        "Base Comparison on dc=samba,dc=org and CN=users,DC=SAMBA,DC=org should == 0");
864
865         /* Check comparisons with a truncated DN */
866         torture_assert(torture, 
867                        typo_dn = ldb_dn_new(mem_ctx, ldb, "c=samba,dc=org"), 
868                        "Failed to create 'typo' DN");
869
870         torture_assert(torture, 
871                        ldb_dn_compare(dn, typo_dn) != 0,
872                        "Comparison on dc=samba,dc=org and c=samba,dc=org should != 0");
873
874         torture_assert(torture, 
875                        ldb_dn_compare_base(typo_dn, dn) != 0,
876                        "Base Comparison of c=samba,dc=org and dc=samba,dc=org should != 0");
877
878         torture_assert(torture, 
879                        ldb_dn_compare_base(dn, typo_dn) != 0,
880                        "Base Comparison on dc=samba,dc=org and c=samba,dc=org should != 0");
881
882         /* Check comparisons with a special DN */
883         torture_assert(torture,
884                        special_dn = ldb_dn_new(mem_ctx, ldb, "@special_dn"),
885                        "Failed to create 'special' DN");
886
887         torture_assert(torture,
888                        ldb_dn_compare(dn, special_dn) != 0,
889                        "Comparison on dc=samba,dc=org and @special_dn should != 0");
890
891         torture_assert(torture,
892                        ldb_dn_compare_base(special_dn, dn) > 0,
893                        "Base Comparison of @special_dn and dc=samba,dc=org should > 0");
894
895         torture_assert(torture,
896                        ldb_dn_compare_base(dn, special_dn) < 0,
897                        "Base Comparison on dc=samba,dc=org and @special_dn should < 0");
898
899         /* Check DN based on MS-ADTS:3.1.1.5.1.2 Naming Constraints*/
900         torture_assert(torture,
901                        dn = ldb_dn_new(mem_ctx, ldb, "CN=New\nLine,DC=SAMBA,DC=org"),
902                        "Failed to create a DN with 0xA in it");
903
904         /* this is a warning until we work out how the DEL: CNs work */
905         if (ldb_dn_validate(dn) != false) {
906                 torture_warning(torture,
907                                 "should have failed to validate a DN with 0xA in it");
908         }
909
910         /* Escaped comma */
911         torture_assert(torture,
912                        dn = ldb_dn_new(mem_ctx, ldb, "CN=A\\,comma,DC=SAMBA,DC=org"),
913                        "Failed to create a DN with an escaped comma in it");
914
915
916         val = data_blob_const("CN=Zer\0,DC=SAMBA,DC=org", 23);
917         torture_assert(torture,
918                        NULL == ldb_dn_from_ldb_val(mem_ctx, ldb, &val),
919                        "should fail to create a DN with 0x0 in it");
920
921         talloc_free(mem_ctx);
922         return true;
923 }
924
925 static bool torture_ldb_dn_invalid_extended(struct torture_context *torture)
926 {
927         TALLOC_CTX *mem_ctx = talloc_new(torture);
928         struct ldb_context *ldb;
929         struct ldb_dn *dn;
930
931         const char *dn_str = "cn=admin,cn=users,dc=samba,dc=org";
932
933         torture_assert(torture, 
934                        ldb = ldb_init(mem_ctx, torture->ev),
935                        "Failed to init ldb");
936
937         torture_assert_int_equal(torture, 
938                                  ldb_register_samba_handlers(ldb), LDB_SUCCESS,
939                                  "Failed to register Samba handlers");
940
941         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
942
943         /* Check behaviour of a normal DN */
944         torture_assert(torture, 
945                        dn = ldb_dn_new(mem_ctx, ldb, "samba,dc=org"), 
946                        "Failed to create a 'normal' invalid DN");
947
948         torture_assert(torture, 
949                        ldb_dn_validate(dn) == false,
950                        "should have failed to validate 'normal' invalid DN");
951
952         /* Now make an extended DN */
953         torture_assert(torture, 
954                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<PID=%s>;%s",
955                                            sid, dn_str), 
956                        "Failed to create an invalid 'extended' DN");
957
958         torture_assert(torture, 
959                        ldb_dn_validate(dn) == false,
960                        "should have failed to validate 'extended' DN");
961
962         torture_assert(torture, 
963                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>%s",
964                                            sid, dn_str), 
965                        "Failed to create an invalid 'extended' DN");
966
967         torture_assert(torture, 
968                        ldb_dn_validate(dn) == false,
969                        "should have failed to validate 'extended' DN");
970
971         torture_assert(torture, 
972                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>;",
973                                            sid), 
974                        "Failed to create an invalid 'extended' DN");
975
976         torture_assert(torture, 
977                        ldb_dn_validate(dn) == false,
978                        "should have failed to validate 'extended' DN");
979
980         torture_assert(torture, 
981                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=%s>;",
982                                            hex_sid), 
983                        "Failed to create an invalid 'extended' DN");
984
985         torture_assert(torture, 
986                        ldb_dn_validate(dn) == false,
987                        "should have failed to validate 'extended' DN");
988
989         torture_assert(torture, 
990                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<SID=%s>;",
991                                            hex_guid), 
992                        "Failed to create an invalid 'extended' DN");
993
994         torture_assert(torture, 
995                        ldb_dn_validate(dn) == false,
996                        "should have failed to validate 'extended' DN");
997
998         torture_assert(torture, 
999                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<SID=%s>;",
1000                                            guid), 
1001                        "Failed to create an invalid 'extended' DN");
1002
1003         torture_assert(torture, 
1004                        ldb_dn_validate(dn) == false,
1005                        "should have failed to validate 'extended' DN");
1006
1007         torture_assert(torture, 
1008                        dn = ldb_dn_new_fmt(mem_ctx, ldb, "<GUID=>"), 
1009                        "Failed to create an invalid 'extended' DN");
1010
1011         torture_assert(torture, 
1012                        ldb_dn_validate(dn) == false,
1013                        "should have failed to validate 'extended' DN");
1014
1015         return true;
1016 }
1017
1018 static bool helper_ldb_message_compare(struct torture_context *torture,
1019                                        struct ldb_message *a,
1020                                        struct ldb_message *b)
1021 {
1022         int i;
1023
1024         if (a->num_elements != b->num_elements) {
1025                 return false;
1026         }
1027
1028         for (i = 0; i < a->num_elements; i++) {
1029                 int j;
1030                 struct ldb_message_element x = a->elements[i];
1031                 struct ldb_message_element y = b->elements[i];
1032
1033                 torture_comment(torture, "#%s\n", x.name);
1034                 torture_assert_int_equal(torture, x.flags, y.flags,
1035                                          "Flags do not match");
1036                 torture_assert_str_equal(torture, x.name, y.name,
1037                                          "Names do not match in field");
1038                 torture_assert_int_equal(torture, x.num_values, y.num_values,
1039                                          "Number of values do not match");
1040
1041                 /*
1042                  * Records cannot round trip via the SDDL string with a
1043                  * nTSecurityDescriptor field.
1044                  *
1045                  * Parsing from SDDL and diffing the NDR dump output gives the
1046                  * following:
1047                  *
1048                  *          in: struct decode_security_descriptor
1049                  *             sd: struct security_descriptor
1050                  *                 revision                 : SECURITY_DESCRIPTOR_REVISION_1 (1)
1051                  *-                type                     : 0x8c14 (35860)
1052                  *-                       0: SEC_DESC_OWNER_DEFAULTED
1053                  *-                       0: SEC_DESC_GROUP_DEFAULTED
1054                  *+                type                     : 0x8c17 (35863)
1055                  *+                       1: SEC_DESC_OWNER_DEFAULTED
1056                  *+                       1: SEC_DESC_GROUP_DEFAULTED
1057                  *                        1: SEC_DESC_DACL_PRESENT
1058                  *                        0: SEC_DESC_DACL_DEFAULTED
1059                  *                        1: SEC_DESC_SACL_PRESENT
1060                  */
1061                 if (strcmp(x.name, "nTSecurityDescriptor") == 0) {
1062                         continue;
1063                 }
1064                 for (j = 0; j < x.num_values; j++) {
1065                         torture_assert_int_equal(torture, x.values[j].length,
1066                                                  y.values[j].length,
1067                                                  "Does not match in length");
1068                         torture_assert_mem_equal(torture,
1069                                                  x.values[j].data,
1070                                                  y.values[j].data,
1071                                                  x.values[j].length,
1072                                                  "Does not match in data");
1073                 }
1074         }
1075         return true;
1076 }
1077
1078 static bool torture_ldb_unpack(struct torture_context *torture)
1079 {
1080         TALLOC_CTX *mem_ctx = talloc_new(torture);
1081         struct ldb_context *ldb;
1082         struct ldb_val data = data_blob_const(dda1d01d_bin, sizeof(dda1d01d_bin));
1083         struct ldb_message *msg = ldb_msg_new(mem_ctx);
1084         const char *ldif_text = dda1d01d_ldif;
1085         struct ldb_ldif ldif;
1086
1087         ldb = samba_ldb_init(mem_ctx, torture->ev, NULL, NULL, NULL);
1088         torture_assert(torture,
1089                        ldb != NULL,
1090                        "Failed to init ldb");
1091
1092         torture_assert_int_equal(torture, ldb_unpack_data(ldb, &data, msg), 0,
1093                                  "ldb_unpack_data failed");
1094
1095         ldif.changetype = LDB_CHANGETYPE_NONE;
1096         ldif.msg = msg;
1097         ldif_text = ldb_ldif_write_string(ldb, mem_ctx, &ldif);
1098
1099         torture_assert_int_equal(torture,
1100                                  strcmp(ldif_text, dda1d01d_ldif), 0,
1101                                  "ldif form differs from binary form");
1102         return true;
1103 }
1104
1105 static bool torture_ldb_unpack_flags(struct torture_context *torture)
1106 {
1107         TALLOC_CTX *mem_ctx = talloc_new(torture);
1108         struct ldb_context *ldb;
1109         struct ldb_val data = data_blob_const(dda1d01d_bin, sizeof(dda1d01d_bin));
1110         struct ldb_message *msg = ldb_msg_new(mem_ctx);
1111         const char *ldif_text = dda1d01d_ldif;
1112         struct ldb_ldif ldif;
1113         unsigned int nb_elements_in_db;
1114
1115         ldb = samba_ldb_init(mem_ctx, torture->ev, NULL, NULL, NULL);
1116         torture_assert(torture,
1117                        ldb != NULL,
1118                        "Failed to init ldb");
1119
1120         torture_assert_int_equal(torture,
1121                                  ldb_unpack_data_only_attr_list_flags(ldb, &data,
1122                                                                       msg,
1123                                                                       NULL, 0,
1124                                                                       LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC,
1125                                                                       &nb_elements_in_db),
1126                                  0,
1127                                  "ldb_unpack_data failed");
1128
1129         ldif.changetype = LDB_CHANGETYPE_NONE;
1130         ldif.msg = msg;
1131         ldif_text = ldb_ldif_write_string(ldb, mem_ctx, &ldif);
1132
1133         torture_assert_int_equal(torture,
1134                                  strcmp(ldif_text, dda1d01d_ldif), 0,
1135                                  "ldif form differs from binary form");
1136
1137         torture_assert_int_equal(torture,
1138                                  ldb_unpack_data_only_attr_list_flags(ldb, &data,
1139                                                                       msg,
1140                                                                       NULL, 0,
1141                                                                       LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC|
1142                                                                       LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC,
1143                                                                       &nb_elements_in_db),
1144                                  0,
1145                                  "ldb_unpack_data failed");
1146
1147         ldif.changetype = LDB_CHANGETYPE_NONE;
1148         ldif.msg = msg;
1149         ldif_text = ldb_ldif_write_string(ldb, mem_ctx, &ldif);
1150
1151         torture_assert_int_equal(torture,
1152                                  strcmp(ldif_text, dda1d01d_ldif), 0,
1153                                  "ldif form differs from binary form");
1154
1155         torture_assert_int_equal(torture,
1156                                  ldb_unpack_data_only_attr_list_flags(ldb, &data,
1157                                                                       msg,
1158                                                                       NULL, 0,
1159                                                                       LDB_UNPACK_DATA_FLAG_NO_DN,
1160                                                                       &nb_elements_in_db),
1161                                  0,
1162                                  "ldb_unpack_data failed");
1163
1164         torture_assert(torture,
1165                        msg->dn == NULL,
1166                        "msg->dn should be NULL");
1167
1168         return true;
1169 }
1170
1171 static bool torture_ldb_parse_ldif(struct torture_context *torture)
1172 {
1173         TALLOC_CTX *mem_ctx = talloc_new(torture);
1174         const char *ldif_text = dda1d01d_ldif;
1175         struct ldb_context *ldb;
1176         struct ldb_ldif *ldif;
1177         struct ldb_val binary;
1178         struct ldb_val data = data_blob_const(dda1d01d_bin, sizeof(dda1d01d_bin));
1179         struct ldb_message *msg = ldb_msg_new(mem_ctx);
1180
1181         ldb = samba_ldb_init(mem_ctx, torture->ev, NULL,NULL,NULL);
1182         torture_assert(torture,
1183                        ldb != NULL,
1184                        "Failed to init ldb");
1185
1186         ldif = ldb_ldif_read_string(ldb, &ldif_text);
1187         torture_assert(torture,
1188                        ldif != NULL,
1189                        "ldb_ldif_read_string failed");
1190         torture_assert_int_equal(torture, ldif->changetype, LDB_CHANGETYPE_NONE,
1191                                  "changetype is incorrect");
1192         torture_assert_int_equal(torture,
1193                                  ldb_pack_data(ldb, ldif->msg, &binary), 0,
1194                                  "ldb_pack_data failed");
1195
1196         torture_assert_int_equal(torture, ldb_unpack_data(ldb, &data, msg), 0,
1197                                  "ldb_unpack_data failed");
1198
1199         torture_assert(torture,
1200                        helper_ldb_message_compare(torture, ldif->msg, msg),
1201                        "Forms differ in memory");
1202
1203         return true;
1204 }
1205
1206 static bool torture_ldb_unpack_only_attr_list(struct torture_context *torture)
1207 {
1208         TALLOC_CTX *mem_ctx = talloc_new(torture);
1209         struct ldb_context *ldb;
1210         struct ldb_val data = data_blob_const(dda1d01d_bin, sizeof(dda1d01d_bin));
1211         struct ldb_message *msg = ldb_msg_new(mem_ctx);
1212         const char *lookup_names[] = {"instanceType", "nonexistant", "whenChanged",
1213                                       "objectClass", "uSNCreated",
1214                                       "showInAdvancedViewOnly", "name", "cnNotHere"};
1215         unsigned int nb_elements_in_db;
1216         const char *ldif_text;
1217         struct ldb_ldif ldif;
1218
1219         ldb = samba_ldb_init(mem_ctx, torture->ev, NULL, NULL, NULL);
1220         torture_assert(torture,
1221                        ldb != NULL,
1222                        "Failed to init samba");
1223
1224         torture_assert_int_equal(torture,
1225                                  ldb_unpack_data_only_attr_list(ldb, &data, msg,
1226                                                           lookup_names, ARRAY_SIZE(lookup_names),
1227                                                           &nb_elements_in_db), 0,
1228                                  "ldb_unpack_data_only_attr_list failed");
1229         torture_assert_int_equal(torture, nb_elements_in_db, 13,
1230                                  "Got wrong count of elements");
1231
1232         /* Compare data in binary form */
1233         torture_assert_int_equal(torture, msg->num_elements, 6,
1234                                  "Got wrong number of parsed elements");
1235
1236         torture_assert_str_equal(torture, msg->elements[0].name, "objectClass",
1237                                  "First element has wrong name");
1238         torture_assert_int_equal(torture, msg->elements[0].num_values, 2,
1239                                  "First element has wrong count of values");
1240         torture_assert_int_equal(torture,
1241                                  msg->elements[0].values[0].length, 3,
1242                                  "First element's first value is of wrong length");
1243         torture_assert_mem_equal(torture,
1244                                  msg->elements[0].values[0].data, "top", 3,
1245                                  "First element's first value is incorrect");
1246         torture_assert_int_equal(torture,
1247                                  msg->elements[0].values[1].length, strlen("container"),
1248                                  "First element's second value is of wrong length");
1249         torture_assert_mem_equal(torture, msg->elements[0].values[1].data,
1250                                  "container", strlen("container"),
1251                                  "First element's second value is incorrect");
1252
1253         torture_assert_str_equal(torture, msg->elements[1].name, "instanceType",
1254                                  "Second element has wrong name");
1255         torture_assert_int_equal(torture, msg->elements[1].num_values, 1,
1256                                  "Second element has too many values");
1257         torture_assert_int_equal(torture, msg->elements[1].values[0].length, 1,
1258                                  "Second element's value is of wrong length");
1259         torture_assert_mem_equal(torture, msg->elements[1].values[0].data,
1260                                  "4", 1,
1261                                  "Second element's value is incorrect");
1262
1263         torture_assert_str_equal(torture, msg->elements[2].name, "whenChanged",
1264                                  "Third element has wrong name");
1265         torture_assert_int_equal(torture, msg->elements[2].num_values, 1,
1266                                  "Third element has too many values");
1267         torture_assert_int_equal(torture, msg->elements[2].values[0].length,
1268                                  strlen("20150708224310.0Z"),
1269                                  "Third element's value is of wrong length");
1270         torture_assert_mem_equal(torture, msg->elements[2].values[0].data,
1271                                  "20150708224310.0Z", strlen("20150708224310.0Z"),
1272                                  "Third element's value is incorrect");
1273
1274         torture_assert_str_equal(torture, msg->elements[3].name, "uSNCreated",
1275                                  "Fourth element has wrong name");
1276         torture_assert_int_equal(torture, msg->elements[3].num_values, 1,
1277                                  "Fourth element has too many values");
1278         torture_assert_int_equal(torture, msg->elements[3].values[0].length, 4,
1279                                  "Fourth element's value is of wrong length");
1280         torture_assert_mem_equal(torture, msg->elements[3].values[0].data,
1281                                  "3467", 4,
1282                                  "Fourth element's value is incorrect");
1283
1284         torture_assert_str_equal(torture, msg->elements[4].name, "showInAdvancedViewOnly",
1285                                  "Fifth element has wrong name");
1286         torture_assert_int_equal(torture, msg->elements[4].num_values, 1,
1287                                  "Fifth element has too many values");
1288         torture_assert_int_equal(torture, msg->elements[4].values[0].length, 4,
1289                                  "Fifth element's value is of wrong length");
1290         torture_assert_mem_equal(torture, msg->elements[4].values[0].data,
1291                                  "TRUE", 4,
1292                                  "Fourth element's value is incorrect");
1293
1294         torture_assert_str_equal(torture, msg->elements[5].name, "name",
1295                                  "Sixth element has wrong name");
1296         torture_assert_int_equal(torture, msg->elements[5].num_values, 1,
1297                                  "Sixth element has too many values");
1298         torture_assert_int_equal(torture, msg->elements[5].values[0].length,
1299                                  strlen("dda1d01d-4bd7-4c49-a184-46f9241b560e"),
1300                                  "Sixth element's value is of wrong length");
1301         torture_assert_mem_equal(torture, msg->elements[5].values[0].data,
1302                                  "dda1d01d-4bd7-4c49-a184-46f9241b560e",
1303                                  strlen("dda1d01d-4bd7-4c49-a184-46f9241b560e"),
1304                                  "Sixth element's value is incorrect");
1305
1306         /* Compare data in ldif form */
1307         ldif.changetype = LDB_CHANGETYPE_NONE;
1308         ldif.msg = msg;
1309         ldif_text = ldb_ldif_write_string(ldb, mem_ctx, &ldif);
1310
1311         torture_assert_str_equal(torture, ldif_text, dda1d01d_ldif_reduced,
1312                                  "Expected fields did not match");
1313
1314         return true;
1315 }
1316
1317 struct torture_suite *torture_ldb(TALLOC_CTX *mem_ctx)
1318 {
1319         struct torture_suite *suite = torture_suite_create(mem_ctx, "ldb");
1320
1321         if (suite == NULL) {
1322                 return NULL;
1323         }
1324
1325         torture_suite_add_simple_test(suite, "attrs", torture_ldb_attrs);
1326         torture_suite_add_simple_test(suite, "dn-attrs", torture_ldb_dn_attrs);
1327         torture_suite_add_simple_test(suite, "dn-extended",
1328                                       torture_ldb_dn_extended);
1329         torture_suite_add_simple_test(suite, "dn-invalid-extended",
1330                                       torture_ldb_dn_invalid_extended);
1331         torture_suite_add_simple_test(suite, "dn", torture_ldb_dn);
1332         torture_suite_add_simple_test(suite, "unpack-data",
1333                                       torture_ldb_unpack);
1334         torture_suite_add_simple_test(suite, "unpack-data-flags",
1335                                       torture_ldb_unpack_flags);
1336         torture_suite_add_simple_test(suite, "parse-ldif",
1337                                       torture_ldb_parse_ldif);
1338         torture_suite_add_simple_test(suite, "unpack-data-only-attr-list",
1339                                       torture_ldb_unpack_only_attr_list);
1340
1341         suite->description = talloc_strdup(suite, "LDB (samba-specific behaviour) tests");
1342
1343         return suite;
1344 }