25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek/*
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek Authors:
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek Michal Zidek <mzidek@redhat.com>
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek Stephen Gallagher <sgallagh@redhat.com>
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek This program is free software; you can redistribute it and/or modify
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek it under the terms of the GNU General Public License as published by
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek the Free Software Foundation; either version 3 of the License, or
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek (at your option) any later version.
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek This program is distributed in the hope that it will be useful,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek but WITHOUT ANY WARRANTY; without even the implied warranty of
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek GNU General Public License for more details.
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek You should have received a copy of the GNU General Public License
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek along with this program. If not, see <http://www.gnu.org/licenses/>.
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek*/
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include <stdlib.h>
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include <check.h>
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include <popt.h>
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include <talloc.h>
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include <sys/stat.h>
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include <sys/types.h>
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include "config.h"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include "tests/common.h"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include "util/util.h"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include "confdb/confdb.h"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include "db/sysdb.h"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include "db/sysdb_services.h"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#include "db/sysdb_ssh.h"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
50c9d542e8bf641412debaa82a4dcf67ddb72258Lukas Slebodnik#define TESTS_PATH "tp_" BASE_FILE_STEM
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#define TEST_CONF_FILE "tests_conf.ldb"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek#define TEST_HOSTNAME "testhost"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekstruct sysdb_test_ctx {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct sysdb_ctx *sysdb;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct confdb_ctx *confdb;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct tevent_context *ev;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct sss_domain_info *domain;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek};
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekstatic int setup_sysdb_tests(struct sysdb_test_ctx **ctx)
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek{
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct sysdb_test_ctx *test_ctx;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek char *conf_db;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek int ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek const char *val[2];
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek val[1] = NULL;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek /* Create tests directory if it doesn't exist */
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek /* (relative to current dir) */
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = mkdir(TESTS_PATH, 0775);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret == -1 && errno != EEXIST) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not create %s directory", TESTS_PATH);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return EFAULT;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek test_ctx = talloc_zero(NULL, struct sysdb_test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (test_ctx == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not allocate memory for test context");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return ENOMEM;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek /* Create an event context
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek * It will not be used except in confdb_init and sysdb_init
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek */
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek test_ctx->ev = tevent_context_init(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (test_ctx->ev == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not create event context");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return EIO;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek conf_db = talloc_asprintf(test_ctx, "%s/%s", TESTS_PATH, TEST_CONF_FILE);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (conf_db == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Out of memory, aborting!");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return ENOMEM;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_MINOR_FAILURE, "CONFDB: %s\n", conf_db);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek /* Connect to the conf db */
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = confdb_init(test_ctx, &test_ctx->confdb, conf_db);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret != EOK) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not initialize connection to the confdb");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek val[0] = "LOCAL";
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = confdb_add_param(test_ctx->confdb, true,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "config/sssd", "domains", val);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret != EOK) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not initialize domains placeholder");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek val[0] = "local";
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = confdb_add_param(test_ctx->confdb, true,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "config/domain/LOCAL", "id_provider", val);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret != EOK) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not initialize provider");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek val[0] = "TRUE";
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = confdb_add_param(test_ctx->confdb, true,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "config/domain/LOCAL", "enumerate", val);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret != EOK) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not initialize LOCAL domain");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek val[0] = "TRUE";
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = confdb_add_param(test_ctx->confdb, true,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "config/domain/LOCAL", "cache_credentials", val);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret != EOK) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not initialize LOCAL domain");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
234958be042980242fff6da936af674da877c5efSimo Sorce ret = sssd_domain_init(test_ctx, test_ctx->confdb, "local",
234958be042980242fff6da936af674da877c5efSimo Sorce TESTS_PATH, &test_ctx->domain);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret != EOK) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not initialize connection to the sysdb (%d)", ret);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
234958be042980242fff6da936af674da877c5efSimo Sorce test_ctx->sysdb = test_ctx->domain->sysdb;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek *ctx = test_ctx;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return EOK;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek}
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekstatic void clean_up(void)
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek{
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek int ret = 0;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret += unlink(TESTS_PATH"/"TEST_CONF_FILE);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret += unlink(TESTS_PATH"/sssd.ldb");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret += rmdir(TESTS_PATH);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret != 0) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fprintf(stderr, "Unable to remove all test files from %s\n",TESTS_PATH);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek}
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekstruct test_data {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct tevent_context *ev;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct sysdb_test_ctx *ctx;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek const char *hostname;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek const char *alias;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
2d6836a90bd326391782a5753f70e8ba666b5defJan Cholasta struct ldb_message *host;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct sysdb_attrs *attrs;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek};
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekstatic int test_sysdb_store_ssh_host(struct test_data *data)
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek{
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek int ret;
2d6836a90bd326391782a5753f70e8ba666b5defJan Cholasta time_t now = time(NULL);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
19d8bc19b5b5597427878645644fa354ef6cb54dMichal Zidek ret = sysdb_store_ssh_host(data->ctx->domain,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->hostname,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->alias,
3ac7c4fe618ede980a4df8d90341ef1fd0f1f62fWilliam B data->ctx->domain->ssh_host_timeout,
2d6836a90bd326391782a5753f70e8ba666b5defJan Cholasta now,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->attrs);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek}
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekstatic int test_sysdb_delete_ssh_host(struct test_data *data)
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek{
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek int ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
19d8bc19b5b5597427878645644fa354ef6cb54dMichal Zidek ret = sysdb_delete_ssh_host(data->ctx->domain, data->hostname);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek}
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
2d6836a90bd326391782a5753f70e8ba666b5defJan Cholastastatic int test_sysdb_get_ssh_host(struct test_data *data)
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek{
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek int ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek const char *attrs[] = { SYSDB_NAME, NULL };
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
19d8bc19b5b5597427878645644fa354ef6cb54dMichal Zidek ret = sysdb_get_ssh_host(data->ctx, data->ctx->domain, data->hostname,
19d8bc19b5b5597427878645644fa354ef6cb54dMichal Zidek attrs, &data->host);
2d6836a90bd326391782a5753f70e8ba666b5defJan Cholasta
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek}
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal ZidekSTART_TEST (store_one_host_test)
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek{
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct sysdb_test_ctx *test_ctx;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct test_data *data;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek int ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = setup_sysdb_tests(&test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret != EOK) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not set up the test");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data = talloc_zero(test_ctx, struct test_data);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (data == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Out of memory!");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->ctx = test_ctx;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->ev = test_ctx->ev;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->hostname = talloc_strdup(test_ctx, TEST_HOSTNAME);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (data->hostname == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Out of memory!");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->attrs = sysdb_new_attrs(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (data->attrs == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Out of memory!");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = test_sysdb_store_ssh_host(data);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail_if(ret != EOK, "Could not store host into database");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek}
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal ZidekEND_TEST
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal ZidekSTART_TEST (delete_existing_host_test)
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek{
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct sysdb_test_ctx *test_ctx;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct test_data *data;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek int ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = setup_sysdb_tests(&test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret != EOK) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not set up the test");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data = talloc_zero(test_ctx, struct test_data);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (data == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Out of memory!");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->ctx = test_ctx;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->ev = test_ctx->ev;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->hostname = talloc_strdup(test_ctx, TEST_HOSTNAME);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (data->hostname == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Out of memory!");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = test_sysdb_delete_ssh_host(data);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail_if(ret != EOK, "Could not delete host from database");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek}
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal ZidekEND_TEST
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal ZidekSTART_TEST (delete_nonexistent_host_test)
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek{
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct sysdb_test_ctx *test_ctx;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct test_data *data;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek int ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = setup_sysdb_tests(&test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret != EOK) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not set up the test");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data = talloc_zero(test_ctx, struct test_data);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (data == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Out of memory!");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->ctx = test_ctx;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->ev = test_ctx->ev;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->hostname = talloc_strdup(test_ctx, "nonexistent_host");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (data->hostname == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Out of memory!");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = test_sysdb_delete_ssh_host(data);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail_if(ret != EOK, "Deletion of nonexistent host returned code %d", ret);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek}
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal ZidekEND_TEST
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
2d6836a90bd326391782a5753f70e8ba666b5defJan CholastaSTART_TEST (sysdb_get_ssh_host_test)
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek{
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct sysdb_test_ctx *test_ctx;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct test_data *data;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek int ret;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = setup_sysdb_tests(&test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret != EOK) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not set up test");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data = talloc_zero(test_ctx, struct test_data);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (data == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Out of memory!");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->ctx = test_ctx;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->ev = test_ctx->ev;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->hostname = talloc_strdup(test_ctx, TEST_HOSTNAME);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (data->hostname == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Out of memory!");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek data->attrs = sysdb_new_attrs(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (data->attrs == NULL) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Out of memory!");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = test_sysdb_store_ssh_host(data);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (ret != EOK) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail("Could not store host '%s' to database", TEST_HOSTNAME);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
2d6836a90bd326391782a5753f70e8ba666b5defJan Cholasta ret = test_sysdb_get_ssh_host(data);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fail_if(ret != EOK, "Could not find host '%s'",TEST_HOSTNAME);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_free(test_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek}
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal ZidekEND_TEST
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal ZidekSuite *create_sysdb_ssh_suite(void)
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek{
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek Suite *s = suite_create("sysdb_ssh");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek TCase *tc_sysdb_ssh = tcase_create("SYSDB_SSH Tests");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek tcase_add_test(tc_sysdb_ssh, store_one_host_test);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek tcase_add_test(tc_sysdb_ssh, delete_existing_host_test);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek tcase_add_test(tc_sysdb_ssh, delete_nonexistent_host_test);
2d6836a90bd326391782a5753f70e8ba666b5defJan Cholasta tcase_add_test(tc_sysdb_ssh, sysdb_get_ssh_host_test);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek suite_add_tcase(s, tc_sysdb_ssh);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return s;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek}
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekint main(int argc, const char *argv[])
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek{
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek int failcount;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek int opt;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek poptContext pc;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek Suite* s;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek SRunner *sr;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek struct poptOption long_options[] = {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek POPT_AUTOHELP
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek SSSD_MAIN_OPTS
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek POPT_TABLEEND
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek };
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
49dd8ee2834d9477418961dbaffa4a03cfa9fd1eRené Genz /* Set debug level to invalid value so we can decide if -d 0 was used. */
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek debug_level = SSSDBG_INVALID;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek pc = poptGetContext(argv[0], argc, (const char **) argv, long_options, 0);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek while ((opt = poptGetNextOpt(pc)) != -1) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek fprintf(stderr, "\nInvalid option %s: %s\n\n",
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek poptBadOption(pc, 0), poptStrerror(opt));
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek poptPrintUsage(pc, stderr, 0);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return 1;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek poptFreeContext(pc);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
6b57784f0f175275fd900eca21c77415e3a5ea52Jakub Hrozek DEBUG_CLI_INIT(debug_level);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
dfe84158c49e44f2207b94d25e61ab4f3fe38366Michal Zidek if (!ldb_modules_path_is_set()) {
dfe84158c49e44f2207b94d25e61ab4f3fe38366Michal Zidek fprintf(stderr, "Warning: LDB_MODULES_PATH is not set, "
dfe84158c49e44f2207b94d25e61ab4f3fe38366Michal Zidek "will use LDB plugins installed in system paths.\n");
dfe84158c49e44f2207b94d25e61ab4f3fe38366Michal Zidek }
dfe84158c49e44f2207b94d25e61ab4f3fe38366Michal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek tests_set_cwd();
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek s = create_sysdb_ssh_suite();
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek sr = srunner_create(s);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek srunner_run_all(sr, CK_ENV);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek failcount = srunner_ntests_failed(sr);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek srunner_free(sr);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek clean_up();
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (failcount != 0) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return EXIT_FAILURE;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek }
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek return EXIT_SUCCESS;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek}