LCOV - code coverage report
Current view: top level - lib/util/tests - test_talloc_keep_secret.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 52 52 100.0 %
Date: 2024-04-21 15:09:00 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #include <stdarg.h>
       2             : #include <stddef.h>
       3             : #include <stdint.h>
       4             : #include <setjmp.h>
       5             : #include <cmocka.h>
       6             : 
       7             : #include <string.h>
       8             : #include <talloc.h>
       9             : #include "lib/util/talloc_keep_secret.h"
      10             : 
      11             : int rep_memset_s(void *dest, size_t destsz, int ch, size_t count);
      12             : 
      13           2 : int rep_memset_s(void *dest, size_t destsz, int ch, size_t count)
      14             : {
      15           2 :         check_expected_ptr(dest);
      16           2 :         check_expected(destsz);
      17           2 :         check_expected(ch);
      18           2 :         check_expected(count);
      19             : 
      20           2 :         return 0;
      21             : }
      22             : 
      23           1 : static void test_talloc_keep_secret(void ** state)
      24             : {
      25           1 :         TALLOC_CTX *pool = NULL;
      26           1 :         char *ptr1 = NULL;
      27           1 :         char *ptr2 = NULL;
      28           1 :         const char *ptr1_talloc_name = NULL;
      29           1 :         size_t ptr1_size;
      30           1 :         size_t i;
      31             : 
      32           1 :         pool = talloc_pool(NULL, 256);
      33           1 :         assert_non_null(pool);
      34             : 
      35           1 :         ptr1 = talloc_strdup(pool, "secret");
      36           1 :         assert_non_null(ptr1);
      37           1 :         assert_string_equal(ptr1, "secret");
      38             : 
      39           1 :         talloc_keep_secret(ptr1);
      40             : 
      41           1 :         ptr1_talloc_name = talloc_get_name(ptr1);
      42           1 :         assert_string_equal(ptr1_talloc_name, "ptr1");
      43             : 
      44           1 :         ptr1_size = talloc_get_size(ptr1);
      45           1 :         assert_int_equal(ptr1_size, strlen(ptr1) + 1);
      46             : 
      47           1 :         expect_string(rep_memset_s, dest, "secret");
      48           1 :         expect_value(rep_memset_s, destsz, strlen(ptr1) + 1);
      49           1 :         expect_value(rep_memset_s, ch, (int)'\0');
      50           1 :         expect_value(rep_memset_s, count, strlen(ptr1) + 1);
      51             : 
      52           1 :         talloc_free(ptr1);
      53             : 
      54           1 :         ptr2 = talloc_size(pool, ptr1_size);
      55           1 :         assert_ptr_equal(ptr1, ptr2);
      56             : 
      57           8 :         for (i = 1; i < ptr1_size; i++) {
      58           6 :                 assert_int_not_equal(ptr2[0], ptr2[i]);
      59             :         }
      60             : 
      61           1 :         talloc_free(pool);
      62           1 : }
      63             : 
      64           1 : static void test_talloc_keep_secret_validate_memset(void **state)
      65             : {
      66           1 :         TALLOC_CTX *mem_ctx = NULL;
      67           1 :         char *password = NULL;
      68             : 
      69           1 :         mem_ctx = talloc_new(NULL);
      70           1 :         assert_non_null(mem_ctx);
      71             : 
      72           1 :         password = talloc_strdup(mem_ctx, "secret");
      73           1 :         assert_non_null(password);
      74           1 :         talloc_keep_secret(password);
      75             : 
      76           1 :         expect_string(rep_memset_s, dest, "secret");
      77           1 :         expect_value(rep_memset_s, destsz, strlen(password) + 1);
      78           1 :         expect_value(rep_memset_s, ch, (int)'\0');
      79           1 :         expect_value(rep_memset_s, count, strlen(password) + 1);
      80             : 
      81           1 :         talloc_free(mem_ctx);
      82           1 : }
      83             : 
      84           1 : int main(void)
      85             : {
      86           1 :     const struct CMUnitTest tests[] = {
      87             :         cmocka_unit_test(test_talloc_keep_secret),
      88             :         cmocka_unit_test(test_talloc_keep_secret_validate_memset),
      89             :     };
      90             : 
      91           1 :     cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
      92             : 
      93           1 :     return cmocka_run_group_tests(tests, NULL, NULL);
      94             : }

Generated by: LCOV version 1.14