LCOV - code coverage report
Current view: top level - tests/cmocka - test_copy_ccache.c (source / functions) Hit Total Coverage
Test: .coverage.total Lines: 107 110 97.3 %
Date: 2015-10-19 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :     Authors:
       3             :         Sumit Bose <sbose@redhat.com>
       4             : 
       5             :     Copyright (C) 2014 Red Hat
       6             : 
       7             :     SSSD tests: Tests ccache utilities
       8             : 
       9             :     This program is free software; you can redistribute it and/or modify
      10             :     it under the terms of the GNU General Public License as published by
      11             :     the Free Software Foundation; either version 3 of the License, or
      12             :     (at your option) any later version.
      13             : 
      14             :     This program is distributed in the hope that it will be useful,
      15             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :     GNU General Public License for more details.
      18             : 
      19             :     You should have received a copy of the GNU General Public License
      20             :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : */
      22             : 
      23             : #include <stdio.h>
      24             : #include <popt.h>
      25             : 
      26             : #include "util/sss_krb5.h"
      27             : #include "providers/krb5/krb5_common.h"
      28             : #include "providers/krb5/krb5_ccache.h"
      29             : #include "tests/cmocka/common_mock.h"
      30             : 
      31             : #define CCACHE_TEST_CLIENT_PRINC "test/client@TEST.CCACHE"
      32             : #define CCACHE_TEST_SERVER_PRINC "test/server@TEST.CCACHE"
      33             : #define CCACHE_PATH TEST_DIR "/ccache_test.ccache"
      34             : 
      35             : struct ccache_test_ctx {
      36             :     krb5_context kctx;
      37             :     const char *ccache_file_name;
      38             :     krb5_principal client_principal;
      39             :     krb5_principal server_principal;
      40             : };
      41             : 
      42           1 : static int setup_ccache(void **state)
      43             : {
      44             :     struct ccache_test_ctx *test_ctx;
      45             :     krb5_error_code kerr;
      46             :     krb5_ccache ccache;
      47             :     krb5_creds test_creds;
      48             :     static krb5_address addr;
      49           1 :     int add=0x12345;
      50             :     krb5_authdata *a;
      51             : 
      52             :     static krb5_address *addrs[] = {
      53             :         &addr,
      54             :         NULL,
      55             :     };
      56             : 
      57           1 :     assert_true(leak_check_setup());
      58             : 
      59             : 
      60           1 :     test_ctx = talloc_zero(global_talloc_context, struct ccache_test_ctx);
      61           1 :     assert_non_null(test_ctx);
      62             : 
      63           1 :     kerr = krb5_init_context(&test_ctx->kctx);
      64           1 :     assert_int_equal(kerr, 0);
      65             : 
      66           1 :     addr.magic = KV5M_ADDRESS;
      67           1 :     addr.addrtype = ADDRTYPE_INET;
      68           1 :     addr.length = 4;
      69           1 :     addr.contents = (krb5_octet *) &add;
      70             : 
      71           1 :     memset(&test_creds, 0, sizeof(test_creds));
      72           1 :     test_creds.magic = KV5M_CREDS;
      73           1 :     kerr = krb5_parse_name(test_ctx->kctx, CCACHE_TEST_CLIENT_PRINC,
      74             :                            &test_ctx->client_principal);
      75           1 :     assert_int_equal(kerr, 0);
      76           1 :     test_creds.client = test_ctx->client_principal;
      77           1 :     kerr = krb5_parse_name(test_ctx->kctx, CCACHE_TEST_SERVER_PRINC,
      78             :                            &test_ctx->server_principal);
      79           1 :     assert_int_equal(kerr, 0);
      80           1 :     test_creds.server = test_ctx->server_principal;
      81             : 
      82           1 :     test_creds.keyblock.magic = KV5M_KEYBLOCK;
      83           1 :     test_creds.keyblock.contents = 0;
      84           1 :     test_creds.keyblock.enctype = 1;
      85           1 :     test_creds.keyblock.length = 1;
      86           1 :     test_creds.keyblock.contents = (unsigned char *) discard_const("1");
      87           1 :     test_creds.times.authtime = 1111;
      88           1 :     test_creds.times.starttime = 2222;
      89           1 :     test_creds.times.endtime = 3333;
      90           1 :     test_creds.times.renew_till = 4444;
      91           1 :     test_creds.is_skey = 1;
      92           1 :     test_creds.ticket_flags = 5555;
      93           1 :     test_creds.addresses = addrs;
      94             : 
      95           1 :     test_creds.ticket.magic = KV5M_DATA;
      96           1 :     test_creds.ticket.length = sizeof("Ticket");
      97           1 :     test_creds.ticket.data = discard_const("Ticket");
      98             : 
      99           1 :     test_creds.authdata = malloc (2 * sizeof(krb5_authdata *));
     100           1 :     assert_non_null(test_creds.authdata);
     101             : 
     102           1 :     a = (krb5_authdata *) malloc(sizeof(krb5_authdata));
     103           1 :     assert_non_null(a);
     104             : 
     105           1 :     a->magic = KV5M_AUTHDATA;
     106           1 :     a->ad_type = KRB5_AUTHDATA_IF_RELEVANT;
     107           1 :     a->contents = (krb5_octet * ) malloc(1);
     108           1 :     assert_non_null(a->contents);
     109           1 :     a->contents[0]=5;
     110           1 :     a->length = 1;
     111           1 :     test_creds.authdata[0] = a;
     112           1 :     test_creds.authdata[1] = NULL;
     113             : 
     114             : 
     115           1 :     test_ctx->ccache_file_name = "FILE:" CCACHE_PATH;
     116             : 
     117           1 :     kerr = krb5_cc_resolve(test_ctx->kctx, test_ctx->ccache_file_name,
     118             :                                &ccache);
     119           1 :     assert_int_equal(kerr, 0);
     120             : 
     121           1 :     kerr = krb5_cc_initialize(test_ctx->kctx, ccache, test_creds.client);
     122           1 :     assert_int_equal(kerr, 0);
     123             : 
     124           1 :     kerr = krb5_cc_store_cred(test_ctx->kctx, ccache, &test_creds);
     125           1 :     assert_int_equal(kerr, 0);
     126             : 
     127           1 :     kerr = krb5_cc_close(test_ctx->kctx, ccache);
     128           1 :     assert_int_equal(kerr, 0);
     129             : 
     130           1 :     check_leaks_push(test_ctx);
     131           1 :     *state = test_ctx;
     132             : 
     133           1 :     krb5_free_authdata(test_ctx->kctx, test_creds.authdata);
     134           1 :     return 0;
     135             : }
     136             : 
     137           1 : static int teardown_ccache(void **state)
     138             : {
     139             :     int ret;
     140           1 :     struct ccache_test_ctx *test_ctx = talloc_get_type(*state,
     141             :                                                         struct ccache_test_ctx);
     142           1 :     assert_non_null(test_ctx);
     143             : 
     144           1 :     krb5_free_principal(test_ctx->kctx, test_ctx->client_principal);
     145           1 :     krb5_free_principal(test_ctx->kctx, test_ctx->server_principal);
     146           1 :     krb5_free_context(test_ctx->kctx);
     147             : 
     148           1 :     ret = unlink(CCACHE_PATH);
     149           1 :     assert_int_equal(ret, 0);
     150             : 
     151           1 :     assert_true(check_leaks_pop(test_ctx) == true);
     152           1 :     talloc_free(test_ctx);
     153           1 :     assert_true(leak_check_teardown());
     154           1 :     return 0;
     155             : }
     156             : 
     157           1 : void test_copy_ccache(void **state)
     158             : {
     159             :     krb5_error_code kerr;
     160             :     char *mem_ccache_name;
     161             :     krb5_ccache ccache;
     162             :     krb5_creds mcreds;
     163             :     krb5_creds creds;
     164             :     krb5_principal mem_principal;
     165           1 :     struct ccache_test_ctx *test_ctx = talloc_get_type(*state,
     166             :                                                         struct ccache_test_ctx);
     167           1 :     assert_non_null(test_ctx);
     168             : 
     169           1 :     kerr = copy_ccache_into_memory(test_ctx, test_ctx->kctx,
     170             :                                    test_ctx->ccache_file_name,
     171             :                                    &mem_ccache_name);
     172           1 :     assert_int_equal(kerr, 0);
     173           1 :     assert_non_null(mem_ccache_name);
     174             : 
     175           1 :     kerr = krb5_cc_resolve(test_ctx->kctx, mem_ccache_name, &ccache);
     176           1 :     assert_int_equal(kerr, 0);
     177             : 
     178           1 :     talloc_free(mem_ccache_name);
     179             : 
     180           1 :     kerr = krb5_cc_get_principal(test_ctx->kctx, ccache, &mem_principal);
     181           1 :     assert_int_equal(kerr, 0);
     182           1 :     assert_non_null(mem_principal);
     183             : 
     184           1 :     assert_true(krb5_principal_compare(test_ctx->kctx, mem_principal,
     185             :                                        test_ctx->client_principal));
     186           1 :     krb5_free_principal(test_ctx->kctx, mem_principal);
     187             : 
     188           1 :     memset(&mcreds, 0, sizeof(mcreds));
     189           1 :     memset(&creds, 0, sizeof(mcreds));
     190           1 :     mcreds.client = test_ctx->client_principal;
     191           1 :     mcreds.server = test_ctx->server_principal;
     192           1 :     kerr = krb5_cc_retrieve_cred(test_ctx->kctx, ccache, 0, &mcreds, &creds);
     193           1 :     assert_int_equal(kerr, 0);
     194           1 :     krb5_free_cred_contents(test_ctx->kctx, &creds);
     195             : 
     196           1 :     kerr = krb5_cc_destroy(test_ctx->kctx, ccache);
     197           1 :     assert_int_equal(kerr, 0);
     198           1 : }
     199             : 
     200           1 : int main(int argc, const char *argv[])
     201             : {
     202             :     poptContext pc;
     203             :     int opt;
     204             :     int rv;
     205           6 :     struct poptOption long_options[] = {
     206             :         POPT_AUTOHELP
     207           5 :         SSSD_DEBUG_OPTS
     208             :         POPT_TABLEEND
     209             :     };
     210             : 
     211           1 :     const struct CMUnitTest tests[] = {
     212             :         cmocka_unit_test_setup_teardown(test_copy_ccache,
     213             :                                         setup_ccache, teardown_ccache),
     214             :     };
     215             : 
     216             :     /* Set debug level to invalid value so we can deside if -d 0 was used. */
     217           1 :     debug_level = SSSDBG_INVALID;
     218             : 
     219           1 :     pc = poptGetContext(argv[0], argc, argv, long_options, 0);
     220           1 :     while((opt = poptGetNextOpt(pc)) != -1) {
     221             :         switch(opt) {
     222             :         default:
     223           0 :             fprintf(stderr, "\nInvalid option %s: %s\n\n",
     224             :                     poptBadOption(pc, 0), poptStrerror(opt));
     225           0 :             poptPrintUsage(pc, stderr, 0);
     226           0 :             return 1;
     227             :         }
     228             :     }
     229           1 :     poptFreeContext(pc);
     230             : 
     231           1 :     DEBUG_CLI_INIT(debug_level);
     232             : 
     233             :     /* Even though normally the tests should clean up after themselves
     234             :      * they might not after a failed run. Remove the old db to be sure */
     235           1 :     tests_set_cwd();
     236             : 
     237           1 :     rv = cmocka_run_group_tests(tests, NULL, NULL);
     238             : 
     239           1 :     return rv;
     240             : }

Generated by: LCOV version 1.10