LCOV - code coverage report
Current view: top level - tests - fail_over-tests.c (source / functions) Hit Total Coverage
Test: .coverage.total Lines: 123 135 91.1 %
Date: 2015-10-19 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*
       2             :    SSSD
       3             : 
       4             :    Fail over tests.
       5             : 
       6             :    Authors:
       7             :         Martin Nagy <mnagy@redhat.com>
       8             : 
       9             :    Copyright (C) Red Hat, Inc 2009
      10             : 
      11             :    This program is free software; you can redistribute it and/or modify
      12             :    it under the terms of the GNU General Public License as published by
      13             :    the Free Software Foundation; either version 3 of the License, or
      14             :    (at your option) any later version.
      15             : 
      16             :    This program is distributed in the hope that it will be useful,
      17             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             :    GNU General Public License for more details.
      20             : 
      21             :    You should have received a copy of the GNU General Public License
      22             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      23             : */
      24             : 
      25             : #include <arpa/inet.h>
      26             : 
      27             : #include <check.h>
      28             : #include <popt.h>
      29             : #include <stdlib.h>
      30             : #include <stdio.h>
      31             : #include <talloc.h>
      32             : #include <tevent.h>
      33             : 
      34             : #include "resolv/async_resolv.h"
      35             : #include "tests/common_check.h"
      36             : #include "util/util.h"
      37             : 
      38             : /* Interface under test */
      39             : #include "providers/fail_over.h"
      40             : 
      41             : int use_net_test;
      42             : 
      43             : struct test_ctx {
      44             :     struct tevent_context *ev;
      45             :     struct resolv_ctx *resolv;
      46             :     struct fo_ctx *fo_ctx;
      47             :     int tasks;
      48             : };
      49             : 
      50             : struct task {
      51             :     struct test_ctx *test_ctx;
      52             :     const char *location;
      53             :     int recv;
      54             :     int port;
      55             :     int new_server_status;
      56             :     int new_port_status;
      57             : };
      58             : 
      59             : static struct test_ctx *
      60           2 : setup_test(void)
      61             : {
      62             :     struct test_ctx *ctx;
      63             :     struct fo_options fopts;
      64             :     int ret;
      65             : 
      66           2 :     ctx = talloc_zero(global_talloc_context, struct test_ctx);
      67           2 :     fail_if(ctx == NULL, "Could not allocate memory for test context");
      68             : 
      69           2 :     ctx->ev = tevent_context_init(ctx);
      70           2 :     if (ctx->ev == NULL) {
      71           0 :         talloc_free(ctx);
      72           0 :         fail("Could not init tevent context");
      73             :     }
      74             : 
      75           2 :     ret = resolv_init(ctx, ctx->ev, 5, &ctx->resolv);
      76           2 :     if (ret != EOK) {
      77           0 :         talloc_free(ctx);
      78           0 :         fail("Could not init resolv context");
      79             :     }
      80             : 
      81           2 :     memset(&fopts, 0, sizeof(fopts));
      82           2 :     fopts.retry_timeout = 30;
      83           2 :     fopts.family_order  = IPV4_FIRST;
      84             : 
      85           2 :     ctx->fo_ctx = fo_context_init(ctx, &fopts);
      86           2 :     if (ctx->fo_ctx == NULL) {
      87           0 :         talloc_free(ctx);
      88           0 :         fail("Could not init fail over context");
      89             :     }
      90             : 
      91           2 :     return ctx;
      92             : }
      93             : 
      94             : static void
      95           9 : test_loop(struct test_ctx *data)
      96             : {
      97          27 :     while (data->tasks != 0)
      98           9 :         tevent_loop_once(data->ev);
      99           9 : }
     100             : 
     101           1 : START_TEST(test_fo_new_service)
     102             : {
     103             :     int i;
     104             :     int ret;
     105             :     struct test_ctx *ctx;
     106             :     struct fo_service *service;
     107             :     struct fo_service *services[10];
     108             : 
     109           1 :     ctx = setup_test();
     110           1 :     ck_leaks_push(ctx);
     111             : 
     112          11 :     for (i = 0; i < 10; i++) {
     113             :         char buf[16];
     114          10 :         sprintf(buf, "service_%d", i);
     115             : 
     116          10 :         ck_leaks_push(ctx);
     117          10 :         ret = fo_new_service(ctx->fo_ctx, buf, NULL, &services[i]);
     118          10 :         fail_if(ret != EOK);
     119             :     }
     120             : 
     121           1 :     ret = fo_new_service(ctx->fo_ctx, "service_3", NULL, &service);
     122           1 :     fail_if(ret != EEXIST);
     123             : 
     124          11 :     for (i = 9; i >= 0; i--) {
     125             :         char buf[16];
     126          10 :         sprintf(buf, "service_%d", i);
     127             : 
     128          10 :         ret = fo_get_service(ctx->fo_ctx, buf, &service);
     129          10 :         fail_if(ret != EOK);
     130          10 :         fail_if(service != services[i]);
     131          10 :         talloc_free(service);
     132          10 :         ck_leaks_pop(ctx);
     133             : 
     134          10 :         ret = fo_get_service(ctx->fo_ctx, buf, &service);
     135          10 :         fail_if(ret != ENOENT);
     136             :     }
     137             : 
     138           1 :     ck_leaks_pop(ctx);
     139           1 :     talloc_free(ctx);
     140             : }
     141           1 : END_TEST
     142             : 
     143             : static void
     144           9 : test_resolve_service_callback(struct tevent_req *req)
     145             : {
     146             :     uint64_t recv_status;
     147             :     int port;
     148             :     struct task *task;
     149           9 :     struct fo_server *server = NULL;
     150             :     struct resolv_hostent *he;
     151             :     int i;
     152             : 
     153           9 :     task = tevent_req_callback_data(req, struct task);
     154             : 
     155           9 :     task->test_ctx->tasks--;
     156             : 
     157           9 :     recv_status = fo_resolve_service_recv(req, &server);
     158           9 :     talloc_free(req);
     159           9 :     fail_if(recv_status != task->recv, "%s: Expected return of %d, got %d",
     160             :             task->location, task->recv, recv_status);
     161           9 :     if (recv_status != EOK)
     162           2 :         return;
     163           7 :     fail_if(server == NULL);
     164           7 :     port = fo_get_server_port(server);
     165           7 :     fail_if(port != task->port, "%s: Expected port %d, got %d", task->location,
     166             :             task->port, port);
     167             : 
     168           7 :     if (task->new_port_status >= 0)
     169           4 :         fo_set_port_status(server, task->new_port_status);
     170           7 :     if (task->new_server_status >= 0)
     171           2 :         fo_set_server_status(server, task->new_server_status);
     172             : 
     173           7 :     if (fo_get_server_name(server) != NULL) {
     174           6 :         he = fo_get_server_hostent(server);
     175           6 :         fail_if(he == NULL, "fo_get_server_hostent() returned NULL");
     176          12 :         for (i = 0; he->addr_list[i]; i++) {
     177             :             char buf[256];
     178             : 
     179           6 :             inet_ntop(he->family, he->addr_list[i]->ipaddr, buf, sizeof(buf));
     180           6 :             fail_if(strcmp(buf, "127.0.0.1") != 0 && strcmp(buf, "::1") != 0);
     181             :         }
     182             :     }
     183             : 
     184             : }
     185             : 
     186             : #define get_request(a, b, c, d, e, f) \
     187             :        _get_request(a, b, c, d, e, f, __location__)
     188             : 
     189             : static void
     190           9 : _get_request(struct test_ctx *test_ctx, struct fo_service *service,
     191             :              int expected_recv, int expected_port, int new_port_status,
     192             :              int new_server_status, const char *location)
     193             : {
     194             :     struct tevent_req *req;
     195             :     struct task *task;
     196             : 
     197           9 :     task = talloc(test_ctx, struct task);
     198           9 :     fail_if(task == NULL);
     199             : 
     200           9 :     task->test_ctx = test_ctx;
     201           9 :     task->recv = expected_recv;
     202           9 :     task->port = expected_port;
     203           9 :     task->new_port_status = new_port_status;
     204           9 :     task->new_server_status = new_server_status;
     205           9 :     task->location = location;
     206           9 :     test_ctx->tasks++;
     207             : 
     208           9 :     req = fo_resolve_service_send(test_ctx, test_ctx->ev,
     209             :                                   test_ctx->resolv,
     210             :                                   test_ctx->fo_ctx, service);
     211           9 :     fail_if(req == NULL, "%s: fo_resolve_service_send() failed", location);
     212             : 
     213           9 :     tevent_req_set_callback(req, test_resolve_service_callback, task);
     214           9 :     test_loop(test_ctx);
     215           9 : }
     216             : 
     217           1 : START_TEST(test_fo_resolve_service)
     218             : {
     219             :     struct test_ctx *ctx;
     220             :     struct fo_service *service[3];
     221             : 
     222           1 :     ctx = setup_test();
     223           1 :     fail_if(ctx == NULL);
     224             : 
     225             :     /* Add service. */
     226           1 :     fail_if(fo_new_service(ctx->fo_ctx, "http", NULL, &service[0]) != EOK);
     227             : 
     228           1 :     fail_if(fo_new_service(ctx->fo_ctx, "ldap", NULL, &service[1]) != EOK);
     229             : 
     230           1 :     fail_if(fo_new_service(ctx->fo_ctx, "ntp", NULL, &service[2]) != EOK);
     231             : 
     232             :     /* Add servers. */
     233           1 :     fail_if(fo_add_server(service[0], "localhost", 20, NULL, true) != EOK);
     234           1 :     fail_if(fo_add_server(service[0], "127.0.0.1", 80, NULL, false) != EOK);
     235             : 
     236           1 :     fail_if(fo_add_server(service[1], "localhost", 30, NULL, false) != EOK);
     237           1 :     fail_if(fo_add_server(service[1], "127.0.0.1", 389, NULL, true) != EOK);
     238           1 :     fail_if(fo_add_server(service[1], "127.0.0.1", 389, NULL, true) != EEXIST);
     239           1 :     fail_if(fo_add_server(service[1], "127.0.0.1", 389, NULL, false) != EEXIST);
     240             : 
     241           1 :     fail_if(fo_add_server(service[2], NULL, 123, NULL, true) != EOK);
     242             : 
     243             :     /* Make requests. */
     244           1 :     get_request(ctx, service[0], EOK, 20, PORT_WORKING, -1);
     245           1 :     get_request(ctx, service[0], EOK, 20, -1, SERVER_NOT_WORKING);
     246           1 :     get_request(ctx, service[0], EOK, 80, PORT_WORKING, -1);
     247           1 :     get_request(ctx, service[0], EOK, 80, PORT_NOT_WORKING, -1);
     248           1 :     get_request(ctx, service[0], ENOENT, 0, -1, -1);
     249             : 
     250           1 :     get_request(ctx, service[1], EOK, 389, PORT_WORKING, -1);
     251           1 :     get_request(ctx, service[1], EOK, 389, -1, SERVER_NOT_WORKING);
     252           1 :     get_request(ctx, service[1], ENOENT, 0, -1, -1);
     253             : 
     254           1 :     get_request(ctx, service[2], EOK, 123, -1, -1);
     255             : 
     256           1 :     talloc_free(ctx);
     257             : }
     258           1 : END_TEST
     259             : 
     260             : Suite *
     261           1 : create_suite(void)
     262             : {
     263           1 :     Suite *s = suite_create("fail_over");
     264             : 
     265           1 :     TCase *tc = tcase_create("FAIL_OVER Tests");
     266             : 
     267           1 :     tcase_add_checked_fixture(tc, ck_leak_check_setup, ck_leak_check_teardown);
     268             :     /* Do some testing */
     269           1 :     tcase_add_test(tc, test_fo_new_service);
     270           1 :     tcase_add_test(tc, test_fo_resolve_service);
     271           1 :     if (use_net_test) {
     272             :     }
     273             :     /* Add all test cases to the test suite */
     274           1 :     suite_add_tcase(s, tc);
     275             : 
     276           1 :     return s;
     277             : }
     278             : 
     279             : int
     280           1 : main(int argc, const char *argv[])
     281             : {
     282             :     int opt;
     283             :     poptContext pc;
     284             :     int failure_count;
     285             :     Suite *suite;
     286             :     SRunner *sr;
     287             : 
     288           1 :     struct poptOption long_options[] = {
     289             :         POPT_AUTOHELP
     290             :         { "debug-level", 'd', POPT_ARG_INT, &debug_level, 0, "Set debug level", NULL },
     291             :         { "use-net-test", 'n', POPT_ARG_NONE, 0, 'n', "Run tests that need an active internet connection", NULL },
     292             :         POPT_TABLEEND
     293             :     };
     294             : 
     295             :     /* Set debug level to invalid value so we can deside if -d 0 was used. */
     296           1 :     debug_level = SSSDBG_INVALID;
     297             : 
     298           1 :     pc = poptGetContext(argv[0], argc, argv, long_options, 0);
     299           1 :     while((opt = poptGetNextOpt(pc)) != -1) {
     300           0 :         switch(opt) {
     301             :         case 'n':
     302           0 :             use_net_test = 1;
     303           0 :             break;
     304             : 
     305             :         default:
     306           0 :             fprintf(stderr, "\nInvalid option %s: %s\n\n",
     307             :                     poptBadOption(pc, 0), poptStrerror(opt));
     308           0 :             poptPrintUsage(pc, stderr, 0);
     309           0 :             return 1;
     310             :         }
     311             :     }
     312           1 :     poptFreeContext(pc);
     313             : 
     314           1 :     DEBUG_CLI_INIT(debug_level);
     315             : 
     316           1 :     tests_set_cwd();
     317             : 
     318           1 :     suite = create_suite();
     319           1 :     sr = srunner_create(suite);
     320             :     /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
     321           1 :     srunner_run_all(sr, CK_ENV);
     322           1 :     failure_count = srunner_ntests_failed(sr);
     323           1 :     srunner_free(sr);
     324           1 :     return (failure_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
     325             : }

Generated by: LCOV version 1.10