cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek/*
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek Copyright (C) 2017 Red Hat
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek SSSD tests: Test KCM JSON marshalling
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek This program is free software; you can redistribute it and/or modify
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek it under the terms of the GNU General Public License as published by
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek the Free Software Foundation; either version 3 of the License, or
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek (at your option) any later version.
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek This program is distributed in the hope that it will be useful,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek GNU General Public License for more details.
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek You should have received a copy of the GNU General Public License
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek*/
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek#include "config.h"
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek#include <stdio.h>
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek#include <popt.h>
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek#include "util/util_creds.h"
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek#include "responder/kcm/kcmsrv_ccache.h"
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek#include "responder/kcm/kcmsrv_ccache_be.h"
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek#include "tests/cmocka/common_mock.h"
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek#define TEST_REALM "TESTREALM"
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek#define TEST_PRINC_COMPONENT "PRINC_NAME"
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek#define TEST_CREDS "TESTCREDS"
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek#define TEST_UUID_STR "5f8f296b-02be-4e86-9235-500e82354186"
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek#define TEST_SEC_KEY_ONEDIGIT TEST_UUID_STR"-0"
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek#define TEST_SEC_KEY_MULTIDIGITS TEST_UUID_STR"-123456"
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek#define TEST_SEC_KEY_NOSEP TEST_UUID_STR"+0"
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozekconst struct kcm_ccdb_ops ccdb_mem_ops;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozekconst struct kcm_ccdb_ops ccdb_sec_ops;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozekstruct kcm_marshalling_test_ctx {
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek krb5_context kctx;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek krb5_principal princ;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek};
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozekstatic int setup_kcm_marshalling(void **state)
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek{
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct kcm_marshalling_test_ctx *test_ctx;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek krb5_error_code kerr;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek test_ctx = talloc_zero(NULL, struct kcm_marshalling_test_ctx);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_non_null(test_ctx);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek kerr = krb5_init_context(&test_ctx->kctx);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_int_equal(kerr, 0);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek kerr = krb5_build_principal(test_ctx->kctx,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek &test_ctx->princ,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek sizeof(TEST_REALM)-1, TEST_REALM,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek TEST_PRINC_COMPONENT, NULL);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_int_equal(kerr, 0);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek *state = test_ctx;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek return 0;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek}
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozekstatic int teardown_kcm_marshalling(void **state)
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek{
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct kcm_marshalling_test_ctx *test_ctx = talloc_get_type(*state,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct kcm_marshalling_test_ctx);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_non_null(test_ctx);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek krb5_free_principal(test_ctx->kctx, test_ctx->princ);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek krb5_free_context(test_ctx->kctx);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek talloc_free(test_ctx);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek return 0;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek}
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozekstatic void assert_cc_name_equal(struct kcm_ccache *cc1,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct kcm_ccache *cc2)
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek{
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek const char *name1, *name2;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek name1 = kcm_cc_get_name(cc1);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek name2 = kcm_cc_get_name(cc2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_string_equal(name1, name2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek}
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozekstatic void assert_cc_uuid_equal(struct kcm_ccache *cc1,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct kcm_ccache *cc2)
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek{
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek uuid_t u1, u2;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek errno_t ret;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek ret = kcm_cc_get_uuid(cc1, u1);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_int_equal(ret, EOK);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek ret = kcm_cc_get_uuid(cc2, u2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_int_equal(ret, EOK);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek ret = uuid_compare(u1, u2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_int_equal(ret, 0);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek}
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozekstatic void assert_cc_princ_equal(struct kcm_ccache *cc1,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct kcm_ccache *cc2)
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek{
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek krb5_principal p1;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek krb5_principal p2;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek char *name1;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek char *name2;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek krb5_error_code kerr;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek p1 = kcm_cc_get_client_principal(cc1);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek p2 = kcm_cc_get_client_principal(cc1);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek kerr = krb5_unparse_name(NULL, p1, &name1);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_int_equal(kerr, 0);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek kerr = krb5_unparse_name(NULL, p2, &name2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_int_equal(kerr, 0);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_string_equal(name1, name2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek krb5_free_unparsed_name(NULL, name1);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek krb5_free_unparsed_name(NULL, name2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek}
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozekstatic void assert_cc_offset_equal(struct kcm_ccache *cc1,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct kcm_ccache *cc2)
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek{
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek int32_t off1;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek int32_t off2;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek off1 = kcm_cc_get_offset(cc1);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek off2 = kcm_cc_get_offset(cc2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_int_equal(off1, off2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek}
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozekstatic void assert_cc_equal(struct kcm_ccache *cc1,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct kcm_ccache *cc2)
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek{
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_cc_name_equal(cc1, cc2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_cc_uuid_equal(cc1, cc2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_cc_princ_equal(cc1, cc2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_cc_offset_equal(cc1, cc2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek}
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozekstatic void test_kcm_ccache_marshall_unmarshall(void **state)
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek{
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct kcm_marshalling_test_ctx *test_ctx = talloc_get_type(*state,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct kcm_marshalling_test_ctx);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek errno_t ret;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct cli_creds owner;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct kcm_ccache *cc;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct kcm_ccache *cc2;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek const char *url;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct sss_iobuf *payload;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek const char *name;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek const char *key;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek uint8_t *data;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek owner.ucred.uid = getuid();
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek owner.ucred.gid = getuid();
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek name = talloc_asprintf(test_ctx, "%"SPRIuid, getuid());
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_non_null(name);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek ret = kcm_cc_new(test_ctx,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek test_ctx->kctx,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek &owner,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek name,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek test_ctx->princ,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek &cc);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_int_equal(ret, EOK);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek ret = kcm_ccache_to_sec_input(test_ctx,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek cc,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek &owner,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek &url,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek &payload);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_int_equal(ret, EOK);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek key = strrchr(url, '/') + 1;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_non_null(key);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek data = sss_iobuf_get_data(payload);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_non_null(data);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek ret = sec_kv_to_ccache(test_ctx,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek key,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek (const char *) data,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek &owner,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek &cc2);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_int_equal(ret, EOK);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek assert_cc_equal(cc, cc2);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek /* This key is exactly one byte shorter than it should be */
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek ret = sec_kv_to_ccache(test_ctx,
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek TEST_UUID_STR"-",
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek (const char *) data,
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek &owner,
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek &cc2);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_int_equal(ret, EINVAL);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek}
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozekvoid test_sec_key_get_uuid(void **state)
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek{
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek errno_t ret;
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek uuid_t uuid;
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek char str_uuid[UUID_STR_SIZE];
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek uuid_clear(uuid);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek ret = sec_key_get_uuid(TEST_SEC_KEY_ONEDIGIT, uuid);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_int_equal(ret, EOK);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek uuid_unparse(uuid, str_uuid);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_string_equal(TEST_UUID_STR, str_uuid);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek ret = sec_key_get_uuid(TEST_SEC_KEY_NOSEP, uuid);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_int_equal(ret, EINVAL);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek ret = sec_key_get_uuid(TEST_UUID_STR, uuid);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_int_equal(ret, EINVAL);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek ret = sec_key_get_uuid(NULL, uuid);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_int_equal(ret, EINVAL);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek}
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozekvoid test_sec_key_get_name(void **state)
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek{
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek const char *name;
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek name = sec_key_get_name(TEST_SEC_KEY_ONEDIGIT);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_non_null(name);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_string_equal(name, "0");
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek name = sec_key_get_name(TEST_SEC_KEY_MULTIDIGITS);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_non_null(name);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_string_equal(name, "123456");
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek name = sec_key_get_name(TEST_UUID_STR);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_null(name);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek name = sec_key_get_name(TEST_SEC_KEY_NOSEP);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_null(name);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek name = sec_key_get_name(NULL);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_null(name);
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek}
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozekvoid test_sec_key_match_name(void **state)
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek{
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_true(sec_key_match_name(TEST_SEC_KEY_ONEDIGIT, "0"));
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_true(sec_key_match_name(TEST_SEC_KEY_MULTIDIGITS, "123456"));
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_false(sec_key_match_name(TEST_SEC_KEY_MULTIDIGITS, "0"));
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_false(sec_key_match_name(TEST_SEC_KEY_ONEDIGIT, "123456"));
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_false(sec_key_match_name(TEST_UUID_STR, "0"));
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_false(sec_key_match_name(TEST_SEC_KEY_NOSEP, "0"));
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_false(sec_key_match_name(TEST_SEC_KEY_ONEDIGIT, NULL));
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek assert_false(sec_key_match_name(NULL, "0"));
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek}
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozekint main(int argc, const char *argv[])
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek{
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek poptContext pc;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek int opt;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek int rv;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek struct poptOption long_options[] = {
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek POPT_AUTOHELP
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek SSSD_DEBUG_OPTS
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek POPT_TABLEEND
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek };
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek const struct CMUnitTest tests[] = {
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek cmocka_unit_test_setup_teardown(test_kcm_ccache_marshall_unmarshall,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek setup_kcm_marshalling,
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek teardown_kcm_marshalling),
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek cmocka_unit_test(test_sec_key_get_uuid),
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek cmocka_unit_test(test_sec_key_get_name),
7d73049884e3a96ca3b00b5bd4104f4edd6287abJakub Hrozek cmocka_unit_test(test_sec_key_match_name),
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek };
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
57c5ea8825c7179fd93382dbcbb07e828e5aec19René Genz /* Set debug level to invalid value so we can decide if -d 0 was used. */
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek debug_level = SSSDBG_INVALID;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek pc = poptGetContext(argv[0], argc, argv, long_options, 0);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek while((opt = poptGetNextOpt(pc)) != -1) {
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek switch(opt) {
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek default:
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek fprintf(stderr, "\nInvalid option %s: %s\n\n",
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek poptBadOption(pc, 0), poptStrerror(opt));
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek poptPrintUsage(pc, stderr, 0);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek return 1;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek }
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek }
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek poptFreeContext(pc);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek DEBUG_CLI_INIT(debug_level);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek /* 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 */
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek tests_set_cwd();
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek rv = cmocka_run_group_tests(tests, NULL, NULL);
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek return rv;
cac0db2f8004ae88b9263dc3888a11a2d3d3d114Jakub Hrozek}