2 Unit tests for the dsdb audit logging code code in audit_log.c
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 int ldb_audit_log_module_init(const char *version);
27 #include "../audit_log.c"
29 #include "lib/ldb/include/ldb_private.h"
33 * Test helper to check ISO 8601 timestamps for validity
35 static void check_timestamp(time_t before, const char* timestamp)
48 * Convert the ISO 8601 timestamp into a time_t
49 * Note for convenience we ignore the value of the microsecond
50 * part of the time stamp.
54 "%4d-%2d-%2dT%2d:%2d:%2d.%6d%1c%4d",
64 assert_int_equal(9, rc);
65 tm.tm_year = tm.tm_year - 1900;
66 tm.tm_mon = tm.tm_mon - 1;
71 * The timestamp should be before <= actual <= after
73 assert_true(difftime(actual, before) >= 0);
74 assert_true(difftime(after, actual) >= 0);
77 static void test_has_password_changed(void **state)
79 struct ldb_context *ldb = NULL;
80 struct ldb_message *msg = NULL;
82 TALLOC_CTX *ctx = talloc_new(NULL);
84 ldb = talloc_zero(ctx, struct ldb_context);
89 msg = ldb_msg_new(ldb);
90 assert_false(has_password_changed(msg));
94 * No password attributes
96 msg = ldb_msg_new(ldb);
97 ldb_msg_add_string(msg, "attr01", "value01");
98 assert_false(has_password_changed(msg));
102 * No password attributes >1 entries
104 msg = ldb_msg_new(ldb);
105 ldb_msg_add_string(msg, "attr01", "value01");
106 ldb_msg_add_string(msg, "attr02", "value03");
107 ldb_msg_add_string(msg, "attr03", "value03");
108 assert_false(has_password_changed(msg));
114 msg = ldb_msg_new(ldb);
115 ldb_msg_add_string(msg, "userPassword", "value01");
116 assert_true(has_password_changed(msg));
120 * clearTextPassword set
122 msg = ldb_msg_new(ldb);
123 ldb_msg_add_string(msg, "clearTextPassword", "value01");
124 assert_true(has_password_changed(msg));
130 msg = ldb_msg_new(ldb);
131 ldb_msg_add_string(msg, "unicodePwd", "value01");
132 assert_true(has_password_changed(msg));
138 msg = ldb_msg_new(ldb);
139 ldb_msg_add_string(msg, "dBCSPwd", "value01");
140 assert_true(has_password_changed(msg));
146 msg = ldb_msg_new(ldb);
147 ldb_msg_add_string(msg, "userPassword", "value01");
148 ldb_msg_add_string(msg, "clearTextPassword", "value02");
149 ldb_msg_add_string(msg, "unicodePwd", "value03");
150 ldb_msg_add_string(msg, "dBCSPwd", "value04");
151 assert_true(has_password_changed(msg));
155 * first attribute is a password attribute
157 msg = ldb_msg_new(ldb);
158 ldb_msg_add_string(msg, "userPassword", "value01");
159 ldb_msg_add_string(msg, "attr02", "value02");
160 ldb_msg_add_string(msg, "attr03", "value03");
161 ldb_msg_add_string(msg, "attr04", "value04");
162 assert_true(has_password_changed(msg));
166 * last attribute is a password attribute
168 msg = ldb_msg_new(ldb);
169 ldb_msg_add_string(msg, "attr01", "value01");
170 ldb_msg_add_string(msg, "attr02", "value02");
171 ldb_msg_add_string(msg, "attr03", "value03");
172 ldb_msg_add_string(msg, "clearTextPassword", "value04");
173 assert_true(has_password_changed(msg));
177 * middle attribute is a password attribute
179 msg = ldb_msg_new(ldb);
180 ldb_msg_add_string(msg, "attr01", "value01");
181 ldb_msg_add_string(msg, "attr02", "value02");
182 ldb_msg_add_string(msg, "unicodePwd", "pwd");
183 ldb_msg_add_string(msg, "attr03", "value03");
184 ldb_msg_add_string(msg, "attr04", "value04");
185 assert_true(has_password_changed(msg));
191 static void test_get_password_action(void **state)
193 struct ldb_context *ldb = NULL;
194 struct ldb_request *req = NULL;
195 struct ldb_reply *reply = NULL;
196 struct dsdb_control_password_acl_validation *pav = NULL;
198 TALLOC_CTX *ctx = talloc_new(NULL);
199 ldb = talloc_zero(ctx, struct ldb_context);
202 * Add request, will always be a reset
204 ldb_build_add_req(&req, ldb, ctx, NULL, NULL, NULL, NULL, NULL);
205 reply = talloc_zero(ctx, struct ldb_reply);
206 assert_string_equal("Reset", get_password_action(req, reply));
211 * No password control acl, expect "Reset"
213 ldb_build_mod_req(&req, ldb, ctx, NULL, NULL, NULL, NULL, NULL);
214 reply = talloc_zero(ctx, struct ldb_reply);
215 assert_string_equal("Reset", get_password_action(req, reply));
220 * dsdb_control_password_acl_validation reset = false, expect "Change"
222 ldb_build_mod_req(&req, ldb, ctx, NULL, NULL, NULL, NULL, NULL);
223 reply = talloc_zero(ctx, struct ldb_reply);
224 pav = talloc_zero(req, struct dsdb_control_password_acl_validation);
226 ldb_reply_add_control(
228 DSDB_CONTROL_PASSWORD_ACL_VALIDATION_OID,
231 assert_string_equal("Change", get_password_action(req, reply));
236 * dsdb_control_password_acl_validation reset = true, expect "Reset"
238 ldb_build_mod_req(&req, ldb, ctx, NULL, NULL, NULL, NULL, NULL);
239 reply = talloc_zero(ctx, struct ldb_reply);
240 pav = talloc_zero(req, struct dsdb_control_password_acl_validation);
241 pav->pwd_reset = true;
243 ldb_reply_add_control(
245 DSDB_CONTROL_PASSWORD_ACL_VALIDATION_OID,
248 assert_string_equal("Reset", get_password_action(req, reply));
256 * Test helper to validate a version object.
258 static void check_version(struct json_t *version, int major, int minor)
260 struct json_t *v = NULL;
262 assert_true(json_is_object(version));
263 assert_int_equal(2, json_object_size(version));
265 v = json_object_get(version, "major");
267 assert_int_equal(major, json_integer_value(v));
269 v = json_object_get(version, "minor");
271 assert_int_equal(minor, json_integer_value(v));
275 * minimal unit test of operation_json, that ensures that all the expected
276 * attributes and objects are in the json object.
278 static void test_operation_json_empty(void **state)
280 struct ldb_context *ldb = NULL;
281 struct ldb_module *module = NULL;
282 struct ldb_request *req = NULL;
283 struct ldb_reply *reply = NULL;
284 struct audit_context *ac = NULL;
286 struct json_object json;
287 json_t *audit = NULL;
293 TALLOC_CTX *ctx = talloc_new(NULL);
295 ldb = talloc_zero(ctx, struct ldb_context);
296 ac = talloc_zero(ctx, struct audit_context);
298 module = talloc_zero(ctx, struct ldb_module);
300 ldb_module_set_private(module, ac);
302 req = talloc_zero(ctx, struct ldb_request);
303 reply = talloc_zero(ctx, struct ldb_reply);
304 reply->error = LDB_SUCCESS;
307 json = operation_json(module, req, reply);
308 assert_int_equal(3, json_object_size(json.root));
311 v = json_object_get(json.root, "type");
313 assert_string_equal("dsdbChange", json_string_value(v));
315 v = json_object_get(json.root, "timestamp");
317 assert_true(json_is_string(v));
318 check_timestamp(before, json_string_value(v));
320 audit = json_object_get(json.root, "dsdbChange");
321 assert_non_null(audit);
322 assert_true(json_is_object(audit));
323 assert_int_equal(10, json_object_size(audit));
325 o = json_object_get(audit, "version");
327 check_version(o, OPERATION_MAJOR, OPERATION_MINOR);
329 v = json_object_get(audit, "statusCode");
331 assert_true(json_is_integer(v));
332 assert_int_equal(LDB_SUCCESS, json_integer_value(v));
334 v = json_object_get(audit, "status");
336 assert_true(json_is_string(v));
337 assert_string_equal("Success", json_string_value(v));
339 v = json_object_get(audit, "operation");
341 assert_true(json_is_string(v));
343 * Search operation constant is zero
345 assert_string_equal("Search", json_string_value(v));
347 v = json_object_get(audit, "remoteAddress");
349 assert_true(json_is_null(v));
351 v = json_object_get(audit, "userSid");
353 assert_true(json_is_null(v));
355 v = json_object_get(audit, "performedAsSystem");
357 assert_true(json_is_boolean(v));
358 assert_true(json_is_false(v));
361 v = json_object_get(audit, "dn");
363 assert_true(json_is_null(v));
365 v = json_object_get(audit, "transactionId");
367 assert_true(json_is_string(v));
369 "00000000-0000-0000-0000-000000000000",
370 json_string_value(v));
372 v = json_object_get(audit, "sessionId");
374 assert_true(json_is_null(v));
382 * unit test of operation_json, that ensures that all the expected
383 * attributes and objects are in the json object.
385 static void test_operation_json(void **state)
387 struct ldb_context *ldb = NULL;
388 struct ldb_module *module = NULL;
389 struct ldb_request *req = NULL;
390 struct ldb_reply *reply = NULL;
391 struct audit_context *ac = NULL;
393 struct tsocket_address *ts = NULL;
395 struct auth_session_info *sess = NULL;
396 struct security_token *token = NULL;
398 const char *const SID = "S-1-5-21-2470180966-3899876309-2637894779";
399 const char * const SESSION = "7130cb06-2062-6a1b-409e-3514c26b1773";
400 struct GUID session_id;
402 struct GUID transaction_id;
403 const char *const TRANSACTION = "7130cb06-2062-6a1b-409e-3514c26b1773";
405 struct ldb_dn *dn = NULL;
406 const char *const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";
408 struct ldb_message *msg = NULL;
410 struct json_object json;
411 json_t *audit = NULL;
424 TALLOC_CTX *ctx = talloc_new(NULL);
426 ldb = talloc_zero(ctx, struct ldb_context);
428 ac = talloc_zero(ctx, struct audit_context);
429 GUID_from_string(TRANSACTION, &transaction_id);
430 ac->transaction_guid = transaction_id;
432 module = talloc_zero(ctx, struct ldb_module);
434 ldb_module_set_private(module, ac);
436 tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 0, &ts);
437 ldb_set_opaque(ldb, "remoteAddress", ts);
439 sess = talloc_zero(ctx, struct auth_session_info);
440 token = talloc_zero(ctx, struct security_token);
441 string_to_sid(&sid, SID);
444 sess->security_token = token;
445 GUID_from_string(SESSION, &session_id);
446 sess->unique_session_token = session_id;
447 ldb_set_opaque(ldb, "sessionInfo", sess);
449 msg = talloc_zero(ctx, struct ldb_message);
450 dn = ldb_dn_new(ctx, ldb, DN);
452 ldb_msg_add_string(msg, "attribute", "the-value");
454 req = talloc_zero(ctx, struct ldb_request);
455 req->operation = LDB_ADD;
456 req->op.add.message = msg;
458 reply = talloc_zero(ctx, struct ldb_reply);
459 reply->error = LDB_ERR_OPERATIONS_ERROR;
462 json = operation_json(module, req, reply);
463 assert_int_equal(3, json_object_size(json.root));
465 v = json_object_get(json.root, "type");
467 assert_string_equal("dsdbChange", json_string_value(v));
469 v = json_object_get(json.root, "timestamp");
471 assert_true(json_is_string(v));
472 check_timestamp(before, json_string_value(v));
474 audit = json_object_get(json.root, "dsdbChange");
475 assert_non_null(audit);
476 assert_true(json_is_object(audit));
477 assert_int_equal(11, json_object_size(audit));
479 o = json_object_get(audit, "version");
481 check_version(o, OPERATION_MAJOR, OPERATION_MINOR);
483 v = json_object_get(audit, "statusCode");
485 assert_true(json_is_integer(v));
486 assert_int_equal(LDB_ERR_OPERATIONS_ERROR, json_integer_value(v));
488 v = json_object_get(audit, "status");
490 assert_true(json_is_string(v));
491 assert_string_equal("Operations error", json_string_value(v));
493 v = json_object_get(audit, "operation");
495 assert_true(json_is_string(v));
496 assert_string_equal("Add", json_string_value(v));
498 v = json_object_get(audit, "remoteAddress");
500 assert_true(json_is_string(v));
501 assert_string_equal("ipv4:127.0.0.1:0", json_string_value(v));
503 v = json_object_get(audit, "userSid");
505 assert_true(json_is_string(v));
506 assert_string_equal(SID, json_string_value(v));
508 v = json_object_get(audit, "performedAsSystem");
510 assert_true(json_is_boolean(v));
511 assert_true(json_is_false(v));
513 v = json_object_get(audit, "dn");
515 assert_true(json_is_string(v));
516 assert_string_equal(DN, json_string_value(v));
518 v = json_object_get(audit, "transactionId");
520 assert_true(json_is_string(v));
521 assert_string_equal(TRANSACTION, json_string_value(v));
523 v = json_object_get(audit, "sessionId");
525 assert_true(json_is_string(v));
526 assert_string_equal(SESSION, json_string_value(v));
528 o = json_object_get(audit, "attributes");
530 assert_true(json_is_object(o));
531 assert_int_equal(1, json_object_size(o));
533 a = json_object_get(o, "attribute");
535 assert_true(json_is_object(a));
537 b = json_object_get(a, "actions");
539 assert_true(json_is_array(b));
540 assert_int_equal(1, json_array_size(b));
542 c = json_array_get(b, 0);
544 assert_true(json_is_object(c));
546 d = json_object_get(c, "action");
548 assert_true(json_is_string(d));
549 assert_string_equal("add", json_string_value(d));
551 e = json_object_get(c, "values");
553 assert_true(json_is_array(e));
554 assert_int_equal(1, json_array_size(e));
556 f = json_array_get(e, 0);
558 assert_true(json_is_object(f));
559 assert_int_equal(1, json_object_size(f));
561 g = json_object_get(f, "value");
563 assert_true(json_is_string(g));
564 assert_string_equal("the-value", json_string_value(g));
572 * unit test of operation_json, that ensures that all the expected
573 * attributes and objects are in the json object.
574 * In this case for an operation performed as the system user.
576 static void test_as_system_operation_json(void **state)
578 struct ldb_context *ldb = NULL;
579 struct ldb_module *module = NULL;
580 struct ldb_request *req = NULL;
581 struct ldb_reply *reply = NULL;
582 struct audit_context *ac = NULL;
584 struct tsocket_address *ts = NULL;
586 struct auth_session_info *sess = NULL;
587 struct auth_session_info *sys_sess = NULL;
588 struct security_token *token = NULL;
589 struct security_token *sys_token = NULL;
591 const char *const SID = "S-1-5-21-2470180966-3899876309-2637894779";
592 const char * const SESSION = "7130cb06-2062-6a1b-409e-3514c26b1773";
593 const char * const SYS_SESSION = "7130cb06-2062-6a1b-409e-3514c26b1998";
594 struct GUID session_id;
595 struct GUID sys_session_id;
597 struct GUID transaction_id;
598 const char *const TRANSACTION = "7130cb06-2062-6a1b-409e-3514c26b1773";
600 struct ldb_dn *dn = NULL;
601 const char *const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";
603 struct ldb_message *msg = NULL;
605 struct json_object json;
606 json_t *audit = NULL;
619 TALLOC_CTX *ctx = talloc_new(NULL);
621 ldb = talloc_zero(ctx, struct ldb_context);
623 ac = talloc_zero(ctx, struct audit_context);
624 GUID_from_string(TRANSACTION, &transaction_id);
625 ac->transaction_guid = transaction_id;
627 module = talloc_zero(ctx, struct ldb_module);
629 ldb_module_set_private(module, ac);
631 tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 0, &ts);
632 ldb_set_opaque(ldb, "remoteAddress", ts);
634 sess = talloc_zero(ctx, struct auth_session_info);
635 token = talloc_zero(ctx, struct security_token);
636 string_to_sid(&sid, SID);
639 sess->security_token = token;
640 GUID_from_string(SESSION, &session_id);
641 sess->unique_session_token = session_id;
642 ldb_set_opaque(ldb, "networkSessionInfo", sess);
644 sys_sess = talloc_zero(ctx, struct auth_session_info);
645 sys_token = talloc_zero(ctx, struct security_token);
646 sys_token->num_sids = 1;
647 sys_token->sids = discard_const(&global_sid_System);
648 sys_sess->security_token = sys_token;
649 GUID_from_string(SYS_SESSION, &sys_session_id);
650 sess->unique_session_token = sys_session_id;
651 ldb_set_opaque(ldb, "sessionInfo", sys_sess);
653 msg = talloc_zero(ctx, struct ldb_message);
654 dn = ldb_dn_new(ctx, ldb, DN);
656 ldb_msg_add_string(msg, "attribute", "the-value");
658 req = talloc_zero(ctx, struct ldb_request);
659 req->operation = LDB_ADD;
660 req->op.add.message = msg;
662 reply = talloc_zero(ctx, struct ldb_reply);
663 reply->error = LDB_ERR_OPERATIONS_ERROR;
666 json = operation_json(module, req, reply);
667 assert_int_equal(3, json_object_size(json.root));
669 v = json_object_get(json.root, "type");
671 assert_string_equal("dsdbChange", json_string_value(v));
673 v = json_object_get(json.root, "timestamp");
675 assert_true(json_is_string(v));
676 check_timestamp(before, json_string_value(v));
678 audit = json_object_get(json.root, "dsdbChange");
679 assert_non_null(audit);
680 assert_true(json_is_object(audit));
681 assert_int_equal(11, json_object_size(audit));
683 o = json_object_get(audit, "version");
685 check_version(o, OPERATION_MAJOR, OPERATION_MINOR);
687 v = json_object_get(audit, "statusCode");
689 assert_true(json_is_integer(v));
690 assert_int_equal(LDB_ERR_OPERATIONS_ERROR, json_integer_value(v));
692 v = json_object_get(audit, "status");
694 assert_true(json_is_string(v));
695 assert_string_equal("Operations error", json_string_value(v));
697 v = json_object_get(audit, "operation");
699 assert_true(json_is_string(v));
700 assert_string_equal("Add", json_string_value(v));
702 v = json_object_get(audit, "remoteAddress");
704 assert_true(json_is_string(v));
705 assert_string_equal("ipv4:127.0.0.1:0", json_string_value(v));
707 v = json_object_get(audit, "userSid");
709 assert_true(json_is_string(v));
710 assert_string_equal(SID, json_string_value(v));
712 v = json_object_get(audit, "performedAsSystem");
714 assert_true(json_is_boolean(v));
715 assert_true(json_is_true(v));
717 v = json_object_get(audit, "dn");
719 assert_true(json_is_string(v));
720 assert_string_equal(DN, json_string_value(v));
722 v = json_object_get(audit, "transactionId");
724 assert_true(json_is_string(v));
725 assert_string_equal(TRANSACTION, json_string_value(v));
727 v = json_object_get(audit, "sessionId");
729 assert_true(json_is_string(v));
730 assert_string_equal(SYS_SESSION, json_string_value(v));
732 o = json_object_get(audit, "attributes");
734 assert_true(json_is_object(o));
735 assert_int_equal(1, json_object_size(o));
737 a = json_object_get(o, "attribute");
739 assert_true(json_is_object(a));
741 b = json_object_get(a, "actions");
743 assert_true(json_is_array(b));
744 assert_int_equal(1, json_array_size(b));
746 c = json_array_get(b, 0);
748 assert_true(json_is_object(c));
750 d = json_object_get(c, "action");
752 assert_true(json_is_string(d));
753 assert_string_equal("add", json_string_value(d));
755 e = json_object_get(c, "values");
757 assert_true(json_is_array(e));
758 assert_int_equal(1, json_array_size(e));
760 f = json_array_get(e, 0);
762 assert_true(json_is_object(f));
763 assert_int_equal(1, json_object_size(f));
765 g = json_object_get(f, "value");
767 assert_true(json_is_string(g));
768 assert_string_equal("the-value", json_string_value(g));
776 * minimal unit test of password_change_json, that ensures that all the expected
777 * attributes and objects are in the json object.
779 static void test_password_change_json_empty(void **state)
781 struct ldb_context *ldb = NULL;
782 struct ldb_module *module = NULL;
783 struct ldb_request *req = NULL;
784 struct ldb_reply *reply = NULL;
785 struct audit_context *ac = NULL;
787 struct json_object json;
788 json_t *audit = NULL;
794 TALLOC_CTX *ctx = talloc_new(NULL);
796 ldb = talloc_zero(ctx, struct ldb_context);
797 ac = talloc_zero(ctx, struct audit_context);
799 module = talloc_zero(ctx, struct ldb_module);
801 ldb_module_set_private(module, ac);
803 req = talloc_zero(ctx, struct ldb_request);
804 reply = talloc_zero(ctx, struct ldb_reply);
805 reply->error = LDB_SUCCESS;
808 json = password_change_json(module, req, reply);
809 assert_int_equal(3, json_object_size(json.root));
812 v = json_object_get(json.root, "type");
814 assert_string_equal("passwordChange", json_string_value(v));
816 v = json_object_get(json.root, "timestamp");
818 assert_true(json_is_string(v));
819 check_timestamp(before, json_string_value(v));
821 audit = json_object_get(json.root, "passwordChange");
822 assert_non_null(audit);
823 assert_true(json_is_object(audit));
824 assert_int_equal(9, json_object_size(audit));
826 o = json_object_get(audit, "version");
829 v = json_object_get(audit, "statusCode");
832 v = json_object_get(audit, "status");
835 v = json_object_get(audit, "remoteAddress");
838 v = json_object_get(audit, "userSid");
841 v = json_object_get(audit, "dn");
844 v = json_object_get(audit, "transactionId");
847 v = json_object_get(audit, "sessionId");
850 v = json_object_get(audit, "action");
859 * minimal unit test of password_change_json, that ensures that all the expected
860 * attributes and objects are in the json object.
862 static void test_password_change_json(void **state)
864 struct ldb_context *ldb = NULL;
865 struct ldb_module *module = NULL;
866 struct ldb_request *req = NULL;
867 struct ldb_reply *reply = NULL;
868 struct audit_context *ac = NULL;
870 struct tsocket_address *ts = NULL;
872 struct auth_session_info *sess = NULL;
873 struct security_token *token = NULL;
875 const char *const SID = "S-1-5-21-2470180966-3899876309-2637894779";
876 const char * const SESSION = "7130cb06-2062-6a1b-409e-3514c26b1773";
877 struct GUID session_id;
879 struct GUID transaction_id;
880 const char *const TRANSACTION = "7130cb06-2062-6a1b-409e-3514c26b1773";
882 struct ldb_dn *dn = NULL;
883 const char *const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";
885 struct ldb_message *msg = NULL;
887 struct json_object json;
888 json_t *audit = NULL;
893 TALLOC_CTX *ctx = talloc_new(NULL);
895 ldb = talloc_zero(ctx, struct ldb_context);
897 ac = talloc_zero(ctx, struct audit_context);
898 GUID_from_string(TRANSACTION, &transaction_id);
899 ac->transaction_guid = transaction_id;
901 module = talloc_zero(ctx, struct ldb_module);
903 ldb_module_set_private(module, ac);
905 tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 0, &ts);
906 ldb_set_opaque(ldb, "remoteAddress", ts);
908 sess = talloc_zero(ctx, struct auth_session_info);
909 token = talloc_zero(ctx, struct security_token);
910 string_to_sid(&sid, SID);
913 sess->security_token = token;
914 GUID_from_string(SESSION, &session_id);
915 sess->unique_session_token = session_id;
916 ldb_set_opaque(ldb, "sessionInfo", sess);
918 msg = talloc_zero(ctx, struct ldb_message);
919 dn = ldb_dn_new(ctx, ldb, DN);
921 ldb_msg_add_string(msg, "planTextPassword", "super-secret");
923 req = talloc_zero(ctx, struct ldb_request);
924 req->operation = LDB_ADD;
925 req->op.add.message = msg;
926 reply = talloc_zero(ctx, struct ldb_reply);
927 reply->error = LDB_SUCCESS;
930 json = password_change_json(module, req, reply);
931 assert_int_equal(3, json_object_size(json.root));
934 v = json_object_get(json.root, "type");
936 assert_string_equal("passwordChange", json_string_value(v));
938 v = json_object_get(json.root, "timestamp");
940 assert_true(json_is_string(v));
941 check_timestamp(before, json_string_value(v));
943 audit = json_object_get(json.root, "passwordChange");
944 assert_non_null(audit);
945 assert_true(json_is_object(audit));
946 assert_int_equal(9, json_object_size(audit));
948 o = json_object_get(audit, "version");
950 check_version(o, PASSWORD_MAJOR,PASSWORD_MINOR);
952 v = json_object_get(audit, "statusCode");
954 assert_true(json_is_integer(v));
955 assert_int_equal(LDB_SUCCESS, json_integer_value(v));
957 v = json_object_get(audit, "status");
959 assert_true(json_is_string(v));
960 assert_string_equal("Success", json_string_value(v));
962 v = json_object_get(audit, "remoteAddress");
964 assert_true(json_is_string(v));
965 assert_string_equal("ipv4:127.0.0.1:0", json_string_value(v));
967 v = json_object_get(audit, "userSid");
969 assert_true(json_is_string(v));
970 assert_string_equal(SID, json_string_value(v));
972 v = json_object_get(audit, "dn");
974 assert_true(json_is_string(v));
975 assert_string_equal(DN, json_string_value(v));
977 v = json_object_get(audit, "transactionId");
979 assert_true(json_is_string(v));
980 assert_string_equal(TRANSACTION, json_string_value(v));
982 v = json_object_get(audit, "sessionId");
984 assert_true(json_is_string(v));
985 assert_string_equal(SESSION, json_string_value(v));
987 v = json_object_get(audit, "action");
989 assert_true(json_is_string(v));
990 assert_string_equal("Reset", json_string_value(v));
999 * minimal unit test of transaction_json, that ensures that all the expected
1000 * attributes and objects are in the json object.
1002 static void test_transaction_json(void **state)
1006 const char * const GUID = "7130cb06-2062-6a1b-409e-3514c26b1773";
1008 struct json_object json;
1009 json_t *audit = NULL;
1014 GUID_from_string(GUID, &guid);
1016 before = time(NULL);
1017 json = transaction_json("delete", &guid);
1019 assert_int_equal(3, json_object_size(json.root));
1022 v = json_object_get(json.root, "type");
1024 assert_string_equal("dsdbTransaction", json_string_value(v));
1026 v = json_object_get(json.root, "timestamp");
1028 assert_true(json_is_string(v));
1029 check_timestamp(before, json_string_value(v));
1031 audit = json_object_get(json.root, "dsdbTransaction");
1032 assert_non_null(audit);
1033 assert_true(json_is_object(audit));
1034 assert_int_equal(3, json_object_size(audit));
1036 o = json_object_get(audit, "version");
1038 check_version(o, TRANSACTION_MAJOR, TRANSACTION_MINOR);
1040 v = json_object_get(audit, "transactionId");
1042 assert_true(json_is_string(v));
1043 assert_string_equal(GUID, json_string_value(v));
1045 v = json_object_get(audit, "action");
1047 assert_true(json_is_string(v));
1048 assert_string_equal("delete", json_string_value(v));
1055 * minimal unit test of commit_failure_json, that ensures that all the
1056 * expected attributes and objects are in the json object.
1058 static void test_commit_failure_json(void **state)
1062 const char * const GUID = "7130cb06-2062-6a1b-409e-3514c26b1773";
1064 struct json_object json;
1065 json_t *audit = NULL;
1070 GUID_from_string(GUID, &guid);
1072 before = time(NULL);
1073 json = commit_failure_json(
1075 LDB_ERR_OPERATIONS_ERROR,
1079 assert_int_equal(3, json_object_size(json.root));
1082 v = json_object_get(json.root, "type");
1084 assert_string_equal("dsdbTransaction", json_string_value(v));
1086 v = json_object_get(json.root, "timestamp");
1088 assert_true(json_is_string(v));
1089 check_timestamp(before, json_string_value(v));
1091 audit = json_object_get(json.root, "dsdbTransaction");
1092 assert_non_null(audit);
1093 assert_true(json_is_object(audit));
1094 assert_int_equal(6, json_object_size(audit));
1096 o = json_object_get(audit, "version");
1098 check_version(o, TRANSACTION_MAJOR, TRANSACTION_MINOR);
1100 v = json_object_get(audit, "transactionId");
1102 assert_true(json_is_string(v));
1103 assert_string_equal(GUID, json_string_value(v));
1105 v = json_object_get(audit, "action");
1107 assert_true(json_is_string(v));
1108 assert_string_equal("prepare", json_string_value(v));
1110 v = json_object_get(audit, "statusCode");
1112 assert_true(json_is_integer(v));
1113 assert_int_equal(LDB_ERR_OPERATIONS_ERROR, json_integer_value(v));
1115 v = json_object_get(audit, "status");
1117 assert_true(json_is_string(v));
1118 assert_string_equal("Operations error", json_string_value(v));
1119 v = json_object_get(audit, "status");
1122 v = json_object_get(audit, "reason");
1124 assert_true(json_is_string(v));
1125 assert_string_equal("because", json_string_value(v));
1132 * minimal unit test of replicated_update_json, that ensures that all the
1133 * expected attributes and objects are in the json object.
1135 static void test_replicated_update_json_empty(void **state)
1137 struct ldb_context *ldb = NULL;
1138 struct ldb_module *module = NULL;
1139 struct ldb_request *req = NULL;
1140 struct ldb_reply *reply = NULL;
1141 struct audit_context *ac = NULL;
1142 struct dsdb_extended_replicated_objects *ro = NULL;
1143 struct repsFromTo1 *source_dsa = NULL;
1145 struct json_object json;
1146 json_t *audit = NULL;
1152 TALLOC_CTX *ctx = talloc_new(NULL);
1154 ldb = talloc_zero(ctx, struct ldb_context);
1155 ac = talloc_zero(ctx, struct audit_context);
1157 module = talloc_zero(ctx, struct ldb_module);
1159 ldb_module_set_private(module, ac);
1161 source_dsa = talloc_zero(ctx, struct repsFromTo1);
1162 ro = talloc_zero(ctx, struct dsdb_extended_replicated_objects);
1163 ro->source_dsa = source_dsa;
1164 req = talloc_zero(ctx, struct ldb_request);
1165 req->op.extended.data = ro;
1166 req->operation = LDB_EXTENDED;
1167 reply = talloc_zero(ctx, struct ldb_reply);
1168 reply->error = LDB_SUCCESS;
1170 before = time(NULL);
1171 json = replicated_update_json(module, req, reply);
1172 assert_int_equal(3, json_object_size(json.root));
1175 v = json_object_get(json.root, "type");
1177 assert_string_equal("replicatedUpdate", json_string_value(v));
1179 v = json_object_get(json.root, "timestamp");
1181 assert_true(json_is_string(v));
1182 check_timestamp(before, json_string_value(v));
1184 audit = json_object_get(json.root, "replicatedUpdate");
1185 assert_non_null(audit);
1186 assert_true(json_is_object(audit));
1187 assert_int_equal(11, json_object_size(audit));
1189 o = json_object_get(audit, "version");
1191 check_version(o, REPLICATION_MAJOR, REPLICATION_MINOR);
1193 v = json_object_get(audit, "statusCode");
1195 assert_true(json_is_integer(v));
1196 assert_int_equal(LDB_SUCCESS, json_integer_value(v));
1198 v = json_object_get(audit, "status");
1200 assert_true(json_is_string(v));
1201 assert_string_equal("Success", json_string_value(v));
1203 v = json_object_get(audit, "transactionId");
1205 assert_true(json_is_string(v));
1206 assert_string_equal(
1207 "00000000-0000-0000-0000-000000000000",
1208 json_string_value(v));
1210 v = json_object_get(audit, "objectCount");
1212 assert_true(json_is_integer(v));
1213 assert_int_equal(0, json_integer_value(v));
1215 v = json_object_get(audit, "linkCount");
1217 assert_true(json_is_integer(v));
1218 assert_int_equal(0, json_integer_value(v));
1220 v = json_object_get(audit, "partitionDN");
1222 assert_true(json_is_null(v));
1224 v = json_object_get(audit, "error");
1226 assert_true(json_is_string(v));
1227 assert_string_equal(
1228 "The operation completed successfully.",
1229 json_string_value(v));
1231 v = json_object_get(audit, "errorCode");
1233 assert_true(json_is_integer(v));
1234 assert_int_equal(0, json_integer_value(v));
1236 v = json_object_get(audit, "sourceDsa");
1238 assert_true(json_is_string(v));
1239 assert_string_equal(
1240 "00000000-0000-0000-0000-000000000000",
1241 json_string_value(v));
1243 v = json_object_get(audit, "invocationId");
1245 assert_true(json_is_string(v));
1246 assert_string_equal(
1247 "00000000-0000-0000-0000-000000000000",
1248 json_string_value(v));
1256 * unit test of replicated_update_json, that ensures that all the expected
1257 * attributes and objects are in the json object.
1259 static void test_replicated_update_json(void **state)
1261 struct ldb_context *ldb = NULL;
1262 struct ldb_module *module = NULL;
1263 struct ldb_request *req = NULL;
1264 struct ldb_reply *reply = NULL;
1265 struct audit_context *ac = NULL;
1266 struct dsdb_extended_replicated_objects *ro = NULL;
1267 struct repsFromTo1 *source_dsa = NULL;
1269 struct GUID transaction_id;
1270 const char *const TRANSACTION = "7130cb06-2062-6a1b-409e-3514c26b1773";
1272 struct ldb_dn *dn = NULL;
1273 const char *const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";
1275 struct GUID source_dsa_obj_guid;
1276 const char *const SOURCE_DSA = "7130cb06-2062-6a1b-409e-3514c26b1793";
1278 struct GUID invocation_id;
1279 const char *const INVOCATION_ID =
1280 "7130cb06-2062-6a1b-409e-3514c26b1893";
1281 struct json_object json;
1282 json_t *audit = NULL;
1288 TALLOC_CTX *ctx = talloc_new(NULL);
1290 ldb = talloc_zero(ctx, struct ldb_context);
1292 ac = talloc_zero(ctx, struct audit_context);
1293 GUID_from_string(TRANSACTION, &transaction_id);
1294 ac->transaction_guid = transaction_id;
1296 module = talloc_zero(ctx, struct ldb_module);
1298 ldb_module_set_private(module, ac);
1300 dn = ldb_dn_new(ctx, ldb, DN);
1301 GUID_from_string(SOURCE_DSA, &source_dsa_obj_guid);
1302 GUID_from_string(INVOCATION_ID, &invocation_id);
1303 source_dsa = talloc_zero(ctx, struct repsFromTo1);
1304 source_dsa->source_dsa_obj_guid = source_dsa_obj_guid;
1305 source_dsa->source_dsa_invocation_id = invocation_id;
1307 ro = talloc_zero(ctx, struct dsdb_extended_replicated_objects);
1308 ro->source_dsa = source_dsa;
1309 ro->num_objects = 808;
1310 ro->linked_attributes_count = 2910;
1311 ro->partition_dn = dn;
1312 ro->error = WERR_NOT_SUPPORTED;
1315 req = talloc_zero(ctx, struct ldb_request);
1316 req->op.extended.data = ro;
1317 req->operation = LDB_EXTENDED;
1319 reply = talloc_zero(ctx, struct ldb_reply);
1320 reply->error = LDB_ERR_NO_SUCH_OBJECT;
1322 before = time(NULL);
1323 json = replicated_update_json(module, req, reply);
1324 assert_int_equal(3, json_object_size(json.root));
1327 v = json_object_get(json.root, "type");
1329 assert_string_equal("replicatedUpdate", json_string_value(v));
1331 v = json_object_get(json.root, "timestamp");
1333 assert_true(json_is_string(v));
1334 check_timestamp(before, json_string_value(v));
1336 audit = json_object_get(json.root, "replicatedUpdate");
1337 assert_non_null(audit);
1338 assert_true(json_is_object(audit));
1339 assert_int_equal(11, json_object_size(audit));
1341 o = json_object_get(audit, "version");
1343 check_version(o, REPLICATION_MAJOR, REPLICATION_MINOR);
1345 v = json_object_get(audit, "statusCode");
1347 assert_true(json_is_integer(v));
1348 assert_int_equal(LDB_ERR_NO_SUCH_OBJECT, json_integer_value(v));
1350 v = json_object_get(audit, "status");
1352 assert_true(json_is_string(v));
1353 assert_string_equal("No such object", json_string_value(v));
1355 v = json_object_get(audit, "transactionId");
1357 assert_true(json_is_string(v));
1358 assert_string_equal(TRANSACTION, json_string_value(v));
1360 v = json_object_get(audit, "objectCount");
1362 assert_true(json_is_integer(v));
1363 assert_int_equal(808, json_integer_value(v));
1365 v = json_object_get(audit, "linkCount");
1367 assert_true(json_is_integer(v));
1368 assert_int_equal(2910, json_integer_value(v));
1370 v = json_object_get(audit, "partitionDN");
1372 assert_true(json_is_string(v));
1373 assert_string_equal(DN, json_string_value(v));
1375 v = json_object_get(audit, "error");
1377 assert_true(json_is_string(v));
1378 assert_string_equal(
1379 "The request is not supported.",
1380 json_string_value(v));
1382 v = json_object_get(audit, "errorCode");
1384 assert_true(json_is_integer(v));
1385 assert_int_equal(W_ERROR_V(WERR_NOT_SUPPORTED), json_integer_value(v));
1387 v = json_object_get(audit, "sourceDsa");
1389 assert_true(json_is_string(v));
1390 assert_string_equal(SOURCE_DSA, json_string_value(v));
1392 v = json_object_get(audit, "invocationId");
1394 assert_true(json_is_string(v));
1395 assert_string_equal(INVOCATION_ID, json_string_value(v));
1403 * minimal unit test of operation_human_readable, that ensures that all the
1404 * expected attributes and objects are in the json object.
1406 static void test_operation_hr_empty(void **state)
1408 struct ldb_context *ldb = NULL;
1409 struct ldb_module *module = NULL;
1410 struct ldb_request *req = NULL;
1411 struct ldb_reply *reply = NULL;
1412 struct audit_context *ac = NULL;
1415 const char *rs = NULL;
1420 TALLOC_CTX *ctx = talloc_new(NULL);
1422 ldb = talloc_zero(ctx, struct ldb_context);
1423 ac = talloc_zero(ctx, struct audit_context);
1425 module = talloc_zero(ctx, struct ldb_module);
1427 ldb_module_set_private(module, ac);
1429 req = talloc_zero(ctx, struct ldb_request);
1430 reply = talloc_zero(ctx, struct ldb_reply);
1431 reply->error = LDB_SUCCESS;
1433 line = operation_human_readable(ctx, module, req, reply);
1434 assert_non_null(line);
1437 * We ignore the timestamp to make this test a little easier
1440 rs = "\\[Search] at \\["
1442 "\\] status \\[Success\\] remote host \\[Unknown\\]"
1443 " SID \\[(NULL SID)\\] DN \\[(null)\\]";
1445 ret = regcomp(®ex, rs, 0);
1446 assert_int_equal(0, ret);
1448 ret = regexec(®ex, line, 0, NULL, 0);
1449 assert_int_equal(0, ret);
1457 * unit test of operation_json, that ensures that all the expected
1458 * attributes and objects are in the json object.
1460 static void test_operation_hr(void **state)
1462 struct ldb_context *ldb = NULL;
1463 struct ldb_module *module = NULL;
1464 struct ldb_request *req = NULL;
1465 struct ldb_reply *reply = NULL;
1466 struct audit_context *ac = NULL;
1468 struct tsocket_address *ts = NULL;
1470 struct auth_session_info *sess = NULL;
1471 struct security_token *token = NULL;
1473 const char *const SID = "S-1-5-21-2470180966-3899876309-2637894779";
1474 const char * const SESSION = "7130cb06-2062-6a1b-409e-3514c26b1773";
1475 struct GUID session_id;
1477 struct GUID transaction_id;
1478 const char *const TRANSACTION = "7130cb06-2062-6a1b-409e-3514c26b1773";
1480 struct ldb_dn *dn = NULL;
1481 const char *const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";
1483 struct ldb_message *msg = NULL;
1486 const char *rs = NULL;
1492 TALLOC_CTX *ctx = talloc_new(NULL);
1494 ldb = talloc_zero(ctx, struct ldb_context);
1496 ac = talloc_zero(ctx, struct audit_context);
1497 GUID_from_string(TRANSACTION, &transaction_id);
1498 ac->transaction_guid = transaction_id;
1500 module = talloc_zero(ctx, struct ldb_module);
1502 ldb_module_set_private(module, ac);
1504 tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 0, &ts);
1505 ldb_set_opaque(ldb, "remoteAddress", ts);
1507 sess = talloc_zero(ctx, struct auth_session_info);
1508 token = talloc_zero(ctx, struct security_token);
1509 string_to_sid(&sid, SID);
1510 token->num_sids = 1;
1512 sess->security_token = token;
1513 GUID_from_string(SESSION, &session_id);
1514 sess->unique_session_token = session_id;
1515 ldb_set_opaque(ldb, "sessionInfo", sess);
1517 msg = talloc_zero(ctx, struct ldb_message);
1518 dn = ldb_dn_new(ctx, ldb, DN);
1520 ldb_msg_add_string(msg, "attribute", "the-value");
1522 req = talloc_zero(ctx, struct ldb_request);
1523 req->operation = LDB_ADD;
1524 req->op.add.message = msg;
1525 reply = talloc_zero(ctx, struct ldb_reply);
1526 reply->error = LDB_SUCCESS;
1528 line = operation_human_readable(ctx, module, req, reply);
1529 assert_non_null(line);
1532 * We ignore the timestamp to make this test a little easier
1535 rs = "\\[Add\\] at \\["
1537 "\\] status \\[Success\\] "
1538 "remote host \\[ipv4:127.0.0.1:0\\] "
1539 "SID \\[S-1-5-21-2470180966-3899876309-2637894779\\] "
1540 "DN \\[dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG\\] "
1541 "attributes \\[attribute \\[the-value\\]\\]";
1543 ret = regcomp(®ex, rs, 0);
1544 assert_int_equal(0, ret);
1546 ret = regexec(®ex, line, 0, NULL, 0);
1547 assert_int_equal(0, ret);
1554 * unit test of operation_json, that ensures that all the expected
1555 * attributes and objects are in the json object.
1556 * In this case the operation is being performed in a system session.
1558 static void test_as_system_operation_hr(void **state)
1560 struct ldb_context *ldb = NULL;
1561 struct ldb_module *module = NULL;
1562 struct ldb_request *req = NULL;
1563 struct ldb_reply *reply = NULL;
1564 struct audit_context *ac = NULL;
1566 struct tsocket_address *ts = NULL;
1568 struct auth_session_info *sess = NULL;
1569 struct auth_session_info *sys_sess = NULL;
1570 struct security_token *token = NULL;
1571 struct security_token *sys_token = NULL;
1573 const char *const SID = "S-1-5-21-2470180966-3899876309-2637894779";
1574 const char * const SESSION = "7130cb06-2062-6a1b-409e-3514c26b1773";
1575 const char * const SYS_SESSION = "7130cb06-2062-6a1b-409e-3514c26b1999";
1576 struct GUID session_id;
1577 struct GUID sys_session_id;
1579 struct GUID transaction_id;
1580 const char *const TRANSACTION = "7130cb06-2062-6a1b-409e-3514c26b1773";
1582 struct ldb_dn *dn = NULL;
1583 const char *const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";
1585 struct ldb_message *msg = NULL;
1588 const char *rs = NULL;
1594 TALLOC_CTX *ctx = talloc_new(NULL);
1596 ldb = talloc_zero(ctx, struct ldb_context);
1598 ac = talloc_zero(ctx, struct audit_context);
1599 GUID_from_string(TRANSACTION, &transaction_id);
1600 ac->transaction_guid = transaction_id;
1602 module = talloc_zero(ctx, struct ldb_module);
1604 ldb_module_set_private(module, ac);
1606 tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 0, &ts);
1607 ldb_set_opaque(ldb, "remoteAddress", ts);
1609 sess = talloc_zero(ctx, struct auth_session_info);
1610 token = talloc_zero(ctx, struct security_token);
1611 string_to_sid(&sid, SID);
1612 token->num_sids = 1;
1614 sess->security_token = token;
1615 GUID_from_string(SESSION, &session_id);
1616 sess->unique_session_token = session_id;
1617 ldb_set_opaque(ldb, "networkSessionInfo", sess);
1619 sys_sess = talloc_zero(ctx, struct auth_session_info);
1620 sys_token = talloc_zero(ctx, struct security_token);
1621 sys_token->num_sids = 1;
1622 sys_token->sids = discard_const(&global_sid_System);
1623 sys_sess->security_token = sys_token;
1624 GUID_from_string(SYS_SESSION, &sys_session_id);
1625 sess->unique_session_token = sys_session_id;
1626 ldb_set_opaque(ldb, "sessionInfo", sys_sess);
1628 msg = talloc_zero(ctx, struct ldb_message);
1629 dn = ldb_dn_new(ctx, ldb, DN);
1631 ldb_msg_add_string(msg, "attribute", "the-value");
1633 req = talloc_zero(ctx, struct ldb_request);
1634 req->operation = LDB_ADD;
1635 req->op.add.message = msg;
1636 reply = talloc_zero(ctx, struct ldb_reply);
1637 reply->error = LDB_SUCCESS;
1639 line = operation_human_readable(ctx, module, req, reply);
1640 assert_non_null(line);
1643 * We ignore the timestamp to make this test a little easier
1646 rs = "\\[Add\\] at \\["
1648 "\\] status \\[Success\\] "
1649 "remote host \\[ipv4:127.0.0.1:0\\] "
1650 "SID \\[S-1-5-21-2470180966-3899876309-2637894779\\] "
1651 "DN \\[dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG\\] "
1652 "attributes \\[attribute \\[the-value\\]\\]";
1654 ret = regcomp(®ex, rs, 0);
1655 assert_int_equal(0, ret);
1657 ret = regexec(®ex, line, 0, NULL, 0);
1658 assert_int_equal(0, ret);
1665 * minimal unit test of password_change_json, that ensures that all the expected
1666 * attributes and objects are in the json object.
1668 static void test_password_change_hr_empty(void **state)
1670 struct ldb_context *ldb = NULL;
1671 struct ldb_module *module = NULL;
1672 struct ldb_request *req = NULL;
1673 struct ldb_reply *reply = NULL;
1674 struct audit_context *ac = NULL;
1677 const char *rs = NULL;
1681 TALLOC_CTX *ctx = talloc_new(NULL);
1683 ldb = talloc_zero(ctx, struct ldb_context);
1684 ac = talloc_zero(ctx, struct audit_context);
1686 module = talloc_zero(ctx, struct ldb_module);
1688 ldb_module_set_private(module, ac);
1690 req = talloc_zero(ctx, struct ldb_request);
1691 reply = talloc_zero(ctx, struct ldb_reply);
1692 reply->error = LDB_SUCCESS;
1694 line = password_change_human_readable(ctx, module, req, reply);
1695 assert_non_null(line);
1698 * We ignore the timestamp to make this test a little easier
1701 rs = "\\[Reset] at \\["
1703 "\\] status \\[Success\\] remote host \\[Unknown\\]"
1704 " SID \\[(NULL SID)\\] DN \\[(null)\\]";
1706 ret = regcomp(®ex, rs, 0);
1707 assert_int_equal(0, ret);
1709 ret = regexec(®ex, line, 0, NULL, 0);
1710 assert_int_equal(0, ret);
1717 * minimal unit test of password_change_json, that ensures that all the expected
1718 * attributes and objects are in the json object.
1720 static void test_password_change_hr(void **state)
1722 struct ldb_context *ldb = NULL;
1723 struct ldb_module *module = NULL;
1724 struct ldb_request *req = NULL;
1725 struct ldb_reply *reply = NULL;
1726 struct audit_context *ac = NULL;
1728 struct tsocket_address *ts = NULL;
1730 struct auth_session_info *sess = NULL;
1731 struct security_token *token = NULL;
1733 const char *const SID = "S-1-5-21-2470180966-3899876309-2637894779";
1734 const char * const SESSION = "7130cb06-2062-6a1b-409e-3514c26b1773";
1735 struct GUID session_id;
1737 struct GUID transaction_id;
1738 const char *const TRANSACTION = "7130cb06-2062-6a1b-409e-3514c26b1773";
1740 struct ldb_dn *dn = NULL;
1741 const char *const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";
1743 struct ldb_message *msg = NULL;
1746 const char *rs = NULL;
1750 TALLOC_CTX *ctx = talloc_new(NULL);
1752 ldb = talloc_zero(ctx, struct ldb_context);
1754 ac = talloc_zero(ctx, struct audit_context);
1755 GUID_from_string(TRANSACTION, &transaction_id);
1756 ac->transaction_guid = transaction_id;
1758 module = talloc_zero(ctx, struct ldb_module);
1760 ldb_module_set_private(module, ac);
1762 tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 0, &ts);
1763 ldb_set_opaque(ldb, "remoteAddress", ts);
1765 sess = talloc_zero(ctx, struct auth_session_info);
1766 token = talloc_zero(ctx, struct security_token);
1767 string_to_sid(&sid, SID);
1768 token->num_sids = 1;
1770 sess->security_token = token;
1771 GUID_from_string(SESSION, &session_id);
1772 sess->unique_session_token = session_id;
1773 ldb_set_opaque(ldb, "sessionInfo", sess);
1775 msg = talloc_zero(ctx, struct ldb_message);
1776 dn = ldb_dn_new(ctx, ldb, DN);
1778 ldb_msg_add_string(msg, "planTextPassword", "super-secret");
1780 req = talloc_zero(ctx, struct ldb_request);
1781 req->operation = LDB_ADD;
1782 req->op.add.message = msg;
1783 reply = talloc_zero(ctx, struct ldb_reply);
1784 reply->error = LDB_SUCCESS;
1786 line = password_change_human_readable(ctx, module, req, reply);
1787 assert_non_null(line);
1790 * We ignore the timestamp to make this test a little easier
1793 rs = "\\[Reset\\] at \\["
1795 "\\] status \\[Success\\] "
1796 "remote host \\[ipv4:127.0.0.1:0\\] "
1797 "SID \\[S-1-5-21-2470180966-3899876309-2637894779\\] "
1798 "DN \\[dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG\\]";
1800 ret = regcomp(®ex, rs, 0);
1801 assert_int_equal(0, ret);
1803 ret = regexec(®ex, line, 0, NULL, 0);
1804 assert_int_equal(0, ret);
1812 * minimal unit test of transaction_json, that ensures that all the expected
1813 * attributes and objects are in the json object.
1815 static void test_transaction_hr(void **state)
1819 const char * const GUID = "7130cb06-2062-6a1b-409e-3514c26b1773";
1822 const char *rs = NULL;
1826 TALLOC_CTX *ctx = talloc_new(NULL);
1828 GUID_from_string(GUID, &guid);
1830 line = transaction_human_readable(ctx, "delete");
1831 assert_non_null(line);
1834 * We ignore the timestamp to make this test a little easier
1837 rs = "\\[delete] at \\[[^[]*\\]";
1839 ret = regcomp(®ex, rs, 0);
1840 assert_int_equal(0, ret);
1842 ret = regexec(®ex, line, 0, NULL, 0);
1843 assert_int_equal(0, ret);
1851 * minimal unit test of commit_failure_hr, that ensures
1852 * that all the expected conten is in the log entry.
1854 static void test_commit_failure_hr(void **state)
1858 const char * const GUID = "7130cb06-2062-6a1b-409e-3514c26b1773";
1861 const char *rs = NULL;
1865 TALLOC_CTX *ctx = talloc_new(NULL);
1867 GUID_from_string(GUID, &guid);
1869 line = commit_failure_human_readable(
1872 LDB_ERR_OPERATIONS_ERROR,
1875 assert_non_null(line);
1878 * We ignore the timestamp to make this test a little easier
1881 rs = "\\[commit\\] at \\[[^[]*\\] status \\[1\\] reason \\[because\\]";
1883 ret = regcomp(®ex, rs, 0);
1884 assert_int_equal(0, ret);
1886 ret = regexec(®ex, line, 0, NULL, 0);
1887 assert_int_equal(0, ret);
1893 static void test_add_transaction_id(void **state)
1895 struct ldb_module *module = NULL;
1896 struct ldb_request *req = NULL;
1897 struct audit_context *ac = NULL;
1899 const char * const GUID = "7130cb06-2062-6a1b-409e-3514c26b1773";
1900 struct ldb_control * control = NULL;
1903 TALLOC_CTX *ctx = talloc_new(NULL);
1905 ac = talloc_zero(ctx, struct audit_context);
1906 GUID_from_string(GUID, &guid);
1907 ac->transaction_guid = guid;
1909 module = talloc_zero(ctx, struct ldb_module);
1910 ldb_module_set_private(module, ac);
1912 req = talloc_zero(ctx, struct ldb_request);
1914 status = add_transaction_id(module, req);
1915 assert_int_equal(LDB_SUCCESS, status);
1917 control = ldb_request_get_control(
1919 DSDB_CONTROL_TRANSACTION_IDENTIFIER_OID);
1920 assert_non_null(control);
1921 assert_memory_equal(
1922 &ac->transaction_guid,
1924 sizeof(struct GUID));
1929 static void test_log_attributes(void **state)
1931 struct ldb_message *msg = NULL;
1935 char lv[MAX_LENGTH+2];
1936 char ex[MAX_LENGTH+80];
1938 TALLOC_CTX *ctx = talloc_new(NULL);
1942 * Test an empty message
1943 * Should get empty attributes representation.
1945 buf = talloc_zero(ctx, char);
1946 msg = talloc_zero(ctx, struct ldb_message);
1948 str = log_attributes(ctx, buf, LDB_ADD, msg);
1949 assert_string_equal("", str);
1955 * Test a message with a single secret attribute
1957 buf = talloc_zero(ctx, char);
1958 msg = talloc_zero(ctx, struct ldb_message);
1959 ldb_msg_add_string(msg, "clearTextPassword", "secret");
1961 str = log_attributes(ctx, buf, LDB_ADD, msg);
1962 assert_string_equal(
1963 "clearTextPassword [REDACTED SECRET ATTRIBUTE]",
1967 * Test as a modify message, should add an action
1968 * action will be unknown as there are no ACL's set
1970 buf = talloc_zero(ctx, char);
1971 str = log_attributes(ctx, buf, LDB_MODIFY, msg);
1972 assert_string_equal(
1973 "unknown: clearTextPassword [REDACTED SECRET ATTRIBUTE]",
1980 * Test a message with a single attribute, single valued attribute
1982 buf = talloc_zero(ctx, char);
1983 msg = talloc_zero(ctx, struct ldb_message);
1984 ldb_msg_add_string(msg, "attribute", "value");
1986 str = log_attributes(ctx, buf, LDB_ADD, msg);
1987 assert_string_equal(
1988 "attribute [value]",
1995 * Test a message with a single attribute, single valued attribute
1998 buf = talloc_zero(ctx, char);
1999 msg = talloc_zero(ctx, struct ldb_message);
2000 ldb_msg_add_string(msg, "attribute", "value");
2002 str = log_attributes(ctx, buf, LDB_MODIFY, msg);
2003 assert_string_equal(
2004 "unknown: attribute [value]",
2011 * Test a message with multiple attributes and a multi-valued attribute
2014 buf = talloc_zero(ctx, char);
2015 msg = talloc_zero(ctx, struct ldb_message);
2016 ldb_msg_add_string(msg, "attribute01", "value01");
2017 ldb_msg_add_string(msg, "attribute02", "value02");
2018 ldb_msg_add_string(msg, "attribute02", "value03");
2020 str = log_attributes(ctx, buf, LDB_MODIFY, msg);
2021 assert_string_equal(
2022 "unknown: attribute01 [value01] "
2023 "unknown: attribute02 [value02] [value03]",
2030 * Test a message with a single attribute, single valued attribute
2031 * with a non printable character. Should be base64 encoded
2033 buf = talloc_zero(ctx, char);
2034 msg = talloc_zero(ctx, struct ldb_message);
2035 ldb_msg_add_string(msg, "attribute", "value\n");
2037 str = log_attributes(ctx, buf, LDB_ADD, msg);
2038 assert_string_equal("attribute {dmFsdWUK}", str);
2044 * Test a message with a single valued attribute
2045 * with more than MAX_LENGTH characters, should be truncated with
2048 buf = talloc_zero(ctx, char);
2049 msg = talloc_zero(ctx, struct ldb_message);
2050 memset(lv, '\0', sizeof(lv));
2051 memset(lv, 'x', MAX_LENGTH+1);
2052 ldb_msg_add_string(msg, "attribute", lv);
2054 str = log_attributes(ctx, buf, LDB_ADD, msg);
2055 snprintf(ex, sizeof(ex), "attribute [%.*s...]", MAX_LENGTH, lv);
2056 assert_string_equal(ex, str);
2065 * minimal unit test of replicated_update_human_readable
2067 static void test_replicated_update_hr_empty(void **state)
2069 struct ldb_context *ldb = NULL;
2070 struct ldb_module *module = NULL;
2071 struct ldb_request *req = NULL;
2072 struct ldb_reply *reply = NULL;
2073 struct audit_context *ac = NULL;
2074 struct dsdb_extended_replicated_objects *ro = NULL;
2075 struct repsFromTo1 *source_dsa = NULL;
2077 const char* line = NULL;
2078 const char *rs = NULL;
2082 TALLOC_CTX *ctx = talloc_new(NULL);
2084 ldb = talloc_zero(ctx, struct ldb_context);
2085 ac = talloc_zero(ctx, struct audit_context);
2087 module = talloc_zero(ctx, struct ldb_module);
2089 ldb_module_set_private(module, ac);
2091 source_dsa = talloc_zero(ctx, struct repsFromTo1);
2092 ro = talloc_zero(ctx, struct dsdb_extended_replicated_objects);
2093 ro->source_dsa = source_dsa;
2094 req = talloc_zero(ctx, struct ldb_request);
2095 req->op.extended.data = ro;
2096 req->operation = LDB_EXTENDED;
2097 reply = talloc_zero(ctx, struct ldb_reply);
2098 reply->error = LDB_SUCCESS;
2100 line = replicated_update_human_readable(ctx, module, req, reply);
2101 assert_non_null(line);
2103 * We ignore the timestamp to make this test a little easier
2106 rs = "at \\[[^[]*\\] "
2107 "status \\[Success\\] "
2108 "error \\[The operation completed successfully.\\] "
2109 "partition \\[(null)\\] objects \\[0\\] links \\[0\\] "
2110 "object \\[00000000-0000-0000-0000-000000000000\\] "
2111 "invocation \\[00000000-0000-0000-0000-000000000000\\]";
2113 ret = regcomp(®ex, rs, 0);
2114 assert_int_equal(0, ret);
2116 ret = regexec(®ex, line, 0, NULL, 0);
2117 assert_int_equal(0, ret);
2125 * unit test of replicated_update_human_readable
2127 static void test_replicated_update_hr(void **state)
2129 struct ldb_context *ldb = NULL;
2130 struct ldb_module *module = NULL;
2131 struct ldb_request *req = NULL;
2132 struct ldb_reply *reply = NULL;
2133 struct audit_context *ac = NULL;
2134 struct dsdb_extended_replicated_objects *ro = NULL;
2135 struct repsFromTo1 *source_dsa = NULL;
2137 struct GUID transaction_id;
2138 const char *const TRANSACTION = "7130cb06-2062-6a1b-409e-3514c26b1773";
2140 struct ldb_dn *dn = NULL;
2141 const char *const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";
2143 struct GUID source_dsa_obj_guid;
2144 const char *const SOURCE_DSA = "7130cb06-2062-6a1b-409e-3514c26b1793";
2146 struct GUID invocation_id;
2147 const char *const INVOCATION_ID =
2148 "7130cb06-2062-6a1b-409e-3514c26b1893";
2150 const char* line = NULL;
2151 const char *rs = NULL;
2156 TALLOC_CTX *ctx = talloc_new(NULL);
2158 ldb = talloc_zero(ctx, struct ldb_context);
2160 ac = talloc_zero(ctx, struct audit_context);
2161 GUID_from_string(TRANSACTION, &transaction_id);
2162 ac->transaction_guid = transaction_id;
2164 module = talloc_zero(ctx, struct ldb_module);
2166 ldb_module_set_private(module, ac);
2168 dn = ldb_dn_new(ctx, ldb, DN);
2169 GUID_from_string(SOURCE_DSA, &source_dsa_obj_guid);
2170 GUID_from_string(INVOCATION_ID, &invocation_id);
2171 source_dsa = talloc_zero(ctx, struct repsFromTo1);
2172 source_dsa->source_dsa_obj_guid = source_dsa_obj_guid;
2173 source_dsa->source_dsa_invocation_id = invocation_id;
2175 ro = talloc_zero(ctx, struct dsdb_extended_replicated_objects);
2176 ro->source_dsa = source_dsa;
2177 ro->num_objects = 808;
2178 ro->linked_attributes_count = 2910;
2179 ro->partition_dn = dn;
2180 ro->error = WERR_NOT_SUPPORTED;
2183 req = talloc_zero(ctx, struct ldb_request);
2184 req->op.extended.data = ro;
2185 req->operation = LDB_EXTENDED;
2187 reply = talloc_zero(ctx, struct ldb_reply);
2188 reply->error = LDB_ERR_NO_SUCH_OBJECT;
2190 line = replicated_update_human_readable(ctx, module, req, reply);
2191 assert_non_null(line);
2194 * We ignore the timestamp to make this test a little easier
2197 rs = "at \\[[^[]*\\] "
2198 "status \\[No such object\\] "
2199 "error \\[The request is not supported.\\] "
2200 "partition \\[dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG\\] "
2201 "objects \\[808\\] links \\[2910\\] "
2202 "object \\[7130cb06-2062-6a1b-409e-3514c26b1793\\] "
2203 "invocation \\[7130cb06-2062-6a1b-409e-3514c26b1893\\]";
2205 ret = regcomp(®ex, rs, 0);
2206 assert_int_equal(0, ret);
2208 ret = regexec(®ex, line, 0, NULL, 0);
2209 assert_int_equal(0, ret);
2216 const struct CMUnitTest tests[] = {
2217 cmocka_unit_test(test_has_password_changed),
2218 cmocka_unit_test(test_get_password_action),
2219 cmocka_unit_test(test_operation_json_empty),
2220 cmocka_unit_test(test_operation_json),
2221 cmocka_unit_test(test_as_system_operation_json),
2222 cmocka_unit_test(test_password_change_json_empty),
2223 cmocka_unit_test(test_password_change_json),
2224 cmocka_unit_test(test_transaction_json),
2225 cmocka_unit_test(test_commit_failure_json),
2226 cmocka_unit_test(test_replicated_update_json_empty),
2227 cmocka_unit_test(test_replicated_update_json),
2228 cmocka_unit_test(test_add_transaction_id),
2229 cmocka_unit_test(test_operation_hr_empty),
2230 cmocka_unit_test(test_operation_hr),
2231 cmocka_unit_test(test_as_system_operation_hr),
2232 cmocka_unit_test(test_password_change_hr_empty),
2233 cmocka_unit_test(test_password_change_hr),
2234 cmocka_unit_test(test_transaction_hr),
2235 cmocka_unit_test(test_commit_failure_hr),
2236 cmocka_unit_test(test_log_attributes),
2237 cmocka_unit_test(test_replicated_update_hr_empty),
2238 cmocka_unit_test(test_replicated_update_hr),
2241 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
2242 return cmocka_run_group_tests(tests, NULL, NULL);