summaryrefslogtreecommitdiffstats
path: root/source4/torture/drs/unit/schemainfo_tests.c
blob: 4b4cca64241817916deffff2220b510c1522cb6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
   Unix SMB/CIFS implementation.

   DRSUAPI schemaInfo unit tests

   Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "system/filesys.h"
#include "torture/smbtorture.h"
#include "dsdb/samdb/samdb.h"
#include "dsdb/samdb/ldb_modules/util.h"
#include "ldb_wrap.h"
#include <ldb_module.h>
#include "torture/rpc/drsuapi.h"
#include "librpc/ndr/libndr.h"
#include "param/param.h"
#include "torture/drs/proto.h"
#include "torture/drs/proto.h"


/**
 * schemaInfo to init ldb context with
 *   Rev:  0
 *   GUID: 00000000-0000-0000-0000-000000000000
 */
#define SCHEMA_INFO_INIT_STR		"FF0000000000000000000000000000000000000000"

/**
 * Default schema_info string to be used for testing
 *   Rev:  01
 *   GUID: 071c82fd-45c7-4351-a3db-51f75a630a7f
 */
#define SCHEMA_INFO_DEFAULT_STR 	"FF00000001FD821C07C7455143A3DB51F75A630A7F"

/**
 * Schema info data to test with
 */
struct schemainfo_data {
	DATA_BLOB 	ndr_blob;
	struct dsdb_schema_info schi;
	WERROR		werr_expected;
	bool 		test_both_ways;
};

/**
 * Schema info test data in human-readable format (... kind of)
 */
static const struct {
	const char 	*schema_info_str;
	uint32_t	revision;
	const char	*guid_str;
	WERROR		werr_expected;
	bool 		test_both_ways;
} _schemainfo_test_data[] = {
	{
		.schema_info_str = "FF0000000000000000000000000000000000000000",
		.revision = 0,
		.guid_str = "00000000-0000-0000-0000-000000000000",
		.werr_expected = WERR_OK,
		.test_both_ways = true
	},
	{
		.schema_info_str = "FF00000001FD821C07C7455143A3DB51F75A630A7F",
		.revision = 1,
		.guid_str = "071c82fd-45c7-4351-a3db-51f75a630a7f",
		.werr_expected = WERR_OK,
		.test_both_ways = true
	},
	{
		.schema_info_str = "FFFFFFFFFFFD821C07C7455143A3DB51F75A630A7F",
		.revision = 0xFFFFFFFF,
		.guid_str = "071c82fd-45c7-4351-a3db-51f75a630a7f",
		.werr_expected = WERR_OK,
		.test_both_ways = true
	},
	{ /* len == 21 */
		.schema_info_str = "FF00000001FD821C07C7455143A3DB51F75A630A7F00",
		.revision = 1,
		.guid_str = "071c82fd-45c7-4351-a3db-51f75a630a7f",
		.werr_expected = WERR_INVALID_PARAMETER,
		.test_both_ways = false
	},
	{ /* marker == FF */
		.schema_info_str = "AA00000001FD821C07C7455143A3DB51F75A630A7F",
		.revision = 1,
		.guid_str = "071c82fd-45c7-4351-a3db-51f75a630a7f",
		.werr_expected = WERR_INVALID_PARAMETER,
		.test_both_ways = false
	}
};

/**
 * Private data to be shared among all test in Test case
 */
struct drsut_schemainfo_data {
	struct ldb_context *ldb;
	struct ldb_module  *ldb_module;
	struct dsdb_schema *schema;

	/* Initial schemaInfo set in ldb to test with */
	struct dsdb_schema_info *schema_info;

	uint32_t test_data_count;
	struct schemainfo_data *test_data;
};

/**
 * torture macro to assert for equal dsdb_schema_info's
 */
#define torture_assert_schema_info_equal(torture_ctx,got,expected,cmt)\
	do { const struct dsdb_schema_info *__got = (got), *__expected = (expected); \
	if (__got->revision != __expected->revision) { \
		torture_result(torture_ctx, TORTURE_FAIL, \
			       __location__": "#got".revision %d did not match "#expected".revision %d: %s", \
			       (int)__got->revision, (int)__expected->revision, cmt); \
		return false; \
	} \
	if (!GUID_equal(&__got->invocation_id, &__expected->invocation_id)) { \
		torture_result(torture_ctx, TORTURE_FAIL, \
			       __location__": "#got".invocation_id did not match "#expected".invocation_id: %s", cmt); \
		return false; \
	} \
	} while(0)

/*
 * forward declaration for internal functions
 */
static bool _drsut_ldb_schema_info_reset(struct torture_context *tctx,
					 struct ldb_context *ldb,
					 const char *schema_info_str,
					 bool in_setup);


/**
 * Creates dsdb_schema_info object based on NDR data
 * passed as hex string
 */
static bool _drsut_schemainfo_new(struct torture_context *tctx,
				  const char *schema_info_str, struct dsdb_schema_info **_si)
{
	WERROR werr;
	DATA_BLOB blob;

	blob = strhex_to_data_blob(tctx, schema_info_str);
	if (!blob.data) {
		torture_comment(tctx, "Not enough memory!\n");
		return false;
	}

	werr = dsdb_schema_info_from_blob(&blob, tctx, _si);
	if (!W_ERROR_IS_OK(werr)) {
		torture_comment(tctx,
		                "Failed to create dsdb_schema_info object for %s: %s",
		                schema_info_str,
		                win_errstr(werr));
		return false;
	}

	data_blob_free(&blob);

	return true;
}

/**
 * Creates dsdb_schema_info object based on predefined data
 * Function is public as it is intended to be used by other
 * tests (e.g. prefixMap tests)
 */
bool drsut_schemainfo_new(struct torture_context *tctx, struct dsdb_schema_info **_si)
{
	return _drsut_schemainfo_new(tctx, SCHEMA_INFO_DEFAULT_STR, _si);
}


/*
 * Tests dsdb_schema_info_new() and dsdb_schema_info_blob_new()
 */
static bool test_dsdb_schema_info_new(struct torture_context *tctx,
				      struct drsut_schemainfo_data *priv)
{
	WERROR werr;
	DATA_BLOB ndr_blob;
	DATA_BLOB ndr_blob_expected;
	struct dsdb_schema_info *schi;
	TALLOC_CTX *mem_ctx;

	mem_ctx = talloc_new(priv);
	torture_assert(tctx, mem_ctx, "Not enough memory!");
	ndr_blob_expected = strhex_to_data_blob(mem_ctx, SCHEMA_INFO_INIT_STR);
	torture_assert(tctx, ndr_blob_expected.data, "Not enough memory!");

	werr = dsdb_schema_info_new(mem_ctx, &schi);
	torture_assert_werr_ok(tctx, werr, "dsdb_schema_info_new() failed");
	torture_assert_int_equal(tctx, schi->revision, 0,
				 "dsdb_schema_info_new() creates schemaInfo with invalid revision");
	torture_assert(tctx, GUID_all_zero(&schi->invocation_id),
			"dsdb_schema_info_new() creates schemaInfo with not ZERO GUID");

	werr = dsdb_schema_info_blob_new(mem_ctx, &ndr_blob);
	torture_assert_werr_ok(tctx, werr, "dsdb_schema_info_blob_new() failed");
	torture_assert_data_blob_equal(tctx, ndr_blob, ndr_blob_expected,
				       "dsdb_schema_info_blob_new() returned invalid blob");

	talloc_free(mem_ctx);
	return true;
}

/*
 * Tests dsdb_schema_info_from_blob()
 */
static bool test_dsdb_schema_info_from_blob(struct torture_context *tctx,
					    struct drsut_schemainfo_data *priv)
{
	uint32_t i;
	WERROR werr;
	char *msg;
	struct dsdb_schema_info *schema_info;
	TALLOC_CTX *mem_ctx;

	mem_ctx = talloc_new(priv);
	torture_assert(tctx, mem_ctx, "Not enough memory!");

	for (i = 0; i < priv->test_data_count; i++) {
		struct schemainfo_data *data = &priv->test_data[i];

		msg = talloc_asprintf(tctx, "dsdb_schema_info_from_blob() [%d]-[%s]",
		                      i, _schemainfo_test_data[i].schema_info_str);

		werr = dsdb_schema_info_from_blob(&data->ndr_blob, mem_ctx, &schema_info);
		torture_assert_werr_equal(tctx, werr, data->werr_expected, msg);

		/* test returned data */
		if (W_ERROR_IS_OK(werr)) {
			torture_assert_schema_info_equal(tctx,
			                                 schema_info, &data->schi,
			                                 "after dsdb_schema_info_from_blob() call");
		}
	}

	talloc_free(mem_ctx);

	return true;
}

/*
 * Tests dsdb_blob_from_schema_info()
 */
static bool test_dsdb_blob_from_schema_info(struct torture_context *tctx,
					    struct drsut_schemainfo_data *priv)
{
	uint32_t i;
	WERROR werr;
	char *msg;
	DATA_BLOB ndr_blob;
	TALLOC_CTX *mem_ctx;

	mem_ctx = talloc_new(priv);
	torture_assert(tctx, mem_ctx, "Not enough memory!");

	for (i = 0; i < priv->test_data_count; i++) {
		struct schemainfo_data *data = &priv->test_data[i];

		/* not all test are valid reverse type of conversion */
		if (!data->test_both_ways) {
			continue;
		}

		msg = talloc_asprintf(tctx, "dsdb_blob_from_schema_info() [%d]-[%s]",
		                      i, _schemainfo_test_data[i].schema_info_str);

		werr = dsdb_blob_from_schema_info(&data->schi, mem_ctx, &ndr_blob);
		torture_assert_werr_equal(tctx, werr, data->werr_expected, msg);

		/* test returned data */
		if (W_ERROR_IS_OK(werr)) {
			torture_assert_data_blob_equal(tctx,
			                               ndr_blob, data->ndr_blob,
			                               "dsdb_blob_from_schema_info()");
		}
	}

	talloc_free(mem_ctx);

	return true;
}

static bool test_dsdb_schema_info_cmp(struct torture_context *tctx,
				      struct drsut_schemainfo_data *priv)
{
	DATA_BLOB blob;
	struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
	struct dsdb_schema_info schema_info;

	ctr = talloc_zero(priv, struct drsuapi_DsReplicaOIDMapping_Ctr);
	torture_assert(tctx, ctr, "Not enough memory!");

	/* not enough elements */
	torture_assert_werr_equal(tctx,
				  dsdb_schema_info_cmp(priv->schema, ctr),
				  WERR_INVALID_PARAMETER,
				  "dsdb_schema_info_cmp(): unexpected result");

	/* an empty element for schemaInfo */
	ctr->num_mappings = 1;
	ctr->mappings = talloc_zero_array(ctr, struct drsuapi_DsReplicaOIDMapping, 1);
	torture_assert(tctx, ctr->mappings, "Not enough memory!");
	torture_assert_werr_equal(tctx,
				  dsdb_schema_info_cmp(priv->schema, ctr),
				  WERR_INVALID_PARAMETER,
				  "dsdb_schema_info_cmp(): unexpected result");

	/* test with invalid schemaInfo - length != 21 */
	blob = strhex_to_data_blob(ctr, "FF00000001FD821C07C7455143A3DB51F75A630A7F00");
	torture_assert(tctx, blob.data, "Not enough memory!");
	ctr->mappings[0].oid.length     = blob.length;
	ctr->mappings[0].oid.binary_oid = blob.data;
	torture_assert_werr_equal(tctx,
				  dsdb_schema_info_cmp(priv->schema, ctr),
				  WERR_INVALID_PARAMETER,
				  "dsdb_schema_info_cmp(): unexpected result");

	/* test with invalid schemaInfo - marker != 0xFF */
	blob = strhex_to_data_blob(ctr, "AA00000001FD821C07C7455143A3DB51F75A630A7F");
	torture_assert(tctx, blob.data, "Not enough memory!");
	ctr->mappings[0].oid.length     = blob.length;
	ctr->mappings[0].oid.binary_oid = blob.data;
	torture_assert_werr_equal(tctx,
				  dsdb_schema_info_cmp(priv->schema, ctr),
				  WERR_INVALID_PARAMETER,
				  "dsdb_schema_info_cmp(): unexpected result");

	/* test with valid schemaInfo, but older one should be ok */
	blob = strhex_to_data_blob(ctr, "FF0000000000000000000000000000000000000000");
	torture_assert(tctx, blob.data, "Not enough memory!");
	ctr->mappings[0].oid.length     = blob.length;
	ctr->mappings[0].oid.binary_oid = blob.data;
	torture_assert_werr_equal(tctx,
				  dsdb_schema_info_cmp(priv->schema, ctr),
				  WERR_OK,
				  "dsdb_schema_info_cmp(): unexpected result");

	/* test with correct schemaInfo, but invalid ATTID */
	schema_info = *priv->schema->schema_info;
	torture_assert_werr_ok(tctx,
		dsdb_blob_from_schema_info(&schema_info, tctx, &blob),
		"dsdb_blob_from_schema_info() failed");
	ctr->mappings[0].id_prefix	= 1;
	ctr->mappings[0].oid.length     = blob.length;
	ctr->mappings[0].oid.binary_oid = blob.data;
	torture_assert_werr_equal(tctx,
				  dsdb_schema_info_cmp(priv->schema, ctr),
				  WERR_INVALID_PARAMETER,
				  "dsdb_schema_info_cmp(): unexpected result");

	/* test with valid schemaInfo */
	ctr->mappings[0].id_prefix	= 0;
	torture_assert_werr_ok(tctx,
			       dsdb_schema_info_cmp(priv->schema, ctr),
			       "dsdb_schema_info_cmp(): unexpected result");

	/* test with valid schemaInfo, but older revision */
	schema_info = *priv->schema->schema_info;
	schema_info.revision -= 1;
	torture_assert_werr_ok(tctx,
		dsdb_blob_from_schema_info(&schema_info, tctx, &blob),
		"dsdb_blob_from_schema_info() failed");
	ctr->mappings[0].oid.length     = blob.length;
	ctr->mappings[0].oid.binary_oid = blob.data;
	torture_assert_werr_equal(tctx,
				  dsdb_schema_info_cmp(priv->schema, ctr),
				  WERR_OK,
				  "dsdb_schema_info_cmp(): unexpected result");

	/* test with valid schemaInfo, but newer revision */
	schema_info = *priv->schema->schema_info;
	schema_info.revision += 1;
	torture_assert_werr_ok(tctx,
		dsdb_blob_from_schema_info(&schema_info, tctx, &blob),
		"dsdb_blob_from_schema_info() failed");
	ctr->mappings[0].oid.length     = blob.length;
	ctr->mappings[0].oid.binary_oid = blob.data;
	torture_assert_werr_equal(tctx,
				  dsdb_schema_info_cmp(priv->schema, ctr),
				  WERR_DS_DRA_SCHEMA_MISMATCH,
				  "dsdb_schema_info_cmp(): unexpected result");

	/* test with valid schemaInfo, but newer revision and other invocationId */
	schema_info = *priv->schema->schema_info;
	schema_info.revision += 1;
	schema_info.invocation_id.time_mid += 1;
	torture_assert_werr_ok(tctx,
		dsdb_blob_from_schema_info(&schema_info, tctx, &blob),
		"dsdb_blob_from_schema_info() failed");
	ctr->mappings[0].oid.length     = blob.length;
	ctr->mappings[0].oid.binary_oid = blob.data;
	torture_assert_werr_equal(tctx,
				  dsdb_schema_info_cmp(priv->schema, ctr),
				  WERR_DS_DRA_SCHEMA_MISMATCH,
				  "dsdb_schema_info_cmp(): unexpected result");

	/* test with valid schemaInfo, but older revision and other invocationId */
	schema_info = *priv->schema->schema_info;
	schema_info.revision -= 1;
	schema_info.invocation_id.time_mid += 1;
	torture_assert_werr_ok(tctx,
		dsdb_blob_from_schema_info(&schema_info, tctx, &blob),
		"dsdb_blob_from_schema_info() failed");
	ctr->mappings[0].oid.length     = blob.length;
	ctr->mappings[0].oid.binary_oid = blob.data;
	torture_assert_werr_equal(tctx,
				  dsdb_schema_info_cmp(priv->schema, ctr),
				  WERR_OK,
				  "dsdb_schema_info_cmp(): unexpected result");

	/* test with valid schemaInfo, but same revision and other invocationId */
	schema_info = *priv->schema->schema_info;
	schema_info.invocation_id.time_mid += 1;
	torture_assert_werr_ok(tctx,
		dsdb_blob_from_schema_info(&schema_info, tctx, &blob),
		"dsdb_blob_from_schema_info() failed");
	ctr->mappings[0].oid.length     = blob.length;
	ctr->mappings[0].oid.binary_oid = blob.data;
	torture_assert_werr_equal(tctx,
				  dsdb_schema_info_cmp(priv->schema, ctr),
				  WERR_DS_DRA_SCHEMA_CONFLICT,
				  "dsdb_schema_info_cmp(): unexpected result");

	talloc_free(ctr);
	return true;
}

/*
 * Tests dsdb_module_schema_info_blob_read()
 *   and dsdb_module_schema_info_blob_write()
 */
static bool test_dsdb_module_schema_info_blob_rw(struct torture_context *tctx,
						struct drsut_schemainfo_data *priv)
{
	int ldb_err;
	DATA_BLOB blob_write;
	DATA_BLOB blob_read;

	/* reset schmeInfo to know value */
	torture_assert(tctx,
		       _drsut_ldb_schema_info_reset(tctx, priv->ldb, SCHEMA_INFO_INIT_STR, false),
		       "_drsut_ldb_schema_info_reset() failed");

	/* write tests' default schemaInfo */
	blob_write = strhex_to_data_blob(priv, SCHEMA_INFO_DEFAULT_STR);
	torture_assert(tctx, blob_write.data, "Not enough memory!");

	ldb_err = dsdb_module_schema_info_blob_write(priv->ldb_module,
						     DSDB_FLAG_TOP_MODULE,
						     &blob_write, NULL);
	torture_assert_int_equal(tctx, ldb_err, LDB_SUCCESS, "dsdb_module_schema_info_blob_write() failed");

	ldb_err = dsdb_module_schema_info_blob_read(priv->ldb_module, DSDB_FLAG_TOP_MODULE,
						    priv, &blob_read, NULL);
	torture_assert_int_equal(tctx, ldb_err, LDB_SUCCESS, "dsdb_module_schema_info_blob_read() failed");

	/* check if we get what we wrote */
	torture_assert_data_blob_equal(tctx, blob_read, blob_write,
				       "Write/Read of schemeInfo blob failed");

	return true;
}

/*
 * Tests dsdb_schema_update_schema_info()
 */
static bool test_dsdb_module_schema_info_update(struct torture_context *tctx,
						struct drsut_schemainfo_data *priv)
{
	int ldb_err;
	WERROR werr;
	DATA_BLOB blob;
	struct dsdb_schema_info *schema_info;

	/* reset schmeInfo to know value */
	torture_assert(tctx,
		       _drsut_ldb_schema_info_reset(tctx, priv->ldb, SCHEMA_INFO_INIT_STR, false),
		       "_drsut_ldb_schema_info_reset() failed");

	ldb_err = dsdb_module_schema_info_update(priv->ldb_module,
						 priv->schema,
						 DSDB_FLAG_TOP_MODULE | DSDB_FLAG_AS_SYSTEM, NULL);
	torture_assert_int_equal(tctx, ldb_err, LDB_SUCCESS, "dsdb_module_schema_info_update() failed");

	/* get updated schemaInfo */
	ldb_err = dsdb_module_schema_info_blob_read(priv->ldb_module, DSDB_FLAG_TOP_MODULE,
						    priv, &blob, NULL);
	torture_assert_int_equal(tctx, ldb_err, LDB_SUCCESS, "dsdb_module_schema_info_blob_read() failed");

	werr = dsdb_schema_info_from_blob(&blob, priv, &schema_info);
	torture_assert_werr_ok(tctx, werr, "dsdb_schema_info_from_blob() failed");

	/* check against default schema_info */
	torture_assert_schema_info_equal(tctx, schema_info, priv->schema_info,
					  "schemaInfo attribute no updated correctly");

	return true;
}


/**
 * Reset schemaInfo record to know value
 */
static bool _drsut_ldb_schema_info_reset(struct torture_context *tctx,
					 struct ldb_context *ldb,
					 const char *schema_info_str,
					 bool in_setup)
{
	bool bret = true;
	int ldb_err;
	DATA_BLOB blob;
	struct ldb_message *msg;
	TALLOC_CTX *mem_ctx = talloc_new(tctx);

	blob = strhex_to_data_blob(mem_ctx, schema_info_str);
	torture_assert_goto(tctx, blob.data, bret, DONE, "Not enough memory!");

	msg = ldb_msg_new(mem_ctx);
	torture_assert_goto(tctx, msg, bret, DONE, "Not enough memory!");

	msg->dn = ldb_get_schema_basedn(ldb);
	ldb_err = ldb_msg_add_value(msg, "schemaInfo", &blob, NULL);
	torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE,
				      "ldb_msg_add_value() failed");

	if (in_setup) {
		ldb_err = ldb_add(ldb, msg);
	} else {
		ldb_err = dsdb_replace(ldb, msg, DSDB_MODIFY_PERMISSIVE);
	}
	torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE,
				      "dsdb_replace() failed");

DONE:
	talloc_free(mem_ctx);
	return bret;
}

/**
 * Prepare temporary LDB and opens it
 */
static bool _drsut_ldb_setup(struct torture_context *tctx, struct drsut_schemainfo_data *priv)
{
	int ldb_err;
	char *ldb_url;
	bool bret = true;
	char *tempdir = NULL;
	NTSTATUS status;
	TALLOC_CTX* mem_ctx;

	mem_ctx = talloc_new(priv);
	torture_assert(tctx, mem_ctx, "Not enough memory!");

	status = torture_temp_dir(tctx, "drs_", &tempdir);
	torture_assert_ntstatus_ok_goto(tctx, status, bret, DONE, "creating temp dir");

	ldb_url = talloc_asprintf(priv, "%s/drs_schemainfo.ldb", tempdir);
	torture_assert_goto(tctx, ldb_url, bret, DONE, "Not enough memory!");

	/* create LDB */
	priv->ldb = ldb_wrap_connect(priv, tctx->ev, tctx->lp_ctx,
	                             ldb_url, NULL, NULL, 0);
	torture_assert_goto(tctx, priv->ldb, bret, DONE,  "ldb_wrap_connect() failed");

	/* set some schemaNamingContext */
	ldb_err = ldb_set_opaque(priv->ldb,
				 "schemaNamingContext",
				 ldb_dn_new(priv->ldb, priv->ldb, "CN=Schema,CN=Config"));
	torture_assert_int_equal_goto(tctx, ldb_err, LDB_SUCCESS, bret, DONE,
				      "ldb_set_opaque() failed");

	/* add schemaInfo attribute so tested layer could work properly */
	torture_assert_goto(tctx,
			    _drsut_ldb_schema_info_reset(tctx, priv->ldb, SCHEMA_INFO_INIT_STR, true),
			    bret, DONE,
			    "_drsut_ldb_schema_info_reset() failed");

DONE:
	talloc_free(tempdir);
	talloc_free(mem_ctx);
	return bret;
}

/*
 * Setup/Teardown for test case
 */
static bool torture_drs_unit_schemainfo_setup(struct torture_context *tctx,
					      struct drsut_schemainfo_data **_priv)
{
	size_t i;
	int ldb_err;
	NTSTATUS status;
	DATA_BLOB ndr_blob;
	struct GUID guid;
	struct drsut_schemainfo_data *priv;

	priv = talloc_zero(tctx, struct drsut_schemainfo_data);
	torture_assert(tctx, priv, "Not enough memory!");

	/* returned allocated pointer here
	 * teardown() will be called even in case of failure,
	 * so we'll get a changes to clean up  */
	*_priv = priv;

	/* create initial schemaInfo */
	torture_assert(tctx,
		       _drsut_schemainfo_new(tctx, SCHEMA_INFO_DEFAULT_STR, &priv->schema_info),
		       "Failed to create schema_info test object");

	/* create data to test with */
	priv->test_data_count = ARRAY_SIZE(_schemainfo_test_data);
	priv->test_data = talloc_array(tctx, struct schemainfo_data, priv->test_data_count);

	for (i = 0; i < ARRAY_SIZE(_schemainfo_test_data); i++) {
		struct schemainfo_data *data = &priv->test_data[i];

		ndr_blob = strhex_to_data_blob(priv,
		                               _schemainfo_test_data[i].schema_info_str);
		torture_assert(tctx, ndr_blob.data, "Not enough memory!");

		status = GUID_from_string(_schemainfo_test_data[i].guid_str, &guid);
		torture_assert_ntstatus_ok(tctx, status,
					   talloc_asprintf(tctx,
					                   "GUID_from_string() failed for %s",
					                   _schemainfo_test_data[i].guid_str));

		data->ndr_blob           = ndr_blob;
		data->schi.invocation_id = guid;
		data->schi.revision      = _schemainfo_test_data[i].revision;
		data->werr_expected      = _schemainfo_test_data[i].werr_expected;
		data->test_both_ways     = _schemainfo_test_data[i].test_both_ways;

	}

	/* create temporary LDB and populate with data */
	if (!_drsut_ldb_setup(tctx, priv)) {
		return false;
	}

	/* create ldb_module mockup object */
	priv->ldb_module = ldb_module_new(priv, priv->ldb, "schemaInfo_test_module", NULL);
	torture_assert(tctx, priv->ldb_module, "Not enough memory!");

	/* create schema mockup object */
	priv->schema = dsdb_new_schema(priv);

	/* set schema_info in dsdb_schema for testing */
	torture_assert(tctx,
		       _drsut_schemainfo_new(tctx, SCHEMA_INFO_DEFAULT_STR, &priv->schema->schema_info),
		       "Failed to create schema_info test object");

	/* pre-cache invocationId for samdb_ntds_invocation_id()
	 * to work with our mock ldb */
	ldb_err = ldb_set_opaque(priv->ldb, "cache.invocation_id",
	                         &priv->schema_info->invocation_id);
	torture_assert_int_equal(tctx, ldb_err, LDB_SUCCESS, "ldb_set_opaque() failed");

	/* Perform all tests in transactions so that
	 * underlying modify calls not to fail */
	ldb_err = ldb_transaction_start(priv->ldb);
	torture_assert_int_equal(tctx,
				 ldb_err,
				 LDB_SUCCESS,
				 "ldb_transaction_start() failed");

	return true;
}

static bool torture_drs_unit_schemainfo_teardown(struct torture_context *tctx,
						 struct drsut_schemainfo_data *priv)
{
	int ldb_err;

	/* commit pending transaction so we will
	 * be able to check what LDB state is */
	ldb_err = ldb_transaction_commit(priv->ldb);
	if (ldb_err != LDB_SUCCESS) {
		torture_comment(tctx, "ldb_transaction_commit() - %s (%s)",
		                ldb_strerror(ldb_err),
		                ldb_errstring(priv->ldb));
	}

	talloc_free(priv);

	return true;
}

/**
 * Test case initialization for
 * drs.unit.schemaInfo
 */
struct torture_tcase * torture_drs_unit_schemainfo(struct torture_suite *suite)
{
	typedef bool (*pfn_setup)(struct torture_context *, void **);
	typedef bool (*pfn_teardown)(struct torture_context *, void *);
	typedef bool (*pfn_run)(struct torture_context *, void *);

	struct torture_tcase * tc = torture_suite_add_tcase(suite, "schemaInfo");

	torture_tcase_set_fixture(tc,
				  (pfn_setup)torture_drs_unit_schemainfo_setup,
				  (pfn_teardown)torture_drs_unit_schemainfo_teardown);

	tc->description = talloc_strdup(tc, "Unit tests for DRSUAPI::schemaInfo implementation");

	torture_tcase_add_simple_test(tc, "dsdb_schema_info_new",
				      (pfn_run)test_dsdb_schema_info_new);
	torture_tcase_add_simple_test(tc, "dsdb_schema_info_from_blob",
				      (pfn_run)test_dsdb_schema_info_from_blob);
	torture_tcase_add_simple_test(tc, "dsdb_blob_from_schema_info",
				      (pfn_run)test_dsdb_blob_from_schema_info);
	torture_tcase_add_simple_test(tc, "dsdb_schema_info_cmp",
				      (pfn_run)test_dsdb_schema_info_cmp);
	torture_tcase_add_simple_test(tc, "dsdb_module_schema_info_blob read|write",
				      (pfn_run)test_dsdb_module_schema_info_blob_rw);
	torture_tcase_add_simple_test(tc, "dsdb_module_schema_info_update",
				      (pfn_run)test_dsdb_module_schema_info_update);


	return tc;
}