a0ab15ceb80290db80c2052520830a95390de385Sumit Bose/*
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose Authors:
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose Sumit Bose <sbose@redhat.com>
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose Copyright (C) 2014 Red Hat
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose SSSD tests: Tests keytab utilities
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose This program is free software; you can redistribute it and/or modify
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose it under the terms of the GNU General Public License as published by
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose the Free Software Foundation; either version 3 of the License, or
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose (at your option) any later version.
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose This program is distributed in the hope that it will be useful,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose but WITHOUT ANY WARRANTY; without even the implied warranty of
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose GNU General Public License for more details.
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose You should have received a copy of the GNU General Public License
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose along with this program. If not, see <http://www.gnu.org/licenses/>.
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose*/
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose#include <stdio.h>
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose#include <popt.h>
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose#include "util/sss_krb5.h"
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose#include "providers/krb5/krb5_common.h"
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose#include "tests/cmocka/common_mock.h"
b1a822a16e3ef97e31d167f9e97efec06fc121dcJakub Hrozek#include "tests/cmocka/common_mock_krb5.h"
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
83788fb29efb971db7662887a726595df5bae093Lukas Slebodnik#define TESTS_PATH "tp_" BASE_FILE_STEM
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose#define KEYTAB_TEST_PRINC "test/keytab@TEST.KEYTAB"
83788fb29efb971db7662887a726595df5bae093Lukas Slebodnik#define KEYTAB_PATH TESTS_PATH "/keytab_test.keytab"
83788fb29efb971db7662887a726595df5bae093Lukas Slebodnik#define EMPTY_KEYTAB_PATH TESTS_PATH "/empty_keytab_test.keytab"
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bosestruct keytab_test_ctx {
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_context kctx;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose const char *keytab_file_name;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_principal principal;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose};
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozekstatic int setup_keytab(void **state)
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose{
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose struct keytab_test_ctx *test_ctx;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_error_code kerr;
b1a822a16e3ef97e31d167f9e97efec06fc121dcJakub Hrozek size_t nkeys = 4;
b1a822a16e3ef97e31d167f9e97efec06fc121dcJakub Hrozek krb5_keytab_entry keys[nkeys];
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
83788fb29efb971db7662887a726595df5bae093Lukas Slebodnik test_dom_suite_setup(TESTS_PATH);
83788fb29efb971db7662887a726595df5bae093Lukas Slebodnik
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_true(leak_check_setup());
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose test_ctx = talloc_zero(global_talloc_context, struct keytab_test_ctx);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_non_null(test_ctx);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_init_context(&test_ctx->kctx);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_equal(kerr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose test_ctx->keytab_file_name = "FILE:" KEYTAB_PATH;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_parse_name(test_ctx->kctx, KEYTAB_TEST_PRINC,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose &test_ctx->principal);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_equal(kerr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
b1a822a16e3ef97e31d167f9e97efec06fc121dcJakub Hrozek memset(&keys, nkeys, nkeys * sizeof(krb5_keytab_entry));
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
b1a822a16e3ef97e31d167f9e97efec06fc121dcJakub Hrozek mock_krb5_keytab_entry(&keys[0], test_ctx->principal, 12345, 1, 1, "11");
b1a822a16e3ef97e31d167f9e97efec06fc121dcJakub Hrozek mock_krb5_keytab_entry(&keys[1], test_ctx->principal, 12345, 1, 2, "12");
b1a822a16e3ef97e31d167f9e97efec06fc121dcJakub Hrozek mock_krb5_keytab_entry(&keys[2], test_ctx->principal, 12345, 2, 1, "21");
b1a822a16e3ef97e31d167f9e97efec06fc121dcJakub Hrozek mock_krb5_keytab_entry(&keys[3], test_ctx->principal, 12345, 2, 2, "22");
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
b1a822a16e3ef97e31d167f9e97efec06fc121dcJakub Hrozek kerr = mock_keytab(test_ctx->kctx, test_ctx->keytab_file_name, keys, nkeys);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_equal(kerr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose check_leaks_push(test_ctx);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose *state = test_ctx;
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek return 0;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose}
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozekstatic int teardown_keytab(void **state)
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose{
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose int ret;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose struct keytab_test_ctx *test_ctx = talloc_get_type(*state,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose struct keytab_test_ctx);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_non_null(test_ctx);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_free_principal(test_ctx->kctx, test_ctx->principal);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_free_context(test_ctx->kctx);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose ret = unlink(KEYTAB_PATH);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_equal(ret, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_true(check_leaks_pop(test_ctx) == true);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose talloc_free(test_ctx);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_true(leak_check_teardown());
83788fb29efb971db7662887a726595df5bae093Lukas Slebodnik
83788fb29efb971db7662887a726595df5bae093Lukas Slebodnik ret = rmdir(TESTS_PATH);
83788fb29efb971db7662887a726595df5bae093Lukas Slebodnik assert_return_code(ret, errno);
83788fb29efb971db7662887a726595df5bae093Lukas Slebodnik
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek return 0;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose}
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bosevoid test_copy_keytab(void **state)
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose{
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_error_code kerr;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose char *mem_keytab_name;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_keytab mem_keytab;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_keytab keytab;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_keytab_entry kent;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose struct keytab_test_ctx *test_ctx = talloc_get_type(*state,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose struct keytab_test_ctx);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_non_null(test_ctx);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = copy_keytab_into_memory(test_ctx, test_ctx->kctx,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose test_ctx->keytab_file_name,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose &mem_keytab_name, &mem_keytab);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_equal(kerr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_non_null(mem_keytab_name);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_resolve(test_ctx->kctx, mem_keytab_name, &keytab);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_equal(kerr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 9, 9,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_not_equal(kerr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 1, 1,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_equal(kerr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 1, 2,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_equal(kerr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 2, 1,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_equal(kerr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 2, 2,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_equal(kerr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose talloc_free(mem_keytab_name);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_close(test_ctx->kctx, keytab);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_equal(kerr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_close(test_ctx->kctx, mem_keytab);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose assert_int_equal(kerr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose}
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bosevoid test_sss_krb5_kt_have_content(void **state)
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose{
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose krb5_error_code kerr;
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose krb5_keytab keytab;
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose struct keytab_test_ctx *test_ctx = talloc_get_type(*state,
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose struct keytab_test_ctx);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose assert_non_null(test_ctx);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose kerr = krb5_kt_resolve(test_ctx->kctx, test_ctx->keytab_file_name, &keytab);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose assert_int_equal(kerr, 0);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose kerr = sss_krb5_kt_have_content(test_ctx->kctx, keytab);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose assert_int_equal(kerr, 0);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose kerr = krb5_kt_close(test_ctx->kctx, keytab);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose assert_int_equal(kerr, 0);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose kerr = krb5_kt_resolve(test_ctx->kctx, "FILE:" EMPTY_KEYTAB_PATH, &keytab);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose assert_int_equal(kerr, 0);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose kerr = sss_krb5_kt_have_content(test_ctx->kctx, keytab);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose assert_int_equal(kerr, KRB5_KT_NOTFOUND);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose kerr = krb5_kt_close(test_ctx->kctx, keytab);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose assert_int_equal(kerr, 0);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose /* no need to remove EMPTY_KEYTAB_PATH because krb5_kt_close() does not
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose * create empty keytab files */
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose}
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bosestatic bool keytab_entries_equal(krb5_keytab_entry kent1,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose krb5_keytab_entry kent2)
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose{
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose if (kent1.vno != kent2.vno
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose || kent1.key.enctype != kent2.key.enctype
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose || kent1.key.length != kent2.key.length
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose || memcmp(kent1.key.contents, kent2.key.contents,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kent1.key.length) != 0 ) {
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose return false;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose }
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose return true;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose}
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bosevoid test_copy_keytab_order(void **state)
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose{
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose krb5_error_code kerr;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose krb5_error_code kerr_mem;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose char *mem_keytab_name;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose krb5_keytab mem_keytab;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose krb5_kt_cursor mem_cursor;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose krb5_keytab_entry mem_kent;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose krb5_keytab keytab;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose krb5_kt_cursor cursor;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose krb5_keytab_entry kent;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose struct keytab_test_ctx *test_ctx = talloc_get_type(*state,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose struct keytab_test_ctx);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_non_null(test_ctx);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = copy_keytab_into_memory(test_ctx, test_ctx->kctx,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose test_ctx->keytab_file_name,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose &mem_keytab_name, &mem_keytab);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_int_equal(kerr, 0);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_non_null(mem_keytab_name);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_resolve(test_ctx->kctx, mem_keytab_name, &mem_keytab);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_int_equal(kerr, 0);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_resolve(test_ctx->kctx, test_ctx->keytab_file_name, &keytab);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_int_equal(kerr, 0);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_start_seq_get(test_ctx->kctx, mem_keytab, &mem_cursor);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_int_equal(kerr, 0);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_start_seq_get(test_ctx->kctx, keytab, &cursor);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_int_equal(kerr, 0);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose while ((kerr = krb5_kt_next_entry(test_ctx->kctx, keytab, &kent,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose &cursor)) == 0) {
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr_mem = krb5_kt_next_entry(test_ctx->kctx, mem_keytab, &mem_kent,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose &mem_cursor);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_int_equal(kerr_mem, 0);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_true(keytab_entries_equal(kent, mem_kent));
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose krb5_free_keytab_entry_contents(test_ctx->kctx, &mem_kent);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose }
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_int_equal(kerr, KRB5_KT_END);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr_mem = krb5_kt_next_entry(test_ctx->kctx, mem_keytab, &mem_kent,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose &mem_cursor);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_int_equal(kerr_mem, KRB5_KT_END);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_end_seq_get(test_ctx->kctx, mem_keytab, &mem_cursor);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_int_equal(kerr, 0);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_end_seq_get(test_ctx->kctx, keytab, &cursor);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_int_equal(kerr, 0);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose talloc_free(mem_keytab_name);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_close(test_ctx->kctx, keytab);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_int_equal(kerr, 0);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_close(test_ctx->kctx, mem_keytab);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_int_equal(kerr, 0);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose}
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Boseint main(int argc, const char *argv[])
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose{
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose poptContext pc;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose int opt;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose int rv;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose struct poptOption long_options[] = {
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose POPT_AUTOHELP
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose SSSD_DEBUG_OPTS
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose POPT_TABLEEND
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose };
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek const struct CMUnitTest tests[] = {
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(test_copy_keytab,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek setup_keytab, teardown_keytab),
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(test_sss_krb5_kt_have_content,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek setup_keytab, teardown_keytab),
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(test_copy_keytab_order,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek setup_keytab, teardown_keytab),
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose };
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
57c5ea8825c7179fd93382dbcbb07e828e5aec19René Genz /* Set debug level to invalid value so we can decide if -d 0 was used. */
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose debug_level = SSSDBG_INVALID;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose pc = poptGetContext(argv[0], argc, argv, long_options, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose while((opt = poptGetNextOpt(pc)) != -1) {
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose switch(opt) {
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose default:
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose fprintf(stderr, "\nInvalid option %s: %s\n\n",
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose poptBadOption(pc, 0), poptStrerror(opt));
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose poptPrintUsage(pc, stderr, 0);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose return 1;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose }
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose }
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose poptFreeContext(pc);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose DEBUG_CLI_INIT(debug_level);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose /* Even though normally the tests should clean up after themselves
57c5ea8825c7179fd93382dbcbb07e828e5aec19René Genz * they might not after a failed run. Remove the old DB to be sure */
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose tests_set_cwd();
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek rv = cmocka_run_group_tests(tests, NULL, NULL);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose return rv;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose}