LCOV - code coverage report
Current view: top level - testsuite/unittests - test_krb5_samba.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 68 68 100.0 %
Date: 2024-04-21 15:09:00 Functions: 7 7 100.0 %

          Line data    Source code
       1             : #include <stdarg.h>
       2             : #include <stddef.h>
       3             : #include <setjmp.h>
       4             : #include <cmocka.h>
       5             : 
       6             : #include <krb5.h>
       7             : 
       8             : #include "includes.h"
       9             : #include "lib/krb5_wrap/krb5_samba.h"
      10             : 
      11             : 
      12           4 : static int setup_krb5_context(void **state)
      13             : {
      14           4 :         krb5_context context = NULL;
      15           4 :         krb5_error_code code;
      16             : 
      17           4 :         code = krb5_init_context(&context);
      18           4 :         assert_return_code(code, code);
      19             : 
      20           4 :         *state = context;
      21             : 
      22           4 :         return 0;
      23             : }
      24             : 
      25           4 : static int teardown_krb5_context(void **state)
      26             : {
      27           4 :         krb5_context context = *state;
      28             : 
      29           4 :         if (context != NULL) {
      30           4 :                 krb5_free_context(context);
      31             :         }
      32           4 :         return 0;
      33             : }
      34             : 
      35           1 : static void test_smb_krb5_kt_open(void **state)
      36             : {
      37           1 :         krb5_context context = *state;
      38           1 :         krb5_keytab keytab = NULL;
      39           1 :         krb5_error_code code;
      40           1 :         char keytab_template[] = "/tmp/keytab.XXXXXX";
      41           1 :         int fd;
      42             : 
      43           1 :         fd = mkstemp(keytab_template);
      44           1 :         assert_return_code(fd, errno);
      45           1 :         unlink(keytab_template);
      46             : 
      47           1 :         code = smb_krb5_kt_open(context,
      48             :                                 keytab_template,
      49             :                                 false,
      50             :                                 &keytab);
      51           1 :         assert_int_equal(code, 0);
      52             : 
      53           1 :         krb5_kt_close(context, keytab);
      54           1 :         close(fd);
      55           1 : }
      56             : 
      57           1 : static void test_smb_krb5_kt_open_file(void **state)
      58           1 : {
      59           1 :         krb5_context context = *state;
      60           1 :         krb5_keytab keytab = NULL;
      61           1 :         krb5_error_code code;
      62           1 :         char keytab_template[] = "/tmp/keytab.XXXXXX";
      63           1 :         char keytab_file[6 + strlen(keytab_template)];
      64           1 :         int fd;
      65             : 
      66           1 :         fd = mkstemp(keytab_template);
      67           1 :         assert_return_code(fd, errno);
      68           1 :         unlink(keytab_template);
      69             : 
      70           1 :         snprintf(keytab_file, sizeof(keytab_file), "FILE:%s", keytab_template);
      71             : 
      72           1 :         code = smb_krb5_kt_open(context,
      73             :                                 keytab_file,
      74             :                                 false,
      75             :                                 &keytab);
      76           1 :         assert_int_equal(code, 0);
      77             : 
      78           1 :         krb5_kt_close(context, keytab);
      79           1 :         close(fd);
      80           1 : }
      81             : 
      82           1 : static void test_smb_krb5_kt_open_fail(void **state)
      83             : {
      84           1 :         krb5_context context = *state;
      85           1 :         krb5_keytab keytab = NULL;
      86           1 :         krb5_error_code code;
      87             : 
      88           1 :         code = smb_krb5_kt_open(context,
      89             :                                 NULL,
      90             :                                 false,
      91             :                                 &keytab);
      92           1 :         assert_int_equal(code, KRB5_KT_BADNAME);
      93           1 :         code = smb_krb5_kt_open(context,
      94             :                                 "wurst",
      95             :                                 false,
      96             :                                 &keytab);
      97           1 :         assert_int_equal(code, KRB5_KT_BADNAME);
      98             : 
      99           1 :         code = smb_krb5_kt_open(context,
     100             :                                 "FILE:wurst",
     101             :                                 false,
     102             :                                 &keytab);
     103           1 :         assert_int_equal(code, KRB5_KT_BADNAME);
     104             : 
     105           1 :         code = smb_krb5_kt_open(context,
     106             :                                 "WRFILE:wurst",
     107             :                                 false,
     108             :                                 &keytab);
     109           1 :         assert_int_equal(code, KRB5_KT_BADNAME);
     110           1 : }
     111             : 
     112           1 : static void test_smb_krb5_kt_open_relative_memory(void **state)
     113             : {
     114           1 :         krb5_context context = *state;
     115           1 :         krb5_keytab keytab = NULL;
     116           1 :         krb5_error_code code;
     117             : 
     118           1 :         code = smb_krb5_kt_open_relative(context,
     119             :                                          NULL,
     120             :                                          true,
     121             :                                          &keytab);
     122           1 :         assert_int_equal(code, 0);
     123             : 
     124           1 :         krb5_kt_close(context, keytab);
     125           1 : }
     126             : 
     127           1 : int main(void) {
     128           1 :         const struct CMUnitTest tests[] = {
     129             :                 cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open,
     130             :                                                 setup_krb5_context,
     131             :                                                 teardown_krb5_context),
     132             :                 cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open_file,
     133             :                                                 setup_krb5_context,
     134             :                                                 teardown_krb5_context),
     135             :                 cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open_fail,
     136             :                                                 setup_krb5_context,
     137             :                                                 teardown_krb5_context),
     138             :                 cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open_relative_memory,
     139             :                                                 setup_krb5_context,
     140             :                                                 teardown_krb5_context),
     141             :         };
     142             : 
     143           1 :         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
     144           1 :         return cmocka_run_group_tests(tests, NULL, NULL);
     145             : }

Generated by: LCOV version 1.14