LCOV - code coverage report
Current view: top level - lib/audit_logging/tests - audit_logging_test.c (source / functions) Hit Total Coverage
Test: coverage report for support-claim-type-attributes 6b5c566e Lines: 601 601 100.0 %
Date: 2023-11-21 12:31:41 Functions: 16 16 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Unit tests for the audit_logging library.
       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             : 
      21             : /*
      22             :  * from cmocka.c:
      23             :  * These headers or their equivalents should be included prior to
      24             :  * including
      25             :  * this header file.
      26             :  *
      27             :  * #include <stdarg.h>
      28             :  * #include <stddef.h>
      29             :  * #include <setjmp.h>
      30             :  *
      31             :  * This allows test applications to use custom definitions of C standard
      32             :  * library functions and types.
      33             :  *
      34             :  */
      35             : 
      36             : /*
      37             :  * Note that the messaging routines (audit_message_send and get_event_server)
      38             :  * are not tested by these unit tests.  Currently they are for integration
      39             :  * test support, and as such are exercised by the integration tests.
      40             :  */
      41             : #include <stdarg.h>
      42             : #include <stddef.h>
      43             : #include <setjmp.h>
      44             : #include <cmocka.h>
      45             : 
      46             : #include <string.h>
      47             : #include <time.h>
      48             : #include <tevent.h>
      49             : #include <config.h>
      50             : #include <talloc.h>
      51             : #include "lib/util/talloc_stack.h"
      52             : 
      53             : #include "lib/util/data_blob.h"
      54             : #include "lib/util/time.h"
      55             : #include "libcli/util/werror.h"
      56             : #include "lib/param/loadparm.h"
      57             : #include "libcli/security/dom_sid.h"
      58             : #include "librpc/ndr/libndr.h"
      59             : 
      60             : #include "lib/audit_logging/audit_logging.h"
      61             : 
      62           1 : static void test_json_add_int(_UNUSED_ void **state)
      63             : {
      64           1 :         struct json_object object;
      65           1 :         struct json_t *value = NULL;
      66           1 :         json_int_t m;
      67           1 :         double n;
      68           1 :         int rc = 0;
      69           1 :         intmax_t big_int = ((intmax_t)1)<<33;
      70             : 
      71           1 :         object = json_new_object();
      72           1 :         assert_false(json_is_invalid(&object));
      73           1 :         rc = json_add_int(&object, "positive_one", 1);
      74           1 :         assert_int_equal(0, rc);
      75           1 :         rc = json_add_int(&object, "zero", 0);
      76           1 :         assert_int_equal(0, rc);
      77           1 :         rc = json_add_int(&object, "negative_one", -1);
      78           1 :         assert_int_equal(0, rc);
      79           1 :         rc = json_add_int(&object, "big_int", big_int);
      80           1 :         assert_int_equal(0, rc);
      81             : 
      82           1 :         assert_int_equal(4, json_object_size(object.root));
      83             : 
      84           1 :         value = json_object_get(object.root, "positive_one");
      85           1 :         assert_true(json_is_integer(value));
      86           1 :         n = json_number_value(value);
      87           1 :         assert_true(n == 1.0);
      88             : 
      89           1 :         value = json_object_get(object.root, "zero");
      90           1 :         assert_true(json_is_integer(value));
      91           1 :         n = json_number_value(value);
      92           1 :         assert_true(n == 0.0);
      93             : 
      94           1 :         value = json_object_get(object.root, "negative_one");
      95           1 :         assert_true(json_is_integer(value));
      96           1 :         n = json_number_value(value);
      97           1 :         assert_true(n == -1.0);
      98             : 
      99           1 :         value = json_object_get(object.root, "big_int");
     100           1 :         assert_true(json_is_integer(value));
     101           1 :         m = json_integer_value(value);
     102           1 :         assert_int_equal(m, big_int);
     103             : 
     104           1 :         object.valid = false;
     105           1 :         rc = json_add_int(&object, "should fail 1", 0xf1);
     106           1 :         assert_int_equal(JSON_ERROR, rc);
     107             : 
     108           1 :         json_free(&object);
     109             : 
     110           1 :         rc = json_add_int(&object, "should fail 2", 0xf2);
     111           1 :         assert_int_equal(JSON_ERROR, rc);
     112           1 : }
     113             : 
     114           1 : static void test_json_add_bool(_UNUSED_ void **state)
     115             : {
     116           1 :         struct json_object object;
     117           1 :         struct json_t *value = NULL;
     118           1 :         int rc = 0;
     119             : 
     120           1 :         object = json_new_object();
     121           1 :         assert_false(json_is_invalid(&object));
     122           1 :         rc = json_add_bool(&object, "true", true);
     123           1 :         assert_int_equal(0, rc);
     124           1 :         rc = json_add_bool(&object, "false", false);
     125           1 :         assert_int_equal(0, rc);
     126             : 
     127           1 :         assert_int_equal(2, json_object_size(object.root));
     128             : 
     129           1 :         value = json_object_get(object.root, "true");
     130           1 :         assert_true(json_is_boolean(value));
     131           1 :         assert_true(value == json_true());
     132             : 
     133           1 :         value = json_object_get(object.root, "false");
     134           1 :         assert_true(json_is_boolean(value));
     135           1 :         assert_true(value == json_false());
     136             : 
     137           1 :         object.valid = false;
     138           1 :         rc = json_add_bool(&object, "should fail 1", true);
     139           1 :         assert_int_equal(JSON_ERROR, rc);
     140             : 
     141           1 :         json_free(&object);
     142             : 
     143           1 :         rc = json_add_bool(&object, "should fail 2", false);
     144           1 :         assert_int_equal(JSON_ERROR, rc);
     145           1 : }
     146             : 
     147           1 : static void test_json_add_string(_UNUSED_ void **state)
     148             : {
     149           1 :         struct json_object object;
     150           1 :         struct json_t *value = NULL;
     151           1 :         const char *s = NULL;
     152           1 :         int rc = 0;
     153             : 
     154           1 :         object = json_new_object();
     155           1 :         assert_false(json_is_invalid(&object));
     156           1 :         rc = json_add_string(&object, "null", NULL);
     157           1 :         assert_int_equal(0, rc);
     158           1 :         rc = json_add_string(&object, "empty", "");
     159           1 :         assert_int_equal(0, rc);
     160           1 :         rc = json_add_string(&object, "name", "value");
     161           1 :         assert_int_equal(0, rc);
     162             : 
     163           1 :         assert_int_equal(3, json_object_size(object.root));
     164             : 
     165           1 :         value = json_object_get(object.root, "null");
     166           1 :         assert_true(json_is_null(value));
     167             : 
     168           1 :         value = json_object_get(object.root, "empty");
     169           1 :         assert_true(json_is_string(value));
     170           1 :         s = json_string_value(value);
     171           1 :         assert_string_equal("", s);
     172             : 
     173           1 :         value = json_object_get(object.root, "name");
     174           1 :         assert_true(json_is_string(value));
     175           1 :         s = json_string_value(value);
     176           1 :         assert_string_equal("value", s);
     177             : 
     178           1 :         object.valid = false;
     179           1 :         rc = json_add_string(&object, "should fail 1", "A value");
     180           1 :         assert_int_equal(JSON_ERROR, rc);
     181             : 
     182           1 :         json_free(&object);
     183             : 
     184           1 :         rc = json_add_string(&object, "should fail 2", "Another value");
     185           1 :         assert_int_equal(JSON_ERROR, rc);
     186           1 : }
     187             : 
     188           1 : static void test_json_add_object(_UNUSED_ void **state)
     189             : {
     190           1 :         struct json_object object;
     191           1 :         struct json_object other;
     192           1 :         struct json_object after;
     193           1 :         struct json_object invalid = json_empty_object;
     194           1 :         struct json_t *value = NULL;
     195           1 :         int rc = 0;
     196             : 
     197           1 :         object = json_new_object();
     198           1 :         assert_false(json_is_invalid(&object));
     199           1 :         other  = json_new_object();
     200           1 :         assert_false(json_is_invalid(&other));
     201           1 :         rc = json_add_object(&object, "null", NULL);
     202           1 :         assert_int_equal(0, rc);
     203           1 :         rc = json_add_object(&object, "other", &other);
     204           1 :         assert_int_equal(0, rc);
     205             : 
     206           1 :         assert_int_equal(2, json_object_size(object.root));
     207             : 
     208           1 :         value = json_object_get(object.root, "null");
     209           1 :         assert_true(json_is_null(value));
     210             : 
     211           1 :         value = json_object_get(object.root, "other");
     212           1 :         assert_true(json_is_object(value));
     213           1 :         assert_ptr_equal(other.root, value);
     214             : 
     215           1 :         rc = json_add_object(&object, "invalid", &invalid);
     216           1 :         assert_int_equal(JSON_ERROR, rc);
     217             : 
     218           1 :         object.valid = false;
     219           1 :         after = json_new_object();
     220           1 :         assert_false(json_is_invalid(&after));
     221           1 :         rc = json_add_object(&object, "after", &after);
     222           1 :         assert_int_equal(JSON_ERROR, rc);
     223             : 
     224           1 :         json_free(&object);
     225             : 
     226           1 :         rc = json_add_object(&object, "after", &after);
     227           1 :         assert_int_equal(JSON_ERROR, rc);
     228             : 
     229           1 :         json_free(&after);
     230           1 : }
     231             : 
     232           1 : static void test_json_add_to_array(_UNUSED_ void **state)
     233             : {
     234           1 :         struct json_object array;
     235           1 :         struct json_object o1;
     236           1 :         struct json_object o2;
     237           1 :         struct json_object o3;
     238           1 :         struct json_object after;
     239           1 :         struct json_object invalid = json_empty_object;
     240           1 :         struct json_t *value = NULL;
     241           1 :         int rc = 0;
     242             : 
     243           1 :         array = json_new_array();
     244           1 :         assert_false(json_is_invalid(&array));
     245           1 :         assert_true(json_is_array(array.root));
     246             : 
     247           1 :         o1 = json_new_object();
     248           1 :         assert_false(json_is_invalid(&o1));
     249           1 :         o2 = json_new_object();
     250           1 :         assert_false(json_is_invalid(&o2));
     251           1 :         o3 = json_new_object();
     252           1 :         assert_false(json_is_invalid(&o3));
     253             : 
     254           1 :         rc = json_add_object(&array, NULL, &o3);
     255           1 :         assert_int_equal(0, rc);
     256           1 :         rc = json_add_object(&array, "", &o2);
     257           1 :         assert_int_equal(0, rc);
     258           1 :         rc = json_add_object(&array, "will-be-ignored", &o1);
     259           1 :         assert_int_equal(0, rc);
     260           1 :         rc = json_add_object(&array, NULL, NULL);
     261           1 :         assert_int_equal(0, rc);
     262             : 
     263           1 :         assert_int_equal(4, json_array_size(array.root));
     264             : 
     265           1 :         value = json_array_get(array.root, 0);
     266           1 :         assert_ptr_equal(o3.root, value);
     267             : 
     268           1 :         value = json_array_get(array.root, 1);
     269           1 :         assert_ptr_equal(o2.root, value);
     270             : 
     271           1 :         value = json_array_get(array.root, 2);
     272           1 :         assert_ptr_equal(o1.root, value);
     273             : 
     274           1 :         value = json_array_get(array.root, 3);
     275           1 :         assert_true(json_is_null(value));
     276             : 
     277           1 :         rc = json_add_object(&array, "invalid", &invalid);
     278           1 :         assert_int_equal(JSON_ERROR, rc);
     279             : 
     280           1 :         array.valid = false;
     281           1 :         after = json_new_object();
     282           1 :         assert_false(json_is_invalid(&after));
     283           1 :         rc = json_add_object(&array, "after", &after);
     284           1 :         assert_int_equal(JSON_ERROR, rc);
     285             : 
     286           1 :         json_free(&array);
     287             : 
     288           1 :         rc = json_add_object(&array, "after", &after);
     289           1 :         assert_int_equal(JSON_ERROR, rc);
     290             : 
     291           1 :         json_free(&after);
     292           1 : }
     293             : 
     294           1 : static void test_json_add_timestamp(_UNUSED_ void **state)
     295             : {
     296           1 :         struct json_object object;
     297           1 :         struct json_t *ts = NULL;
     298           1 :         const char *t = NULL;
     299           1 :         int rc;
     300           1 :         int usec, tz;
     301           1 :         char c[2];
     302           1 :         struct tm tm;
     303           1 :         time_t before;
     304           1 :         time_t after;
     305           1 :         time_t actual;
     306           1 :         struct timeval tv;
     307           1 :         int ret;
     308             : 
     309           1 :         object = json_new_object();
     310           1 :         assert_false(json_is_invalid(&object));
     311             : 
     312           1 :         ret = gettimeofday(&tv, NULL);
     313           1 :         assert_int_equal(0, ret);
     314           1 :         before = tv.tv_sec;
     315             : 
     316           1 :         rc = json_add_timestamp(&object);
     317           1 :         assert_int_equal(0, rc);
     318             : 
     319           1 :         ret = gettimeofday(&tv, NULL);
     320           1 :         assert_int_equal(0, ret);
     321           1 :         after = tv.tv_sec;
     322             : 
     323           1 :         ts = json_object_get(object.root, "timestamp");
     324           1 :         assert_true(json_is_string(ts));
     325             : 
     326             :         /*
     327             :          * Convert the returned ISO 8601 timestamp into a time_t
     328             :          * Note for convenience we ignore the value of the microsecond
     329             :          * part of the time stamp.
     330             :          */
     331           1 :         t = json_string_value(ts);
     332           1 :         rc = sscanf(
     333             :                 t,
     334             :                 "%4d-%2d-%2dT%2d:%2d:%2d.%6d%1c%4d",
     335             :                 &tm.tm_year,
     336             :                 &tm.tm_mon,
     337             :                 &tm.tm_mday,
     338             :                 &tm.tm_hour,
     339             :                 &tm.tm_min,
     340             :                 &tm.tm_sec,
     341             :                 &usec,
     342             :                 c,
     343             :                 &tz);
     344           1 :         assert_int_equal(9, rc);
     345           1 :         tm.tm_year = tm.tm_year - 1900;
     346           1 :         tm.tm_mon = tm.tm_mon - 1;
     347           1 :         tm.tm_isdst = -1;
     348           1 :         actual = mktime(&tm);
     349             : 
     350             :         /*
     351             :          * The timestamp should be before <= actual <= after
     352             :          */
     353           1 :         assert_true(difftime(actual, before) >= 0);
     354           1 :         assert_true(difftime(after, actual) >= 0);
     355             : 
     356           1 :         object.valid = false;
     357           1 :         rc = json_add_timestamp(&object);
     358           1 :         assert_int_equal(JSON_ERROR, rc);
     359             : 
     360           1 :         json_free(&object);
     361             : 
     362           1 :         rc = json_add_timestamp(&object);
     363           1 :         assert_int_equal(JSON_ERROR, rc);
     364           1 : }
     365             : 
     366           1 : static void test_json_add_stringn(_UNUSED_ void **state)
     367             : {
     368           1 :         struct json_object object;
     369           1 :         struct json_t *value = NULL;
     370           1 :         const char *s = NULL;
     371           1 :         int rc = 0;
     372             : 
     373           1 :         object = json_new_object();
     374           1 :         assert_false(json_is_invalid(&object));
     375           1 :         rc = json_add_stringn(&object, "null", NULL, 10);
     376           1 :         assert_int_equal(0, rc);
     377           1 :         rc = json_add_stringn(&object, "null-zero-len", NULL, 0);
     378           1 :         assert_int_equal(0, rc);
     379           1 :         rc = json_add_stringn(&object, "empty", "", 1);
     380           1 :         assert_int_equal(0, rc);
     381           1 :         rc = json_add_stringn(&object, "empty-zero-len", "", 0);
     382           1 :         assert_int_equal(0, rc);
     383           1 :         rc = json_add_stringn(&object, "value-less-than-len", "123456", 7);
     384           1 :         assert_int_equal(0, rc);
     385           1 :         rc = json_add_stringn(&object, "value-greater-than-len", "abcd", 3);
     386           1 :         assert_int_equal(0, rc);
     387           1 :         rc = json_add_stringn(&object, "value-equal-len", "ZYX", 3);
     388           1 :         assert_int_equal(0, rc);
     389           1 :         rc = json_add_stringn(
     390             :             &object, "value-len-is-zero", "this will be null", 0);
     391           1 :         assert_int_equal(0, rc);
     392             : 
     393           1 :         assert_int_equal(8, json_object_size(object.root));
     394             : 
     395           1 :         value = json_object_get(object.root, "null");
     396           1 :         assert_true(json_is_null(value));
     397             : 
     398           1 :         value = json_object_get(object.root, "null-zero-len");
     399           1 :         assert_true(json_is_null(value));
     400             : 
     401           1 :         value = json_object_get(object.root, "empty");
     402           1 :         assert_true(json_is_string(value));
     403           1 :         s = json_string_value(value);
     404           1 :         assert_string_equal("", s);
     405             : 
     406           1 :         value = json_object_get(object.root, "empty-zero-len");
     407           1 :         assert_true(json_is_null(value));
     408             : 
     409           1 :         value = json_object_get(object.root, "value-greater-than-len");
     410           1 :         assert_true(json_is_string(value));
     411           1 :         s = json_string_value(value);
     412           1 :         assert_string_equal("abc", s);
     413           1 :         assert_int_equal(3, strlen(s));
     414             : 
     415           1 :         value = json_object_get(object.root, "value-equal-len");
     416           1 :         assert_true(json_is_string(value));
     417           1 :         s = json_string_value(value);
     418           1 :         assert_string_equal("ZYX", s);
     419           1 :         assert_int_equal(3, strlen(s));
     420             : 
     421           1 :         value = json_object_get(object.root, "value-len-is-zero");
     422           1 :         assert_true(json_is_null(value));
     423             : 
     424           1 :         object.valid = false;
     425           1 :         rc = json_add_stringn(&object, "fail-01", "xxxxxxx", 1);
     426           1 :         assert_int_equal(JSON_ERROR, rc);
     427             : 
     428           1 :         json_free(&object);
     429             : 
     430           1 :         rc = json_add_stringn(&object, "fail-02", "xxxxxxx", 1);
     431           1 :         assert_int_equal(JSON_ERROR, rc);
     432           1 : }
     433             : 
     434           1 : static void test_json_add_version(_UNUSED_ void **state)
     435             : {
     436           1 :         struct json_object object;
     437           1 :         struct json_t *version = NULL;
     438           1 :         struct json_t *v = NULL;
     439           1 :         double n;
     440           1 :         int rc;
     441             : 
     442           1 :         object = json_new_object();
     443           1 :         assert_false(json_is_invalid(&object));
     444           1 :         rc = json_add_version(&object, 3, 1);
     445           1 :         assert_int_equal(0, rc);
     446             : 
     447           1 :         assert_int_equal(1, json_object_size(object.root));
     448             : 
     449           1 :         version = json_object_get(object.root, "version");
     450           1 :         assert_true(json_is_object(version));
     451           1 :         assert_int_equal(2, json_object_size(version));
     452             : 
     453           1 :         v = json_object_get(version, "major");
     454           1 :         assert_true(json_is_integer(v));
     455           1 :         n = json_number_value(v);
     456           1 :         assert_true(n == 3.0);
     457             : 
     458           1 :         v = json_object_get(version, "minor");
     459           1 :         assert_true(json_is_integer(v));
     460           1 :         n = json_number_value(v);
     461           1 :         assert_true(n == 1.0);
     462             : 
     463           1 :         object.valid = false;
     464           1 :         rc = json_add_version(&object, 3, 1);
     465           1 :         assert_int_equal(JSON_ERROR, rc);
     466             : 
     467           1 :         json_free(&object);
     468             : 
     469           1 :         rc = json_add_version(&object, 3, 1);
     470           1 :         assert_int_equal(JSON_ERROR, rc);
     471           1 : }
     472             : 
     473           1 : static void test_json_add_address(_UNUSED_ void **state)
     474             : {
     475           1 :         struct json_object object;
     476           1 :         struct json_t *value = NULL;
     477           1 :         struct tsocket_address *ip4  = NULL;
     478           1 :         struct tsocket_address *ip6  = NULL;
     479           1 :         struct tsocket_address *pipe = NULL;
     480             : 
     481           1 :         struct tsocket_address *after = NULL;
     482           1 :         const char *s = NULL;
     483           1 :         int rc;
     484             : 
     485           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     486             : 
     487           1 :         object = json_new_object();
     488           1 :         assert_false(json_is_invalid(&object));
     489             : 
     490           1 :         rc = json_add_address(&object, "null", NULL);
     491           1 :         assert_int_equal(0, rc);
     492             : 
     493           1 :         rc = tsocket_address_inet_from_strings(
     494             :                 ctx,
     495             :                 "ip",
     496             :                 "127.0.0.1",
     497             :                 21,
     498             :                 &ip4);
     499           1 :         assert_int_equal(0, rc);
     500           1 :         rc = json_add_address(&object, "ip4", ip4);
     501           1 :         assert_int_equal(0, rc);
     502             : 
     503           1 :         rc = tsocket_address_inet_from_strings(
     504             :                 ctx,
     505             :                 "ip",
     506             :                 "2001:db8:0:0:1:0:0:1",
     507             :                 42,
     508             :                 &ip6);
     509           1 :         assert_int_equal(0, rc);
     510           1 :         rc = json_add_address(&object, "ip6", ip6);
     511           1 :         assert_int_equal(0, rc);
     512             : 
     513           1 :         rc = tsocket_address_unix_from_path(ctx, "/samba/pipe", &pipe);
     514           1 :         assert_int_equal(0, rc);
     515           1 :         rc = json_add_address(&object, "pipe", pipe);
     516           1 :         assert_int_equal(0, rc);
     517             : 
     518           1 :         assert_int_equal(4, json_object_size(object.root));
     519             : 
     520           1 :         value = json_object_get(object.root, "null");
     521           1 :         assert_true(json_is_null(value));
     522             : 
     523           1 :         value = json_object_get(object.root, "ip4");
     524           1 :         assert_true(json_is_string(value));
     525           1 :         s = json_string_value(value);
     526           1 :         assert_string_equal("ipv4:127.0.0.1:21", s);
     527             : 
     528           1 :         value = json_object_get(object.root, "ip6");
     529           1 :         assert_true(json_is_string(value));
     530           1 :         s = json_string_value(value);
     531           1 :         assert_string_equal("ipv6:2001:db8::1:0:0:1:42", s);
     532             : 
     533           1 :         value = json_object_get(object.root, "pipe");
     534           1 :         assert_true(json_is_string(value));
     535           1 :         s = json_string_value(value);
     536           1 :         assert_string_equal("unix:/samba/pipe", s);
     537             : 
     538           1 :         object.valid = false;
     539           1 :         rc = tsocket_address_inet_from_strings(
     540             :             ctx, "ip", "127.0.0.11", 23, &after);
     541           1 :         assert_int_equal(0, rc);
     542           1 :         rc = json_add_address(&object, "invalid_object", after);
     543           1 :         assert_int_equal(JSON_ERROR, rc);
     544             : 
     545           1 :         json_free(&object);
     546             : 
     547           1 :         rc = json_add_address(&object, "freed object", after);
     548           1 :         assert_int_equal(JSON_ERROR, rc);
     549             : 
     550           1 :         TALLOC_FREE(ctx);
     551           1 : }
     552             : 
     553           1 : static void test_json_add_sid(_UNUSED_ void **state)
     554             : {
     555           1 :         struct json_object object;
     556           1 :         struct json_t *value = NULL;
     557           1 :         const char *SID = "S-1-5-21-2470180966-3899876309-2637894779";
     558           1 :         struct dom_sid sid;
     559           1 :         const char *s = NULL;
     560           1 :         int rc;
     561             : 
     562           1 :         object = json_new_object();
     563           1 :         assert_false(json_is_invalid(&object));
     564             : 
     565           1 :         rc = json_add_sid(&object, "null", NULL);
     566           1 :         assert_int_equal(0, rc);
     567             : 
     568           1 :         assert_true(string_to_sid(&sid, SID));
     569           1 :         rc = json_add_sid(&object, "sid", &sid);
     570           1 :         assert_int_equal(0, rc);
     571             : 
     572           1 :         assert_int_equal(2, json_object_size(object.root));
     573             : 
     574           1 :         value = json_object_get(object.root, "null");
     575           1 :         assert_true(json_is_null(value));
     576             : 
     577           1 :         value = json_object_get(object.root, "sid");
     578           1 :         assert_true(json_is_string(value));
     579           1 :         s = json_string_value(value);
     580           1 :         assert_string_equal(SID, s);
     581             : 
     582           1 :         object.valid = false;
     583           1 :         rc = json_add_sid(&object, "invalid_object", &sid);
     584           1 :         assert_int_equal(JSON_ERROR, rc);
     585             : 
     586           1 :         json_free(&object);
     587             : 
     588           1 :         rc = json_add_sid(&object, "freed_object", &sid);
     589           1 :         assert_int_equal(JSON_ERROR, rc);
     590           1 : }
     591             : 
     592           1 : static void test_json_add_guid(_UNUSED_ void **state)
     593             : {
     594           1 :         struct json_object object;
     595           1 :         struct json_t *value = NULL;
     596           1 :         const char *GUID = "3ab88633-1e57-4c1a-856c-d1bc4b15bbb1";
     597           1 :         struct GUID guid;
     598           1 :         const char *s = NULL;
     599           1 :         NTSTATUS status;
     600           1 :         int rc;
     601             : 
     602           1 :         object = json_new_object();
     603           1 :         assert_false(json_is_invalid(&object));
     604             : 
     605           1 :         rc = json_add_guid(&object, "null", NULL);
     606           1 :         assert_int_equal(0, rc);
     607             : 
     608           1 :         status = GUID_from_string(GUID, &guid);
     609           1 :         assert_true(NT_STATUS_IS_OK(status));
     610           1 :         rc = json_add_guid(&object, "guid", &guid);
     611           1 :         assert_int_equal(0, rc);
     612             : 
     613           1 :         assert_int_equal(2, json_object_size(object.root));
     614             : 
     615           1 :         value = json_object_get(object.root, "null");
     616           1 :         assert_true(json_is_null(value));
     617             : 
     618           1 :         value = json_object_get(object.root, "guid");
     619           1 :         assert_true(json_is_string(value));
     620           1 :         s = json_string_value(value);
     621           1 :         assert_string_equal(GUID, s);
     622             : 
     623           1 :         object.valid = false;
     624           1 :         rc = json_add_guid(&object, "invalid_object", &guid);
     625           1 :         assert_int_equal(JSON_ERROR, rc);
     626             : 
     627           1 :         json_free(&object);
     628             : 
     629           1 :         rc = json_add_guid(&object, "freed_object", &guid);
     630           1 :         assert_int_equal(JSON_ERROR, rc);
     631           1 : }
     632             : 
     633           1 : static void test_json_to_string(_UNUSED_ void **state)
     634             : {
     635           1 :         struct json_object object;
     636           1 :         char *s = NULL;
     637           1 :         int rc;
     638             : 
     639           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     640             : 
     641           1 :         object = json_new_object();
     642           1 :         assert_false(json_is_invalid(&object));
     643             : 
     644           1 :         s = json_to_string(ctx, &object);
     645           1 :         assert_string_equal("{}", s);
     646           1 :         TALLOC_FREE(s);
     647             : 
     648           1 :         rc = json_add_string(&object, "name", "value");
     649           1 :         assert_int_equal(0, rc);
     650           1 :         s = json_to_string(ctx, &object);
     651           1 :         assert_string_equal("{\"name\": \"value\"}", s);
     652           1 :         TALLOC_FREE(s);
     653             : 
     654           1 :         object.valid = false;
     655           1 :         s = json_to_string(ctx, &object);
     656           1 :         assert_null(s);
     657             : 
     658           1 :         json_free(&object);
     659             : 
     660           1 :         object.valid = true;
     661           1 :         object.root = NULL;
     662             : 
     663           1 :         s = json_to_string(ctx, &object);
     664           1 :         assert_null(s);
     665           1 :         TALLOC_FREE(ctx);
     666           1 : }
     667             : 
     668           1 : static void test_json_get_array(_UNUSED_ void **state)
     669             : {
     670           1 :         struct json_object object;
     671           1 :         struct json_object array;
     672           1 :         struct json_object stored_array = json_new_array();
     673           1 :         json_t *value = NULL;
     674           1 :         json_t *o = NULL;
     675           1 :         struct json_object o1;
     676           1 :         struct json_object o2;
     677           1 :         int rc;
     678             : 
     679           1 :         assert_false(json_is_invalid(&stored_array));
     680             : 
     681           1 :         object = json_new_object();
     682           1 :         assert_false(json_is_invalid(&object));
     683             : 
     684           1 :         array = json_get_array(&object, "not-there");
     685           1 :         assert_true(array.valid);
     686           1 :         assert_non_null(array.root);
     687           1 :         assert_true(json_is_array(array.root));
     688           1 :         json_free(&array);
     689             : 
     690           1 :         o1 = json_new_object();
     691           1 :         assert_false(json_is_invalid(&o1));
     692           1 :         rc = json_add_string(&o1, "value", "value-one");
     693           1 :         assert_int_equal(0, rc);
     694           1 :         rc = json_add_object(&stored_array, NULL, &o1);
     695           1 :         assert_int_equal(0, rc);
     696           1 :         rc = json_add_object(&object, "stored_array", &stored_array);
     697           1 :         assert_int_equal(0, rc);
     698             : 
     699           1 :         array = json_get_array(&object, "stored_array");
     700           1 :         assert_true(array.valid);
     701           1 :         assert_non_null(array.root);
     702           1 :         assert_true(json_is_array(array.root));
     703             : 
     704           1 :         assert_int_equal(1, json_array_size(array.root));
     705             : 
     706           1 :         o = json_array_get(array.root, 0);
     707           1 :         assert_non_null(o);
     708           1 :         assert_true(json_is_object(o));
     709             : 
     710           1 :         value = json_object_get(o, "value");
     711           1 :         assert_non_null(value);
     712           1 :         assert_true(json_is_string(value));
     713             : 
     714           1 :         assert_string_equal("value-one", json_string_value(value));
     715           1 :         json_free(&array);
     716             : 
     717             :         /*
     718             :          * Now update the array and add it back to the object
     719             :          */
     720           1 :         array = json_get_array(&object, "stored_array");
     721           1 :         assert_true(json_is_array(array.root));
     722           1 :         o2 = json_new_object();
     723           1 :         assert_false(json_is_invalid(&o2));
     724           1 :         rc = json_add_string(&o2, "value", "value-two");
     725           1 :         assert_int_equal(0, rc);
     726           1 :         assert_true(o2.valid);
     727           1 :         rc = json_add_object(&array, NULL, &o2);
     728           1 :         assert_int_equal(0, rc);
     729           1 :         assert_true(json_is_array(array.root));
     730           1 :         rc = json_add_object(&object, "stored_array", &array);
     731           1 :         assert_int_equal(0, rc);
     732           1 :         assert_true(json_is_array(array.root));
     733             : 
     734           1 :         array = json_get_array(&object, "stored_array");
     735           1 :         assert_non_null(array.root);
     736           1 :         assert_true(json_is_array(array.root));
     737           1 :         assert_true(array.valid);
     738           1 :         assert_true(json_is_array(array.root));
     739             : 
     740           1 :         assert_int_equal(2, json_array_size(array.root));
     741             : 
     742           1 :         o = json_array_get(array.root, 0);
     743           1 :         assert_non_null(o);
     744           1 :         assert_true(json_is_object(o));
     745             : 
     746           1 :         assert_non_null(value);
     747           1 :         assert_true(json_is_string(value));
     748             : 
     749           1 :         assert_string_equal("value-one", json_string_value(value));
     750             : 
     751           1 :         o = json_array_get(array.root, 1);
     752           1 :         assert_non_null(o);
     753           1 :         assert_true(json_is_object(o));
     754             : 
     755           1 :         value = json_object_get(o, "value");
     756           1 :         assert_non_null(value);
     757           1 :         assert_true(json_is_string(value));
     758             : 
     759           1 :         assert_string_equal("value-two", json_string_value(value));
     760             : 
     761           1 :         json_free(&array);
     762           1 :         json_free(&object);
     763             : 
     764           1 :         array = json_get_array(&object, "stored_array");
     765           1 :         assert_false(array.valid);
     766           1 :         json_free(&array);
     767           1 : }
     768             : 
     769           1 : static void test_json_get_object(_UNUSED_ void **state)
     770             : {
     771           1 :         struct json_object object;
     772           1 :         struct json_object o1;
     773           1 :         struct json_object o2;
     774           1 :         struct json_object o3;
     775           1 :         json_t *value = NULL;
     776           1 :         int rc;
     777             : 
     778           1 :         object = json_new_object();
     779           1 :         assert_false(json_is_invalid(&object));
     780             : 
     781           1 :         o1 = json_get_object(&object, "not-there");
     782           1 :         assert_true(o1.valid);
     783           1 :         assert_non_null(o1.root);
     784           1 :         assert_true(json_is_object(o1.root));
     785           1 :         json_free(&o1);
     786             : 
     787           1 :         o1 = json_new_object();
     788           1 :         assert_false(json_is_invalid(&o1));
     789           1 :         rc = json_add_string(&o1, "value", "value-one");
     790           1 :         assert_int_equal(0, rc);
     791           1 :         rc = json_add_object(&object, "stored_object", &o1);
     792           1 :         assert_int_equal(0, rc);
     793             : 
     794           1 :         o2 = json_get_object(&object, "stored_object");
     795           1 :         assert_true(o2.valid);
     796           1 :         assert_non_null(o2.root);
     797           1 :         assert_true(json_is_object(o2.root));
     798             : 
     799           1 :         value = json_object_get(o2.root, "value");
     800           1 :         assert_non_null(value);
     801           1 :         assert_true(json_is_string(value));
     802             : 
     803           1 :         assert_string_equal("value-one", json_string_value(value));
     804             : 
     805           1 :         rc = json_add_string(&o2, "value", "value-two");
     806           1 :         assert_int_equal(0, rc);
     807           1 :         rc = json_add_object(&object, "stored_object", &o2);
     808           1 :         assert_int_equal(0, rc);
     809             : 
     810           1 :         o3 = json_get_object(&object, "stored_object");
     811           1 :         assert_true(o3.valid);
     812           1 :         assert_non_null(o3.root);
     813           1 :         assert_true(json_is_object(o3.root));
     814             : 
     815           1 :         value = json_object_get(o3.root, "value");
     816           1 :         assert_non_null(value);
     817           1 :         assert_true(json_is_string(value));
     818             : 
     819           1 :         assert_string_equal("value-two", json_string_value(value));
     820             : 
     821           1 :         json_free(&o3);
     822           1 :         json_free(&object);
     823             : 
     824           1 :         o3 = json_get_object(&object, "stored_object");
     825           1 :         assert_false(o3.valid);
     826           1 :         json_free(&o3);
     827           1 : }
     828             : 
     829           1 : static void test_audit_get_timestamp(_UNUSED_ void **state)
     830             : {
     831           1 :         const char *t = NULL;
     832           1 :         char *c;
     833           1 :         struct tm tm = {};
     834           1 :         time_t before;
     835           1 :         time_t after;
     836           1 :         time_t actual;
     837           1 :         struct timeval tv;
     838           1 :         int ret;
     839           1 :         char *env_tz = NULL;
     840           1 :         char *orig_tz = NULL;
     841             : 
     842           1 :         TALLOC_CTX *ctx = talloc_new(NULL);
     843             : 
     844             :         /*
     845             :          * Explicitly set the time zone to UTC to make the test easier
     846             :          */
     847           1 :         env_tz = getenv("TZ");
     848           1 :         if (env_tz != NULL) {
     849           1 :                 orig_tz = talloc_strdup(ctx, env_tz);
     850             :         }
     851           1 :         setenv("TZ", "UTC", 1);
     852             : 
     853           1 :         ret = gettimeofday(&tv, NULL);
     854           1 :         assert_int_equal(0, ret);
     855           1 :         before = tv.tv_sec;
     856             : 
     857           1 :         t = audit_get_timestamp(ctx);
     858             : 
     859           1 :         ret = gettimeofday(&tv, NULL);
     860           1 :         assert_int_equal(0, ret);
     861           1 :         after = tv.tv_sec;
     862             : 
     863           1 :         c = strptime(t, "%a, %d %b %Y %H:%M:%S", &tm);
     864             : 
     865             :         /*
     866             :          * Restore the time zone if we changed it
     867             :          */
     868           1 :         if (orig_tz != NULL) {
     869           1 :                 setenv("TZ", orig_tz, 1);
     870           1 :                 TALLOC_FREE(orig_tz);
     871             :         }
     872             : 
     873           1 :         assert_non_null(c);
     874           1 :         tm.tm_isdst = -1;
     875           1 :         if (c != NULL && *c == '.') {
     876           1 :                 char *e;
     877           1 :                 strtod(c, &e);
     878           1 :                 c = e;
     879             :         }
     880           1 :         if (c != NULL && *c == ' ') {
     881           1 :                 assert_string_equal(" UTC", c);
     882           1 :                 c += 4;
     883             :         }
     884           1 :         assert_int_equal(0, strlen(c));
     885             : 
     886           1 :         actual = mktime(&tm);
     887             : 
     888             :         /*
     889             :          * The timestamp should be before <= actual <= after
     890             :          */
     891           1 :         assert_true(difftime(actual, before) >= 0);
     892           1 :         assert_true(difftime(after, actual) >= 0);
     893             : 
     894           1 :         TALLOC_FREE(ctx);
     895           1 : }
     896             : 
     897           1 : int main(_UNUSED_ int argc, _UNUSED_ const char **argv)
     898             : {
     899           1 :         const struct CMUnitTest tests[] = {
     900             :                 cmocka_unit_test(test_json_add_int),
     901             :                 cmocka_unit_test(test_json_add_bool),
     902             :                 cmocka_unit_test(test_json_add_string),
     903             :                 cmocka_unit_test(test_json_add_object),
     904             :                 cmocka_unit_test(test_json_add_to_array),
     905             :                 cmocka_unit_test(test_json_add_timestamp),
     906             :                 cmocka_unit_test(test_json_add_stringn),
     907             :                 cmocka_unit_test(test_json_add_version),
     908             :                 cmocka_unit_test(test_json_add_address),
     909             :                 cmocka_unit_test(test_json_add_sid),
     910             :                 cmocka_unit_test(test_json_add_guid),
     911             :                 cmocka_unit_test(test_json_to_string),
     912             :                 cmocka_unit_test(test_json_get_array),
     913             :                 cmocka_unit_test(test_json_get_object),
     914             :                 cmocka_unit_test(test_audit_get_timestamp),
     915             :         };
     916             : 
     917           1 :         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
     918           1 :         return cmocka_run_group_tests(tests, NULL, NULL);
     919             : }

Generated by: LCOV version 1.14