7a90f18b5be3612cc1b3e184480a559ede184c4a
[metze/samba/wip.git] / source4 / torture / drs / unit / prefixmap_tests.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    DRSUAPI prefixMap unit tests
5
6    Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
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 "system/filesys.h"
24 #include "torture/smbtorture.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "torture/rpc/drsuapi.h"
27 #include "torture/drs/proto.h"
28 #include "param/param.h"
29
30
31 /**
32  * Private data to be shared among all test in Test case
33  */
34 struct drsut_prefixmap_data {
35         struct dsdb_schema_prefixmap *pfm_new;
36         struct dsdb_schema_prefixmap *pfm_full;
37
38         /* default schemaInfo value to test with */
39         const char *schi_default_str;
40         struct dsdb_schema_info *schi_default;
41
42         struct ldb_context *ldb_ctx;
43 };
44
45 /**
46  * Test-oid data structure
47  */
48 struct drsut_pfm_oid_data {
49         uint32_t        id;
50         const char      *bin_oid;
51         const char      *oid_prefix;
52 };
53
54 /**
55  * Default prefixMap initialization data
56  */
57 static const struct drsut_pfm_oid_data _prefixmap_test_new_data[] = {
58         {.id=0x00000000, .bin_oid="5504",                 .oid_prefix="2.5.4"},
59         {.id=0x00000001, .bin_oid="5506",                 .oid_prefix="2.5.6"},
60         {.id=0x00000002, .bin_oid="2A864886F7140102",     .oid_prefix="1.2.840.113556.1.2"},
61         {.id=0x00000003, .bin_oid="2A864886F7140103",     .oid_prefix="1.2.840.113556.1.3"},
62         {.id=0x00000004, .bin_oid="6086480165020201",     .oid_prefix="2.16.840.1.101.2.2.1"},
63         {.id=0x00000005, .bin_oid="6086480165020203",     .oid_prefix="2.16.840.1.101.2.2.3"},
64         {.id=0x00000006, .bin_oid="6086480165020105",     .oid_prefix="2.16.840.1.101.2.1.5"},
65         {.id=0x00000007, .bin_oid="6086480165020104",     .oid_prefix="2.16.840.1.101.2.1.4"},
66         {.id=0x00000008, .bin_oid="5505",                 .oid_prefix="2.5.5"},
67         {.id=0x00000009, .bin_oid="2A864886F7140104",     .oid_prefix="1.2.840.113556.1.4"},
68         {.id=0x0000000A, .bin_oid="2A864886F7140105",     .oid_prefix="1.2.840.113556.1.5"},
69         {.id=0x00000013, .bin_oid="0992268993F22C64",     .oid_prefix="0.9.2342.19200300.100"},
70         {.id=0x00000014, .bin_oid="6086480186F84203",     .oid_prefix="2.16.840.1.113730.3"},
71         {.id=0x00000015, .bin_oid="0992268993F22C6401",   .oid_prefix="0.9.2342.19200300.100.1"},
72         {.id=0x00000016, .bin_oid="6086480186F8420301",   .oid_prefix="2.16.840.1.113730.3.1"},
73         {.id=0x00000017, .bin_oid="2A864886F7140105B658", .oid_prefix="1.2.840.113556.1.5.7000"},
74         {.id=0x00000018, .bin_oid="5515",                 .oid_prefix="2.5.21"},
75         {.id=0x00000019, .bin_oid="5512",                 .oid_prefix="2.5.18"},
76         {.id=0x0000001A, .bin_oid="5514",                 .oid_prefix="2.5.20"},
77 };
78
79 /**
80  * Data to be used for creating full prefix map for testing
81  */
82 static const struct drsut_pfm_oid_data _prefixmap_full_map_data[] = {
83         {.id=0x00000000, .bin_oid="0x5504",                     .oid_prefix="2.5.4"},
84         {.id=0x00000001, .bin_oid="0x5506",                     .oid_prefix="2.5.6"},
85         {.id=0x00000002, .bin_oid="0x2A864886F7140102",         .oid_prefix="1.2.840.113556.1.2"},
86         {.id=0x00000003, .bin_oid="0x2A864886F7140103",         .oid_prefix="1.2.840.113556.1.3"},
87         {.id=0x00000004, .bin_oid="0x6086480165020201",         .oid_prefix="2.16.840.1.101.2.2.1"},
88         {.id=0x00000005, .bin_oid="0x6086480165020203",         .oid_prefix="2.16.840.1.101.2.2.3"},
89         {.id=0x00000006, .bin_oid="0x6086480165020105",         .oid_prefix="2.16.840.1.101.2.1.5"},
90         {.id=0x00000007, .bin_oid="0x6086480165020104",         .oid_prefix="2.16.840.1.101.2.1.4"},
91         {.id=0x00000008, .bin_oid="0x5505",                     .oid_prefix="2.5.5"},
92         {.id=0x00000009, .bin_oid="0x2A864886F7140104",         .oid_prefix="1.2.840.113556.1.4"},
93         {.id=0x0000000a, .bin_oid="0x2A864886F7140105",         .oid_prefix="1.2.840.113556.1.5"},
94         {.id=0x00000013, .bin_oid="0x0992268993F22C64",         .oid_prefix="0.9.2342.19200300.100"},
95         {.id=0x00000014, .bin_oid="0x6086480186F84203",         .oid_prefix="2.16.840.1.113730.3"},
96         {.id=0x00000015, .bin_oid="0x0992268993F22C6401",       .oid_prefix="0.9.2342.19200300.100.1"},
97         {.id=0x00000016, .bin_oid="0x6086480186F8420301",       .oid_prefix="2.16.840.1.113730.3.1"},
98         {.id=0x00000017, .bin_oid="0x2A864886F7140105B658",     .oid_prefix="1.2.840.113556.1.5.7000"},
99         {.id=0x00000018, .bin_oid="0x5515",                     .oid_prefix="2.5.21"},
100         {.id=0x00000019, .bin_oid="0x5512",                     .oid_prefix="2.5.18"},
101         {.id=0x0000001a, .bin_oid="0x5514",                     .oid_prefix="2.5.20"},
102         {.id=0x0000000b, .bin_oid="0x2A864886F71401048204",     .oid_prefix="1.2.840.113556.1.4.260"},
103         {.id=0x0000000c, .bin_oid="0x2A864886F714010538",       .oid_prefix="1.2.840.113556.1.5.56"},
104         {.id=0x0000000d, .bin_oid="0x2A864886F71401048206",     .oid_prefix="1.2.840.113556.1.4.262"},
105         {.id=0x0000000e, .bin_oid="0x2A864886F714010539",       .oid_prefix="1.2.840.113556.1.5.57"},
106         {.id=0x0000000f, .bin_oid="0x2A864886F71401048207",     .oid_prefix="1.2.840.113556.1.4.263"},
107         {.id=0x00000010, .bin_oid="0x2A864886F71401053A",       .oid_prefix="1.2.840.113556.1.5.58"},
108         {.id=0x00000011, .bin_oid="0x2A864886F714010549",       .oid_prefix="1.2.840.113556.1.5.73"},
109         {.id=0x00000012, .bin_oid="0x2A864886F71401048231",     .oid_prefix="1.2.840.113556.1.4.305"},
110         {.id=0x0000001b, .bin_oid="0x2B060104018B3A6577",       .oid_prefix="1.3.6.1.4.1.1466.101.119"},
111         {.id=0x0000001c, .bin_oid="0x6086480186F8420302",       .oid_prefix="2.16.840.1.113730.3.2"},
112         {.id=0x0000001d, .bin_oid="0x2B06010401817A01",         .oid_prefix="1.3.6.1.4.1.250.1"},
113         {.id=0x0000001e, .bin_oid="0x2A864886F70D0109",         .oid_prefix="1.2.840.113549.1.9"},
114         {.id=0x0000001f, .bin_oid="0x0992268993F22C6404",       .oid_prefix="0.9.2342.19200300.100.4"},
115         {.id=0x00000020, .bin_oid="0x2A864886F714010617",       .oid_prefix="1.2.840.113556.1.6.23"},
116         {.id=0x00000021, .bin_oid="0x2A864886F71401061201",     .oid_prefix="1.2.840.113556.1.6.18.1"},
117         {.id=0x00000022, .bin_oid="0x2A864886F71401061202",     .oid_prefix="1.2.840.113556.1.6.18.2"},
118         {.id=0x00000023, .bin_oid="0x2A864886F71401060D03",     .oid_prefix="1.2.840.113556.1.6.13.3"},
119         {.id=0x00000024, .bin_oid="0x2A864886F71401060D04",     .oid_prefix="1.2.840.113556.1.6.13.4"},
120         {.id=0x00000025, .bin_oid="0x2B0601010101",             .oid_prefix="1.3.6.1.1.1.1"},
121         {.id=0x00000026, .bin_oid="0x2B0601010102",             .oid_prefix="1.3.6.1.1.1.2"},
122         {.id=0x000003ed, .bin_oid="0x2A864886F7140104B65866",   .oid_prefix="1.2.840.113556.1.4.7000.102"},
123         {.id=0x00000428, .bin_oid="0x2A864886F7140105B6583E",   .oid_prefix="1.2.840.113556.1.5.7000.62"},
124         {.id=0x0000044c, .bin_oid="0x2A864886F7140104B6586683", .oid_prefix="1.2.840.113556.1.4.7000.102:0x83"},
125         {.id=0x0000044f, .bin_oid="0x2A864886F7140104B6586681", .oid_prefix="1.2.840.113556.1.4.7000.102:0x81"},
126         {.id=0x0000047d, .bin_oid="0x2A864886F7140105B6583E81", .oid_prefix="1.2.840.113556.1.5.7000.62:0x81"},
127         {.id=0x00000561, .bin_oid="0x2A864886F7140105B6583E83", .oid_prefix="1.2.840.113556.1.5.7000.62:0x83"},
128         {.id=0x000007d1, .bin_oid="0x2A864886F71401061401",     .oid_prefix="1.2.840.113556.1.6.20.1"},
129         {.id=0x000007e1, .bin_oid="0x2A864886F71401061402",     .oid_prefix="1.2.840.113556.1.6.20.2"},
130         {.id=0x00001b86, .bin_oid="0x2A817A",                   .oid_prefix="1.2.250"},
131         {.id=0x00001c78, .bin_oid="0x2A817A81",                 .oid_prefix="1.2.250:0x81"},
132         {.id=0x00001c7b, .bin_oid="0x2A817A8180",               .oid_prefix="1.2.250:0x8180"},
133 };
134
135
136 /**
137  * OID-to-ATTID mappings to be used for testing
138  */
139 static const struct {
140         const char      *oid;
141         uint32_t        id;
142         uint32_t        attid;
143 } _prefixmap_test_data[] = {
144         {.oid="2.5.4.0",                .id=0x00000000, .attid=0x000000},
145         {.oid="2.5.4.42",               .id=0x00000000, .attid=0x00002a},
146         {.oid="1.2.840.113556.1.2.1",   .id=0x00000002, .attid=0x020001},
147         {.oid="1.2.840.113556.1.2.13",  .id=0x00000002, .attid=0x02000d},
148         {.oid="1.2.840.113556.1.2.281", .id=0x00000002, .attid=0x020119},
149         {.oid="1.2.840.113556.1.4.125", .id=0x00000009, .attid=0x09007d},
150         {.oid="1.2.840.113556.1.4.146", .id=0x00000009, .attid=0x090092},
151         {.oid="1.2.250.1",              .id=0x00001b86, .attid=0x1b860001},
152         {.oid="1.2.250.130",            .id=0x00001b86, .attid=0x1b860082},
153         {.oid="1.2.250.16386",          .id=0x00001c78, .attid=0x1c788002},
154         {.oid="1.2.250.2097154",        .id=0x00001c7b, .attid=0x1c7b8002},
155 };
156
157
158 /**
159  * Creates dsdb_schema_prefixmap based on predefined data
160  *
161  */
162 static WERROR _drsut_prefixmap_new(const struct drsut_pfm_oid_data *_pfm_init_data, uint32_t count,
163                                    TALLOC_CTX *mem_ctx, struct dsdb_schema_prefixmap **_pfm)
164 {
165         uint32_t i;
166         struct dsdb_schema_prefixmap *pfm;
167
168         pfm = talloc(mem_ctx, struct dsdb_schema_prefixmap);
169         W_ERROR_HAVE_NO_MEMORY(pfm);
170
171         pfm->length = count;
172         pfm->prefixes = talloc_array(pfm, struct dsdb_schema_prefixmap_oid, pfm->length);
173         if (!pfm->prefixes) {
174                 talloc_free(pfm);
175                 return WERR_NOMEM;
176         }
177
178         for (i = 0; i < pfm->length; i++) {
179                 pfm->prefixes[i].id = _pfm_init_data[i].id;
180                 pfm->prefixes[i].bin_oid = strhex_to_data_blob(pfm, _pfm_init_data[i].bin_oid);
181                 if (!pfm->prefixes[i].bin_oid.data) {
182                         talloc_free(pfm);
183                         return WERR_NOMEM;
184                 }
185         }
186
187         *_pfm = pfm;
188
189         return WERR_OK;
190 }
191
192 static bool _torture_drs_pfm_compare_same(struct torture_context *tctx,
193                                           const struct dsdb_schema_prefixmap *pfm_left,
194                                           const struct dsdb_schema_prefixmap *pfm_right,
195                                           bool quiet)
196 {
197         uint32_t i;
198         char *err_msg = NULL;
199
200         if (pfm_left->length != pfm_right->length) {
201                 err_msg = talloc_asprintf(tctx, "prefixMaps differ in size; left = %d, right = %d",
202                                           pfm_left->length, pfm_right->length);
203                 goto failed;
204         }
205
206         for (i = 0; i < pfm_left->length; i++) {
207                 struct dsdb_schema_prefixmap_oid *entry_left = &pfm_left->prefixes[i];
208                 struct dsdb_schema_prefixmap_oid *entry_right = &pfm_right->prefixes[i];
209
210                 if (entry_left->id != entry_right->id) {
211                         err_msg = talloc_asprintf(tctx, "Different IDs for index=%d", i);
212                         goto failed;
213                 }
214                 if (data_blob_cmp(&entry_left->bin_oid, &entry_right->bin_oid)) {
215                         err_msg = talloc_asprintf(tctx, "Different bin_oid for index=%d", i);
216                         goto failed;
217                 }
218         }
219
220         return true;
221
222 failed:
223         if (!quiet) {
224                 torture_comment(tctx, "_torture_drs_pfm_compare_same: %s", err_msg);
225         }
226         talloc_free(err_msg);
227
228         return false;
229 }
230
231 /*
232  * Tests dsdb_schema_pfm_new()
233  */
234 static bool torture_drs_unit_pfm_new(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
235 {
236         WERROR werr;
237         bool bret;
238         TALLOC_CTX *mem_ctx;
239         struct dsdb_schema_prefixmap *pfm = NULL;
240
241         mem_ctx = talloc_new(priv);
242
243         /* create new prefix map */
244         werr = dsdb_schema_pfm_new(mem_ctx, &pfm);
245         torture_assert_werr_ok(tctx, werr, "dsdb_schema_pfm_new() failed!");
246         torture_assert(tctx, pfm != NULL, "NULL prefixMap created!");
247         torture_assert(tctx, pfm->length > 0, "Empty prefixMap created!");
248         torture_assert(tctx, pfm->prefixes != NULL, "No prefixes for newly created prefixMap!");
249
250         /* compare newly created prefixMap with template one */
251         bret = _torture_drs_pfm_compare_same(tctx, priv->pfm_new, pfm, false);
252
253         talloc_free(mem_ctx);
254
255         return bret;
256 }
257
258 /**
259  * Tests dsdb_schema_pfm_make_attid() using full prefixMap.
260  * In this test we know exactly which ATTID and prefixMap->ID
261  * should be returned, i.e. no prefixMap entries should be added.
262  */
263 static bool torture_drs_unit_pfm_make_attid_full_map(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
264 {
265         WERROR werr;
266         uint32_t i, count;
267         uint32_t attid;
268         char *err_msg;
269
270         count = ARRAY_SIZE(_prefixmap_test_data);
271         for (i = 0; i < count; i++) {
272                 werr = dsdb_schema_pfm_make_attid(priv->pfm_full, _prefixmap_test_data[i].oid, &attid);
273                 /* prepare error message */
274                 err_msg = talloc_asprintf(priv, "dsdb_schema_pfm_make_attid() failed with %s",
275                                                 _prefixmap_test_data[i].oid);
276                 torture_assert(tctx, err_msg, "Unexpected: Have no memory!");
277                 /* verify result and returned ATTID */
278                 torture_assert_werr_ok(tctx, werr, err_msg);
279                 torture_assert_int_equal(tctx, attid, _prefixmap_test_data[i].attid, err_msg);
280                 /* reclaim memory for prepared error message */
281                 talloc_free(err_msg);
282         }
283
284         return true;
285 }
286
287 /**
288  * Tests dsdb_schema_pfm_make_attid() using initially small prefixMap.
289  * In this test we don't know exactly which ATTID and prefixMap->ID
290  * should be returned, but we can verify lo-word of ATTID.
291  * This test verifies implementation branch when a new
292  * prefix should be added into prefixMap.
293  */
294 static bool torture_drs_unit_pfm_make_attid_small_map(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
295 {
296         WERROR werr;
297         uint32_t i, j;
298         uint32_t idx;
299         uint32_t attid, attid_2;
300         char *err_msg;
301         struct dsdb_schema_prefixmap *pfm = NULL;
302         TALLOC_CTX *mem_ctx;
303
304         mem_ctx = talloc_new(priv);
305
306         /* create new prefix map */
307         werr = dsdb_schema_pfm_new(mem_ctx, &pfm);
308         torture_assert_werr_ok(tctx, werr, "dsdb_schema_pfm_new() failed!");
309         torture_assert(tctx, pfm != NULL, "NULL prefixMap created!");
310         torture_assert(tctx, pfm->length > 0, "Empty prefixMap created!");
311         torture_assert(tctx, pfm->prefixes != NULL, "No prefixes for newly created prefixMap!");
312
313         /* make some ATTIDs and check result */
314         for (i = 0; i < ARRAY_SIZE(_prefixmap_test_data); i++) {
315                 werr = dsdb_schema_pfm_make_attid(pfm, _prefixmap_test_data[i].oid, &attid);
316
317                 /* prepare error message */
318                 err_msg = talloc_asprintf(mem_ctx, "dsdb_schema_pfm_make_attid() failed with %s",
319                                                 _prefixmap_test_data[i].oid);
320                 torture_assert(tctx, err_msg, "Unexpected: Have no memory!");
321
322                 /* verify result and returned ATTID */
323                 torture_assert_werr_ok(tctx, werr, err_msg);
324                 /* verify ATTID lo-word */
325                 torture_assert_int_equal(tctx, attid & 0xFFFF, _prefixmap_test_data[i].attid & 0xFFFF, err_msg);
326
327                 /* try again, this time verify for whole ATTID */
328                 werr = dsdb_schema_pfm_make_attid(pfm, _prefixmap_test_data[i].oid, &attid_2);
329                 torture_assert_werr_ok(tctx, werr, err_msg);
330                 torture_assert_int_equal(tctx, attid_2, attid, err_msg);
331
332                 /* reclaim memory for prepared error message */
333                 talloc_free(err_msg);
334
335                 /* check there is such an index in modified prefixMap */
336                 idx = (attid >> 16);
337                 for (j = 0; j < pfm->length; j++) {
338                         if (pfm->prefixes[j].id == idx)
339                                 break;
340                 }
341                 if (j >= pfm->length) {
342                         torture_result(tctx, TORTURE_FAIL, __location__": No prefix for ATTID=0x%08X", attid);
343                         return false;
344                 }
345
346         }
347
348         talloc_free(mem_ctx);
349
350         return true;
351 }
352
353 /**
354  * Tests dsdb_schema_pfm_attid_from_oid() using full prefixMap.
355  * In this test we know exactly which ATTID and prefixMap->ID
356  * should be returned- dsdb_schema_pfm_attid_from_oid() should succeed.
357  */
358 static bool torture_drs_unit_pfm_attid_from_oid_full_map(struct torture_context *tctx,
359                                                          struct drsut_prefixmap_data *priv)
360 {
361         WERROR werr;
362         uint32_t i, count;
363         uint32_t attid;
364         char *err_msg;
365
366         count = ARRAY_SIZE(_prefixmap_test_data);
367         for (i = 0; i < count; i++) {
368                 werr = dsdb_schema_pfm_attid_from_oid(priv->pfm_full,
369                                                       _prefixmap_test_data[i].oid,
370                                                       &attid);
371                 /* prepare error message */
372                 err_msg = talloc_asprintf(priv, "dsdb_schema_pfm_attid_from_oid() failed with %s",
373                                                 _prefixmap_test_data[i].oid);
374                 torture_assert(tctx, err_msg, "Unexpected: Have no memory!");
375                 /* verify result and returned ATTID */
376                 torture_assert_werr_ok(tctx, werr, err_msg);
377                 torture_assert_int_equal(tctx, attid, _prefixmap_test_data[i].attid, err_msg);
378                 /* reclaim memory for prepared error message */
379                 talloc_free(err_msg);
380         }
381
382         return true;
383 }
384
385 /**
386  * Tests dsdb_schema_pfm_attid_from_oid() using base (initial) prefixMap.
387  * dsdb_schema_pfm_attid_from_oid() should fail when testing with OID
388  * that are not already in the prefixMap.
389  */
390 static bool torture_drs_unit_pfm_attid_from_oid_base_map(struct torture_context *tctx,
391                                                          struct drsut_prefixmap_data *priv)
392 {
393         WERROR werr;
394         uint32_t i;
395         uint32_t attid;
396         char *err_msg;
397         struct dsdb_schema_prefixmap *pfm = NULL;
398         struct dsdb_schema_prefixmap pfm_prev;
399         TALLOC_CTX *mem_ctx;
400         const struct {
401                 const char      *oid;
402                 uint32_t        attid;
403                 bool            exists; /* if this prefix already exists or should be added */
404         } _test_data[] = {
405                 {.oid="2.5.4.0",                .attid=0x00000000, true},
406                 {.oid="2.5.4.42",               .attid=0x0000002a, true},
407                 {.oid="1.2.840.113556.1.2.1",   .attid=0x00020001, true},
408                 {.oid="1.2.840.113556.1.2.13",  .attid=0x0002000d, true},
409                 {.oid="1.2.840.113556.1.2.281", .attid=0x00020119, true},
410                 {.oid="1.2.840.113556.1.4.125", .attid=0x0009007d, true},
411                 {.oid="1.2.840.113556.1.4.146", .attid=0x00090092, true},
412                 {.oid="1.2.250.1",              .attid=0x1b860001, false},
413                 {.oid="1.2.250.16386",          .attid=0x1c788002, false},
414                 {.oid="1.2.250.2097154",        .attid=0x1c7b8002, false},
415         };
416
417         mem_ctx = talloc_new(priv);
418         torture_assert(tctx, mem_ctx, "Unexpected: Have no memory!");
419
420         /* create new prefix map */
421         werr = dsdb_schema_pfm_new(mem_ctx, &pfm);
422         torture_assert_werr_ok(tctx, werr, "dsdb_schema_pfm_new() failed!");
423
424         /* keep initial pfm around for testing */
425         pfm_prev = *pfm;
426         pfm_prev.prefixes = talloc_reference(mem_ctx, pfm->prefixes);
427
428         /* get some ATTIDs and check result */
429         for (i = 0; i < ARRAY_SIZE(_test_data); i++) {
430                 werr = dsdb_schema_pfm_attid_from_oid(pfm, _test_data[i].oid, &attid);
431
432                 /* prepare error message */
433                 err_msg = talloc_asprintf(mem_ctx,
434                                           "dsdb_schema_pfm_attid_from_oid() failed for %s",
435                                           _test_data[i].oid);
436                 torture_assert(tctx, err_msg, "Unexpected: Have no memory!");
437
438
439                 /* verify pfm hasn't been altered */
440                 if (_test_data[i].exists) {
441                         /* should succeed and return valid ATTID */
442                         torture_assert_werr_ok(tctx, werr, err_msg);
443                         /* verify ATTID */
444                         torture_assert_int_equal(tctx, attid, _test_data[i].attid, err_msg);
445                 } else {
446                         /* should fail */
447                         torture_assert_werr_equal(tctx, werr, WERR_NOT_FOUND, err_msg);
448                 }
449
450                 /* prefixMap should never be changed */
451                 if (!_torture_drs_pfm_compare_same(tctx, &pfm_prev, pfm, true)) {
452                         torture_fail(tctx, "schema->prefixmap has changed");
453                 }
454
455                 /* reclaim memory for prepared error message */
456                 talloc_free(err_msg);
457         }
458
459         talloc_free(mem_ctx);
460
461         return true;
462 }
463
464 /**
465  * Tests dsdb_schema_pfm_oid_from_attid() using full prefixMap.
466  */
467 static bool torture_drs_unit_pfm_oid_from_attid(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
468 {
469         WERROR werr;
470         uint32_t i, count;
471         char *err_msg;
472         const char *oid;
473
474         count = ARRAY_SIZE(_prefixmap_test_data);
475         for (i = 0; i < count; i++) {
476                 oid = NULL;
477                 werr = dsdb_schema_pfm_oid_from_attid(priv->pfm_full, _prefixmap_test_data[i].attid,
478                                                       priv, &oid);
479                 /* prepare error message */
480                 err_msg = talloc_asprintf(priv, "dsdb_schema_pfm_oid_from_attid() failed with 0x%08X",
481                                                 _prefixmap_test_data[i].attid);
482                 torture_assert(tctx, err_msg, "Unexpected: Have no memory!");
483                 /* verify result and returned ATTID */
484                 torture_assert_werr_ok(tctx, werr, err_msg);
485                 torture_assert(tctx, oid, "dsdb_schema_pfm_oid_from_attid() returned NULL OID!!!");
486                 torture_assert_str_equal(tctx, oid, _prefixmap_test_data[i].oid, err_msg);
487                 /* reclaim memory for prepared error message */
488                 talloc_free(err_msg);
489                 /* free memory for OID */
490                 talloc_free(discard_const(oid));
491         }
492
493         return true;
494 }
495
496 /**
497  * Tests dsdb_schema_pfm_oid_from_attid() for handling
498  * correctly different type of attid values.
499  * See: MS-ADTS, 3.1.1.2.6 ATTRTYP
500  */
501 static bool torture_drs_unit_pfm_oid_from_attid_check_attid(struct torture_context *tctx,
502                                                             struct drsut_prefixmap_data *priv)
503 {
504         WERROR werr;
505         const char *oid;
506
507         /* Test with valid prefixMap attid */
508         werr = dsdb_schema_pfm_oid_from_attid(priv->pfm_full, 0x00010001, tctx, &oid);
509         torture_assert_werr_ok(tctx, werr, "Testing prefixMap type attid = 0x00010001");
510
511         /* Test with valid attid but invalid index */
512         werr = dsdb_schema_pfm_oid_from_attid(priv->pfm_full, 0x01110001, tctx, &oid);
513         torture_assert_werr_equal(tctx, werr, WERR_DS_NO_ATTRIBUTE_OR_VALUE,
514                                   "Testing invalid-index attid = 0x01110001");
515
516         /* Test with attid in msDS-IntId range */
517         werr = dsdb_schema_pfm_oid_from_attid(priv->pfm_full, 0x80000000, tctx, &oid);
518         torture_assert_werr_equal(tctx, werr, WERR_INVALID_PARAMETER,
519                                   "Testing msDS-IntId type attid = 0x80000000");
520         werr = dsdb_schema_pfm_oid_from_attid(priv->pfm_full, 0xBFFFFFFF, tctx, &oid);
521         torture_assert_werr_equal(tctx, werr, WERR_INVALID_PARAMETER,
522                                   "Testing msDS-IntId type attid = 0xBFFFFFFF");
523
524         /* Test with attid in RESERVED range */
525         werr = dsdb_schema_pfm_oid_from_attid(priv->pfm_full, 0xC0000000, tctx, &oid);
526         torture_assert_werr_equal(tctx, werr, WERR_INVALID_PARAMETER,
527                                   "Testing RESERVED type attid = 0xC0000000");
528         werr = dsdb_schema_pfm_oid_from_attid(priv->pfm_full, 0xFFFEFFFF, tctx, &oid);
529         torture_assert_werr_equal(tctx, werr, WERR_INVALID_PARAMETER,
530                                   "Testing RESERVED type attid = 0xFFFEFFFF");
531
532         /* Test with attid in INTERNAL range */
533         werr = dsdb_schema_pfm_oid_from_attid(priv->pfm_full, 0xFFFF0000, tctx, &oid);
534         torture_assert_werr_equal(tctx, werr, WERR_INVALID_PARAMETER,
535                                   "Testing INTERNAL type attid = 0xFFFF0000");
536         werr = dsdb_schema_pfm_oid_from_attid(priv->pfm_full, 0xFFFFFFFF, tctx, &oid);
537         torture_assert_werr_equal(tctx, werr, WERR_INVALID_PARAMETER,
538                                   "Testing INTERNAL type attid = 0xFFFFFFFF");
539
540         return true;
541 }
542
543 /**
544  * Test Schema prefixMap conversions to/from drsuapi prefixMap
545  * representation.
546  */
547 static bool torture_drs_unit_pfm_to_from_drsuapi(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
548 {
549         WERROR werr;
550         const char *schema_info;
551         struct dsdb_schema_prefixmap *pfm;
552         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
553         TALLOC_CTX *mem_ctx;
554
555         mem_ctx = talloc_new(tctx);
556         torture_assert(tctx, mem_ctx, "Unexpected: Have no memory!");
557
558         /* convert Schema_prefixMap to drsuapi_prefixMap */
559         werr = dsdb_drsuapi_pfm_from_schema_pfm(priv->pfm_full, priv->schi_default_str, mem_ctx, &ctr);
560         torture_assert_werr_ok(tctx, werr, "dsdb_drsuapi_pfm_from_schema_pfm() failed");
561         torture_assert(tctx, ctr && ctr->mappings, "drsuapi_prefixMap not constructed correctly");
562         torture_assert_int_equal(tctx, ctr->num_mappings, priv->pfm_full->length + 1,
563                                  "drs_mappings count does not match");
564         /* look for schema_info entry - it should be the last one */
565         schema_info = hex_encode_talloc(mem_ctx,
566                                         ctr->mappings[ctr->num_mappings - 1].oid.binary_oid,
567                                         ctr->mappings[ctr->num_mappings - 1].oid.length);
568         torture_assert_str_equal(tctx,
569                                  schema_info,
570                                  priv->schi_default_str,
571                                  "schema_info not stored correctly or not last entry");
572
573         /* compare schema_prefixMap and drsuapi_prefixMap */
574         werr = dsdb_schema_pfm_contains_drsuapi_pfm(priv->pfm_full, ctr);
575         torture_assert_werr_ok(tctx, werr, "dsdb_schema_pfm_contains_drsuapi_pfm() failed");
576
577         /* convert back drsuapi_prefixMap to schema_prefixMap */
578         werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, mem_ctx, &pfm, &schema_info);
579         torture_assert_werr_ok(tctx, werr, "dsdb_schema_pfm_from_drsuapi_pfm() failed");
580         torture_assert_str_equal(tctx, schema_info, priv->schi_default_str, "Fetched schema_info is different");
581
582         /* compare against the original */
583         if (!_torture_drs_pfm_compare_same(tctx, priv->pfm_full, pfm, true)) {
584                 talloc_free(mem_ctx);
585                 return false;
586         }
587
588         /* test conversion with partial drsuapi_prefixMap */
589         ctr->num_mappings--;
590         werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, false, mem_ctx, &pfm, NULL);
591         torture_assert_werr_ok(tctx, werr, "dsdb_schema_pfm_from_drsuapi_pfm() failed");
592         /* compare against the original */
593         if (!_torture_drs_pfm_compare_same(tctx, priv->pfm_full, pfm, false)) {
594                 talloc_free(mem_ctx);
595                 return false;
596         }
597
598         talloc_free(mem_ctx);
599         return true;
600 }
601
602
603 /**
604  * Test Schema prefixMap conversions to/from ldb_val
605  * blob representation.
606  */
607 static bool torture_drs_unit_pfm_to_from_ldb_val(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
608 {
609         WERROR werr;
610         const char *schema_info;
611         struct dsdb_schema *schema;
612         struct ldb_val pfm_ldb_val;
613         struct ldb_val schema_info_ldb_val;
614         TALLOC_CTX *mem_ctx;
615
616         mem_ctx = talloc_new(tctx);
617         torture_assert(tctx, mem_ctx, "Unexpected: Have no memory!");
618
619         schema = dsdb_new_schema(mem_ctx);
620         torture_assert(tctx, schema, "Unexpected: failed to allocate schema object");
621
622         /* set priv->pfm_full as prefixMap for new schema object */
623         schema->prefixmap = priv->pfm_full;
624         schema->schema_info = priv->schi_default_str;
625
626         /* convert schema_prefixMap to ldb_val blob */
627         werr = dsdb_get_oid_mappings_ldb(schema, mem_ctx, &pfm_ldb_val, &schema_info_ldb_val);
628         torture_assert_werr_ok(tctx, werr, "dsdb_get_oid_mappings_ldb() failed");
629         torture_assert(tctx, pfm_ldb_val.data && pfm_ldb_val.length,
630                        "pfm_ldb_val not constructed correctly");
631         torture_assert(tctx, schema_info_ldb_val.data && schema_info_ldb_val.length,
632                        "schema_info_ldb_val not constructed correctly");
633         /* look for schema_info entry - it should be the last one */
634         schema_info = hex_encode_talloc(mem_ctx,
635                                         schema_info_ldb_val.data,
636                                         schema_info_ldb_val.length);
637         torture_assert_str_equal(tctx,
638                                  schema_info,
639                                  priv->schi_default_str,
640                                  "schema_info not stored correctly or not last entry");
641
642         /* convert pfm_ldb_val back to schema_prefixMap */
643         schema->prefixmap = NULL;
644         schema->schema_info = NULL;
645         werr = dsdb_load_oid_mappings_ldb(schema, &pfm_ldb_val, &schema_info_ldb_val);
646         torture_assert_werr_ok(tctx, werr, "dsdb_load_oid_mappings_ldb() failed");
647         /* compare against the original */
648         if (!_torture_drs_pfm_compare_same(tctx, schema->prefixmap, priv->pfm_full, false)) {
649                 talloc_free(mem_ctx);
650                 return false;
651         }
652
653         talloc_free(mem_ctx);
654         return true;
655 }
656
657 /**
658  * Test read/write in ldb implementation
659  */
660 static bool torture_drs_unit_pfm_read_write_ldb(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
661 {
662         WERROR werr;
663         struct dsdb_schema *schema;
664         struct dsdb_schema_prefixmap *pfm;
665         TALLOC_CTX *mem_ctx;
666
667         mem_ctx = talloc_new(tctx);
668         torture_assert(tctx, mem_ctx, "Unexpected: Have no memory!");
669
670         /* makeup a dsdb_schema to test with */
671         schema = dsdb_new_schema(mem_ctx);
672         torture_assert(tctx, schema, "Unexpected: failed to allocate schema object");
673         /* set priv->pfm_full as prefixMap for new schema object */
674         schema->prefixmap = priv->pfm_full;
675         schema->schema_info = priv->schi_default_str;
676
677         /* write prfixMap to ldb */
678         werr = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, priv->ldb_ctx, schema);
679         torture_assert_werr_ok(tctx, werr, "dsdb_write_prefixes_from_schema_to_ldb() failed");
680
681         /* read from ldb what we have written */
682         werr = dsdb_read_prefixes_from_ldb(priv->ldb_ctx, mem_ctx, &pfm);
683         torture_assert_werr_ok(tctx, werr, "dsdb_read_prefixes_from_ldb() failed");
684
685         /* compare data written/read */
686         if (!_torture_drs_pfm_compare_same(tctx, schema->prefixmap, priv->pfm_full, false)) {
687                 torture_fail(tctx, "prefixMap read/write in LDB is not consistent");
688         }
689
690         talloc_free(mem_ctx);
691
692         return true;
693 }
694
695 /**
696  * Test rdsdb_create_prefix_mapping
697  */
698 static bool torture_drs_unit_dsdb_create_prefix_mapping(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
699 {
700         WERROR werr;
701         uint32_t i;
702         struct dsdb_schema *schema;
703         TALLOC_CTX *mem_ctx;
704         const struct {
705                 const char      *oid;
706                 uint32_t        attid;
707                 bool            exists; /* if this prefix already exists or should be added */
708         } _test_data[] = {
709                 {.oid="2.5.4.0",                .attid=0x00000000, true},
710                 {.oid="2.5.4.42",               .attid=0x0000002a, true},
711                 {.oid="1.2.840.113556.1.2.1",   .attid=0x00020001, true},
712                 {.oid="1.2.840.113556.1.2.13",  .attid=0x0002000d, true},
713                 {.oid="1.2.840.113556.1.2.281", .attid=0x00020119, true},
714                 {.oid="1.2.840.113556.1.4.125", .attid=0x0009007d, true},
715                 {.oid="1.2.840.113556.1.4.146", .attid=0x00090092, true},
716                 {.oid="1.2.250.1",              .attid=0x1b860001, false},
717                 {.oid="1.2.250.16386",          .attid=0x1c788002, false},
718                 {.oid="1.2.250.2097154",        .attid=0x1c7b8002, false},
719         };
720
721         mem_ctx = talloc_new(tctx);
722         torture_assert(tctx, mem_ctx, "Unexpected: Have no memory!");
723
724         /* makeup a dsdb_schema to test with */
725         schema = dsdb_new_schema(mem_ctx);
726         torture_assert(tctx, schema, "Unexpected: failed to allocate schema object");
727         /* set priv->pfm_full as prefixMap for new schema object */
728         schema->schema_info = priv->schi_default_str;
729         werr = _drsut_prefixmap_new(_prefixmap_test_new_data, ARRAY_SIZE(_prefixmap_test_new_data),
730                                     schema, &schema->prefixmap);
731         torture_assert_werr_ok(tctx, werr, "_drsut_prefixmap_new() failed");
732         /* write prfixMap to ldb */
733         werr = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, priv->ldb_ctx, schema);
734         torture_assert_werr_ok(tctx, werr, "dsdb_write_prefixes_from_schema_to_ldb() failed");
735
736         for (i = 0; i < ARRAY_SIZE(_test_data); i++) {
737                 struct dsdb_schema_prefixmap *pfm_ldb;
738                 struct dsdb_schema_prefixmap *pfm_prev;
739
740                 /* add ref to prefixMap so we can use it later */
741                 pfm_prev = talloc_reference(schema, schema->prefixmap);
742
743                 /* call dsdb_create_prefix_mapping() and check result accordingly */
744                 werr = dsdb_create_prefix_mapping(priv->ldb_ctx, schema, _test_data[i].oid);
745                 torture_assert_werr_ok(tctx, werr, "dsdb_create_prefix_mapping() failed");
746
747                 /* verify pfm has been altered or not if needed */
748                 if (_test_data[i].exists) {
749                         torture_assert(tctx, pfm_prev == schema->prefixmap,
750                                        "schema->prefixmap has been reallocated!");
751                         if (!_torture_drs_pfm_compare_same(tctx, pfm_prev, schema->prefixmap, true)) {
752                                 torture_fail(tctx, "schema->prefixmap has changed");
753                         }
754                 } else {
755                         torture_assert(tctx, pfm_prev != schema->prefixmap,
756                                        "schema->prefixmap should be reallocated!");
757                         if (_torture_drs_pfm_compare_same(tctx, pfm_prev, schema->prefixmap, true)) {
758                                 torture_fail(tctx, "schema->prefixmap should be changed");
759                         }
760                 }
761
762                 /* read from ldb what we have written */
763                 werr = dsdb_read_prefixes_from_ldb(priv->ldb_ctx, mem_ctx, &pfm_ldb);
764                 torture_assert_werr_ok(tctx, werr, "dsdb_read_prefixes_from_ldb() failed");
765                 /* compare data written/read */
766                 if (!_torture_drs_pfm_compare_same(tctx, schema->prefixmap, pfm_ldb, true)) {
767                         torture_fail(tctx, "schema->prefixmap and pfm in LDB are different");
768                 }
769                 /* free mem for pfm read from LDB */
770                 talloc_free(pfm_ldb);
771
772                 /* release prefixMap pointer */
773                 talloc_unlink(schema, pfm_prev);
774         }
775
776         talloc_free(mem_ctx);
777
778         return true;
779 }
780
781 /**
782  * Prepare temporary LDB and opens it
783  */
784 static bool torture_drs_unit_ldb_setup(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
785 {
786         int ldb_err;
787         char *ldb_url;
788         bool bret = true;
789         TALLOC_CTX* mem_ctx;
790         char *tempdir;
791         NTSTATUS status;
792
793         mem_ctx = talloc_new(priv);
794
795         status = torture_temp_dir(tctx, "drs_", &tempdir);
796         torture_assert_ntstatus_ok(tctx, status, "creating temp dir");
797
798         ldb_url = talloc_asprintf(priv, "%s/drs_test.ldb", tempdir);
799
800         /* create LDB */
801         priv->ldb_ctx = ldb_init(priv, tctx->ev);
802         ldb_err = ldb_connect(priv->ldb_ctx, ldb_url, 0, NULL);
803         torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE, "ldb_connect() failed");
804
805         /* set some schemaNamingContext */
806         ldb_err = ldb_set_opaque(priv->ldb_ctx,
807                                  "schemaNamingContext",
808                                  ldb_dn_new(priv->ldb_ctx, priv->ldb_ctx, "CN=Schema,CN=Config"));
809         torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE, "ldb_set_opaque() failed");
810
811         /* add prefixMap attribute so tested layer could work properly */
812         {
813                 struct ldb_message *msg = ldb_msg_new(mem_ctx);
814                 msg->dn = ldb_get_schema_basedn(priv->ldb_ctx);
815                 ldb_err = ldb_msg_add_string(msg, "prefixMap", "prefixMap");
816                 torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE,
817                                               "ldb_msg_add_empty() failed");
818
819                 ldb_err = ldb_add(priv->ldb_ctx, msg);
820                 torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE, "ldb_add() failed");
821         }
822
823 DONE:
824         talloc_free(mem_ctx);
825         return bret;
826 }
827
828 /*
829  * Setup/Teardown for test case
830  */
831 static bool torture_drs_unit_prefixmap_setup(struct torture_context *tctx, struct drsut_prefixmap_data **_priv)
832 {
833         WERROR werr;
834         DATA_BLOB blob;
835         struct drsut_prefixmap_data *priv;
836
837         priv = *_priv = talloc_zero(tctx, struct drsut_prefixmap_data);
838         torture_assert(tctx, priv != NULL, "Not enough memory");
839
840         werr = _drsut_prefixmap_new(_prefixmap_test_new_data, ARRAY_SIZE(_prefixmap_test_new_data),
841                                     tctx, &priv->pfm_new);
842         torture_assert_werr_ok(tctx, werr, "failed to create pfm_new");
843
844         werr = _drsut_prefixmap_new(_prefixmap_full_map_data, ARRAY_SIZE(_prefixmap_full_map_data),
845                                     tctx, &priv->pfm_full);
846         torture_assert_werr_ok(tctx, werr, "failed to create pfm_test");
847
848         torture_assert(tctx, drsut_schemainfo_new(tctx, &priv->schi_default),
849                        "drsut_schemainfo_new() failed");
850
851         werr = dsdb_blob_from_schema_info(priv->schi_default, priv, &blob);
852         torture_assert_werr_ok(tctx, werr, "dsdb_blob_from_schema_info() failed");
853
854         priv->schi_default_str = data_blob_hex_string_upper(priv, &blob);
855
856         /* create temporary LDB and populate with data */
857         if (!torture_drs_unit_ldb_setup(tctx, priv)) {
858                 return false;
859         }
860
861         return true;
862 }
863
864 static bool torture_drs_unit_prefixmap_teardown(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
865 {
866         talloc_free(priv);
867
868         return true;
869 }
870
871 /**
872  * Test case initialization for
873  * DRS-UNIT.prefixMap
874  */
875 struct torture_tcase * torture_drs_unit_prefixmap(struct torture_suite *suite)
876 {
877         typedef bool (*pfn_setup)(struct torture_context *, void **);
878         typedef bool (*pfn_teardown)(struct torture_context *, void *);
879         typedef bool (*pfn_run)(struct torture_context *, void *);
880
881         struct torture_tcase * tc = torture_suite_add_tcase(suite, "prefixMap");
882
883         torture_tcase_set_fixture(tc,
884                                   (pfn_setup)torture_drs_unit_prefixmap_setup,
885                                   (pfn_teardown)torture_drs_unit_prefixmap_teardown);
886
887         tc->description = talloc_strdup(tc, "Unit tests for DRSUAPI::prefixMap implementation");
888
889         torture_tcase_add_simple_test(tc, "new", (pfn_run)torture_drs_unit_pfm_new);
890
891         torture_tcase_add_simple_test(tc, "make_attid_full_map", (pfn_run)torture_drs_unit_pfm_make_attid_full_map);
892         torture_tcase_add_simple_test(tc, "make_attid_small_map", (pfn_run)torture_drs_unit_pfm_make_attid_small_map);
893
894         torture_tcase_add_simple_test(tc, "attid_from_oid_full_map",
895                                       (pfn_run)torture_drs_unit_pfm_attid_from_oid_full_map);
896         torture_tcase_add_simple_test(tc, "attid_from_oid_empty_map",
897                                       (pfn_run)torture_drs_unit_pfm_attid_from_oid_base_map);
898
899         torture_tcase_add_simple_test(tc, "oid_from_attid_full_map", (pfn_run)torture_drs_unit_pfm_oid_from_attid);
900         torture_tcase_add_simple_test(tc, "oid_from_attid_check_attid",
901                                       (pfn_run)torture_drs_unit_pfm_oid_from_attid_check_attid);
902
903         torture_tcase_add_simple_test(tc, "pfm_to_from_drsuapi", (pfn_run)torture_drs_unit_pfm_to_from_drsuapi);
904
905         torture_tcase_add_simple_test(tc, "pfm_to_from_ldb_val", (pfn_run)torture_drs_unit_pfm_to_from_ldb_val);
906
907         torture_tcase_add_simple_test(tc, "pfm_read_write_ldb", (pfn_run)torture_drs_unit_pfm_read_write_ldb);
908
909         torture_tcase_add_simple_test(tc, "dsdb_create_prefix_mapping", (pfn_run)torture_drs_unit_dsdb_create_prefix_mapping);
910
911         return tc;
912 }