2 Unix SMB/CIFS implementation.
4 DRSUAPI schemaInfo unit tests
6 Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010
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/>.
23 #include "system/filesys.h"
24 #include "torture/smbtorture.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "dsdb/samdb/ldb_modules/util.h"
27 #include "lib/ldb_wrap.h"
28 #include "lib/ldb/include/ldb_module.h"
29 #include "torture/rpc/drsuapi.h"
30 #include "librpc/ndr/libndr.h"
31 #include "param/param.h"
35 * schemaInfo to init ldb context with
37 * GUID: 00000000-0000-0000-0000-000000000000
39 #define SCHEMA_INFO_INIT_STR "FF0000000000000000000000000000000000000000"
42 * Default schema_info string to be used for testing
44 * GUID: 071c82fd-45c7-4351-a3db-51f75a630a7f
46 #define SCHEMA_INFO_DEFAULT_STR "FF00000001FD821C07C7455143A3DB51F75A630A7F"
49 * Schema info data to test with
51 struct schemainfo_data {
53 struct dsdb_schema_info schi;
59 * Schema info test data in human-readable format (... kind of)
62 const char *schema_info_str;
67 } _schemainfo_test_data[] = {
69 .schema_info_str = "FF0000000000000000000000000000000000000000",
71 .guid_str = "00000000-0000-0000-0000-000000000000",
72 .werr_expected = WERR_OK,
73 .test_both_ways = true
76 .schema_info_str = "FF00000001FD821C07C7455143A3DB51F75A630A7F",
78 .guid_str = "071c82fd-45c7-4351-a3db-51f75a630a7f",
79 .werr_expected = WERR_OK,
80 .test_both_ways = true
83 .schema_info_str = "FFFFFFFFFFFD821C07C7455143A3DB51F75A630A7F",
84 .revision = 0xFFFFFFFF,
85 .guid_str = "071c82fd-45c7-4351-a3db-51f75a630a7f",
86 .werr_expected = WERR_OK,
87 .test_both_ways = true
90 .schema_info_str = "FF00000001FD821C07C7455143A3DB51F75A630A7F00",
92 .guid_str = "071c82fd-45c7-4351-a3db-51f75a630a7f",
93 .werr_expected = WERR_INVALID_PARAMETER,
94 .test_both_ways = false
97 .schema_info_str = "AA00000001FD821C07C7455143A3DB51F75A630A7F",
99 .guid_str = "071c82fd-45c7-4351-a3db-51f75a630a7f",
100 .werr_expected = WERR_INVALID_PARAMETER,
101 .test_both_ways = false
106 * Private data to be shared among all test in Test case
108 struct drsut_schemainfo_data {
109 struct ldb_context *ldb;
110 struct ldb_module *ldb_module;
111 struct dsdb_schema *schema;
113 /* Initial schemaInfo set in ldb to test with */
114 struct dsdb_schema_info *schema_info;
116 uint32_t test_data_count;
117 struct schemainfo_data *test_data;
121 * torture macro to assert for equal dsdb_schema_info's
123 #define torture_assert_schema_info_equal(torture_ctx,got,expected,cmt)\
124 do { const struct dsdb_schema_info *__got = (got), *__expected = (expected); \
125 if (__got->revision != __expected->revision) { \
126 torture_result(torture_ctx, TORTURE_FAIL, \
127 __location__": "#got".revision %d did not match "#expected".revision %d: %s", \
128 (int)__got->revision, (int)__expected->revision, cmt); \
131 if (!GUID_equal(&__got->invocation_id, &__expected->invocation_id)) { \
132 torture_result(torture_ctx, TORTURE_FAIL, \
133 __location__": "#got".invocation_id did not match "#expected".invocation_id: %s", cmt); \
139 * forward declaration for internal functions
141 static bool _drsut_ldb_schema_info_reset(struct torture_context *tctx,
142 struct ldb_context *ldb,
143 const char *schema_info_str,
148 * Creates dsdb_schema_info object based on NDR data
149 * passed as hex string
151 static bool _drsut_schemainfo_new(struct torture_context *tctx,
152 const char *schema_info_str, struct dsdb_schema_info **_si)
157 blob = strhex_to_data_blob(tctx, schema_info_str);
159 torture_comment(tctx, "Not enough memory!\n");
163 werr = dsdb_schema_info_from_blob(&blob, tctx, _si);
164 if (!W_ERROR_IS_OK(werr)) {
165 torture_comment(tctx,
166 "Failed to create dsdb_schema_info object for %s: %s",
172 data_blob_free(&blob);
178 * Creates dsdb_schema_info object based on predefined data
179 * Function is public as it is intended to be used by other
180 * tests (e.g. prefixMap tests)
182 bool drsut_schemainfo_new(struct torture_context *tctx, struct dsdb_schema_info **_si)
184 return _drsut_schemainfo_new(tctx, SCHEMA_INFO_DEFAULT_STR, _si);
189 * Tests dsdb_schema_info_from_blob()
191 static bool test_dsdb_schema_info_from_blob(struct torture_context *tctx,
192 struct drsut_schemainfo_data *priv)
197 struct dsdb_schema_info *schema_info;
200 mem_ctx = talloc_new(priv);
201 torture_assert(tctx, mem_ctx, "Not enough memory!");
203 for (i = 0; i < priv->test_data_count; i++) {
204 struct schemainfo_data *data = &priv->test_data[i];
206 msg = talloc_asprintf(tctx, "dsdb_schema_info_from_blob() [%d]-[%s]",
207 i, _schemainfo_test_data[i].schema_info_str);
209 werr = dsdb_schema_info_from_blob(&data->ndr_blob, mem_ctx, &schema_info);
210 torture_assert_werr_equal(tctx, werr, data->werr_expected, msg);
212 /* test returned data */
213 if (W_ERROR_IS_OK(werr)) {
214 torture_assert_schema_info_equal(tctx,
215 schema_info, &data->schi,
216 "after dsdb_schema_info_from_blob() call");
220 talloc_free(mem_ctx);
226 * Tests dsdb_blob_from_schema_info()
228 static bool test_dsdb_blob_from_schema_info(struct torture_context *tctx,
229 struct drsut_schemainfo_data *priv)
237 mem_ctx = talloc_new(priv);
238 torture_assert(tctx, mem_ctx, "Not enough memory!");
240 for (i = 0; i < priv->test_data_count; i++) {
241 struct schemainfo_data *data = &priv->test_data[i];
243 /* not all test are valid reverse type of conversion */
244 if (!data->test_both_ways) {
248 msg = talloc_asprintf(tctx, "dsdb_blob_from_schema_info() [%d]-[%s]",
249 i, _schemainfo_test_data[i].schema_info_str);
251 werr = dsdb_blob_from_schema_info(&data->schi, mem_ctx, &ndr_blob);
252 torture_assert_werr_equal(tctx, werr, data->werr_expected, msg);
254 /* test returned data */
255 if (W_ERROR_IS_OK(werr)) {
256 torture_assert_data_blob_equal(tctx,
257 ndr_blob, data->ndr_blob,
258 "dsdb_blob_from_schema_info()");
262 talloc_free(mem_ctx);
268 * Tests dsdb_module_schema_info_blob_read()
269 * and dsdb_module_schema_info_blob_write()
271 static bool test_dsdb_module_schema_info_blob_rw(struct torture_context *tctx,
272 struct drsut_schemainfo_data *priv)
275 DATA_BLOB blob_write;
278 /* reset schmeInfo to know value */
280 _drsut_ldb_schema_info_reset(tctx, priv->ldb, SCHEMA_INFO_INIT_STR, false),
281 "_drsut_ldb_schema_info_reset() failed");
283 /* write tests' default schemaInfo */
284 blob_write = strhex_to_data_blob(priv, SCHEMA_INFO_DEFAULT_STR);
285 torture_assert(tctx, blob_write.data, "Not enough memory!");
287 werr = dsdb_module_schema_info_blob_write(priv->ldb_module,
288 DSDB_FLAG_TOP_MODULE,
290 torture_assert_werr_ok(tctx, werr, "dsdb_module_schema_info_blob_write() failed");
292 werr = dsdb_module_schema_info_blob_read(priv->ldb_module, DSDB_FLAG_TOP_MODULE,
294 torture_assert_werr_ok(tctx, werr, "dsdb_module_schema_info_blob_read() failed");
296 /* check if we get what we wrote */
297 torture_assert_data_blob_equal(tctx, blob_read, blob_write,
298 "Write/Read of schemeInfo blob failed");
304 * Tests dsdb_schema_update_schema_info()
306 static bool test_dsdb_module_schema_info_update(struct torture_context *tctx,
307 struct drsut_schemainfo_data *priv)
311 struct dsdb_schema_info *schema_info;
313 /* reset schmeInfo to know value */
315 _drsut_ldb_schema_info_reset(tctx, priv->ldb, SCHEMA_INFO_INIT_STR, false),
316 "_drsut_ldb_schema_info_reset() failed");
318 werr = dsdb_module_schema_info_update(priv->ldb_module,
320 DSDB_FLAG_TOP_MODULE | DSDB_FLAG_AS_SYSTEM);
321 torture_assert_werr_ok(tctx, werr, "dsdb_module_schema_info_update() failed");
323 /* get updated schemaInfo */
324 werr = dsdb_module_schema_info_blob_read(priv->ldb_module, DSDB_FLAG_TOP_MODULE,
326 torture_assert_werr_ok(tctx, werr, "dsdb_module_schema_info_blob_read() failed");
328 werr = dsdb_schema_info_from_blob(&blob, priv, &schema_info);
329 torture_assert_werr_ok(tctx, werr, "dsdb_schema_info_from_blob() failed");
331 /* check against default schema_info */
332 torture_assert_schema_info_equal(tctx, schema_info, priv->schema_info,
333 "schemaInfo attribute no updated correctly");
340 * Reset schemaInfo record to know value
342 static bool _drsut_ldb_schema_info_reset(struct torture_context *tctx,
343 struct ldb_context *ldb,
344 const char *schema_info_str,
350 struct ldb_message *msg;
351 TALLOC_CTX *mem_ctx = talloc_new(tctx);
353 blob = strhex_to_data_blob(mem_ctx, schema_info_str);
354 torture_assert_goto(tctx, blob.data, bret, DONE, "Not enough memory!");
356 msg = ldb_msg_new(mem_ctx);
357 torture_assert_goto(tctx, msg, bret, DONE, "Not enough memory!");
359 msg->dn = ldb_get_schema_basedn(ldb);
360 ldb_err = ldb_msg_add_value(msg, "schemaInfo", &blob, NULL);
361 torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE,
362 "ldb_msg_add_value() failed");
365 ldb_err = ldb_add(ldb, msg);
367 ldb_err = dsdb_replace(ldb, msg, DSDB_MODIFY_PERMISSIVE);
369 torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE,
370 "dsdb_replace() failed");
373 talloc_free(mem_ctx);
378 * Prepare temporary LDB and opens it
380 static bool _drsut_ldb_setup(struct torture_context *tctx, struct drsut_schemainfo_data *priv)
385 char *tempdir = NULL;
389 mem_ctx = talloc_new(priv);
390 torture_assert(tctx, mem_ctx, "Not enough memory!");
392 status = torture_temp_dir(tctx, "drs_", &tempdir);
393 torture_assert_ntstatus_ok_goto(tctx, status, bret, DONE, "creating temp dir");
395 ldb_url = talloc_asprintf(priv, "%s/drs_schemainfo.ldb", tempdir);
396 torture_assert_goto(tctx, ldb_url, bret, DONE, "Not enough memory!");
399 priv->ldb = ldb_wrap_connect(priv, tctx->ev, tctx->lp_ctx,
400 ldb_url, NULL, NULL, 0);
401 torture_assert_goto(tctx, priv->ldb, bret, DONE, "ldb_wrap_connect() failed");
403 /* set some schemaNamingContext */
404 ldb_err = ldb_set_opaque(priv->ldb,
405 "schemaNamingContext",
406 ldb_dn_new(priv->ldb, priv->ldb, "CN=Schema,CN=Config"));
407 torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE,
408 "ldb_set_opaque() failed");
410 /* add schemaInfo attribute so tested layer could work properly */
411 torture_assert_goto(tctx,
412 _drsut_ldb_schema_info_reset(tctx, priv->ldb, SCHEMA_INFO_INIT_STR, true),
414 "_drsut_ldb_schema_info_reset() failed");
417 talloc_free(tempdir);
418 talloc_free(mem_ctx);
423 * Setup/Teardown for test case
425 static bool torture_drs_unit_schemainfo_setup(struct torture_context *tctx,
426 struct drsut_schemainfo_data **_priv)
433 struct drsut_schemainfo_data *priv;
435 priv = talloc_zero(tctx, struct drsut_schemainfo_data);
436 torture_assert(tctx, priv, "Not enough memory!");
438 /* returned allocated pointer here
439 * teardown() will be called even in case of failure,
440 * so we'll get a changes to clean up */
443 /* create initial schemaInfo */
445 _drsut_schemainfo_new(tctx, SCHEMA_INFO_DEFAULT_STR, &priv->schema_info),
446 "Failed to create schema_info test object");
448 /* create data to test with */
449 priv->test_data_count = ARRAY_SIZE(_schemainfo_test_data);
450 priv->test_data = talloc_array(tctx, struct schemainfo_data, priv->test_data_count);
452 for (i = 0; i < ARRAY_SIZE(_schemainfo_test_data); i++) {
453 struct schemainfo_data *data = &priv->test_data[i];
455 ndr_blob = strhex_to_data_blob(priv,
456 _schemainfo_test_data[i].schema_info_str);
457 torture_assert(tctx, ndr_blob.data, "Not enough memory!");
459 status = GUID_from_string(_schemainfo_test_data[i].guid_str, &guid);
460 torture_assert_ntstatus_ok(tctx, status,
461 talloc_asprintf(tctx,
462 "GUID_from_string() failed for %s",
463 _schemainfo_test_data[i].guid_str));
465 data->ndr_blob = ndr_blob;
466 data->schi.invocation_id = guid;
467 data->schi.revision = _schemainfo_test_data[i].revision;
468 data->werr_expected = _schemainfo_test_data[i].werr_expected;
469 data->test_both_ways = _schemainfo_test_data[i].test_both_ways;
473 /* create temporary LDB and populate with data */
474 if (!_drsut_ldb_setup(tctx, priv)) {
478 /* create ldb_module mockup object */
479 priv->ldb_module = ldb_module_new(priv, priv->ldb, "schemaInfo_test_module", NULL);
480 torture_assert(tctx, priv->ldb_module, "Not enough memory!");
482 /* create schema mockup object */
483 priv->schema = dsdb_new_schema(priv, lp_iconv_convenience(tctx->lp_ctx));
485 /* pre-cache invocationId for samdb_ntds_invocation_id()
486 * to work with our mock ldb */
487 ldb_err = ldb_set_opaque(priv->ldb, "cache.invocation_id",
488 &priv->schema_info->invocation_id);
489 torture_assert_int_equal(tctx, ldb_err, LDB_SUCCESS, "ldb_set_opaque() failed");
491 /* Perform all tests in transactions so that
492 * underlying modify calls not to fail */
493 ldb_err = ldb_transaction_start(priv->ldb);
494 torture_assert_int_equal(tctx,
497 "ldb_transaction_start() failed");
502 static bool torture_drs_unit_schemainfo_teardown(struct torture_context *tctx,
503 struct drsut_schemainfo_data *priv)
507 /* commit pending transaction so we will
508 * be able to check what LDB state is */
509 ldb_err = ldb_transaction_commit(priv->ldb);
510 if (ldb_err != LDB_SUCCESS) {
511 torture_comment(tctx, "ldb_transaction_commit() - %s (%s)",
512 ldb_strerror(ldb_err),
513 ldb_errstring(priv->ldb));
522 * Test case initialization for
523 * DRS-UNIT.schemaInfo
525 struct torture_tcase * torture_drs_unit_schemainfo(struct torture_suite *suite)
527 typedef bool (*pfn_setup)(struct torture_context *, void **);
528 typedef bool (*pfn_teardown)(struct torture_context *, void *);
529 typedef bool (*pfn_run)(struct torture_context *, void *);
531 struct torture_tcase * tc = torture_suite_add_tcase(suite, "schemaInfo");
533 torture_tcase_set_fixture(tc,
534 (pfn_setup)torture_drs_unit_schemainfo_setup,
535 (pfn_teardown)torture_drs_unit_schemainfo_teardown);
537 tc->description = talloc_strdup(tc, "Unit tests for DRSUAPI::schemaInfo implementation");
539 torture_tcase_add_simple_test(tc, "dsdb_schema_info_from_blob",
540 (pfn_run)test_dsdb_schema_info_from_blob);
541 torture_tcase_add_simple_test(tc, "dsdb_blob_from_schema_info",
542 (pfn_run)test_dsdb_blob_from_schema_info);
543 torture_tcase_add_simple_test(tc, "dsdb_module_schema_info_blob read|write",
544 (pfn_run)test_dsdb_module_schema_info_blob_rw);
545 torture_tcase_add_simple_test(tc, "dsdb_module_schema_info_update",
546 (pfn_run)test_dsdb_module_schema_info_update);