LCOV - code coverage report
Current view: top level - source4/dsdb/samdb/ldb_modules/tests - test_audit_util.c (source / functions) Hit Total Coverage
Test: coverage report for fix-15632 9995c5c2 Lines: 674 674 100.0 %
Date: 2024-04-13 12:30:31 Functions: 18 18 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unit tests for the dsdb audit logging utility code code in audit_util.c
       3             : 
       4             :    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
       5             : 
       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.
      10             : 
      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.
      15             : 
      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/>.
      18             : */
      19             : 
      20             : #include <stdarg.h>
      21             : #include <stddef.h>
      22             : #include <setjmp.h>
      23             : #include <unistd.h>
      24             : #include <cmocka.h>
      25             : 
      26             : #include "../audit_util.c"
      27             : 
      28             : #include "lib/ldb/include/ldb_private.h"
      29             : 
      30           1 : static void test_dsdb_audit_add_ldb_value(void **state)
      31             : {
      32           1 :         struct json_object object;
      33           1 :         struct json_object array;
      34           1 :         struct ldb_val val = data_blob_null;
      35           1 :         struct json_t *el  = NULL;
      36           1 :         struct json_t *atr = NULL;
      37           1 :         char* base64 = NULL;
      38             : 
      39           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
      40             :         /*
      41             :          * Test a non array object
      42             :          */
      43           1 :         object = json_new_object();
      44           1 :         assert_false(json_is_invalid(&object));
      45           1 :         dsdb_audit_add_ldb_value(&object, val);
      46           1 :         assert_true(json_is_invalid(&object));
      47           1 :         json_free(&object);
      48             : 
      49           1 :         array = json_new_array();
      50           1 :         assert_false(json_is_invalid(&array));
      51             :         /*
      52             :          * Test a data_blob_null, should encode as a JSON null value.
      53             :          */
      54           1 :         val = data_blob_null;
      55           1 :         dsdb_audit_add_ldb_value(&array, val);
      56           1 :         el = json_array_get(array.root, 0);
      57           1 :         assert_true(json_is_null(el));
      58             : 
      59             :         /*
      60             :          * Test a +ve length but a null data ptr, should encode as a null.
      61             :          */
      62           1 :         val = data_blob_null;
      63           1 :         val.length = 1;
      64           1 :         dsdb_audit_add_ldb_value(&array, val);
      65           1 :         el = json_array_get(array.root, 1);
      66           1 :         assert_true(json_is_null(el));
      67             : 
      68             :         /*
      69             :          * Test a zero length but a non null data ptr, should encode as a null.
      70             :          */
      71           1 :         val = data_blob_null;
      72           1 :         val.data = discard_const("Data on the stack");
      73           1 :         dsdb_audit_add_ldb_value(&array, val);
      74           1 :         el = json_array_get(array.root, 2);
      75           1 :         assert_true(json_is_null(el));
      76             : 
      77             :         /*
      78             :          * Test a printable value.
      79             :          * value should not be encoded
      80             :          * truncated and base64 should be missing
      81             :          */
      82           1 :         val = data_blob_string_const("A value of interest");
      83           1 :         dsdb_audit_add_ldb_value(&array, val);
      84           1 :         el = json_array_get(array.root, 3);
      85           1 :         assert_true(json_is_object(el));
      86           1 :         atr = json_object_get(el, "value");
      87           1 :         assert_true(json_is_string(atr));
      88           1 :         assert_string_equal("A value of interest", json_string_value(atr));
      89           1 :         assert_null(json_object_get(el, "truncated"));
      90           1 :         assert_null(json_object_get(el, "base64"));
      91             : 
      92             :         /*
      93             :          * Test non printable value, should be base64 encoded.
      94             :          * truncated should be missing and base64 should be set.
      95             :          */
      96           1 :         val = data_blob_string_const("A value of interest\n");
      97           1 :         dsdb_audit_add_ldb_value(&array, val);
      98           1 :         el = json_array_get(array.root, 4);
      99           1 :         assert_true(json_is_object(el));
     100           1 :         atr = json_object_get(el, "value");
     101           1 :         assert_true(json_is_string(atr));
     102           1 :         assert_string_equal(
     103             :                 "QSB2YWx1ZSBvZiBpbnRlcmVzdAo=",
     104             :                 json_string_value(atr));
     105           1 :         atr = json_object_get(el, "base64");
     106           1 :         assert_true(json_is_boolean(atr));
     107           1 :         assert_true(json_boolean(atr));
     108           1 :         assert_null(json_object_get(el, "truncated"));
     109             : 
     110             :         /*
     111             :          * test a printable value exactly max bytes long
     112             :          * should not be truncated or encoded.
     113             :          */
     114           1 :         val = data_blob_null;
     115           1 :         val.length = MAX_LENGTH;
     116           1 :         val.data = (unsigned char *)generate_random_str_list(
     117             :                 ctx,
     118             :                 MAX_LENGTH,
     119             :                 "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
     120             :                 "1234567890!@#$%^&*()");
     121             : 
     122           1 :         dsdb_audit_add_ldb_value(&array, val);
     123             : 
     124           1 :         el = json_array_get(array.root, 5);
     125           1 :         assert_true(json_is_object(el));
     126           1 :         atr = json_object_get(el, "value");
     127           1 :         assert_true(json_is_string(atr));
     128           1 :         assert_int_equal(MAX_LENGTH, strlen(json_string_value(atr)));
     129           1 :         assert_memory_equal(val.data, json_string_value(atr), MAX_LENGTH);
     130             : 
     131           1 :         assert_null(json_object_get(el, "base64"));
     132           1 :         assert_null(json_object_get(el, "truncated"));
     133             : 
     134             : 
     135             :         /*
     136             :          * test a printable value exactly max + 1 bytes long
     137             :          * should be truncated and not encoded.
     138             :          */
     139           1 :         val = data_blob_null;
     140           1 :         val.length = MAX_LENGTH + 1;
     141           1 :         val.data = (unsigned char *)generate_random_str_list(
     142             :                 ctx,
     143             :                 MAX_LENGTH + 1,
     144             :                 "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
     145             :                 "1234567890!@#$%^&*()");
     146             : 
     147           1 :         dsdb_audit_add_ldb_value(&array, val);
     148             : 
     149           1 :         el = json_array_get(array.root, 6);
     150           1 :         assert_true(json_is_object(el));
     151           1 :         atr = json_object_get(el, "value");
     152           1 :         assert_true(json_is_string(atr));
     153           1 :         assert_int_equal(MAX_LENGTH, strlen(json_string_value(atr)));
     154           1 :         assert_memory_equal(val.data, json_string_value(atr), MAX_LENGTH);
     155             : 
     156           1 :         atr = json_object_get(el, "truncated");
     157           1 :         assert_true(json_is_boolean(atr));
     158           1 :         assert_true(json_boolean(atr));
     159             : 
     160           1 :         assert_null(json_object_get(el, "base64"));
     161             : 
     162           1 :         TALLOC_FREE(val.data);
     163             : 
     164             :         /*
     165             :          * test a non-printable value exactly max bytes long
     166             :          * should not be truncated but should be encoded.
     167             :          */
     168           1 :         val = data_blob_null;
     169           1 :         val.length = MAX_LENGTH;
     170           1 :         val.data = (unsigned char *)generate_random_str_list(
     171             :                 ctx,
     172             :                 MAX_LENGTH,
     173             :                 "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
     174             :                 "1234567890!@#$%^&*()");
     175             : 
     176           1 :         val.data[0] = 0x03;
     177           1 :         dsdb_audit_add_ldb_value(&array, val);
     178           1 :         base64 = ldb_base64_encode(ctx, (char*) val.data, MAX_LENGTH);
     179             : 
     180           1 :         el = json_array_get(array.root, 7);
     181           1 :         assert_true(json_is_object(el));
     182           1 :         atr = json_object_get(el, "value");
     183           1 :         assert_true(json_is_string(atr));
     184           1 :         assert_int_equal(strlen(base64), strlen(json_string_value(atr)));
     185           1 :         assert_string_equal(base64, json_string_value(atr));
     186             : 
     187           1 :         atr = json_object_get(el, "base64");
     188           1 :         assert_true(json_is_boolean(atr));
     189           1 :         assert_true(json_boolean(atr));
     190             : 
     191           1 :         assert_null(json_object_get(el, "truncated"));
     192           1 :         TALLOC_FREE(base64);
     193           1 :         TALLOC_FREE(val.data);
     194             : 
     195             :         /*
     196             :          * test a non-printable value exactly max + 1 bytes long
     197             :          * should be truncated and encoded.
     198             :          */
     199           1 :         val = data_blob_null;
     200           1 :         val.length = MAX_LENGTH + 1;
     201           1 :         val.data = (unsigned char *)generate_random_str_list(
     202             :                 ctx,
     203             :                 MAX_LENGTH + 1,
     204             :                 "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
     205             :                 "1234567890!@#$%^&*()");
     206             : 
     207           1 :         val.data[0] = 0x03;
     208           1 :         dsdb_audit_add_ldb_value(&array, val);
     209             :         /*
     210             :          * The data is truncated before it is base 64 encoded
     211             :          */
     212           1 :         base64 = ldb_base64_encode(ctx, (char*) val.data, MAX_LENGTH);
     213             : 
     214           1 :         el = json_array_get(array.root, 8);
     215           1 :         assert_true(json_is_object(el));
     216           1 :         atr = json_object_get(el, "value");
     217           1 :         assert_true(json_is_string(atr));
     218           1 :         assert_int_equal(strlen(base64), strlen(json_string_value(atr)));
     219           1 :         assert_string_equal(base64, json_string_value(atr));
     220             : 
     221           1 :         atr = json_object_get(el, "base64");
     222           1 :         assert_true(json_is_boolean(atr));
     223           1 :         assert_true(json_boolean(atr));
     224             : 
     225           1 :         atr = json_object_get(el, "truncated");
     226           1 :         assert_true(json_is_boolean(atr));
     227           1 :         assert_true(json_boolean(atr));
     228             : 
     229           1 :         TALLOC_FREE(base64);
     230           1 :         TALLOC_FREE(val.data);
     231             : 
     232           1 :         json_free(&array);
     233           1 :         TALLOC_FREE(ctx);
     234           1 : }
     235             : 
     236           1 : static void test_dsdb_audit_attributes_json(void **state)
     237             : {
     238           1 :         struct ldb_message *msg = NULL;
     239             : 
     240           1 :         struct json_object o;
     241           1 :         json_t *a = NULL;
     242           1 :         json_t *v = NULL;
     243           1 :         json_t *x = NULL;
     244           1 :         json_t *y = NULL;
     245             : 
     246           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     247             : 
     248             : 
     249             :         /*
     250             :          * Test an empty message
     251             :          * Should get an empty attributes object
     252             :          */
     253           1 :         msg = talloc_zero(ctx, struct ldb_message);
     254             : 
     255           1 :         o = dsdb_audit_attributes_json(LDB_ADD, msg);
     256           1 :         assert_true(json_is_object(o.root));
     257           1 :         assert_int_equal(0, json_object_size(o.root));
     258           1 :         json_free(&o);
     259             : 
     260           1 :         o = dsdb_audit_attributes_json(LDB_MODIFY, msg);
     261           1 :         assert_true(json_is_object(o.root));
     262           1 :         assert_int_equal(0, json_object_size(o.root));
     263           1 :         json_free(&o);
     264             : 
     265             :         /*
     266             :          * Test a message with a single secret attribute
     267             :          * should only have that object and it should have no value
     268             :          * attribute and redacted should be set.
     269             :          */
     270           1 :         msg = talloc_zero(ctx, struct ldb_message);
     271           1 :         ldb_msg_add_string(msg, "clearTextPassword", "secret");
     272             : 
     273           1 :         o = dsdb_audit_attributes_json(LDB_ADD, msg);
     274           1 :         assert_true(json_is_object(o.root));
     275           1 :         assert_int_equal(1, json_object_size(o.root));
     276             : 
     277           1 :         a = json_object_get(o.root, "clearTextPassword");
     278           1 :         assert_int_equal(1, json_object_size(a));
     279             : 
     280           1 :         v = json_object_get(a, "actions");
     281           1 :         assert_true(json_is_array(v));
     282           1 :         assert_int_equal(1, json_array_size(v));
     283             : 
     284           1 :         a = json_array_get(v, 0);
     285           1 :         v = json_object_get(a, "redacted");
     286           1 :         assert_true(json_is_boolean(v));
     287           1 :         assert_true(json_boolean(v));
     288             : 
     289           1 :         json_free(&o);
     290             : 
     291             :         /*
     292             :          * Test as a modify message, should add an action attribute
     293             :          */
     294           1 :         o = dsdb_audit_attributes_json(LDB_MODIFY, msg);
     295           1 :         assert_true(json_is_object(o.root));
     296           1 :         assert_int_equal(1, json_object_size(o.root));
     297             : 
     298           1 :         a = json_object_get(o.root, "clearTextPassword");
     299           1 :         assert_true(json_is_object(a));
     300           1 :         assert_int_equal(1, json_object_size(a));
     301             : 
     302           1 :         v = json_object_get(a, "actions");
     303           1 :         assert_true(json_is_array(v));
     304           1 :         assert_int_equal(1, json_array_size(v));
     305             : 
     306           1 :         a = json_array_get(v, 0);
     307           1 :         v = json_object_get(a, "redacted");
     308           1 :         assert_true(json_is_boolean(v));
     309           1 :         assert_true(json_boolean(v));
     310             : 
     311           1 :         v = json_object_get(a, "action");
     312           1 :         assert_true(json_is_string(v));
     313           1 :         assert_string_equal("unknown", json_string_value(v));
     314             : 
     315           1 :         json_free(&o);
     316           1 :         TALLOC_FREE(msg);
     317             : 
     318             :         /*
     319             :          * Test a message with a single attribute, single valued attribute
     320             :          */
     321           1 :         msg = talloc_zero(ctx, struct ldb_message);
     322           1 :         ldb_msg_add_string(msg, "attribute", "value");
     323             : 
     324           1 :         o = dsdb_audit_attributes_json(LDB_ADD, msg);
     325           1 :         assert_true(json_is_object(o.root));
     326           1 :         assert_int_equal(1, json_object_size(o.root));
     327             : 
     328           1 :         a = json_object_get(o.root, "attribute");
     329           1 :         assert_true(json_is_object(a));
     330           1 :         assert_int_equal(1, json_object_size(a));
     331             : 
     332           1 :         v = json_object_get(a, "actions");
     333           1 :         assert_true(json_is_array(v));
     334           1 :         assert_int_equal(1, json_array_size(v));
     335             : 
     336           1 :         x = json_array_get(v, 0);
     337           1 :         assert_int_equal(2, json_object_size(x));
     338           1 :         y = json_object_get(x, "action");
     339           1 :         assert_string_equal("add", json_string_value(y));
     340             : 
     341           1 :         y = json_object_get(x, "values");
     342           1 :         assert_true(json_is_array(y));
     343           1 :         assert_int_equal(1, json_array_size(y));
     344             : 
     345           1 :         x = json_array_get(y, 0);
     346           1 :         assert_true(json_is_object(x));
     347           1 :         assert_int_equal(1, json_object_size(x));
     348           1 :         y = json_object_get(x, "value");
     349           1 :         assert_string_equal("value", json_string_value(y));
     350             : 
     351           1 :         json_free(&o);
     352           1 :         TALLOC_FREE(msg);
     353             : 
     354             :         /*
     355             :          * Test a message with a single attribute, single valued attribute
     356             :          * And as a modify
     357             :          */
     358           1 :         msg = talloc_zero(ctx, struct ldb_message);
     359           1 :         ldb_msg_add_string(msg, "attribute", "value");
     360             : 
     361           1 :         o = dsdb_audit_attributes_json(LDB_MODIFY, msg);
     362           1 :         assert_true(json_is_object(o.root));
     363           1 :         assert_int_equal(1, json_object_size(o.root));
     364             : 
     365           1 :         a = json_object_get(o.root, "attribute");
     366           1 :         assert_true(json_is_object(a));
     367           1 :         assert_int_equal(1, json_object_size(a));
     368             : 
     369           1 :         v = json_object_get(a, "actions");
     370           1 :         assert_true(json_is_array(v));
     371           1 :         assert_int_equal(1, json_array_size(v));
     372             : 
     373           1 :         x = json_array_get(v, 0);
     374           1 :         assert_int_equal(2, json_object_size(x));
     375           1 :         y = json_object_get(x, "action");
     376           1 :         assert_string_equal("unknown", json_string_value(y));
     377             : 
     378           1 :         y = json_object_get(x, "values");
     379           1 :         assert_true(json_is_array(y));
     380           1 :         assert_int_equal(1, json_array_size(y));
     381             : 
     382           1 :         x = json_array_get(y, 0);
     383           1 :         assert_true(json_is_object(x));
     384           1 :         assert_int_equal(1, json_object_size(x));
     385           1 :         y = json_object_get(x, "value");
     386           1 :         assert_string_equal("value", json_string_value(y));
     387             : 
     388           1 :         json_free(&o);
     389           1 :         TALLOC_FREE(msg);
     390             : 
     391             :         /*
     392             :          * Test a message with a multi-valued attribute
     393             :          */
     394           1 :         msg = talloc_zero(ctx, struct ldb_message);
     395           1 :         ldb_msg_add_string(msg, "attribute01", "value01");
     396           1 :         ldb_msg_add_string(msg, "attribute02", "value02");
     397           1 :         ldb_msg_add_string(msg, "attribute02", "value03");
     398             : 
     399           1 :         o = dsdb_audit_attributes_json(LDB_ADD, msg);
     400           1 :         assert_true(json_is_object(o.root));
     401           1 :         assert_int_equal(2, json_object_size(o.root));
     402             : 
     403           1 :         a = json_object_get(o.root, "attribute01");
     404           1 :         assert_true(json_is_object(a));
     405           1 :         assert_int_equal(1, json_object_size(a));
     406             : 
     407           1 :         v = json_object_get(a, "actions");
     408           1 :         assert_true(json_is_array(v));
     409           1 :         assert_int_equal(1, json_array_size(v));
     410             : 
     411           1 :         x = json_array_get(v, 0);
     412           1 :         assert_int_equal(2, json_object_size(x));
     413           1 :         y = json_object_get(x, "action");
     414           1 :         assert_string_equal("add", json_string_value(y));
     415             : 
     416           1 :         y = json_object_get(x, "values");
     417           1 :         assert_true(json_is_array(y));
     418           1 :         assert_int_equal(1, json_array_size(y));
     419             : 
     420           1 :         x = json_array_get(y, 0);
     421           1 :         assert_true(json_is_object(x));
     422           1 :         assert_int_equal(1, json_object_size(x));
     423           1 :         y = json_object_get(x, "value");
     424           1 :         assert_string_equal("value01", json_string_value(y));
     425             : 
     426           1 :         a = json_object_get(o.root, "attribute02");
     427           1 :         assert_true(json_is_object(a));
     428           1 :         assert_int_equal(1, json_object_size(a));
     429             : 
     430           1 :         v = json_object_get(a, "actions");
     431           1 :         assert_true(json_is_array(v));
     432           1 :         assert_int_equal(1, json_array_size(v));
     433             : 
     434           1 :         x = json_array_get(v, 0);
     435           1 :         assert_int_equal(2, json_object_size(x));
     436           1 :         y = json_object_get(x, "action");
     437           1 :         assert_string_equal("add", json_string_value(y));
     438             : 
     439           1 :         y = json_object_get(x, "values");
     440           1 :         assert_true(json_is_array(y));
     441           1 :         assert_int_equal(2, json_array_size(y));
     442             : 
     443           1 :         x = json_array_get(y, 0);
     444           1 :         assert_true(json_is_object(x));
     445           1 :         assert_int_equal(1, json_object_size(x));
     446           1 :         v = json_object_get(x, "value");
     447           1 :         assert_string_equal("value02", json_string_value(v));
     448             : 
     449           1 :         x = json_array_get(y, 1);
     450           1 :         assert_true(json_is_object(x));
     451           1 :         assert_int_equal(1, json_object_size(x));
     452           1 :         v = json_object_get(x, "value");
     453           1 :         assert_string_equal("value03", json_string_value(v));
     454             : 
     455           1 :         json_free(&o);
     456           1 :         TALLOC_FREE(msg);
     457             : 
     458           1 :         TALLOC_FREE(ctx);
     459           1 : }
     460             : 
     461           1 : static void test_dsdb_audit_get_remote_address(void **state)
     462             : {
     463           1 :         struct ldb_context *ldb = NULL;
     464           1 :         const struct tsocket_address *ts = NULL;
     465           1 :         struct tsocket_address *in = NULL;
     466             : 
     467           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     468             : 
     469             :         /*
     470             :          * Test a freshly initialized ldb
     471             :          * should return NULL
     472             :          */
     473           1 :         ldb = ldb_init(ctx, NULL);
     474           1 :         ts = dsdb_audit_get_remote_address(ldb);
     475           1 :         assert_null(ts);
     476             : 
     477             :         /*
     478             :          * opaque set to null, should return NULL
     479             :          */
     480           1 :         ldb_set_opaque(ldb, "remoteAddress", NULL);
     481           1 :         ts = dsdb_audit_get_remote_address(ldb);
     482           1 :         assert_null(ts);
     483             : 
     484             :         /*
     485             :          * Ensure that the value set is returned
     486             :          */
     487           1 :         tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 0, &in);
     488           1 :         ldb_set_opaque(ldb, "remoteAddress", in);
     489           1 :         ts = dsdb_audit_get_remote_address(ldb);
     490           1 :         assert_non_null(ts);
     491           1 :         assert_ptr_equal(in, ts);
     492             : 
     493           1 :         TALLOC_FREE(ldb);
     494           1 :         TALLOC_FREE(ctx);
     495             : 
     496           1 : }
     497             : 
     498           1 : static void test_dsdb_audit_get_ldb_error_string(void **state)
     499             : {
     500           1 :         struct ldb_context *ldb = NULL;
     501           1 :         struct ldb_module *module = NULL;
     502           1 :         const char *s = NULL;
     503           1 :         const char * const text = "Custom reason";
     504             : 
     505           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     506             : 
     507           1 :         ldb = ldb_init(ctx, NULL);
     508           1 :         module = talloc_zero(ctx, struct ldb_module);
     509           1 :         module->ldb = ldb;
     510             : 
     511             :         /*
     512             :          * No ldb error string set should get the default error description for
     513             :          * the status code
     514             :          */
     515           1 :         s = dsdb_audit_get_ldb_error_string(module, LDB_ERR_OPERATIONS_ERROR);
     516           1 :         assert_string_equal("Operations error", s);
     517             : 
     518             :         /*
     519             :          * Set the error string that should now be returned instead of the
     520             :          * default description.
     521             :          */
     522           1 :         ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, text);
     523           1 :         s = dsdb_audit_get_ldb_error_string(module, LDB_ERR_OPERATIONS_ERROR);
     524             :         /*
     525             :          * Only test the start of the string as ldb_error adds location data.
     526             :          */
     527           1 :         assert_int_equal(0, strncmp(text, s, strlen(text)));
     528             : 
     529           1 :         TALLOC_FREE(ctx);
     530           1 : }
     531             : 
     532           1 : static void test_dsdb_audit_get_user_sid(void **state)
     533             : {
     534           1 :         struct ldb_context *ldb        = NULL;
     535           1 :         struct ldb_module *module      = NULL;
     536           1 :         const struct dom_sid *sid      = NULL;
     537           1 :         struct auth_session_info *sess = NULL;
     538           1 :         struct security_token *token   = NULL;
     539           1 :         struct dom_sid sids[2];
     540           1 :         const char * const SID0 = "S-1-5-21-2470180966-3899876309-2637894779";
     541           1 :         const char * const SID1 = "S-1-5-21-4284042908-2889457889-3672286761";
     542           1 :         struct dom_sid_buf sid_buf;
     543             : 
     544             : 
     545           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     546             : 
     547           1 :         ldb = ldb_init(ctx, NULL);
     548           1 :         module = talloc_zero(ctx, struct ldb_module);
     549           1 :         module->ldb = ldb;
     550             : 
     551             :         /*
     552             :          * Freshly initialised structures, will be no session data
     553             :          * so expect NULL
     554             :          */
     555           1 :         sid = dsdb_audit_get_user_sid(module);
     556           1 :         assert_null(sid);
     557             : 
     558             :         /*
     559             :          * Now add a NULL session info
     560             :          */
     561           1 :         ldb_set_opaque(ldb, DSDB_SESSION_INFO, sess);
     562           1 :         sid = dsdb_audit_get_user_sid(module);
     563           1 :         assert_null(sid);
     564             : 
     565             :         /*
     566             :          * Now add a session info with no user sid
     567             :          */
     568           1 :         sess = talloc_zero(ctx, struct auth_session_info);
     569           1 :         ldb_set_opaque(ldb, DSDB_SESSION_INFO, sess);
     570           1 :         sid = dsdb_audit_get_user_sid(module);
     571           1 :         assert_null(sid);
     572             : 
     573             :         /*
     574             :          * Now add an empty security token.
     575             :          */
     576           1 :         token = talloc_zero(ctx, struct security_token);
     577           1 :         sess->security_token = token;
     578           1 :         sid = dsdb_audit_get_user_sid(module);
     579           1 :         assert_null(sid);
     580             : 
     581             :         /*
     582             :          * Add a single SID
     583             :          */
     584           1 :         string_to_sid(&sids[0], SID0);
     585           1 :         token->num_sids = 1;
     586           1 :         token->sids = sids;
     587           1 :         sid = dsdb_audit_get_user_sid(module);
     588           1 :         assert_non_null(sid);
     589           1 :         dom_sid_str_buf(sid, &sid_buf);
     590           1 :         assert_string_equal(SID0, sid_buf.buf);
     591             : 
     592             :         /*
     593             :          * Add a second SID, should still use the first SID
     594             :          */
     595           1 :         string_to_sid(&sids[1], SID1);
     596           1 :         token->num_sids = 2;
     597           1 :         sid = dsdb_audit_get_user_sid(module);
     598           1 :         assert_non_null(sid);
     599           1 :         dom_sid_str_buf(sid, &sid_buf);
     600           1 :         assert_string_equal(SID0, sid_buf.buf);
     601             : 
     602             : 
     603             :         /*
     604             :          * Now test a null sid in the first position
     605             :          */
     606           1 :         token->num_sids = 1;
     607           1 :         token->sids = NULL;
     608           1 :         sid = dsdb_audit_get_user_sid(module);
     609           1 :         assert_null(sid);
     610             : 
     611           1 :         TALLOC_FREE(ctx);
     612           1 : }
     613             : 
     614           1 : static void test_dsdb_audit_get_actual_sid(void **state)
     615             : {
     616           1 :         struct ldb_context *ldb        = NULL;
     617           1 :         const struct dom_sid *sid      = NULL;
     618           1 :         struct auth_session_info *sess = NULL;
     619           1 :         struct security_token *token   = NULL;
     620           1 :         struct dom_sid sids[2];
     621           1 :         const char * const SID0 = "S-1-5-21-2470180966-3899876309-2637894779";
     622           1 :         const char * const SID1 = "S-1-5-21-4284042908-2889457889-3672286761";
     623           1 :         struct dom_sid_buf sid_buf;
     624             : 
     625             : 
     626           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     627             : 
     628           1 :         ldb = ldb_init(ctx, NULL);
     629             : 
     630             :         /*
     631             :          * Freshly initialised structures, will be no session data
     632             :          * so expect NULL
     633             :          */
     634           1 :         sid = dsdb_audit_get_actual_sid(ldb);
     635           1 :         assert_null(sid);
     636             : 
     637             :         /*
     638             :          * Now add a NULL session info
     639             :          */
     640           1 :         ldb_set_opaque(ldb, DSDB_NETWORK_SESSION_INFO, NULL);
     641           1 :         sid = dsdb_audit_get_actual_sid(ldb);
     642           1 :         assert_null(sid);
     643             : 
     644             :         /*
     645             :          * Now add a session info with no user sid
     646             :          */
     647           1 :         sess = talloc_zero(ctx, struct auth_session_info);
     648           1 :         ldb_set_opaque(ldb, DSDB_NETWORK_SESSION_INFO, sess);
     649           1 :         sid = dsdb_audit_get_actual_sid(ldb);
     650           1 :         assert_null(sid);
     651             : 
     652             :         /*
     653             :          * Now add an empty security token.
     654             :          */
     655           1 :         token = talloc_zero(ctx, struct security_token);
     656           1 :         sess->security_token = token;
     657           1 :         sid = dsdb_audit_get_actual_sid(ldb);
     658           1 :         assert_null(sid);
     659             : 
     660             :         /*
     661             :          * Add a single SID
     662             :          */
     663           1 :         string_to_sid(&sids[0], SID0);
     664           1 :         token->num_sids = 1;
     665           1 :         token->sids = sids;
     666           1 :         sid = dsdb_audit_get_actual_sid(ldb);
     667           1 :         assert_non_null(sid);
     668           1 :         dom_sid_str_buf(sid, &sid_buf);
     669           1 :         assert_string_equal(SID0, sid_buf.buf);
     670             : 
     671             :         /*
     672             :          * Add a second SID, should still use the first SID
     673             :          */
     674           1 :         string_to_sid(&sids[1], SID1);
     675           1 :         token->num_sids = 2;
     676           1 :         sid = dsdb_audit_get_actual_sid(ldb);
     677           1 :         assert_non_null(sid);
     678           1 :         dom_sid_str_buf(sid, &sid_buf);
     679           1 :         assert_string_equal(SID0, sid_buf.buf);
     680             : 
     681             : 
     682             :         /*
     683             :          * Now test a null sid in the first position
     684             :          */
     685           1 :         token->num_sids = 1;
     686           1 :         token->sids = NULL;
     687           1 :         sid = dsdb_audit_get_actual_sid(ldb);
     688           1 :         assert_null(sid);
     689             : 
     690           1 :         TALLOC_FREE(ctx);
     691           1 : }
     692             : 
     693           1 : static void test_dsdb_audit_is_system_session(void **state)
     694             : {
     695           1 :         struct ldb_context *ldb        = NULL;
     696           1 :         struct ldb_module *module      = NULL;
     697           1 :         const struct dom_sid *sid      = NULL;
     698           1 :         struct auth_session_info *sess = NULL;
     699           1 :         struct security_token *token   = NULL;
     700           1 :         struct dom_sid sids[2];
     701           1 :         const char * const SID0 = "S-1-5-21-2470180966-3899876309-2637894779";
     702           1 :         const char * const SID1 = "S-1-5-21-4284042908-2889457889-3672286761";
     703             : 
     704             : 
     705           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     706             : 
     707           1 :         ldb = ldb_init(ctx, NULL);
     708           1 :         module = talloc_zero(ctx, struct ldb_module);
     709           1 :         module->ldb = ldb;
     710             : 
     711             :         /*
     712             :          * Freshly initialised structures, will be no session data
     713             :          * so expect NULL
     714             :          */
     715           1 :         assert_false(dsdb_audit_is_system_session(module));
     716             : 
     717             :         /*
     718             :          * Now add a NULL session info
     719             :          */
     720           1 :         ldb_set_opaque(ldb, DSDB_SESSION_INFO, NULL);
     721           1 :         assert_false(dsdb_audit_is_system_session(module));
     722             : 
     723             :         /*
     724             :          * Now add a session info with no user sid
     725             :          */
     726           1 :         sess = talloc_zero(ctx, struct auth_session_info);
     727           1 :         ldb_set_opaque(ldb, DSDB_SESSION_INFO, sess);
     728           1 :         assert_false(dsdb_audit_is_system_session(module));
     729             : 
     730             :         /*
     731             :          * Now add an empty security token.
     732             :          */
     733           1 :         token = talloc_zero(ctx, struct security_token);
     734           1 :         sess->security_token = token;
     735           1 :         assert_false(dsdb_audit_is_system_session(module));
     736             : 
     737             :         /*
     738             :          * Add a single SID, non system sid
     739             :          */
     740           1 :         string_to_sid(&sids[0], SID0);
     741           1 :         token->num_sids = 1;
     742           1 :         token->sids = sids;
     743           1 :         assert_false(dsdb_audit_is_system_session(module));
     744             : 
     745             :         /*
     746             :          * Add the system SID to the second position,
     747             :          * this should be ignored.
     748             :          */
     749           1 :         token->num_sids = 2;
     750           1 :         sids[1] = global_sid_System;
     751           1 :         assert_false(dsdb_audit_is_system_session(module));
     752             : 
     753             :         /*
     754             :          * Add a single SID, system sid
     755             :          */
     756           1 :         token->num_sids = 1;
     757           1 :         sids[0] = global_sid_System;
     758           1 :         token->sids = sids;
     759           1 :         assert_true(dsdb_audit_is_system_session(module));
     760             : 
     761             :         /*
     762             :          * Add a non system SID to position 2
     763             :          */
     764           1 :         sids[0] = global_sid_System;
     765           1 :         string_to_sid(&sids[1], SID1);
     766           1 :         token->num_sids = 2;
     767           1 :         token->sids = sids;
     768           1 :         assert_true(dsdb_audit_is_system_session(module));
     769             : 
     770             :         /*
     771             :          * Now test a null sid in the first position
     772             :          */
     773           1 :         token->num_sids = 1;
     774           1 :         token->sids = NULL;
     775           1 :         sid = dsdb_audit_get_user_sid(module);
     776           1 :         assert_null(sid);
     777             : 
     778           1 :         TALLOC_FREE(ctx);
     779           1 : }
     780             : 
     781           1 : static void test_dsdb_audit_get_unique_session_token(void **state)
     782             : {
     783           1 :         struct ldb_context *ldb = NULL;
     784           1 :         struct ldb_module *module = NULL;
     785           1 :         struct auth_session_info *sess = NULL;
     786           1 :         const struct GUID *guid;
     787           1 :         const char * const GUID_S = "7130cb06-2062-6a1b-409e-3514c26b1773";
     788           1 :         struct GUID in;
     789           1 :         char *guid_str;
     790           1 :         struct GUID_txt_buf guid_buff;
     791             : 
     792             : 
     793           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     794             : 
     795           1 :         ldb = ldb_init(ctx, NULL);
     796           1 :         module = talloc_zero(ctx, struct ldb_module);
     797           1 :         module->ldb = ldb;
     798             : 
     799             :         /*
     800             :          * Test a freshly initialized ldb
     801             :          * should return NULL
     802             :          */
     803           1 :         guid = dsdb_audit_get_unique_session_token(module);
     804           1 :         assert_null(guid);
     805             : 
     806             :         /*
     807             :          * Now add a NULL session info
     808             :          */
     809           1 :         ldb_set_opaque(ldb, DSDB_SESSION_INFO, NULL);
     810           1 :         guid = dsdb_audit_get_unique_session_token(module);
     811           1 :         assert_null(guid);
     812             : 
     813             :         /*
     814             :          * Now add a session info with no session id
     815             :          * Note if the memory has not been zeroed correctly all bets are
     816             :          *      probably off.
     817             :          */
     818           1 :         sess = talloc_zero(ctx, struct auth_session_info);
     819           1 :         ldb_set_opaque(ldb, DSDB_SESSION_INFO, sess);
     820           1 :         guid = dsdb_audit_get_unique_session_token(module);
     821             :         /*
     822             :          * We will get a GUID, but it's contents will be undefined
     823             :          */
     824           1 :         assert_non_null(guid);
     825             : 
     826             :         /*
     827             :          * Now set the session id and confirm that we get it back.
     828             :          */
     829           1 :         GUID_from_string(GUID_S, &in);
     830           1 :         sess->unique_session_token = in;
     831           1 :         guid = dsdb_audit_get_unique_session_token(module);
     832           1 :         assert_non_null(guid);
     833           1 :         guid_str = GUID_buf_string(guid, &guid_buff);
     834           1 :         assert_string_equal(GUID_S, guid_str);
     835             : 
     836           1 :         TALLOC_FREE(ctx);
     837             : 
     838           1 : }
     839             : 
     840           1 : static void test_dsdb_audit_get_actual_unique_session_token(void **state)
     841             : {
     842           1 :         struct ldb_context *ldb = NULL;
     843           1 :         struct auth_session_info *sess = NULL;
     844           1 :         const struct GUID *guid;
     845           1 :         const char * const GUID_S = "7130cb06-2062-6a1b-409e-3514c26b1773";
     846           1 :         struct GUID in;
     847           1 :         char *guid_str;
     848           1 :         struct GUID_txt_buf guid_buff;
     849             : 
     850             : 
     851           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     852             : 
     853           1 :         ldb = ldb_init(ctx, NULL);
     854             : 
     855             :         /*
     856             :          * Test a freshly initialized ldb
     857             :          * should return NULL
     858             :          */
     859           1 :         guid = dsdb_audit_get_actual_unique_session_token(ldb);
     860           1 :         assert_null(guid);
     861             : 
     862             :         /*
     863             :          * Now add a NULL session info
     864             :          */
     865           1 :         ldb_set_opaque(ldb, DSDB_NETWORK_SESSION_INFO, NULL);
     866           1 :         guid = dsdb_audit_get_actual_unique_session_token(ldb);
     867           1 :         assert_null(guid);
     868             : 
     869             :         /*
     870             :          * Now add a session info with no session id
     871             :          * Note if the memory has not been zeroed correctly all bets are
     872             :          *      probably off.
     873             :          */
     874           1 :         sess = talloc_zero(ctx, struct auth_session_info);
     875           1 :         ldb_set_opaque(ldb, DSDB_NETWORK_SESSION_INFO, sess);
     876           1 :         guid = dsdb_audit_get_actual_unique_session_token(ldb);
     877             :         /*
     878             :          * We will get a GUID, but it's contents will be undefined
     879             :          */
     880           1 :         assert_non_null(guid);
     881             : 
     882             :         /*
     883             :          * Now set the session id and confirm that we get it back.
     884             :          */
     885           1 :         GUID_from_string(GUID_S, &in);
     886           1 :         sess->unique_session_token = in;
     887           1 :         guid = dsdb_audit_get_actual_unique_session_token(ldb);
     888           1 :         assert_non_null(guid);
     889           1 :         guid_str = GUID_buf_string(guid, &guid_buff);
     890           1 :         assert_string_equal(GUID_S, guid_str);
     891             : 
     892           1 :         TALLOC_FREE(ctx);
     893             : 
     894           1 : }
     895             : 
     896           1 : static void test_dsdb_audit_get_remote_host(void **state)
     897             : {
     898           1 :         struct ldb_context *ldb = NULL;
     899           1 :         char *rh = NULL;
     900           1 :         struct tsocket_address *in = NULL;
     901             : 
     902           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     903             : 
     904           1 :         ldb = ldb_init(ctx, NULL);
     905             : 
     906             :         /*
     907             :          * Test a freshly initialized ldb
     908             :          * should return "Unknown"
     909             :          */
     910           1 :         rh = dsdb_audit_get_remote_host(ldb, ctx);
     911           1 :         assert_string_equal("Unknown", rh);
     912           1 :         TALLOC_FREE(rh);
     913             : 
     914             :         /*
     915             :          * opaque set to null, should return NULL
     916             :          */
     917           1 :         ldb_set_opaque(ldb, "remoteAddress", NULL);
     918           1 :         rh = dsdb_audit_get_remote_host(ldb, ctx);
     919           1 :         assert_string_equal("Unknown", rh);
     920           1 :         TALLOC_FREE(rh);
     921             : 
     922             :         /*
     923             :          * Ensure that the value set is returned
     924             :          */
     925           1 :         tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 42, &in);
     926           1 :         ldb_set_opaque(ldb, "remoteAddress", in);
     927           1 :         rh = dsdb_audit_get_remote_host(ldb, ctx);
     928           1 :         assert_string_equal("ipv4:127.0.0.1:42", rh);
     929           1 :         TALLOC_FREE(rh);
     930             : 
     931           1 :         TALLOC_FREE(ctx);
     932             : 
     933           1 : }
     934             : 
     935           1 : static void test_dsdb_audit_get_primary_dn(void **state)
     936             : {
     937           1 :         struct ldb_request *req = NULL;
     938           1 :         struct ldb_message *msg = NULL;
     939           1 :         struct ldb_context *ldb = NULL;
     940             : 
     941           1 :         struct ldb_dn *dn = NULL;
     942             : 
     943           1 :         const char * const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";
     944           1 :         const char *s = NULL;
     945             : 
     946           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     947             : 
     948           1 :         req = talloc_zero(ctx, struct ldb_request);
     949           1 :         msg = talloc_zero(ctx, struct ldb_message);
     950           1 :         ldb = ldb_init(ctx, NULL);
     951           1 :         dn = ldb_dn_new(ctx, ldb, DN);
     952             : 
     953             :         /*
     954             :          * Try an empty request.
     955             :          */
     956           1 :         s = dsdb_audit_get_primary_dn(req);
     957           1 :         assert_null(s);
     958             : 
     959             :         /*
     960             :          * Now try an add with a null message.
     961             :          */
     962           1 :         req->operation = LDB_ADD;
     963           1 :         req->op.add.message = NULL;
     964           1 :         s = dsdb_audit_get_primary_dn(req);
     965           1 :         assert_null(s);
     966             : 
     967             :         /*
     968             :          * Now try an mod with a null message.
     969             :          */
     970           1 :         req->operation = LDB_MODIFY;
     971           1 :         req->op.mod.message = NULL;
     972           1 :         s = dsdb_audit_get_primary_dn(req);
     973           1 :         assert_null(s);
     974             : 
     975             :         /*
     976             :          * Now try an add with a missing dn
     977             :          */
     978           1 :         req->operation = LDB_ADD;
     979           1 :         req->op.add.message = msg;
     980           1 :         s = dsdb_audit_get_primary_dn(req);
     981           1 :         assert_null(s);
     982             : 
     983             :         /*
     984             :          * Now try a mod with a messing dn
     985             :          */
     986           1 :         req->operation = LDB_ADD;
     987           1 :         req->op.mod.message = msg;
     988           1 :         s = dsdb_audit_get_primary_dn(req);
     989           1 :         assert_null(s);
     990             : 
     991             :         /*
     992             :          * Add a dn to the message
     993             :          */
     994           1 :         msg->dn = dn;
     995             : 
     996             :         /*
     997             :          * Now try an add with a dn
     998             :          */
     999           1 :         req->operation = LDB_ADD;
    1000           1 :         req->op.add.message = msg;
    1001           1 :         s = dsdb_audit_get_primary_dn(req);
    1002           1 :         assert_non_null(s);
    1003           1 :         assert_string_equal(DN, s);
    1004             : 
    1005             :         /*
    1006             :          * Now try a mod with a dn
    1007             :          */
    1008           1 :         req->operation = LDB_MODIFY;
    1009           1 :         req->op.mod.message = msg;
    1010           1 :         s = dsdb_audit_get_primary_dn(req);
    1011           1 :         assert_non_null(s);
    1012           1 :         assert_string_equal(DN, s);
    1013             : 
    1014             :         /*
    1015             :          * Try a delete without a dn
    1016             :          */
    1017           1 :         req->operation = LDB_DELETE;
    1018           1 :         req->op.del.dn = NULL;
    1019           1 :         s = dsdb_audit_get_primary_dn(req);
    1020           1 :         assert_null(s);
    1021             : 
    1022             :         /*
    1023             :          * Try a delete with a dn
    1024             :          */
    1025           1 :         req->operation = LDB_DELETE;
    1026           1 :         req->op.del.dn = dn;
    1027           1 :         s = dsdb_audit_get_primary_dn(req);
    1028           1 :         assert_non_null(s);
    1029           1 :         assert_string_equal(DN, s);
    1030             : 
    1031             :         /*
    1032             :          * Try a rename without a dn
    1033             :          */
    1034           1 :         req->operation = LDB_RENAME;
    1035           1 :         req->op.rename.olddn = NULL;
    1036           1 :         s = dsdb_audit_get_primary_dn(req);
    1037           1 :         assert_null(s);
    1038             : 
    1039             :         /*
    1040             :          * Try a rename with a dn
    1041             :          */
    1042           1 :         req->operation = LDB_RENAME;
    1043           1 :         req->op.rename.olddn = dn;
    1044           1 :         s = dsdb_audit_get_primary_dn(req);
    1045           1 :         assert_non_null(s);
    1046           1 :         assert_string_equal(DN, s);
    1047             : 
    1048             :         /*
    1049             :          * Try an extended operation, i.e. one that does not have a DN
    1050             :          * associated with it for logging purposes.
    1051             :          */
    1052           1 :         req->operation = LDB_EXTENDED;
    1053           1 :         s = dsdb_audit_get_primary_dn(req);
    1054           1 :         assert_null(s);
    1055             : 
    1056           1 :         TALLOC_FREE(ctx);
    1057           1 : }
    1058             : 
    1059           1 : static void test_dsdb_audit_get_message(void **state)
    1060             : {
    1061           1 :         struct ldb_request *req = NULL;
    1062           1 :         struct ldb_message *msg = NULL;
    1063           1 :         const struct ldb_message *r = NULL;
    1064             : 
    1065             : 
    1066           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
    1067             : 
    1068           1 :         req = talloc_zero(ctx, struct ldb_request);
    1069           1 :         msg = talloc_zero(ctx, struct ldb_message);
    1070             : 
    1071             :         /*
    1072             :          * Test an empty message
    1073             :          */
    1074           1 :         r = dsdb_audit_get_message(req);
    1075           1 :         assert_null(r);
    1076             : 
    1077             :         /*
    1078             :          * Test an add message
    1079             :          */
    1080           1 :         req->operation = LDB_ADD;
    1081           1 :         req->op.add.message = msg;
    1082           1 :         r = dsdb_audit_get_message(req);
    1083           1 :         assert_ptr_equal(msg, r);
    1084             : 
    1085             :         /*
    1086             :          * Test a modify message
    1087             :          */
    1088           1 :         req->operation = LDB_MODIFY;
    1089           1 :         req->op.mod.message = msg;
    1090           1 :         r = dsdb_audit_get_message(req);
    1091           1 :         assert_ptr_equal(msg, r);
    1092             : 
    1093             :         /*
    1094             :          * Test a Delete message, i.e. trigger the default case
    1095             :          */
    1096           1 :         req->operation = LDB_DELETE;
    1097           1 :         r = dsdb_audit_get_message(req);
    1098           1 :         assert_null(r);
    1099             : 
    1100           1 :         TALLOC_FREE(ctx);
    1101           1 : }
    1102             : 
    1103           1 : static void test_dsdb_audit_get_secondary_dn(void **state)
    1104             : {
    1105           1 :         struct ldb_request *req = NULL;
    1106           1 :         struct ldb_context *ldb = NULL;
    1107             : 
    1108           1 :         struct ldb_dn *dn = NULL;
    1109             : 
    1110           1 :         const char * const DN = "dn=CN=USER,CN=Users,DC=SAMBA,DC=ORG";
    1111           1 :         const char *s = NULL;
    1112             : 
    1113           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
    1114             : 
    1115           1 :         req = talloc_zero(ctx, struct ldb_request);
    1116           1 :         ldb = ldb_init(ctx, NULL);
    1117           1 :         dn = ldb_dn_new(ctx, ldb, DN);
    1118             : 
    1119             :         /*
    1120             :          * Try an empty request.
    1121             :          */
    1122           1 :         s = dsdb_audit_get_secondary_dn(req);
    1123           1 :         assert_null(s);
    1124             : 
    1125             :         /*
    1126             :          * Try a rename without a dn
    1127             :          */
    1128           1 :         req->operation = LDB_RENAME;
    1129           1 :         req->op.rename.newdn = NULL;
    1130           1 :         s = dsdb_audit_get_secondary_dn(req);
    1131           1 :         assert_null(s);
    1132             : 
    1133             :         /*
    1134             :          * Try a rename with a dn
    1135             :          */
    1136           1 :         req->operation = LDB_RENAME;
    1137           1 :         req->op.rename.newdn = dn;
    1138           1 :         s = dsdb_audit_get_secondary_dn(req);
    1139           1 :         assert_non_null(s);
    1140           1 :         assert_string_equal(DN, s);
    1141             : 
    1142             :         /*
    1143             :          * Try an extended operation, i.e. one that does not have a DN
    1144             :          * associated with it for logging purposes.
    1145             :          */
    1146           1 :         req->operation = LDB_EXTENDED;
    1147           1 :         s = dsdb_audit_get_primary_dn(req);
    1148           1 :         assert_null(s);
    1149             : 
    1150           1 :         TALLOC_FREE(ctx);
    1151           1 : }
    1152             : 
    1153           1 : static void test_dsdb_audit_get_operation_name(void **state)
    1154             : {
    1155           1 :         struct ldb_request *req = NULL;
    1156             : 
    1157           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
    1158             : 
    1159           1 :         req = talloc_zero(ctx, struct ldb_request);
    1160             : 
    1161           1 :         req->operation =  LDB_SEARCH;
    1162           1 :         assert_string_equal("Search", dsdb_audit_get_operation_name(req));
    1163             : 
    1164           1 :         req->operation =  LDB_ADD;
    1165           1 :         assert_string_equal("Add", dsdb_audit_get_operation_name(req));
    1166             : 
    1167           1 :         req->operation =  LDB_MODIFY;
    1168           1 :         assert_string_equal("Modify", dsdb_audit_get_operation_name(req));
    1169             : 
    1170           1 :         req->operation =  LDB_DELETE;
    1171           1 :         assert_string_equal("Delete", dsdb_audit_get_operation_name(req));
    1172             : 
    1173           1 :         req->operation =  LDB_RENAME;
    1174           1 :         assert_string_equal("Rename", dsdb_audit_get_operation_name(req));
    1175             : 
    1176           1 :         req->operation =  LDB_EXTENDED;
    1177           1 :         assert_string_equal("Extended", dsdb_audit_get_operation_name(req));
    1178             : 
    1179           1 :         req->operation =  LDB_REQ_REGISTER_CONTROL;
    1180           1 :         assert_string_equal(
    1181             :                 "Register Control",
    1182             :                 dsdb_audit_get_operation_name(req));
    1183             : 
    1184           1 :         req->operation =  LDB_REQ_REGISTER_PARTITION;
    1185           1 :         assert_string_equal(
    1186             :                 "Register Partition",
    1187             :                 dsdb_audit_get_operation_name(req));
    1188             : 
    1189             :         /*
    1190             :          * Trigger the default case
    1191             :          */
    1192           1 :         req->operation =  -1;
    1193           1 :         assert_string_equal("Unknown", dsdb_audit_get_operation_name(req));
    1194             : 
    1195           1 :         TALLOC_FREE(ctx);
    1196           1 : }
    1197             : 
    1198           1 : static void test_dsdb_audit_get_modification_action(void **state)
    1199             : {
    1200           1 :         assert_string_equal(
    1201             :                 "add",
    1202             :                 dsdb_audit_get_modification_action(LDB_FLAG_MOD_ADD));
    1203           1 :         assert_string_equal(
    1204             :                 "delete",
    1205             :                 dsdb_audit_get_modification_action(LDB_FLAG_MOD_DELETE));
    1206           1 :         assert_string_equal(
    1207             :                 "replace",
    1208             :                 dsdb_audit_get_modification_action(LDB_FLAG_MOD_REPLACE));
    1209             :         /*
    1210             :          * Trigger the default case
    1211             :          */
    1212           1 :         assert_string_equal(
    1213             :                 "unknown",
    1214             :                 dsdb_audit_get_modification_action(0));
    1215           1 : }
    1216             : 
    1217           1 : static void test_dsdb_audit_is_password_attribute(void **state)
    1218             : {
    1219           1 :         assert_true(dsdb_audit_is_password_attribute("userPassword"));
    1220           1 :         assert_true(dsdb_audit_is_password_attribute("clearTextPassword"));
    1221           1 :         assert_true(dsdb_audit_is_password_attribute("unicodePwd"));
    1222           1 :         assert_true(dsdb_audit_is_password_attribute("dBCSPwd"));
    1223             : 
    1224           1 :         assert_false(dsdb_audit_is_password_attribute("xserPassword"));
    1225           1 : }
    1226             : 
    1227           1 : static void test_dsdb_audit_redact_attribute(void **state)
    1228             : {
    1229           1 :         assert_true(dsdb_audit_redact_attribute("userPassword"));
    1230             : 
    1231           1 :         assert_true(dsdb_audit_redact_attribute("pekList"));
    1232           1 :         assert_true(dsdb_audit_redact_attribute("clearTextPassword"));
    1233           1 :         assert_true(dsdb_audit_redact_attribute("initialAuthIncoming"));
    1234             : 
    1235           1 :         assert_false(dsdb_audit_redact_attribute("supaskrt"));
    1236           1 : }
    1237             : 
    1238           1 : int main(void) {
    1239           1 :         const struct CMUnitTest tests[] = {
    1240             :                 cmocka_unit_test(test_dsdb_audit_add_ldb_value),
    1241             :                 cmocka_unit_test(test_dsdb_audit_attributes_json),
    1242             :                 cmocka_unit_test(test_dsdb_audit_get_remote_address),
    1243             :                 cmocka_unit_test(test_dsdb_audit_get_ldb_error_string),
    1244             :                 cmocka_unit_test(test_dsdb_audit_get_user_sid),
    1245             :                 cmocka_unit_test(test_dsdb_audit_get_actual_sid),
    1246             :                 cmocka_unit_test(test_dsdb_audit_is_system_session),
    1247             :                 cmocka_unit_test(test_dsdb_audit_get_unique_session_token),
    1248             :                 cmocka_unit_test(test_dsdb_audit_get_actual_unique_session_token),
    1249             :                 cmocka_unit_test(test_dsdb_audit_get_remote_host),
    1250             :                 cmocka_unit_test(test_dsdb_audit_get_primary_dn),
    1251             :                 cmocka_unit_test(test_dsdb_audit_get_message),
    1252             :                 cmocka_unit_test(test_dsdb_audit_get_secondary_dn),
    1253             :                 cmocka_unit_test(test_dsdb_audit_get_operation_name),
    1254             :                 cmocka_unit_test(test_dsdb_audit_get_modification_action),
    1255             :                 cmocka_unit_test(test_dsdb_audit_is_password_attribute),
    1256             :                 cmocka_unit_test(test_dsdb_audit_redact_attribute),
    1257             :         };
    1258             : 
    1259           1 :         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
    1260           1 :         return cmocka_run_group_tests(tests, NULL, NULL);
    1261             : }

Generated by: LCOV version 1.14