LCOV - code coverage report
Current view: top level - lib/audit_logging/tests - audit_logging_error_test.c (source / functions) Hit Total Coverage
Test: coverage report for fix-15632 9995c5c2 Lines: 399 403 99.0 %
Date: 2024-04-13 12:30:31 Functions: 30 30 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             :  * Unit tests for lib/audit_logging/audit_logging.c
      38             :  *
      39             :  * These tests exercise the error handling code and mock the jannson functions
      40             :  * to trigger errors.
      41             :  *
      42             :  */
      43             : #include <stdarg.h>
      44             : #include <stddef.h>
      45             : #include <setjmp.h>
      46             : #include <cmocka.h>
      47             : 
      48             : #include "includes.h"
      49             : 
      50             : #include "librpc/ndr/libndr.h"
      51             : #include "lib/tsocket/tsocket.h"
      52             : #include "libcli/security/dom_sid.h"
      53             : #include "lib/messaging/messaging.h"
      54             : #include "auth/common_auth.h"
      55             : 
      56             : #include "lib/audit_logging/audit_logging.h"
      57             : 
      58             : const int JANSSON_FAILURE = -1;
      59             : const int CALL_ORIG = -2;
      60             : 
      61             : /*
      62             :  * cmocka wrappers for json_object
      63             :  */
      64             : json_t *__wrap_json_object(void);
      65             : json_t *__real_json_object(void);
      66          24 : json_t *__wrap_json_object(void)
      67             : {
      68             : 
      69          24 :         bool fail = (bool)mock();
      70          24 :         if (fail) {
      71             :                 return NULL;
      72             :         }
      73          23 :         return __real_json_object();
      74             : }
      75             : 
      76             : /*
      77             :  * cmocka wrappers for json_array
      78             :  */
      79             : json_t *__wrap_json_array(void);
      80             : json_t *__real_json_array(void);
      81           4 : json_t *__wrap_json_array(void)
      82             : {
      83             : 
      84           4 :         bool fail = (bool)mock();
      85           4 :         if (fail) {
      86             :                 return NULL;
      87             :         }
      88           3 :         return __real_json_array();
      89             : }
      90             : 
      91             : /*
      92             :  * cmoka wrappers for json_integer
      93             :  */
      94             : json_t *__wrap_json_integer(json_int_t value);
      95             : json_t *__real_json_integer(json_int_t value);
      96           7 : json_t *__wrap_json_integer(json_int_t value)
      97             : {
      98             : 
      99           7 :         bool fail = (bool)mock();
     100           7 :         if (fail) {
     101             :                 return NULL;
     102             :         }
     103           6 :         return __real_json_integer(value);
     104             : }
     105             : 
     106             : /*
     107             :  * cmocka wrappers for json_string
     108             :  */
     109             : json_t *__wrap_json_string(const char *value);
     110             : json_t *__real_json_string(const char *value);
     111           7 : json_t *__wrap_json_string(const char *value)
     112             : {
     113             : 
     114           7 :         bool fail = (bool)mock();
     115           7 :         if (fail) {
     116             :                 return NULL;
     117             :         }
     118           5 :         return __real_json_string(value);
     119             : }
     120             : 
     121             : /*
     122             :  * cmocka wrappers for json_stringn
     123             :  */
     124             : json_t *__wrap_json_stringn(const char *value, size_t len);
     125             : json_t *__real_json_stringn(const char *value, size_t len);
     126           2 : json_t *__wrap_json_stringn(const char *value, size_t len)
     127             : {
     128             : 
     129           2 :         bool fail = (bool)mock();
     130           2 :         if (fail) {
     131             :                 return NULL;
     132             :         }
     133           1 :         return __real_json_stringn(value, len);
     134             : }
     135             : 
     136             : /*
     137             :  * cmocka wrappers for json_dumps
     138             :  */
     139             : char *__wrap_json_dumps(const json_t *json, size_t flags);
     140             : char *__real_json_dumps(const json_t *json, size_t flags);
     141           2 : char *__wrap_json_dumps(const json_t *json, size_t flags)
     142             : {
     143             : 
     144           2 :         bool fail = (bool)mock();
     145           2 :         if (fail) {
     146             :                 return NULL;
     147             :         }
     148           1 :         return __real_json_dumps(json, flags);
     149             : }
     150             : 
     151             : /*
     152             :  * cmocka wrappers for json_object_set_new
     153             :  */
     154             : int __wrap_json_object_set_new(json_t *object, const char *key, json_t *value);
     155             : int __real_json_object_set_new(json_t *object, const char *key, json_t *value);
     156          23 : int __wrap_json_object_set_new(json_t *object, const char *key, json_t *value)
     157             : {
     158          23 :         int rc = (int)mock();
     159          23 :         if (rc != CALL_ORIG) {
     160             :                 return rc;
     161             :         }
     162           4 :         return __real_json_object_set_new(object, key, value);
     163             : }
     164             : 
     165             : /*
     166             :  * cmocka wrappers for json_array_append_new
     167             :  */
     168             : int __wrap_json_array_append_new(json_t *object,
     169             :                                  const char *key,
     170             :                                  json_t *value);
     171             : int __real_json_array_append_new(json_t *object,
     172             :                                  const char *key,
     173             :                                  json_t *value);
     174           2 : int __wrap_json_array_append_new(json_t *object, const char *key, json_t *value)
     175             : {
     176           2 :         int rc = (int)mock();
     177           2 :         if (rc != CALL_ORIG) {
     178             :                 return rc;
     179             :         }
     180           0 :         return __real_json_array_append_new(object, key, value);
     181             : }
     182             : 
     183             : /*
     184             :  * cmocka wrappers for json_array_extend
     185             :  */
     186             : int __wrap_json_array_extend(json_t *array, json_t *other_array);
     187             : int __real_json_array_extend(json_t *array, json_t *other_array);
     188           1 : int __wrap_json_array_extend(json_t *array, json_t *other_array)
     189             : {
     190             : 
     191           1 :         int rc = (int)mock();
     192           1 :         if (rc != CALL_ORIG) {
     193             :                 return rc;
     194             :         }
     195           0 :         return __real_json_array_extend(array, other_array);
     196             : }
     197             : 
     198             : /*
     199             :  * cmocka wrappers for json_object_update
     200             :  */
     201             : int __wrap_json_object_update(json_t *object, json_t *other_object);
     202             : int __real_json_object_update(json_t *object, json_t *other_object);
     203           1 : int __wrap_json_object_update(json_t *object, json_t *other_object)
     204             : {
     205             : 
     206           1 :         int rc = (int)mock();
     207           1 :         if (rc != CALL_ORIG) {
     208             :                 return rc;
     209             :         }
     210           0 :         return __real_json_array_extend(object, other_object);
     211             : }
     212             : 
     213             : /*
     214             :  * cmocka wrappers for gettimeofday
     215             :  */
     216             : int __wrap_gettimeofday(struct timeval *tv, struct timezone *tz);
     217             : int __real_gettimeofday(struct timeval *tv, struct timezone *tz);
     218           4 : int __wrap_gettimeofday(struct timeval *tv, struct timezone *tz)
     219             : {
     220             : 
     221           4 :         int rc = (int)mock();
     222           4 :         if (rc != 0) {
     223             :                 return rc;
     224             :         }
     225           3 :         return __real_gettimeofday(tv, tz);
     226             : }
     227             : 
     228             : /*
     229             :  * cmocka wrappers for localtime
     230             :  */
     231             : struct tm *__wrap_localtime(const time_t *timep);
     232             : struct tm *__real_localtime(const time_t *timep);
     233           3 : struct tm *__wrap_localtime(const time_t *timep)
     234             : {
     235           3 :         bool fail = (bool)mock();
     236           3 :         if (fail) {
     237             :                 return NULL;
     238             :         }
     239           2 :         return __real_localtime(timep);
     240             : }
     241             : 
     242             : /*
     243             :  * cmocka wrappers for talloc_named_const
     244             :  */
     245             : static const void *REAL_TALLOC = "Here";
     246             : 
     247             : void *__wrap_talloc_named_const(const void *context,
     248             :                                 size_t size,
     249             :                                 const char *name);
     250             : void *__real_talloc_named_const(const void *context,
     251             :                                 size_t size,
     252             :                                 const char *name);
     253           5 : void *__wrap_talloc_named_const(const void *context,
     254             :                                 size_t size,
     255             :                                 const char *name)
     256             : {
     257             : 
     258           5 :         void *ret = (void *)mock();
     259             : 
     260           5 :         if (ret == NULL) {
     261             :                 return NULL;
     262             :         }
     263           4 :         return __real_talloc_named_const(context, size, name);
     264             : }
     265             : 
     266             : /*
     267             :  * cmocka wrappers for talloc_strdup
     268             :  */
     269             : char *__wrap_talloc_strdup(const void *t, const char *p);
     270             : char *__real_talloc_strdup(const void *t, const char *p);
     271           1 : char *__wrap_talloc_strdup(const void *t, const char *p)
     272             : {
     273             : 
     274           1 :         void *ret = (void *)mock();
     275             : 
     276           1 :         if (ret == NULL) {
     277             :                 return NULL;
     278             :         }
     279           0 :         return __real_talloc_strdup(t, p);
     280             : }
     281             : 
     282             : char *__wrap_tsocket_address_string(const struct tsocket_address *addr,
     283             :                                     TALLOC_CTX *mem_ctx);
     284             : char *__real_tsocket_address_string(const struct tsocket_address *addr,
     285             :                                     TALLOC_CTX *mem_ctx);
     286           2 : char *__wrap_tsocket_address_string(const struct tsocket_address *addr,
     287             :                                     TALLOC_CTX *mem_ctx)
     288             : {
     289             : 
     290           2 :         bool fail = (bool)mock();
     291           2 :         if (fail) {
     292             :                 return NULL;
     293             :         }
     294           1 :         return __real_tsocket_address_string(addr, mem_ctx);
     295             : }
     296             : 
     297           1 : static void test_json_add_int(_UNUSED_ void **state)
     298             : {
     299           1 :         struct json_object object;
     300           1 :         int rc = 0;
     301             : 
     302           1 :         will_return(__wrap_json_object, false);
     303           1 :         object = json_new_object();
     304           1 :         assert_false(json_is_invalid(&object));
     305             : 
     306             :         /*
     307             :          * Test json integer failure
     308             :          */
     309           1 :         will_return(__wrap_json_integer, true);
     310           1 :         rc = json_add_int(&object, "name", 2);
     311             : 
     312           1 :         assert_false(json_is_invalid(&object));
     313           1 :         assert_int_equal(JSON_ERROR, rc);
     314             : 
     315             :         /*
     316             :          * Test json object set new failure
     317             :          */
     318           1 :         will_return(__wrap_json_integer, false);
     319           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     320           1 :         rc = json_add_int(&object, "name", 2);
     321             : 
     322           1 :         assert_false(json_is_invalid(&object));
     323           1 :         assert_int_equal(JSON_ERROR, rc);
     324           1 :         json_free(&object);
     325           1 : }
     326             : 
     327           1 : static void test_json_add_bool(_UNUSED_ void **state)
     328             : {
     329           1 :         struct json_object object;
     330           1 :         int rc = 0;
     331             : 
     332           1 :         will_return(__wrap_json_object, false);
     333           1 :         object = json_new_object();
     334           1 :         assert_false(json_is_invalid(&object));
     335             : 
     336             :         /*
     337             :          * json_boolean does not return an error code.
     338             :          * Test json object set new failure
     339             :          */
     340           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     341           1 :         rc = json_add_bool(&object, "name", true);
     342             : 
     343           1 :         assert_false(json_is_invalid(&object));
     344           1 :         assert_int_equal(JSON_ERROR, rc);
     345             : 
     346           1 :         json_free(&object);
     347           1 : }
     348             : 
     349           1 : static void test_json_add_string(_UNUSED_ void **state)
     350             : {
     351           1 :         struct json_object object;
     352           1 :         int rc = 0;
     353             : 
     354           1 :         will_return(__wrap_json_object, false);
     355           1 :         object = json_new_object();
     356           1 :         assert_false(json_is_invalid(&object));
     357             : 
     358             :         /*
     359             :          * Test json string failure
     360             :          */
     361           1 :         will_return(__wrap_json_string, true);
     362           1 :         rc = json_add_string(&object, "name", "value");
     363             : 
     364           1 :         assert_false(json_is_invalid(&object));
     365           1 :         assert_int_equal(JSON_ERROR, rc);
     366             : 
     367             :         /*
     368             :          * Test json object set new failure
     369             :          */
     370           1 :         will_return(__wrap_json_string, false);
     371           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     372           1 :         rc = json_add_string(&object, "name", "value");
     373             : 
     374           1 :         assert_false(json_is_invalid(&object));
     375           1 :         assert_int_equal(JSON_ERROR, rc);
     376             : 
     377             :         /*
     378             :          * Test json object set new failure for a NULL string
     379             :          */
     380           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     381           1 :         rc = json_add_string(&object, "null", NULL);
     382             : 
     383           1 :         assert_false(json_is_invalid(&object));
     384           1 :         assert_int_equal(JSON_ERROR, rc);
     385             : 
     386           1 :         json_free(&object);
     387           1 : }
     388             : 
     389           1 : static void test_json_add_object(_UNUSED_ void **state)
     390             : {
     391           1 :         struct json_object object;
     392           1 :         struct json_object value;
     393           1 :         int rc = 0;
     394             : 
     395           1 :         will_return(__wrap_json_object, false);
     396           1 :         will_return(__wrap_json_object, false);
     397             : 
     398           1 :         object = json_new_object();
     399           1 :         assert_false(json_is_invalid(&object));
     400             : 
     401           1 :         value = json_new_object();
     402           1 :         assert_false(json_is_invalid(&value));
     403             : 
     404             :         /*
     405             :          * Test json object set new failure
     406             :          */
     407           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     408           1 :         rc = json_add_object(&object, "name", &value);
     409             : 
     410           1 :         assert_false(json_is_invalid(&object));
     411           1 :         assert_false(json_is_invalid(&value));
     412           1 :         assert_int_equal(JSON_ERROR, rc);
     413             : 
     414             :         /*
     415             :          * Test json object set new failure for a NULL value
     416             :          */
     417           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     418           1 :         rc = json_add_object(&object, "null", NULL);
     419             : 
     420           1 :         assert_false(json_is_invalid(&object));
     421           1 :         assert_int_equal(JSON_ERROR, rc);
     422             : 
     423           1 :         json_free(&object);
     424           1 :         json_free(&value);
     425           1 : }
     426             : 
     427           1 : static void test_json_add_to_array(_UNUSED_ void **state)
     428             : {
     429           1 :         struct json_object array;
     430           1 :         struct json_object value;
     431           1 :         int rc = 0;
     432             : 
     433           1 :         will_return(__wrap_json_array, false);
     434           1 :         will_return(__wrap_json_object, false);
     435             : 
     436           1 :         array = json_new_array();
     437           1 :         assert_false(json_is_invalid(&array));
     438             : 
     439           1 :         value = json_new_object();
     440           1 :         assert_false(json_is_invalid(&value));
     441             : 
     442             :         /*
     443             :          * Test json array append new failure
     444             :          */
     445           1 :         will_return(__wrap_json_array_append_new, JANSSON_FAILURE);
     446           1 :         rc = json_add_object(&array, "name", &value);
     447             : 
     448           1 :         assert_false(json_is_invalid(&array));
     449           1 :         assert_false(json_is_invalid(&value));
     450           1 :         assert_int_equal(JSON_ERROR, rc);
     451             : 
     452             :         /*
     453             :          * Test json append new failure with a NULL value
     454             :          */
     455           1 :         will_return(__wrap_json_array_append_new, JANSSON_FAILURE);
     456           1 :         rc = json_add_object(&array, "null", NULL);
     457             : 
     458           1 :         assert_false(json_is_invalid(&array));
     459           1 :         assert_int_equal(JSON_ERROR, rc);
     460             : 
     461           1 :         json_free(&array);
     462           1 :         json_free(&value);
     463           1 : }
     464             : 
     465           1 : static void test_json_add_timestamp(_UNUSED_ void **state)
     466             : {
     467           1 :         struct json_object object;
     468           1 :         int rc = 0;
     469             : 
     470           1 :         will_return(__wrap_json_object, false);
     471           1 :         object = json_new_object();
     472           1 :         assert_false(json_is_invalid(&object));
     473             : 
     474             :         /*
     475             :          * Test json string failure
     476             :          */
     477           1 :         will_return(__wrap_gettimeofday, 0);
     478           1 :         will_return(__wrap_localtime, false);
     479           1 :         will_return(__wrap_json_string, true);
     480           1 :         rc = json_add_timestamp(&object);
     481             : 
     482             :         /*
     483             :          * Test json_object_set_new failure
     484             :          */
     485           1 :         will_return(__wrap_gettimeofday, 0);
     486           1 :         will_return(__wrap_localtime, false);
     487           1 :         will_return(__wrap_json_string, false);
     488           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     489           1 :         rc = json_add_timestamp(&object);
     490             : 
     491           1 :         assert_false(json_is_invalid(&object));
     492           1 :         assert_int_equal(JSON_ERROR, rc);
     493             : 
     494             :         /*
     495             :          * Test gettimeofday failure
     496             :          */
     497           1 :         will_return(__wrap_gettimeofday, -1);
     498           1 :         rc = json_add_timestamp(&object);
     499             : 
     500           1 :         assert_false(json_is_invalid(&object));
     501           1 :         assert_int_equal(JSON_ERROR, rc);
     502             : 
     503             :         /*
     504             :          * Test local time failure
     505             :          */
     506           1 :         will_return(__wrap_gettimeofday, 0);
     507           1 :         will_return(__wrap_localtime, true);
     508           1 :         rc = json_add_timestamp(&object);
     509             : 
     510           1 :         assert_false(json_is_invalid(&object));
     511           1 :         assert_int_equal(JSON_ERROR, rc);
     512             : 
     513           1 :         json_free(&object);
     514           1 : }
     515             : 
     516           1 : static void test_json_add_stringn(_UNUSED_ void **state)
     517             : {
     518           1 :         struct json_object object;
     519           1 :         int rc = 0;
     520             : 
     521           1 :         will_return(__wrap_json_object, false);
     522           1 :         object = json_new_object();
     523           1 :         assert_false(json_is_invalid(&object));
     524             : 
     525             :         /*
     526             :          * Test json string failure
     527             :          */
     528           1 :         will_return(__wrap_json_stringn, true);
     529           1 :         rc = json_add_stringn(&object, "name", "value", 3);
     530             : 
     531           1 :         assert_false(json_is_invalid(&object));
     532           1 :         assert_int_equal(JSON_ERROR, rc);
     533             : 
     534             :         /*
     535             :          * Test json object set new failure
     536             :          */
     537           1 :         will_return(__wrap_json_stringn, false);
     538           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     539           1 :         rc = json_add_stringn(&object, "name", "value", 3);
     540             : 
     541           1 :         assert_false(json_is_invalid(&object));
     542           1 :         assert_int_equal(JSON_ERROR, rc);
     543             : 
     544             :         /*
     545             :          * Test json object set new failure for a NULL string
     546             :          */
     547           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     548           1 :         rc = json_add_stringn(&object, "null", NULL, 2);
     549             : 
     550           1 :         assert_false(json_is_invalid(&object));
     551           1 :         assert_int_equal(JSON_ERROR, rc);
     552             : 
     553             :         /*
     554             :          * Test json object set new failure for a zero string size
     555             :          */
     556           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     557           1 :         rc = json_add_stringn(&object, "zero", "no value", 0);
     558             : 
     559           1 :         assert_false(json_is_invalid(&object));
     560           1 :         assert_int_equal(JSON_ERROR, rc);
     561           1 :         json_free(&object);
     562           1 : }
     563             : 
     564           1 : static void test_json_add_version(_UNUSED_ void **state)
     565             : {
     566           1 :         struct json_object object;
     567           1 :         int rc = 0;
     568             : 
     569             :         /*
     570             :          * Fail creating the version object
     571             :          */
     572           1 :         will_return(__wrap_json_object, false);
     573           1 :         object = json_new_object();
     574           1 :         assert_false(json_is_invalid(&object));
     575             : 
     576           1 :         will_return(__wrap_json_object, true);
     577           1 :         rc = json_add_version(&object, 1, 11);
     578             : 
     579           1 :         assert_false(json_is_invalid(&object));
     580           1 :         assert_int_equal(JSON_ERROR, rc);
     581             : 
     582           1 :         assert_false(json_is_invalid(&object));
     583           1 :         assert_int_equal(JSON_ERROR, rc);
     584             : 
     585           1 :         json_free(&object);
     586             : 
     587             :         /*
     588             :          * Fail adding the major version
     589             :          */
     590           1 :         will_return(__wrap_json_object, false);
     591           1 :         object = json_new_object();
     592           1 :         assert_false(json_is_invalid(&object));
     593             : 
     594           1 :         will_return(__wrap_json_object, false);
     595           1 :         will_return(__wrap_json_integer, false);
     596           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     597           1 :         rc = json_add_version(&object, 2, 12);
     598             : 
     599           1 :         assert_false(json_is_invalid(&object));
     600           1 :         assert_int_equal(JSON_ERROR, rc);
     601             : 
     602           1 :         assert_false(json_is_invalid(&object));
     603           1 :         assert_int_equal(JSON_ERROR, rc);
     604             : 
     605           1 :         json_free(&object);
     606             : 
     607             :         /*
     608             :          * Fail adding the minor version
     609             :          */
     610           1 :         will_return(__wrap_json_object, false);
     611           1 :         object = json_new_object();
     612           1 :         assert_false(json_is_invalid(&object));
     613             : 
     614           1 :         will_return(__wrap_json_object, false);
     615           1 :         will_return(__wrap_json_integer, false);
     616           1 :         will_return(__wrap_json_object_set_new, CALL_ORIG);
     617           1 :         will_return(__wrap_json_integer, false);
     618           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     619           1 :         rc = json_add_version(&object, 3, 13);
     620             : 
     621           1 :         assert_false(json_is_invalid(&object));
     622           1 :         assert_int_equal(JSON_ERROR, rc);
     623             : 
     624           1 :         assert_false(json_is_invalid(&object));
     625           1 :         assert_int_equal(JSON_ERROR, rc);
     626             : 
     627           1 :         json_free(&object);
     628             : 
     629             :         /*
     630             :          * Fail adding the version object
     631             :          */
     632           1 :         will_return(__wrap_json_object, false);
     633           1 :         object = json_new_object();
     634           1 :         assert_false(json_is_invalid(&object));
     635             : 
     636           1 :         will_return(__wrap_json_object, false);
     637           1 :         will_return(__wrap_json_integer, false);
     638           1 :         will_return(__wrap_json_object_set_new, CALL_ORIG);
     639           1 :         will_return(__wrap_json_integer, false);
     640           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     641           1 :         rc = json_add_version(&object, 4, 14);
     642             : 
     643           1 :         assert_false(json_is_invalid(&object));
     644           1 :         assert_int_equal(JSON_ERROR, rc);
     645             : 
     646           1 :         json_free(&object);
     647           1 : }
     648             : 
     649           1 : static void test_json_add_address(_UNUSED_ void **state)
     650             : {
     651           1 :         struct json_object object;
     652           1 :         int rc = 0;
     653           1 :         struct tsocket_address *ip = NULL;
     654             : 
     655           1 :         TALLOC_CTX *ctx = NULL;
     656             : 
     657           1 :         will_return(__wrap_talloc_named_const, REAL_TALLOC);
     658           1 :         ctx = talloc_new(NULL);
     659             : 
     660             :         /*
     661             :          * Add a null address
     662             :          */
     663           1 :         will_return(__wrap_json_object, false);
     664           1 :         object = json_new_object();
     665           1 :         assert_false(json_is_invalid(&object));
     666             : 
     667           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     668           1 :         rc = json_add_address(&object, "name", NULL);
     669             : 
     670           1 :         assert_false(json_is_invalid(&object));
     671           1 :         assert_int_equal(JSON_ERROR, rc);
     672             : 
     673             :         /*
     674             :          * Add a non null address, json_object_set_new failure
     675             :          */
     676           1 :         rc = tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 21, &ip);
     677           1 :         assert_int_equal(0, rc);
     678             : 
     679           1 :         will_return(__wrap_talloc_named_const, REAL_TALLOC);
     680           1 :         will_return(__wrap_tsocket_address_string, false);
     681           1 :         will_return(__wrap_json_string, false);
     682           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     683           1 :         rc = json_add_address(&object, "name", ip);
     684             : 
     685           1 :         assert_false(json_is_invalid(&object));
     686           1 :         assert_int_equal(JSON_ERROR, rc);
     687             : 
     688             :         /*
     689             :          * Add a non null address, with a talloc failure
     690             :          */
     691           1 :         rc = tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 21, &ip);
     692           1 :         assert_int_equal(0, rc);
     693             : 
     694           1 :         will_return(__wrap_talloc_named_const, NULL);
     695           1 :         rc = json_add_address(&object, "name", ip);
     696             : 
     697           1 :         assert_false(json_is_invalid(&object));
     698           1 :         assert_int_equal(JSON_ERROR, rc);
     699             : 
     700             :         /*
     701             :          * Add a non null address, tsocket_address_string failure
     702             :          */
     703           1 :         rc = tsocket_address_inet_from_strings(ctx, "ip", "127.0.0.1", 21, &ip);
     704           1 :         assert_int_equal(0, rc);
     705             : 
     706           1 :         will_return(__wrap_talloc_named_const, REAL_TALLOC);
     707           1 :         will_return(__wrap_tsocket_address_string, true);
     708           1 :         rc = json_add_address(&object, "name", ip);
     709             : 
     710           1 :         assert_false(json_is_invalid(&object));
     711           1 :         assert_int_equal(JSON_ERROR, rc);
     712             : 
     713           1 :         TALLOC_FREE(ctx);
     714           1 :         json_free(&object);
     715           1 : }
     716             : 
     717           1 : static void test_json_add_sid(void **state)
     718             : {
     719           1 :         struct json_object object;
     720           1 :         const char *SID = "S-1-5-21-2470180966-3899876309-2637894779";
     721           1 :         struct dom_sid sid;
     722           1 :         int rc;
     723             : 
     724             :         /*
     725             :          * Add a null SID
     726             :          */
     727           1 :         will_return(__wrap_json_object, false);
     728           1 :         object = json_new_object();
     729           1 :         assert_false(json_is_invalid(&object));
     730             : 
     731           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     732           1 :         rc = json_add_sid(&object, "null", NULL);
     733           1 :         assert_int_equal(JSON_ERROR, rc);
     734             : 
     735             :         /*
     736             :          * Add a non null SID
     737             :          */
     738           1 :         assert_true(string_to_sid(&sid, SID));
     739           1 :         will_return(__wrap_json_string, false);
     740           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     741           1 :         rc = json_add_sid(&object, "sid", &sid);
     742           1 :         assert_int_equal(JSON_ERROR, rc);
     743             : 
     744           1 :         json_free(&object);
     745           1 : }
     746             : 
     747           1 : static void test_json_add_guid(void **state)
     748             : {
     749           1 :         struct json_object object;
     750           1 :         const char *GUID = "3ab88633-1e57-4c1a-856c-d1bc4b15bbb1";
     751           1 :         struct GUID guid;
     752           1 :         NTSTATUS status;
     753           1 :         int rc;
     754             : 
     755             :         /*
     756             :          * Add a null GUID
     757             :          */
     758           1 :         will_return(__wrap_json_object, false);
     759           1 :         object = json_new_object();
     760           1 :         assert_false(json_is_invalid(&object));
     761             : 
     762           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     763           1 :         rc = json_add_guid(&object, "null", NULL);
     764           1 :         assert_int_equal(JSON_ERROR, rc);
     765             : 
     766             :         /*
     767             :          * Add a non null GUID
     768             :          */
     769           1 :         status = GUID_from_string(GUID, &guid);
     770           1 :         assert_true(NT_STATUS_IS_OK(status));
     771           1 :         will_return(__wrap_json_string, false);
     772           1 :         will_return(__wrap_json_object_set_new, JANSSON_FAILURE);
     773           1 :         rc = json_add_guid(&object, "guid", &guid);
     774           1 :         assert_int_equal(JSON_ERROR, rc);
     775             : 
     776           1 :         json_free(&object);
     777           1 : }
     778             : 
     779           1 : static void test_json_to_string(_UNUSED_ void **state)
     780             : {
     781           1 :         struct json_object object;
     782           1 :         char *s = NULL;
     783           1 :         TALLOC_CTX *ctx = NULL;
     784             : 
     785           1 :         will_return(__wrap_talloc_named_const, REAL_TALLOC);
     786           1 :         ctx = talloc_new(NULL);
     787             : 
     788           1 :         will_return(__wrap_json_object, false);
     789           1 :         object = json_new_object();
     790           1 :         assert_false(json_is_invalid(&object));
     791             : 
     792             :         /*
     793             :          * json_dumps failure
     794             :          */
     795           1 :         will_return(__wrap_json_dumps, true);
     796           1 :         s = json_to_string(ctx, &object);
     797           1 :         assert_null(s);
     798             : 
     799             :         /*
     800             :          * talloc failure
     801             :          */
     802           1 :         will_return(__wrap_json_dumps, false);
     803           1 :         will_return(__wrap_talloc_strdup, NULL);
     804           1 :         s = json_to_string(ctx, &object);
     805           1 :         assert_null(s);
     806           1 :         TALLOC_FREE(ctx);
     807           1 :         json_free(&object);
     808           1 : }
     809             : 
     810           1 : static void test_json_get_array(_UNUSED_ void **state)
     811             : {
     812           1 :         struct json_object object;
     813           1 :         struct json_object stored_array;
     814           1 :         struct json_object array;
     815             : 
     816           1 :         int rc;
     817             : 
     818           1 :         will_return(__wrap_json_object, false);
     819           1 :         object = json_new_object();
     820           1 :         assert_false(json_is_invalid(&object));
     821             : 
     822           1 :         will_return(__wrap_json_array, false);
     823           1 :         stored_array = json_new_array();
     824           1 :         assert_false(json_is_invalid(&stored_array));
     825             : 
     826           1 :         will_return(__wrap_json_object_set_new, CALL_ORIG);
     827           1 :         rc = json_add_object(&object, "array", &stored_array);
     828           1 :         assert_int_equal(0, rc);
     829             : 
     830             :         /*
     831             :          * json array failure
     832             :          */
     833           1 :         will_return(__wrap_json_array, true);
     834           1 :         array = json_get_array(&object, "array");
     835           1 :         assert_true(json_is_invalid(&array));
     836             : 
     837             :         /*
     838             :          * json array extend failure
     839             :          */
     840           1 :         will_return(__wrap_json_array, false);
     841           1 :         will_return(__wrap_json_array_extend, true);
     842           1 :         array = json_get_array(&object, "array");
     843           1 :         assert_true(json_is_invalid(&array));
     844             : 
     845           1 :         json_free(&stored_array);
     846           1 :         json_free(&object);
     847           1 : }
     848             : 
     849           1 : static void test_json_get_object(_UNUSED_ void **state)
     850             : {
     851           1 :         struct json_object object;
     852           1 :         struct json_object stored;
     853           1 :         struct json_object retrieved;
     854             : 
     855           1 :         int rc;
     856             : 
     857           1 :         will_return(__wrap_json_object, false);
     858           1 :         object = json_new_object();
     859           1 :         assert_false(json_is_invalid(&object));
     860             : 
     861           1 :         will_return(__wrap_json_object, false);
     862           1 :         stored = json_new_object();
     863           1 :         assert_false(json_is_invalid(&stored));
     864             : 
     865           1 :         will_return(__wrap_json_object_set_new, CALL_ORIG);
     866           1 :         rc = json_add_object(&object, "stored", &stored);
     867           1 :         assert_int_equal(0, rc);
     868             : 
     869             :         /*
     870             :          * json object update failure
     871             :          */
     872           1 :         will_return(__wrap_json_object, false);
     873           1 :         will_return(__wrap_json_object_update, true);
     874           1 :         retrieved = json_get_object(&object, "stored");
     875           1 :         assert_true(json_is_invalid(&retrieved));
     876             : 
     877           1 :         json_free(&object);
     878           1 : }
     879             : 
     880           1 : int main(_UNUSED_ int argc, _UNUSED_ const char **argv)
     881             : {
     882           1 :         const struct CMUnitTest tests[] = {
     883             :             cmocka_unit_test(test_json_add_int),
     884             :             cmocka_unit_test(test_json_add_bool),
     885             :             cmocka_unit_test(test_json_add_string),
     886             :             cmocka_unit_test(test_json_add_object),
     887             :             cmocka_unit_test(test_json_add_to_array),
     888             :             cmocka_unit_test(test_json_add_timestamp),
     889             :             cmocka_unit_test(test_json_add_stringn),
     890             :             cmocka_unit_test(test_json_add_version),
     891             :             cmocka_unit_test(test_json_add_address),
     892             :             cmocka_unit_test(test_json_add_sid),
     893             :             cmocka_unit_test(test_json_add_guid),
     894             :             cmocka_unit_test(test_json_to_string),
     895             :             cmocka_unit_test(test_json_get_array),
     896             :             cmocka_unit_test(test_json_get_object),
     897             :         };
     898             : 
     899           1 :         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
     900           1 :         return cmocka_run_group_tests(tests, NULL, NULL);
     901             : }

Generated by: LCOV version 1.14