summaryrefslogtreecommitdiffstats
path: root/source4/dsdb/samdb/ldb_modules/tests/test_audit_log_errors.c
blob: 29317686908b14cdf351c7e38ec91eb65c4b1ec8 (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
/*
   Unit tests for the dsdb audit logging code code in audit_log.c

   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018

   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/>.
*/

/*
 * These tests exercise the error handling code
 */

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <unistd.h>
#include <cmocka.h>

int ldb_audit_log_module_init(const char *version);
#include "../audit_log.c"
#include "lib/ldb/include/ldb_private.h"

/*
 * cmocka wrappers for json_new_object
 */
struct json_object __wrap_json_new_object(void);
struct json_object __real_json_new_object(void);
struct json_object __wrap_json_new_object(void)
{

	bool use_real = (bool)mock();
	if (!use_real) {
		return json_empty_object;
	}
	return __real_json_new_object();
}

/*
 * cmocka wrappers for json_add_version
 */
int __wrap_json_add_version(struct json_object *object, int major, int minor);
int __real_json_add_version(struct json_object *object, int major, int minor);
int __wrap_json_add_version(struct json_object *object, int major, int minor)
{

	int ret = (int)mock();
	if (ret) {
		return ret;
	}
	return __real_json_add_version(object, major, minor);
}

/*
 * cmocka wrappers for json_add_version
 */
int __wrap_json_add_timestamp(struct json_object *object);
int __real_json_add_timestamp(struct json_object *object);
int __wrap_json_add_timestamp(struct json_object *object)
{

	int ret = (int)mock();
	if (ret) {
		return ret;
	}
	return __real_json_add_timestamp(object);
}
/*
 * unit test of operation_json, that ensures that all the expected
 * attributes and objects are in the json object.
 */
static void test_operation_json(void **state)
{
	struct ldb_context *ldb = NULL;
	struct ldb_module  *module = NULL;
	struct ldb_request *req = NULL;
	struct ldb_reply *reply = NULL;
	struct audit_private *audit_private = NULL;

	struct tsocket_address *ts = NULL;

	struct auth_session_info *sess = NULL;
	struct security_token *token = NULL;
	struct dom_sid sid;
	const char *const SID = "S-1-5-21-2470180966-3899876309-2637894779";
	const char * const SESSION = "7130cb06-2062-6a1b-409e-3514c26b1773";
	struct GUID session_id;

	struct GUID transaction_id;
	const char *const TRANSACTION = "7130cb06-2062-6a1b-409e-3514c26b1773";

	struct ldb_dn *dn = NULL;
	const char *const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";

	struct ldb_message *msg = NULL;

	struct json_object json;


	/*
	 * Test setup
	 */
	TALLOC_CTX *ctx = talloc_new(NULL);

	ldb = ldb_init(ctx, NULL);

	audit_private = talloc_zero(ctx, struct audit_private);
	GUID_from_string(TRANSACTION, &transaction_id);
	audit_private->transaction_guid = transaction_id;

	module = talloc_zero(ctx, struct ldb_module);
	module->ldb = ldb;
	ldb_module_set_private(module, audit_private);

	tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 0, &ts);
	ldb_set_opaque(ldb, "remoteAddress", ts);

	sess = talloc_zero(ctx, struct auth_session_info);
	token = talloc_zero(ctx, struct security_token);
	string_to_sid(&sid, SID);
	token->num_sids = 1;
	token->sids = &sid;
	sess->security_token = token;
	GUID_from_string(SESSION, &session_id);
	sess->unique_session_token = session_id;
	ldb_set_opaque(ldb, DSDB_SESSION_INFO, sess);

	msg = talloc_zero(ctx, struct ldb_message);
	dn = ldb_dn_new(ctx, ldb, DN);
	msg->dn = dn;
	ldb_msg_add_string(msg, "attribute", "the-value");

	req = talloc_zero(ctx, struct ldb_request);
	req->operation =  LDB_ADD;
	req->op.add.message = msg;

	reply = talloc_zero(ctx, struct ldb_reply);
	reply->error = LDB_ERR_OPERATIONS_ERROR;

	/*
	 * Fail on the creation of the audit json object
	 */

	will_return(__wrap_json_new_object, false);

	json = operation_json(module, req, reply);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail adding the version object .
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, JSON_ERROR);

	json = operation_json(module, req, reply);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail on creation of the wrapper.
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_new_object, false);

	json = operation_json(module, req, reply);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail adding the timestamp to the wrapper object.
	 */
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_timestamp, JSON_ERROR);

	json = operation_json(module, req, reply);
	assert_true(json_is_invalid(&json));

	/*
	 * Now test the happy path
	 */
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_timestamp, 0);

	json = operation_json(module, req, reply);
	assert_false(json_is_invalid(&json));
	json_free(&json);

	TALLOC_FREE(ctx);

}

/*
 * minimal unit test of password_change_json, that ensures that all the expected
 * attributes and objects are in the json object.
 */
static void test_password_change_json(void **state)
{
	struct ldb_context *ldb = NULL;
	struct ldb_module  *module = NULL;
	struct ldb_request *req = NULL;
	struct ldb_reply *reply = NULL;
	struct audit_private *audit_private = NULL;

	struct tsocket_address *ts = NULL;

	struct auth_session_info *sess = NULL;
	struct security_token *token = NULL;
	struct dom_sid sid;
	const char *const SID = "S-1-5-21-2470180966-3899876309-2637894779";
	const char * const SESSION = "7130cb06-2062-6a1b-409e-3514c26b1773";
	struct GUID session_id;

	struct GUID transaction_id;
	const char *const TRANSACTION = "7130cb06-2062-6a1b-409e-3514c26b1773";

	struct ldb_dn *dn = NULL;
	const char *const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";

	struct ldb_message *msg = NULL;

	struct json_object json;

	TALLOC_CTX *ctx = talloc_new(NULL);

	ldb = ldb_init(ctx, NULL);

	audit_private = talloc_zero(ctx, struct audit_private);
	GUID_from_string(TRANSACTION, &transaction_id);
	audit_private->transaction_guid = transaction_id;

	module = talloc_zero(ctx, struct ldb_module);
	module->ldb = ldb;
	ldb_module_set_private(module, audit_private);

	tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 0, &ts);
	ldb_set_opaque(ldb, "remoteAddress", ts);

	sess = talloc_zero(ctx, struct auth_session_info);
	token = talloc_zero(ctx, struct security_token);
	string_to_sid(&sid, SID);
	token->num_sids = 1;
	token->sids = &sid;
	sess->security_token = token;
	GUID_from_string(SESSION, &session_id);
	sess->unique_session_token = session_id;
	ldb_set_opaque(ldb, DSDB_SESSION_INFO, sess);

	msg = talloc_zero(ctx, struct ldb_message);
	dn = ldb_dn_new(ctx, ldb, DN);
	msg->dn = dn;
	ldb_msg_add_string(msg, "planTextPassword", "super-secret");

	req = talloc_zero(ctx, struct ldb_request);
	req->operation =  LDB_ADD;
	req->op.add.message = msg;
	reply = talloc_zero(ctx, struct ldb_reply);
	reply->error = LDB_SUCCESS;


	/*
	 * Fail on the creation of the audit json object
	 */

	will_return(__wrap_json_new_object, false);
	json = password_change_json(module, req, reply);

	assert_true(json_is_invalid(&json));

	/*
	 * Fail adding the version object .
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, JSON_ERROR);

	json = password_change_json(module, req, reply);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail on creation of the wrapper.
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, false);

	json = password_change_json(module, req, reply);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail on creation of the time stamp.
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_timestamp, JSON_ERROR);

	json = password_change_json(module, req, reply);
	assert_true(json_is_invalid(&json));

	/*
	 * Now test the happy path
	 */
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_timestamp, 0);

	json = password_change_json(module, req, reply);
	assert_false(json_is_invalid(&json));
	json_free(&json);

	TALLOC_FREE(ctx);
}


/*
 * minimal unit test of transaction_json, that ensures that all the expected
 * attributes and objects are in the json object.
 */
static void test_transaction_json(void **state)
{

	struct GUID guid;
	const char * const GUID = "7130cb06-2062-6a1b-409e-3514c26b1773";

	struct json_object json;

	GUID_from_string(GUID, &guid);


	/*
	 * Fail on the creation of the audit json object
	 */

	will_return(__wrap_json_new_object, false);

	json = transaction_json("delete", &guid, 10000099);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail adding the version object .
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, JSON_ERROR);

	json = transaction_json("delete", &guid, 10000099);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail on creation of the wrapper.
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, false);

	json = transaction_json("delete", &guid, 10000099);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail on creation of the time stamp.
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_timestamp, JSON_ERROR);

	json = transaction_json("delete", &guid, 10000099);
	assert_true(json_is_invalid(&json));

	/*
	 * Now test the happy path
	 */
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_timestamp, 0);

	json = transaction_json("delete", &guid, 10000099);
	assert_false(json_is_invalid(&json));
	json_free(&json);
}

/*
 * minimal unit test of commit_failure_json, that ensures that all the
 * expected attributes and objects are in the json object.
 */
static void test_commit_failure_json(void **state)
{

	struct GUID guid;
	const char * const GUID = "7130cb06-2062-6a1b-409e-3514c26b1773";

	struct json_object json;

	GUID_from_string(GUID, &guid);


	/*
	 * Fail on the creation of the audit json object
	 */

	will_return(__wrap_json_new_object, false);

	json = commit_failure_json(
		"prepare",
		987876,
		LDB_ERR_OPERATIONS_ERROR,
		"because",
		&guid);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail adding the version object .
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, JSON_ERROR);

	json = commit_failure_json(
		"prepare",
		987876,
		LDB_ERR_OPERATIONS_ERROR,
		"because",
		&guid);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail on creation of the wrapper.
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, false);

	json = commit_failure_json(
		"prepare",
		987876,
		LDB_ERR_OPERATIONS_ERROR,
		"because",
		&guid);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail on creation of the time stamp.
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_timestamp, JSON_ERROR);

	json = commit_failure_json(
		"prepare",
		987876,
		LDB_ERR_OPERATIONS_ERROR,
		"because",
		&guid);
	assert_true(json_is_invalid(&json));

	/*
	 * Now test the happy path
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_timestamp, 0);

	json = commit_failure_json(
		"prepare",
		987876,
		LDB_ERR_OPERATIONS_ERROR,
		"because",
		&guid);
	assert_false(json_is_invalid(&json));
	json_free(&json);
}

/*
 * unit test of replicated_update_json, that ensures that all the expected
 * attributes and objects are in the json object.
 */
static void test_replicated_update_json(void **state)
{
	struct ldb_context *ldb = NULL;
	struct ldb_module  *module = NULL;
	struct ldb_request *req = NULL;
	struct ldb_reply *reply = NULL;
	struct audit_private *audit_private = NULL;
	struct dsdb_extended_replicated_objects *ro = NULL;
	struct repsFromTo1 *source_dsa = NULL;

	struct GUID transaction_id;
	const char *const TRANSACTION = "7130cb06-2062-6a1b-409e-3514c26b1773";

	struct ldb_dn *dn = NULL;
	const char *const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";

	struct GUID source_dsa_obj_guid;
	const char *const SOURCE_DSA = "7130cb06-2062-6a1b-409e-3514c26b1793";

	struct GUID invocation_id;
	const char *const INVOCATION_ID =
		"7130cb06-2062-6a1b-409e-3514c26b1893";
	struct json_object json;

	TALLOC_CTX *ctx = talloc_new(NULL);

	ldb = ldb_init(ctx, NULL);

	audit_private = talloc_zero(ctx, struct audit_private);
	GUID_from_string(TRANSACTION, &transaction_id);
	audit_private->transaction_guid = transaction_id;

	module = talloc_zero(ctx, struct ldb_module);
	module->ldb = ldb;
	ldb_module_set_private(module, audit_private);

	dn = ldb_dn_new(ctx, ldb, DN);
	GUID_from_string(SOURCE_DSA, &source_dsa_obj_guid);
	GUID_from_string(INVOCATION_ID, &invocation_id);
	source_dsa = talloc_zero(ctx, struct repsFromTo1);
	source_dsa->source_dsa_obj_guid = source_dsa_obj_guid;
	source_dsa->source_dsa_invocation_id = invocation_id;

	ro = talloc_zero(ctx, struct dsdb_extended_replicated_objects);
	ro->source_dsa = source_dsa;
	ro->num_objects = 808;
	ro->linked_attributes_count = 2910;
	ro->partition_dn = dn;
	ro->error = WERR_NOT_SUPPORTED;


	req = talloc_zero(ctx, struct ldb_request);
	req->op.extended.data = ro;
	req->operation = LDB_EXTENDED;

	reply = talloc_zero(ctx, struct ldb_reply);
	reply->error = LDB_ERR_NO_SUCH_OBJECT;


	/*
	 * Fail on the creation of the audit json object
	 */

	will_return(__wrap_json_new_object, false);

	json = replicated_update_json(module, req, reply);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail adding the version object .
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, JSON_ERROR);

	json = replicated_update_json(module, req, reply);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail on creation of the wrapper.
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, false);

	json = replicated_update_json(module, req, reply);
	assert_true(json_is_invalid(&json));

	/*
	 * Fail on creation of the time stamp.
	 */

	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_timestamp, JSON_ERROR);

	json = replicated_update_json(module, req, reply);
	assert_true(json_is_invalid(&json));

	/*
	 * Now test the happy path.
	 */
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_version, 0);
	will_return(__wrap_json_new_object, true);
	will_return(__wrap_json_add_timestamp, 0);

	json = replicated_update_json(module, req, reply);
	assert_false(json_is_invalid(&json));
	json_free(&json);

	TALLOC_FREE(ctx);
}

int main(void) {
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(test_operation_json),
		cmocka_unit_test(test_password_change_json),
		cmocka_unit_test(test_transaction_json),
		cmocka_unit_test(test_commit_failure_json),
		cmocka_unit_test(test_replicated_update_json),
	};

	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
	return cmocka_run_group_tests(tests, NULL, NULL);
}