summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakub Hrozek <jhrozek@redhat.com>2015-05-29 13:59:05 +0200
committerJakub Hrozek <jhrozek@redhat.com>2015-06-14 21:47:19 +0200
commitb1a822a16e3ef97e31d167f9e97efec06fc121dc (patch)
tree99456fbaf7b328eb42d8c4bd38c04116587c4199
parent298e22fc97a99994e025c0d507737d88fe6fafef (diff)
downloadsssd-b1a822a16e3ef97e31d167f9e97efec06fc121dc.tar.gz
sssd-b1a822a16e3ef97e31d167f9e97efec06fc121dc.tar.xz
sssd-b1a822a16e3ef97e31d167f9e97efec06fc121dc.zip
TESTS: Split off keytab creation into a common module
This change will make the keytab creating reusable by other tests. Reviewed-by: Sumit Bose <sbose@redhat.com>
-rw-r--r--Makefile.am2
-rw-r--r--src/tests/cmocka/common_mock_krb5.c103
-rw-r--r--src/tests/cmocka/common_mock_krb5.h47
-rw-r--r--src/tests/cmocka/test_copy_keytab.c45
4 files changed, 161 insertions, 36 deletions
diff --git a/Makefile.am b/Makefile.am
index 816f18e4c..6912fa971 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -619,6 +619,7 @@ dist_noinst_HEADERS = \
src/tests/cmocka/common_mock_resp.h \
src/tests/cmocka/common_mock_sdap.h \
src/tests/cmocka/common_mock_sysdb_objects.h \
+ src/tests/cmocka/common_mock_krb5.h \
src/tests/cmocka/test_expire_common.h \
src/sss_client/pam_message.h \
src/sss_client/ssh/sss_ssh_client.h \
@@ -2300,6 +2301,7 @@ test_copy_ccache_LDADD = \
$(NULL)
test_copy_keytab_SOURCES = \
+ src/tests/cmocka/common_mock_krb5.c \
src/tests/cmocka/test_copy_keytab.c \
src/providers/krb5/krb5_keytab.c \
src/util/sss_krb5.c \
diff --git a/src/tests/cmocka/common_mock_krb5.c b/src/tests/cmocka/common_mock_krb5.c
new file mode 100644
index 000000000..e253119f5
--- /dev/null
+++ b/src/tests/cmocka/common_mock_krb5.c
@@ -0,0 +1,103 @@
+/*
+ Authors:
+ Sumit Bose <sbose@redhat.com>
+ Jakub Hrozek <jhrozek@redhat.com>
+
+ Copyright (C) 2015 Red Hat
+
+ SSSD tests: Tests keytab utilities
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "util/sss_krb5.h"
+#include "tests/cmocka/common_mock.h"
+#include "tests/cmocka/common_mock_krb5.h"
+
+int mock_keytab(krb5_context kctx,
+ const char *kt_path,
+ krb5_keytab_entry *kt_keys,
+ size_t nkeys)
+{
+ krb5_error_code kerr;
+ krb5_keytab keytab;
+ size_t n;
+
+ kerr = krb5_kt_resolve(kctx, kt_path, &keytab);
+ assert_int_equal(kerr, 0);
+
+ for (n = 0; n < nkeys; n++) {
+ kerr = krb5_kt_add_entry(kctx, keytab, &kt_keys[n]);
+ assert_int_equal(kerr, 0);
+ }
+
+ kerr = krb5_kt_close(kctx, keytab);
+ assert_int_equal(kerr, 0);
+
+ return EOK;
+}
+
+void mock_krb5_keytab_entry(krb5_keytab_entry *kent,
+ krb5_principal principal,
+ krb5_timestamp timestamp,
+ krb5_kvno vno,
+ krb5_enctype enctype,
+ const char *key)
+{
+ memset(kent, 0, sizeof(krb5_keytab_entry));
+
+ kent->magic = KV5M_KEYTAB_ENTRY;
+ kent->principal = principal;
+ kent->timestamp = timestamp;
+ kent->vno = vno;
+ kent->key.magic = KV5M_KEYBLOCK;
+ kent->key.enctype = enctype;
+ kent->key.length = strlen(key) - 1;
+ kent->key.contents = (krb5_octet *) discard_const(key);
+}
+
+int mock_keytab_with_contents(TALLOC_CTX *mem_ctx,
+ const char *keytab_path,
+ const char *keytab_princ)
+{
+ krb5_context kctx;
+ krb5_principal principal;
+ krb5_error_code kerr;
+ size_t nkeys = 2;
+ krb5_keytab_entry keys[nkeys];
+ char *keytab_file_name;
+
+ kerr = krb5_init_context(&kctx);
+ assert_int_equal(kerr, 0);
+
+ keytab_file_name = talloc_asprintf(mem_ctx, "FILE:%s", keytab_path);
+ assert_non_null(keytab_file_name);
+
+ kerr = krb5_parse_name(kctx, keytab_princ, &principal);
+ assert_int_equal(kerr, 0);
+
+ memset(&keys, nkeys, nkeys * sizeof(krb5_keytab_entry));
+
+ mock_krb5_keytab_entry(&keys[0], principal, 12345, 1, 1, "11");
+ mock_krb5_keytab_entry(&keys[1], principal, 12345, 1, 2, "12");
+
+ kerr = mock_keytab(kctx, keytab_file_name, keys, nkeys);
+ assert_int_equal(kerr, 0);
+
+ krb5_free_principal(kctx, principal);
+ krb5_free_context(kctx);
+ talloc_free(keytab_file_name);
+
+ return 0;
+}
diff --git a/src/tests/cmocka/common_mock_krb5.h b/src/tests/cmocka/common_mock_krb5.h
new file mode 100644
index 000000000..5d7247bef
--- /dev/null
+++ b/src/tests/cmocka/common_mock_krb5.h
@@ -0,0 +1,47 @@
+/*
+ Authors:
+ Sumit Bose <sbose@redhat.com>
+ Jakub Hrozek <jhrozek@redhat.com>
+
+ Copyright (C) 2015 Red Hat
+
+ SSSD tests: Tests keytab utilities
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __COMMON_MOCK_KRB5_H_
+#define __COMMON_MOCK_KRB5_H_
+
+#include "util/sss_krb5.h"
+#include "tests/cmocka/common_mock.h"
+
+void mock_krb5_keytab_entry(krb5_keytab_entry *kent,
+ krb5_principal principal,
+ krb5_timestamp timestamp,
+ krb5_kvno vno,
+ krb5_enctype enctype,
+ const char *key);
+
+int mock_keytab(krb5_context kctx,
+ const char *kt_path,
+ krb5_keytab_entry *kt_keys,
+ size_t nkeys);
+
+/* Dummy keys with user-selected principal */
+int mock_keytab_with_contents(TALLOC_CTX *mem_ctx,
+ const char *keytab_path,
+ const char *keytab_princ);
+
+#endif /* __COMMON_MOCK_KRB5_H_ */
diff --git a/src/tests/cmocka/test_copy_keytab.c b/src/tests/cmocka/test_copy_keytab.c
index 1999de3bf..b83e03f2c 100644
--- a/src/tests/cmocka/test_copy_keytab.c
+++ b/src/tests/cmocka/test_copy_keytab.c
@@ -26,6 +26,7 @@
#include "util/sss_krb5.h"
#include "providers/krb5/krb5_common.h"
#include "tests/cmocka/common_mock.h"
+#include "tests/cmocka/common_mock_krb5.h"
#define KEYTAB_TEST_PRINC "test/keytab@TEST.KEYTAB"
#define KEYTAB_PATH TEST_DIR "/keytab_test.keytab"
@@ -41,8 +42,8 @@ static int setup_keytab(void **state)
{
struct keytab_test_ctx *test_ctx;
krb5_error_code kerr;
- krb5_keytab keytab;
- krb5_keytab_entry kent;
+ size_t nkeys = 4;
+ krb5_keytab_entry keys[nkeys];
assert_true(leak_check_setup());
@@ -54,46 +55,18 @@ static int setup_keytab(void **state)
test_ctx->keytab_file_name = "FILE:" KEYTAB_PATH;
- kerr = krb5_kt_resolve(test_ctx->kctx, test_ctx->keytab_file_name, &keytab);
- assert_int_equal(kerr, 0);
-
kerr = krb5_parse_name(test_ctx->kctx, KEYTAB_TEST_PRINC,
&test_ctx->principal);
assert_int_equal(kerr, 0);
- memset(&kent, 0, sizeof(kent));
- kent.magic = KV5M_KEYTAB_ENTRY;
- kent.principal = test_ctx->principal;
- kent.timestamp = 12345;
- kent.vno = 1;
- kent.key.magic = KV5M_KEYBLOCK;
- kent.key.enctype = 1;
- kent.key.length = 2;
- kent.key.contents = (krb5_octet *) discard_const("11");
-
- kerr = krb5_kt_add_entry(test_ctx->kctx, keytab, &kent);
- assert_int_equal(kerr, 0);
-
- kent.key.enctype = 2;
- kent.key.contents = (krb5_octet *) discard_const("12");
+ memset(&keys, nkeys, nkeys * sizeof(krb5_keytab_entry));
- kerr = krb5_kt_add_entry(test_ctx->kctx, keytab, &kent);
- assert_int_equal(kerr, 0);
+ mock_krb5_keytab_entry(&keys[0], test_ctx->principal, 12345, 1, 1, "11");
+ mock_krb5_keytab_entry(&keys[1], test_ctx->principal, 12345, 1, 2, "12");
+ mock_krb5_keytab_entry(&keys[2], test_ctx->principal, 12345, 2, 1, "21");
+ mock_krb5_keytab_entry(&keys[3], test_ctx->principal, 12345, 2, 2, "22");
- kent.vno = 2;
- kent.key.enctype = 1;
- kent.key.contents = (krb5_octet *) discard_const("21");
-
- kerr = krb5_kt_add_entry(test_ctx->kctx, keytab, &kent);
- assert_int_equal(kerr, 0);
-
- kent.key.enctype = 2;
- kent.key.contents = (krb5_octet *) discard_const("22");
-
- kerr = krb5_kt_add_entry(test_ctx->kctx, keytab, &kent);
- assert_int_equal(kerr, 0);
-
- kerr = krb5_kt_close(test_ctx->kctx, keytab);
+ kerr = mock_keytab(test_ctx->kctx, test_ctx->keytab_file_name, keys, nkeys);
assert_int_equal(kerr, 0);
check_leaks_push(test_ctx);