a0ab15ceb80290db80c2052520830a95390de385Sumit Bose Sumit Bose <sbose@redhat.com>
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose Copyright (C) 2014 Red Hat
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose SSSD tests: Tests keytab utilities
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 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 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#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 test_ctx = talloc_zero(global_talloc_context, struct keytab_test_ctx);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose test_ctx->keytab_file_name = "FILE:" KEYTAB_PATH;
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_parse_name(test_ctx->kctx, KEYTAB_TEST_PRINC,
b1a822a16e3ef97e31d167f9e97efec06fc121dcJakub Hrozek memset(&keys, nkeys, nkeys * sizeof(krb5_keytab_entry));
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");
b1a822a16e3ef97e31d167f9e97efec06fc121dcJakub Hrozek kerr = mock_keytab(test_ctx->kctx, test_ctx->keytab_file_name, keys, nkeys);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose struct keytab_test_ctx *test_ctx = talloc_get_type(*state,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_free_principal(test_ctx->kctx, test_ctx->principal);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose struct keytab_test_ctx *test_ctx = talloc_get_type(*state,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = copy_keytab_into_memory(test_ctx, test_ctx->kctx,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_resolve(test_ctx->kctx, mem_keytab_name, &keytab);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 9, 9,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 1, 1,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 1, 2,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 2, 1,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 2, 2,
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose kerr = krb5_kt_close(test_ctx->kctx, mem_keytab);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose struct keytab_test_ctx *test_ctx = talloc_get_type(*state,
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose kerr = krb5_kt_resolve(test_ctx->kctx, test_ctx->keytab_file_name, &keytab);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose kerr = sss_krb5_kt_have_content(test_ctx->kctx, keytab);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose kerr = krb5_kt_resolve(test_ctx->kctx, "FILE:" EMPTY_KEYTAB_PATH, &keytab);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose kerr = sss_krb5_kt_have_content(test_ctx->kctx, keytab);
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose /* no need to remove EMPTY_KEYTAB_PATH because krb5_kt_close() does not
5dcf3ffa3aa228701a79556dc0b889dba0aac535Sumit Bose * create empty keytab files */
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bosestatic bool keytab_entries_equal(krb5_keytab_entry kent1,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose || memcmp(kent1.key.contents, kent2.key.contents,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose return false;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose return true;
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose struct keytab_test_ctx *test_ctx = talloc_get_type(*state,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = copy_keytab_into_memory(test_ctx, test_ctx->kctx,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_resolve(test_ctx->kctx, mem_keytab_name, &mem_keytab);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_resolve(test_ctx->kctx, test_ctx->keytab_file_name, &keytab);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_start_seq_get(test_ctx->kctx, mem_keytab, &mem_cursor);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_start_seq_get(test_ctx->kctx, keytab, &cursor);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose while ((kerr = krb5_kt_next_entry(test_ctx->kctx, keytab, &kent,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr_mem = krb5_kt_next_entry(test_ctx->kctx, mem_keytab, &mem_kent,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose assert_true(keytab_entries_equal(kent, mem_kent));
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 kerr_mem = krb5_kt_next_entry(test_ctx->kctx, mem_keytab, &mem_kent,
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_end_seq_get(test_ctx->kctx, mem_keytab, &mem_cursor);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_end_seq_get(test_ctx->kctx, keytab, &cursor);
576ad637181b80d39a4e136c9afbf34c57f76156Sumit Bose kerr = krb5_kt_close(test_ctx->kctx, mem_keytab);
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(test_copy_keytab,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(test_sss_krb5_kt_have_content,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(test_copy_keytab_order,
57c5ea8825c7179fd93382dbcbb07e828e5aec19René Genz /* Set debug level to invalid value so we can decide if -d 0 was used. */
a0ab15ceb80290db80c2052520830a95390de385Sumit Bose pc = poptGetContext(argv[0], argc, argv, long_options, 0);
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 */