s4/drs(tort): Test case for dsdb_create_prefix_mapping() implemented
[Samba/fernandojvsilva.git] / source4 / torture / drs / unit / prefixmap_tests.c
blob89fe250037551cbb7716b3ad6b22e17e5c487884
1 /*
2 Unix SMB/CIFS implementation.
4 DRSUAPI prefixMap unit tests
6 Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "torture/smbtorture.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "torture/rpc/drsuapi.h"
27 #include "param/param.h"
30 /**
31 * Default schema_info string to be used for testing
33 #define SCHEMA_INFO_DEFAULT "FF0000000000000000000000000000000123456789"
35 /**
36 * Private data to be shared among all test in Test case
38 struct drsut_prefixmap_data {
39 struct dsdb_schema_prefixmap *pfm_new;
40 struct dsdb_schema_prefixmap *pfm_full;
42 struct ldb_context *ldb_ctx;
45 /**
46 * Test-oid data structure
48 struct drsut_pfm_oid_data {
49 uint32_t id;
50 const char *bin_oid;
51 const char *oid_prefix;
54 /**
55 * Default prefixMap initialization data
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"},
79 /**
80 * Data to be used for creating full prefix map for testing
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"},
137 * OID-to-ATTID mappings to be used for testing
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},
159 * Creates dsdb_schema_prefixmap based on predefined data
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)
165 uint32_t i;
166 struct dsdb_schema_prefixmap *pfm;
168 pfm = talloc(mem_ctx, struct dsdb_schema_prefixmap);
169 W_ERROR_HAVE_NO_MEMORY(pfm);
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;
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;
187 *_pfm = pfm;
189 return WERR_OK;
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)
196 uint32_t i;
198 torture_assert_int_equal(tctx, pfm_left->length, pfm_right->length,
199 "prefixMaps differ in size");
200 for (i = 0; i < pfm_left->length; i++) {
201 struct dsdb_schema_prefixmap_oid *entry_left = &pfm_left->prefixes[i];
202 struct dsdb_schema_prefixmap_oid *entry_right = &pfm_right->prefixes[i];
204 torture_assert(tctx, entry_left->id == entry_right->id,
205 talloc_asprintf(tctx, "Different IDs for index=%d", i));
206 torture_assert_data_blob_equal(tctx, entry_left->bin_oid, entry_right->bin_oid,
207 talloc_asprintf(tctx, "Different bin_oid for index=%d", i));
210 return true;
214 * Tests dsdb_schema_pfm_new()
216 static bool torture_drs_unit_pfm_new(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
218 WERROR werr;
219 bool bret;
220 TALLOC_CTX *mem_ctx;
221 struct dsdb_schema_prefixmap *pfm = NULL;
223 mem_ctx = talloc_new(priv);
225 /* create new prefix map */
226 werr = dsdb_schema_pfm_new(mem_ctx, &pfm);
227 torture_assert_werr_ok(tctx, werr, "dsdb_schema_pfm_new() failed!");
228 torture_assert(tctx, pfm != NULL, "NULL prefixMap created!");
229 torture_assert(tctx, pfm->length > 0, "Empty prefixMap created!");
230 torture_assert(tctx, pfm->prefixes != NULL, "No prefixes for newly created prefixMap!");
232 /* compare newly created prefixMap with template one */
233 bret = _torture_drs_pfm_compare_same(tctx, priv->pfm_new, pfm);
235 talloc_free(mem_ctx);
237 return bret;
241 * Tests dsdb_schema_pfm_make_attid() using full prefixMap.
242 * In this test we know exactly which ATTID and prefixMap->ID
243 * should be returned, i.e. no prefixMap entries should be added.
245 static bool torture_drs_unit_pfm_make_attid_full_map(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
247 WERROR werr;
248 uint32_t i, count;
249 uint32_t attid;
250 char *err_msg;
252 count = ARRAY_SIZE(_prefixmap_test_data);
253 for (i = 0; i < count; i++) {
254 werr = dsdb_schema_pfm_make_attid(priv->pfm_full, _prefixmap_test_data[i].oid, &attid);
255 /* prepare error message */
256 err_msg = talloc_asprintf(priv, "dsdb_schema_pfm_make_attid() failed with %s",
257 _prefixmap_test_data[i].oid);
258 torture_assert(tctx, err_msg, "Unexpected: Have no memory!");
259 /* verify result and returned ATTID */
260 torture_assert_werr_ok(tctx, werr, err_msg);
261 torture_assert_int_equal(tctx, attid, _prefixmap_test_data[i].attid, err_msg);
262 /* reclaim memory for prepared error message */
263 talloc_free(err_msg);
266 return true;
270 * Tests dsdb_schema_pfm_make_attid() using initially small prefixMap.
271 * In this test we don't know exactly which ATTID and prefixMap->ID
272 * should be returned, but we can verify lo-word of ATTID.
273 * This test verifies implementation branch when a new
274 * prefix should be added into prefixMap.
276 static bool torture_drs_unit_pfm_make_attid_small_map(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
278 WERROR werr;
279 uint32_t i, j;
280 uint32_t idx;
281 uint32_t attid, attid_2;
282 char *err_msg;
283 struct dsdb_schema_prefixmap *pfm = NULL;
284 TALLOC_CTX *mem_ctx;
286 mem_ctx = talloc_new(priv);
288 /* create new prefix map */
289 werr = dsdb_schema_pfm_new(mem_ctx, &pfm);
290 torture_assert_werr_ok(tctx, werr, "dsdb_schema_pfm_new() failed!");
291 torture_assert(tctx, pfm != NULL, "NULL prefixMap created!");
292 torture_assert(tctx, pfm->length > 0, "Empty prefixMap created!");
293 torture_assert(tctx, pfm->prefixes != NULL, "No prefixes for newly created prefixMap!");
295 /* make some ATTIDs and check result */
296 for (i = 0; i < ARRAY_SIZE(_prefixmap_test_data); i++) {
297 werr = dsdb_schema_pfm_make_attid(pfm, _prefixmap_test_data[i].oid, &attid);
299 /* prepare error message */
300 err_msg = talloc_asprintf(mem_ctx, "dsdb_schema_pfm_make_attid() failed with %s",
301 _prefixmap_test_data[i].oid);
302 torture_assert(tctx, err_msg, "Unexpected: Have no memory!");
304 /* verify result and returned ATTID */
305 torture_assert_werr_ok(tctx, werr, err_msg);
306 /* verify ATTID lo-word */
307 torture_assert_int_equal(tctx, attid & 0xFFFF, _prefixmap_test_data[i].attid & 0xFFFF, err_msg);
309 /* try again, this time verify for whole ATTID */
310 werr = dsdb_schema_pfm_make_attid(pfm, _prefixmap_test_data[i].oid, &attid_2);
311 torture_assert_werr_ok(tctx, werr, err_msg);
312 torture_assert_int_equal(tctx, attid_2, attid, err_msg);
314 /* reclaim memory for prepared error message */
315 talloc_free(err_msg);
317 /* check there is such an index in modified prefixMap */
318 idx = (attid >> 16);
319 for (j = 0; j < pfm->length; j++) {
320 if (pfm->prefixes[j].id == idx)
321 break;
323 if (j >= pfm->length) {
324 torture_result(tctx, TORTURE_FAIL, __location__": No prefix for ATTID=0x%08X", attid);
325 return false;
330 talloc_free(mem_ctx);
332 return true;
336 * Tests dsdb_schema_pfm_oid_from_attid() using full prefixMap.
338 static bool torture_drs_unit_pfm_oid_from_attid(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
340 WERROR werr;
341 uint32_t i, count;
342 char *err_msg;
343 const char *oid;
345 count = ARRAY_SIZE(_prefixmap_test_data);
346 for (i = 0; i < count; i++) {
347 oid = NULL;
348 werr = dsdb_schema_pfm_oid_from_attid(priv->pfm_full, _prefixmap_test_data[i].attid,
349 priv, &oid);
350 /* prepare error message */
351 err_msg = talloc_asprintf(priv, "dsdb_schema_pfm_oid_from_attid() failed with 0x%08X",
352 _prefixmap_test_data[i].attid);
353 torture_assert(tctx, err_msg, "Unexpected: Have no memory!");
354 /* verify result and returned ATTID */
355 torture_assert_werr_ok(tctx, werr, err_msg);
356 torture_assert(tctx, oid, "dsdb_schema_pfm_oid_from_attid() returned NULL OID!!!");
357 torture_assert_str_equal(tctx, oid, _prefixmap_test_data[i].oid, err_msg);
358 /* reclaim memory for prepared error message */
359 talloc_free(err_msg);
360 /* free memory for OID */
361 talloc_free(discard_const(oid));
364 return true;
368 * Test Schema prefixMap conversions to/from drsuapi prefixMap
369 * representation.
371 static bool torture_drs_unit_pfm_to_from_drsuapi(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
373 WERROR werr;
374 const char *schema_info;
375 struct dsdb_schema_prefixmap *pfm;
376 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
377 TALLOC_CTX *mem_ctx;
379 mem_ctx = talloc_new(tctx);
380 torture_assert(tctx, mem_ctx, "Unexpected: Have no memory!");
382 /* convert Schema_prefixMap to drsuapi_prefixMap */
383 werr = dsdb_drsuapi_pfm_from_schema_pfm(priv->pfm_full, SCHEMA_INFO_DEFAULT, mem_ctx, &ctr);
384 torture_assert_werr_ok(tctx, werr, "dsdb_drsuapi_pfm_from_schema_pfm() failed");
385 torture_assert(tctx, ctr && ctr->mappings, "drsuapi_prefixMap not constructed correctly");
386 torture_assert_int_equal(tctx, ctr->num_mappings, priv->pfm_full->length + 1,
387 "drs_mappings count does not match");
388 /* look for schema_info entry - it should be the last one */
389 schema_info = hex_encode_talloc(mem_ctx,
390 ctr->mappings[ctr->num_mappings - 1].oid.binary_oid,
391 ctr->mappings[ctr->num_mappings - 1].oid.length);
392 torture_assert_str_equal(tctx,
393 schema_info,
394 SCHEMA_INFO_DEFAULT,
395 "schema_info not stored correctly or not last entry");
397 /* compare schema_prefixMap and drsuapi_prefixMap */
398 werr = dsdb_schema_pfm_contains_drsuapi_pfm(priv->pfm_full, ctr);
399 torture_assert_werr_ok(tctx, werr, "dsdb_schema_pfm_contains_drsuapi_pfm() failed");
401 /* convert back drsuapi_prefixMap to schema_prefixMap */
402 werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, mem_ctx, &pfm, &schema_info);
403 torture_assert_werr_ok(tctx, werr, "dsdb_schema_pfm_from_drsuapi_pfm() failed");
404 torture_assert_str_equal(tctx, schema_info, SCHEMA_INFO_DEFAULT, "Fetched schema_info is different");
406 /* compare against the original */
407 if (!_torture_drs_pfm_compare_same(tctx, priv->pfm_full, pfm)) {
408 talloc_free(mem_ctx);
409 return false;
412 /* test conversion with partial drsuapi_prefixMap */
413 ctr->num_mappings--;
414 werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, false, mem_ctx, &pfm, NULL);
415 torture_assert_werr_ok(tctx, werr, "dsdb_schema_pfm_from_drsuapi_pfm() failed");
416 /* compare against the original */
417 if (!_torture_drs_pfm_compare_same(tctx, priv->pfm_full, pfm)) {
418 talloc_free(mem_ctx);
419 return false;
422 talloc_free(mem_ctx);
423 return true;
428 * Test Schema prefixMap conversions to/from ldb_val
429 * blob representation.
431 static bool torture_drs_unit_pfm_to_from_ldb_val(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
433 WERROR werr;
434 const char *schema_info;
435 struct dsdb_schema *schema;
436 struct ldb_val pfm_ldb_val;
437 struct ldb_val schema_info_ldb_val;
438 TALLOC_CTX *mem_ctx;
440 mem_ctx = talloc_new(tctx);
441 torture_assert(tctx, mem_ctx, "Unexpected: Have no memory!");
443 schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(tctx->lp_ctx));
444 torture_assert(tctx, schema, "Unexpected: failed to allocate schema object");
446 /* set priv->pfm_full as prefixMap for new schema object */
447 schema->prefixmap = priv->pfm_full;
448 schema->schema_info = SCHEMA_INFO_DEFAULT;
450 /* convert schema_prefixMap to ldb_val blob */
451 werr = dsdb_get_oid_mappings_ldb(schema, mem_ctx, &pfm_ldb_val, &schema_info_ldb_val);
452 torture_assert_werr_ok(tctx, werr, "dsdb_get_oid_mappings_ldb() failed");
453 torture_assert(tctx, pfm_ldb_val.data && pfm_ldb_val.length,
454 "pfm_ldb_val not constructed correctly");
455 torture_assert(tctx, schema_info_ldb_val.data && schema_info_ldb_val.length,
456 "schema_info_ldb_val not constructed correctly");
457 /* look for schema_info entry - it should be the last one */
458 schema_info = hex_encode_talloc(mem_ctx,
459 schema_info_ldb_val.data,
460 schema_info_ldb_val.length);
461 torture_assert_str_equal(tctx,
462 schema_info,
463 SCHEMA_INFO_DEFAULT,
464 "schema_info not stored correctly or not last entry");
466 /* convert pfm_ldb_val back to schema_prefixMap */
467 schema->prefixmap = NULL;
468 schema->schema_info = NULL;
469 werr = dsdb_load_oid_mappings_ldb(schema, &pfm_ldb_val, &schema_info_ldb_val);
470 torture_assert_werr_ok(tctx, werr, "dsdb_load_oid_mappings_ldb() failed");
471 /* compare against the original */
472 if (!_torture_drs_pfm_compare_same(tctx, schema->prefixmap, priv->pfm_full)) {
473 talloc_free(mem_ctx);
474 return false;
477 talloc_free(mem_ctx);
478 return true;
482 * Test read/write in ldb implementation
484 static bool torture_drs_unit_pfm_read_write_ldb(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
486 WERROR werr;
487 struct dsdb_schema *schema;
488 struct dsdb_schema_prefixmap *pfm;
489 TALLOC_CTX *mem_ctx;
491 mem_ctx = talloc_new(tctx);
492 torture_assert(tctx, mem_ctx, "Unexpected: Have no memory!");
494 /* makeup a dsdb_schema to test with */
495 schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(tctx->lp_ctx));
496 torture_assert(tctx, schema, "Unexpected: failed to allocate schema object");
497 /* set priv->pfm_full as prefixMap for new schema object */
498 schema->prefixmap = priv->pfm_full;
499 schema->schema_info = SCHEMA_INFO_DEFAULT;
501 /* write prfixMap to ldb */
502 werr = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, priv->ldb_ctx, schema);
503 torture_assert_werr_ok(tctx, werr, "dsdb_write_prefixes_from_schema_to_ldb() failed");
505 /* read from ldb what we have written */
506 werr = dsdb_read_prefixes_from_ldb(priv->ldb_ctx, mem_ctx, &pfm);
507 torture_assert_werr_ok(tctx, werr, "dsdb_read_prefixes_from_ldb() failed");
509 /* compare data written/read */
510 _torture_drs_pfm_compare_same(tctx, schema->prefixmap, priv->pfm_full);
512 talloc_free(mem_ctx);
514 if (tctx->last_result != TORTURE_OK) {
515 return false;
518 return true;
522 * Test rdsdb_create_prefix_mapping
524 static bool torture_drs_unit_dsdb_create_prefix_mapping(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
526 WERROR werr;
527 uint32_t i;
528 struct dsdb_schema *schema;
529 TALLOC_CTX *mem_ctx;
530 const struct {
531 const char *oid;
532 uint32_t attid;
533 bool exists; /* if this prefix already exists or should be added */
534 } _test_data[] = {
535 {.oid="2.5.4.0", .attid=0x00000000, true},
536 {.oid="2.5.4.42", .attid=0x0000002a, true},
537 {.oid="1.2.840.113556.1.2.1", .attid=0x00020001, true},
538 {.oid="1.2.840.113556.1.2.13", .attid=0x0002000d, true},
539 {.oid="1.2.840.113556.1.2.281", .attid=0x00020119, true},
540 {.oid="1.2.840.113556.1.4.125", .attid=0x0009007d, true},
541 {.oid="1.2.840.113556.1.4.146", .attid=0x00090092, true},
542 {.oid="1.2.250.1", .attid=0x1b860001, false},
543 {.oid="1.2.250.16386", .attid=0x1c788002, false},
544 {.oid="1.2.250.2097154", .attid=0x1c7b8002, false},
547 mem_ctx = talloc_new(tctx);
548 torture_assert(tctx, mem_ctx, "Unexpected: Have no memory!");
550 /* makeup a dsdb_schema to test with */
551 schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(tctx->lp_ctx));
552 torture_assert(tctx, schema, "Unexpected: failed to allocate schema object");
553 /* set priv->pfm_full as prefixMap for new schema object */
554 schema->schema_info = SCHEMA_INFO_DEFAULT;
555 werr = _drsut_prefixmap_new(_prefixmap_test_new_data, ARRAY_SIZE(_prefixmap_test_new_data),
556 schema, &schema->prefixmap);
557 torture_assert_werr_ok(tctx, werr, "_drsut_prefixmap_new() failed");
558 /* write prfixMap to ldb */
559 werr = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, priv->ldb_ctx, schema);
560 torture_assert_werr_ok(tctx, werr, "dsdb_write_prefixes_from_schema_to_ldb() failed");
562 for (i = 0; i < ARRAY_SIZE(_test_data); i++) {
563 struct dsdb_schema_prefixmap *pfm_ldb;
564 struct dsdb_schema_prefixmap *pfm_prev;
566 /* add ref to prefixMap so we can use it later */
567 pfm_prev = talloc_reference(schema, schema->prefixmap);
569 /* call dsdb_create_prefix_mapping() and check result accordingly */
570 werr = dsdb_create_prefix_mapping(priv->ldb_ctx, schema, _test_data[i].oid);
571 torture_assert_werr_ok(tctx, werr, "dsdb_create_prefix_mapping() failed");
573 /* verify pfm has been altered or not if needed */
574 if (_test_data[i].exists) {
575 torture_assert(tctx, pfm_prev == schema->prefixmap,
576 "schema->prefixmap has been reallocated!");
577 if (!_torture_drs_pfm_compare_same(tctx, pfm_prev, schema->prefixmap)) {
578 torture_fail(tctx, "schema->prefixmap has changed");
580 } else {
581 torture_assert(tctx, pfm_prev != schema->prefixmap,
582 "schema->prefixmap should be reallocated!");
583 if (_torture_drs_pfm_compare_same(tctx, pfm_prev, schema->prefixmap)) {
584 torture_fail(tctx, "schema->prefixmap should be changed");
588 /* read from ldb what we have written */
589 werr = dsdb_read_prefixes_from_ldb(priv->ldb_ctx, mem_ctx, &pfm_ldb);
590 torture_assert_werr_ok(tctx, werr, "dsdb_read_prefixes_from_ldb() failed");
591 /* compare data written/read */
592 if (!_torture_drs_pfm_compare_same(tctx, schema->prefixmap, pfm_ldb)) {
593 torture_fail(tctx, "schema->prefixmap and pfm in LDB are different");
595 /* free mem for pfm read from LDB */
596 talloc_free(pfm_ldb);
598 /* release prefixMap pointer */
599 talloc_unlink(schema, pfm_prev);
602 talloc_free(mem_ctx);
604 return true;
608 * Prepare temporary LDB and opens it
610 static bool torture_drs_unit_ldb_setup(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
612 int fd = -1;
613 int ldb_err;
614 char *ldb_url;
615 bool bret = true;
616 TALLOC_CTX* mem_ctx;
618 mem_ctx = talloc_new(priv);
620 /* try to find to make temporary LDB_ULR */
621 ldb_url = getenv("LDB_URL");
622 if (!ldb_url) {
623 const char *temp_dir;
624 temp_dir = getenv("TEST_DATA_PREFIX");
625 if (!temp_dir) {
626 temp_dir = "/tmp";
628 ldb_url = talloc_asprintf(priv, "%s/drs_XXXXXX", temp_dir);
629 fd = mkstemp(ldb_url);
630 torture_assert(tctx, fd != -1,
631 talloc_asprintf(mem_ctx, "mkstemp() failed: %s", strerror(errno)));
634 /* create LDB */
635 priv->ldb_ctx = ldb_init(priv, tctx->ev);
636 ldb_err = ldb_connect(priv->ldb_ctx, ldb_url, 0, NULL);
637 torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE, "ldb_connect() failed");
639 /* set some schemaNamingContext */
640 ldb_err = ldb_set_opaque(priv->ldb_ctx,
641 "schemaNamingContext",
642 ldb_dn_new(priv->ldb_ctx, priv->ldb_ctx, "CN=Schema,CN=Config"));
643 torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE, "ldb_set_opaque() failed");
645 /* add prefixMap attribute so tested layer could work properly */
647 struct ldb_message *msg = ldb_msg_new(mem_ctx);
648 msg->dn = samdb_schema_dn(priv->ldb_ctx);
649 ldb_err = ldb_msg_add_string(msg, "prefixMap", "prefixMap");
650 torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE,
651 "ldb_msg_add_empty() failed");
653 ldb_err = ldb_add(priv->ldb_ctx, msg);
654 torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE, "ldb_add() failed");
657 DONE:
658 if (fd != -1) {
659 close(fd);
660 unlink(ldb_url);
662 talloc_free(mem_ctx);
663 return bret;
667 * Setup/Teardown for test case
669 static bool torture_drs_unit_prefixmap_setup(struct torture_context *tctx, struct drsut_prefixmap_data **priv)
671 WERROR werr;
673 *priv = talloc_zero(tctx, struct drsut_prefixmap_data);
674 torture_assert(tctx, *priv != NULL, "Not enough memory");
676 werr = _drsut_prefixmap_new(_prefixmap_test_new_data, ARRAY_SIZE(_prefixmap_test_new_data), tctx, &(*priv)->pfm_new);
677 torture_assert_werr_ok(tctx, werr, "failed to create pfm_new");
679 werr = _drsut_prefixmap_new(_prefixmap_full_map_data, ARRAY_SIZE(_prefixmap_full_map_data), tctx, &(*priv)->pfm_full);
680 torture_assert_werr_ok(tctx, werr, "failed to create pfm_test");
682 /* create temporary LDB and populate with data */
683 if (!torture_drs_unit_ldb_setup(tctx, *priv)) {
684 return false;
687 return true;
690 static bool torture_drs_unit_prefixmap_teardown(struct torture_context *tctx, struct drsut_prefixmap_data *priv)
692 talloc_free(priv);
694 return true;
698 * Test case initialization for
699 * DRS-UNIT.prefixMap
701 struct torture_tcase * torture_drs_unit_prefixmap(struct torture_suite *suite)
703 typedef bool (*pfn_setup)(struct torture_context *, void **);
704 typedef bool (*pfn_teardown)(struct torture_context *, void *);
705 typedef bool (*pfn_run)(struct torture_context *, void *);
707 struct torture_tcase * tc = torture_suite_add_tcase(suite, "prefixMap");
709 torture_tcase_set_fixture(tc,
710 (pfn_setup)torture_drs_unit_prefixmap_setup,
711 (pfn_teardown)torture_drs_unit_prefixmap_teardown);
713 tc->description = talloc_strdup(tc, "Unit tests for DRSUAPI::prefixMap implementation");
715 torture_tcase_add_simple_test(tc, "new", (pfn_run)torture_drs_unit_pfm_new);
717 torture_tcase_add_simple_test(tc, "make_attid_full_map", (pfn_run)torture_drs_unit_pfm_make_attid_full_map);
718 torture_tcase_add_simple_test(tc, "make_attid_small_map", (pfn_run)torture_drs_unit_pfm_make_attid_small_map);
719 torture_tcase_add_simple_test(tc, "oid_from_attid_full_map", (pfn_run)torture_drs_unit_pfm_oid_from_attid);
721 torture_tcase_add_simple_test(tc, "pfm_to_from_drsuapi", (pfn_run)torture_drs_unit_pfm_to_from_drsuapi);
723 torture_tcase_add_simple_test(tc, "pfm_to_from_ldb_val", (pfn_run)torture_drs_unit_pfm_to_from_ldb_val);
725 torture_tcase_add_simple_test(tc, "pfm_read_write_ldb", (pfn_run)torture_drs_unit_pfm_read_write_ldb);
727 torture_tcase_add_simple_test(tc, "dsdb_create_prefix_mapping", (pfn_run)torture_drs_unit_dsdb_create_prefix_mapping);
729 return tc;