LCOV - code coverage report
Current view: top level - tests - sysdb_ssh-tests.c (source / functions) Hit Total Coverage
Test: .coverage.total Lines: 130 161 80.7 %
Date: 2015-10-19 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /*
       2             :    Authors:
       3             :     Michal Zidek <mzidek@redhat.com>
       4             :     Stephen Gallagher <sgallagh@redhat.com>
       5             : 
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include <stdlib.h>
      21             : #include <check.h>
      22             : #include <popt.h>
      23             : #include <talloc.h>
      24             : #include <sys/stat.h>
      25             : #include <sys/types.h>
      26             : 
      27             : 
      28             : #include "config.h"
      29             : #include "tests/common.h"
      30             : #include "util/util.h"
      31             : #include "confdb/confdb.h"
      32             : #include "confdb/confdb_setup.h"
      33             : #include "db/sysdb.h"
      34             : #include "db/sysdb_services.h"
      35             : #include "db/sysdb_ssh.h"
      36             : 
      37             : #define TESTS_PATH "tp_" BASE_FILE_STEM
      38             : #define TEST_CONF_FILE "tests_conf.ldb"
      39             : #define TEST_HOSTNAME "testhost"
      40             : 
      41             : struct sysdb_test_ctx {
      42             :     struct sysdb_ctx *sysdb;
      43             :     struct confdb_ctx *confdb;
      44             :     struct tevent_context *ev;
      45             :     struct sss_domain_info *domain;
      46             : };
      47             : 
      48           4 : static int setup_sysdb_tests(struct sysdb_test_ctx **ctx)
      49             : {
      50             :     struct sysdb_test_ctx *test_ctx;
      51             :     char *conf_db;
      52             :     int ret;
      53             : 
      54             :     const char *val[2];
      55           4 :     val[1] = NULL;
      56             : 
      57             :     /* Create tests directory if it doesn't exist */
      58             :     /* (relative to current dir) */
      59           4 :     ret = mkdir(TESTS_PATH, 0775);
      60           4 :     if (ret == -1 && errno != EEXIST) {
      61           0 :         fail("Could not create %s directory", TESTS_PATH);
      62             :         return EFAULT;
      63             :     }
      64             : 
      65           4 :     test_ctx = talloc_zero(NULL, struct sysdb_test_ctx);
      66           4 :     if (test_ctx == NULL) {
      67           0 :         fail("Could not allocate memory for test context");
      68             :         return ENOMEM;
      69             :     }
      70             : 
      71             :     /* Create an event context
      72             :      * It will not be used except in confdb_init and sysdb_init
      73             :      */
      74           4 :     test_ctx->ev = tevent_context_init(test_ctx);
      75           4 :     if (test_ctx->ev == NULL) {
      76           0 :         fail("Could not create event context");
      77             :         talloc_free(test_ctx);
      78             :         return EIO;
      79             :     }
      80             : 
      81           4 :     conf_db = talloc_asprintf(test_ctx, "%s/%s", TESTS_PATH, TEST_CONF_FILE);
      82           4 :     if (conf_db == NULL) {
      83           0 :         fail("Out of memory, aborting!");
      84             :         talloc_free(test_ctx);
      85             :         return ENOMEM;
      86             :     }
      87           4 :     DEBUG(SSSDBG_MINOR_FAILURE, "CONFDB: %s\n", conf_db);
      88             : 
      89             :     /* Connect to the conf db */
      90           4 :     ret = confdb_init(test_ctx, &test_ctx->confdb, conf_db);
      91           4 :     if (ret != EOK) {
      92           0 :         fail("Could not initialize connection to the confdb");
      93             :         talloc_free(test_ctx);
      94             :         return ret;
      95             :     }
      96             : 
      97           4 :     val[0] = "LOCAL";
      98           4 :     ret = confdb_add_param(test_ctx->confdb, true,
      99             :                            "config/sssd", "domains", val);
     100           4 :     if (ret != EOK) {
     101           0 :         fail("Could not initialize domains placeholder");
     102             :         talloc_free(test_ctx);
     103             :         return ret;
     104             :     }
     105             : 
     106           4 :     val[0] = "local";
     107           4 :     ret = confdb_add_param(test_ctx->confdb, true,
     108             :                            "config/domain/LOCAL", "id_provider", val);
     109           4 :     if (ret != EOK) {
     110           0 :         fail("Could not initialize provider");
     111             :         talloc_free(test_ctx);
     112             :         return ret;
     113             :     }
     114             : 
     115           4 :     val[0] = "TRUE";
     116           4 :     ret = confdb_add_param(test_ctx->confdb, true,
     117             :                            "config/domain/LOCAL", "enumerate", val);
     118           4 :     if (ret != EOK) {
     119           0 :         fail("Could not initialize LOCAL domain");
     120             :         talloc_free(test_ctx);
     121             :         return ret;
     122             :     }
     123             : 
     124           4 :     val[0] = "TRUE";
     125           4 :     ret = confdb_add_param(test_ctx->confdb, true,
     126             :                            "config/domain/LOCAL", "cache_credentials", val);
     127           4 :     if (ret != EOK) {
     128           0 :         fail("Could not initialize LOCAL domain");
     129             :         talloc_free(test_ctx);
     130             :         return ret;
     131             :     }
     132             : 
     133           4 :     ret = sssd_domain_init(test_ctx, test_ctx->confdb, "local",
     134             :                            TESTS_PATH, &test_ctx->domain);
     135           4 :     if (ret != EOK) {
     136           0 :         fail("Could not initialize connection to the sysdb (%d)", ret);
     137             :         talloc_free(test_ctx);
     138             :         return ret;
     139             :     }
     140           4 :     test_ctx->sysdb = test_ctx->domain->sysdb;
     141             : 
     142           4 :     *ctx = test_ctx;
     143           4 :     return EOK;
     144             : }
     145             : 
     146           1 : static void clean_up(void)
     147             : {
     148           1 :     int ret = 0;
     149             : 
     150           1 :     ret += unlink(TESTS_PATH"/"TEST_CONF_FILE);
     151           1 :     ret += unlink(TESTS_PATH"/sssd.ldb");
     152           1 :     ret += rmdir(TESTS_PATH);
     153             : 
     154           1 :     if (ret != 0) {
     155           0 :         fprintf(stderr, "Unable to remove all test files from %s\n",TESTS_PATH);
     156             :     }
     157           1 : }
     158             : 
     159             : struct test_data {
     160             :     struct tevent_context *ev;
     161             :     struct sysdb_test_ctx *ctx;
     162             : 
     163             :     const char *hostname;
     164             :     const char *alias;
     165             : 
     166             :     struct ldb_message *host;
     167             :     struct sysdb_attrs *attrs;
     168             : };
     169             : 
     170           2 : static int test_sysdb_store_ssh_host(struct test_data *data)
     171             : {
     172             :     int ret;
     173           2 :     time_t now = time(NULL);
     174             : 
     175           4 :     ret = sysdb_store_ssh_host(data->ctx->domain,
     176             :                                data->hostname,
     177             :                                data->alias,
     178           2 :                                data->ctx->domain->ssh_host_timeout,
     179             :                                now,
     180             :                                data->attrs);
     181           2 :     return ret;
     182             : }
     183             : 
     184           2 : static int test_sysdb_delete_ssh_host(struct test_data *data)
     185             : {
     186             :     int ret;
     187             : 
     188           2 :     ret = sysdb_delete_ssh_host(data->ctx->domain, data->hostname);
     189           2 :     return ret;
     190             : }
     191             : 
     192           1 : static int test_sysdb_get_ssh_host(struct test_data *data)
     193             : {
     194             :     int ret;
     195           1 :     const char *attrs[] = { SYSDB_NAME, NULL };
     196             : 
     197           1 :     ret = sysdb_get_ssh_host(data->ctx, data->ctx->domain, data->hostname,
     198             :                              attrs, &data->host);
     199             : 
     200           1 :     return ret;
     201             : }
     202             : 
     203           1 : START_TEST (store_one_host_test)
     204             : {
     205             :     struct sysdb_test_ctx *test_ctx;
     206             :     struct test_data *data;
     207             :     int ret;
     208             : 
     209           1 :     ret = setup_sysdb_tests(&test_ctx);
     210           1 :     if (ret != EOK) {
     211           0 :         fail("Could not set up the test");
     212             :         return;
     213             :     }
     214             : 
     215           1 :     data = talloc_zero(test_ctx, struct test_data);
     216           1 :     if (data == NULL) {
     217           0 :         fail("Out of memory!");
     218             :         talloc_free(test_ctx);
     219             :         return;
     220             :     }
     221             : 
     222           1 :     data->ctx = test_ctx;
     223           1 :     data->ev = test_ctx->ev;
     224           1 :     data->hostname = talloc_strdup(test_ctx, TEST_HOSTNAME);
     225           1 :     if (data->hostname == NULL) {
     226           0 :         fail("Out of memory!");
     227             :         talloc_free(test_ctx);
     228             :         return;
     229             :     }
     230             : 
     231           1 :     data->attrs = sysdb_new_attrs(test_ctx);
     232           1 :     if (data->attrs == NULL) {
     233           0 :         fail("Out of memory!");
     234             :         talloc_free(test_ctx);
     235             :         return;
     236             :     }
     237             : 
     238           1 :     ret = test_sysdb_store_ssh_host(data);
     239             : 
     240           1 :     fail_if(ret != EOK, "Could not store host into database");
     241           1 :     talloc_free(test_ctx);
     242             : }
     243             : END_TEST
     244             : 
     245           1 : START_TEST (delete_existing_host_test)
     246             : {
     247             :     struct sysdb_test_ctx *test_ctx;
     248             :     struct test_data *data;
     249             :     int ret;
     250             : 
     251           1 :     ret = setup_sysdb_tests(&test_ctx);
     252           1 :     if (ret != EOK) {
     253           0 :         fail("Could not set up the test");
     254             :         return;
     255             :     }
     256             : 
     257           1 :     data = talloc_zero(test_ctx, struct test_data);
     258           1 :     if (data == NULL) {
     259           0 :         fail("Out of memory!");
     260             :         return;
     261             :     }
     262             : 
     263           1 :     data->ctx = test_ctx;
     264           1 :     data->ev = test_ctx->ev;
     265           1 :     data->hostname = talloc_strdup(test_ctx, TEST_HOSTNAME);
     266           1 :     if (data->hostname == NULL) {
     267           0 :         fail("Out of memory!");
     268             :         talloc_free(test_ctx);
     269             :         return;
     270             :     }
     271             : 
     272           1 :     ret = test_sysdb_delete_ssh_host(data);
     273             : 
     274           1 :     fail_if(ret != EOK, "Could not delete host from database");
     275           1 :     talloc_free(test_ctx);
     276             : }
     277             : END_TEST
     278             : 
     279           1 : START_TEST (delete_nonexistent_host_test)
     280             : {
     281             :     struct sysdb_test_ctx *test_ctx;
     282             :     struct test_data *data;
     283             :     int ret;
     284             : 
     285           1 :     ret = setup_sysdb_tests(&test_ctx);
     286           1 :     if (ret != EOK) {
     287           0 :         fail("Could not set up the test");
     288             :         return;
     289             :     }
     290             : 
     291           1 :     data = talloc_zero(test_ctx, struct test_data);
     292           1 :     if (data == NULL) {
     293           0 :         fail("Out of memory!");
     294             :         talloc_free(test_ctx);
     295             :         return;
     296             :     }
     297             : 
     298           1 :     data->ctx = test_ctx;
     299           1 :     data->ev = test_ctx->ev;
     300           1 :     data->hostname = talloc_strdup(test_ctx, "nonexistent_host");
     301           1 :     if (data->hostname == NULL) {
     302           0 :         fail("Out of memory!");
     303             :         talloc_free(test_ctx);
     304             :         return;
     305             :     }
     306             : 
     307           1 :     ret = test_sysdb_delete_ssh_host(data);
     308             : 
     309           1 :     fail_if(ret != EOK, "Deletion of nonexistent host returned code %d", ret);
     310           1 :     talloc_free(test_ctx);
     311             : 
     312             : }
     313             : END_TEST
     314             : 
     315           1 : START_TEST (sysdb_get_ssh_host_test)
     316             : {
     317             :     struct sysdb_test_ctx *test_ctx;
     318             :     struct test_data *data;
     319             :     int ret;
     320             : 
     321           1 :     ret = setup_sysdb_tests(&test_ctx);
     322           1 :     if (ret != EOK) {
     323           0 :         fail("Could not set up test");
     324             :         return;
     325             :     }
     326             : 
     327           1 :     data = talloc_zero(test_ctx, struct test_data);
     328           1 :     if (data == NULL) {
     329           0 :         fail("Out of memory!");
     330             :         talloc_free(test_ctx);
     331             :         return;
     332             :     }
     333             : 
     334           1 :     data->ctx = test_ctx;
     335           1 :     data->ev = test_ctx->ev;
     336           1 :     data->hostname = talloc_strdup(test_ctx, TEST_HOSTNAME);
     337           1 :     if (data->hostname == NULL) {
     338           0 :         fail("Out of memory!");
     339             :         talloc_free(test_ctx);
     340             :         return;
     341             :     }
     342             : 
     343           1 :     data->attrs = sysdb_new_attrs(test_ctx);
     344           1 :     if (data->attrs == NULL) {
     345           0 :         fail("Out of memory!");
     346             :         talloc_free(test_ctx);
     347             :         return;
     348             :     }
     349             : 
     350           1 :     ret = test_sysdb_store_ssh_host(data);
     351           1 :     if (ret != EOK) {
     352           0 :         fail("Could not store host '%s' to database", TEST_HOSTNAME);
     353             :         talloc_free(test_ctx);
     354             :         return;
     355             :     }
     356             : 
     357           1 :     ret = test_sysdb_get_ssh_host(data);
     358             : 
     359           1 :     fail_if(ret != EOK, "Could not find host '%s'",TEST_HOSTNAME);
     360           1 :     talloc_free(test_ctx);
     361             : }
     362             : END_TEST
     363             : 
     364             : 
     365           1 : Suite *create_sysdb_ssh_suite(void)
     366             : {
     367           1 :     Suite *s = suite_create("sysdb_ssh");
     368           1 :     TCase *tc_sysdb_ssh = tcase_create("SYSDB_SSH Tests");
     369             : 
     370           1 :     tcase_add_test(tc_sysdb_ssh, store_one_host_test);
     371           1 :     tcase_add_test(tc_sysdb_ssh, delete_existing_host_test);
     372           1 :     tcase_add_test(tc_sysdb_ssh, delete_nonexistent_host_test);
     373           1 :     tcase_add_test(tc_sysdb_ssh, sysdb_get_ssh_host_test);
     374           1 :     suite_add_tcase(s, tc_sysdb_ssh);
     375           1 :     return s;
     376             : }
     377             : 
     378           1 : int main(int argc, const char *argv[])
     379             : {
     380             :     int failcount;
     381             :     int opt;
     382             :     poptContext pc;
     383             :     Suite* s;
     384             :     SRunner *sr;
     385             : 
     386           6 :     struct poptOption long_options[] = {
     387             :         POPT_AUTOHELP
     388           5 :         SSSD_MAIN_OPTS
     389             :         POPT_TABLEEND
     390             :     };
     391             : 
     392             :     /* Set debug level to invalid value so we can deside if -d 0 was used. */
     393           1 :     debug_level = SSSDBG_INVALID;
     394             : 
     395           1 :     pc = poptGetContext(argv[0], argc, (const char **) argv, long_options, 0);
     396           1 :     while ((opt = poptGetNextOpt(pc)) != -1) {
     397           0 :         fprintf(stderr, "\nInvalid option %s: %s\n\n",
     398             :                 poptBadOption(pc, 0), poptStrerror(opt));
     399           0 :         poptPrintUsage(pc, stderr, 0);
     400           0 :         return 1;
     401             :     }
     402           1 :     poptFreeContext(pc);
     403             : 
     404           1 :     DEBUG_CLI_INIT(debug_level);
     405             : 
     406           1 :     if (!ldb_modules_path_is_set()) {
     407           0 :         fprintf(stderr, "Warning: LDB_MODULES_PATH is not set, "
     408             :                 "will use LDB plugins installed in system paths.\n");
     409             :     }
     410             : 
     411           1 :     tests_set_cwd();
     412             : 
     413           1 :     s = create_sysdb_ssh_suite();
     414             : 
     415           1 :     sr = srunner_create(s);
     416           1 :     srunner_run_all(sr, CK_ENV);
     417           1 :     failcount = srunner_ntests_failed(sr);
     418           1 :     srunner_free(sr);
     419             : 
     420           1 :     clean_up();
     421           1 :     if (failcount != 0) {
     422           0 :         return EXIT_FAILURE;
     423             :     }
     424             : 
     425           1 :     return EXIT_SUCCESS;
     426             : }

Generated by: LCOV version 1.10