LCOV - code coverage report
Current view: top level - testing - testing_api_helpers.c (source / functions) Hit Total Coverage
Test: GNU Taler anastasis coverage report Lines: 34 54 63.0 %
Date: 2021-06-16 06:33:01 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of ANASTASIS
       3             :   Copyright (C) 2014-2021 Taler Systems SA
       4             : 
       5             :   ANASTASIS is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU General Public License as
       7             :   published by the Free Software Foundation; either version 3, or
       8             :   (at your option) any later version.
       9             : 
      10             :   ANASTASIS is distributed in the hope that it will be useful, but
      11             :   WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :   ANASTASISABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :   GNU General Public License for more details.
      14             : 
      15             :   You should have received a copy of the GNU General Public
      16             :   License along with ANASTASIS; see the file COPYING.  If not, see
      17             :   <http://www.gnu.org/licenses/>
      18             : */
      19             : 
      20             : /**
      21             :  * @file lib/testing_api_helpers.c
      22             :  * @brief helper functions for test library.
      23             :  * @author Christian Grothoff
      24             :  * @author Marcello Stanisci
      25             :  */
      26             : 
      27             : #include "platform.h"
      28             : #include "anastasis_testing_lib.h"
      29             : #include <gnunet/gnunet_curl_lib.h>
      30             : 
      31             : 
      32             : struct GNUNET_OS_Process *
      33           2 : ANASTASIS_TESTING_run_anastasis (const char *config_filename,
      34             :                                  const char *anastasis_url)
      35             : {
      36             :   struct GNUNET_OS_Process *anastasis_proc;
      37             :   unsigned int iter;
      38             :   char *wget_cmd;
      39             : 
      40             :   anastasis_proc
      41           2 :     = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ALL,
      42             :                                NULL, NULL, NULL,
      43             :                                "anastasis-httpd",
      44             :                                "anastasis-httpd",
      45             :                                "--log=INFO",
      46             :                                "-c", config_filename,
      47             :                                NULL);
      48           2 :   if (NULL == anastasis_proc)
      49           0 :     ANASTASIS_FAIL ();
      50             : 
      51           2 :   GNUNET_asprintf (&wget_cmd,
      52             :                    "wget -q -t 1 -T 1"
      53             :                    " %s"
      54             :                    " -o /dev/null -O /dev/null",
      55             :                    anastasis_url);
      56             : 
      57             :   /* give child time to start and bind against the socket */
      58           2 :   fprintf (stderr,
      59             :            "Waiting for `anastasis-httpd' to be ready\n");
      60           2 :   iter = 0;
      61             :   do
      62             :   {
      63          18 :     if (100 == iter)
      64             :     {
      65           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      66             :                   "Failed to launch `anastasis-httpd' (or `wget')\n");
      67           0 :       GNUNET_OS_process_kill (anastasis_proc,
      68             :                               SIGTERM);
      69           0 :       GNUNET_OS_process_wait (anastasis_proc);
      70           0 :       GNUNET_OS_process_destroy (anastasis_proc);
      71           0 :       ANASTASIS_FAIL ();
      72             :     }
      73             :     {
      74          18 :       struct timespec req = {
      75             :         .tv_nsec = 10000
      76             :       };
      77             : 
      78          18 :       nanosleep (&req,
      79             :                  NULL);
      80             :     }
      81          18 :     iter++;
      82             :   }
      83          18 :   while (0 != system (wget_cmd));
      84           2 :   GNUNET_free (wget_cmd);
      85           2 :   fprintf (stderr,
      86             :            "\n");
      87           2 :   return anastasis_proc;
      88             : }
      89             : 
      90             : 
      91             : char *
      92           2 : ANASTASIS_TESTING_prepare_anastasis (const char *config_filename)
      93             : {
      94             :   struct GNUNET_CONFIGURATION_Handle *cfg;
      95             :   unsigned long long port;
      96             :   struct GNUNET_OS_Process *dbinit_proc;
      97             :   enum GNUNET_OS_ProcessStatusType type;
      98             :   unsigned long code;
      99             :   char *base_url;
     100             : 
     101           2 :   cfg = GNUNET_CONFIGURATION_create ();
     102           2 :   if (GNUNET_OK !=
     103           2 :       GNUNET_CONFIGURATION_load (cfg,
     104             :                                  config_filename))
     105           0 :     ANASTASIS_FAIL ();
     106           2 :   if (GNUNET_OK !=
     107           2 :       GNUNET_CONFIGURATION_get_value_number (cfg,
     108             :                                              "anastasis",
     109             :                                              "PORT",
     110             :                                              &port))
     111             :   {
     112           0 :     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     113             :                                "anastasis",
     114             :                                "PORT");
     115           0 :     GNUNET_CONFIGURATION_destroy (cfg);
     116           0 :     return NULL;
     117             :   }
     118             : 
     119           2 :   GNUNET_CONFIGURATION_destroy (cfg);
     120             : 
     121           2 :   if (GNUNET_OK !=
     122           2 :       GNUNET_NETWORK_test_port_free (IPPROTO_TCP,
     123           2 :                                      (uint16_t) port))
     124             :   {
     125           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     126             :                 "Required port %llu not available, skipping.\n",
     127             :                 port);
     128           0 :     return NULL;
     129             :   }
     130             : 
     131             :   /* DB preparation */
     132           2 :   if (NULL == (dbinit_proc = GNUNET_OS_start_process
     133             :                                (GNUNET_OS_INHERIT_STD_ALL,
     134             :                                NULL, NULL, NULL,
     135             :                                "anastasis-dbinit",
     136             :                                "anastasis-dbinit",
     137             :                                "-c", config_filename,
     138             :                                "-r",
     139             :                                NULL)))
     140             :   {
     141           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     142             :                 "Failed to run anastasis-dbinit. Check your PATH.\n");
     143           0 :     return NULL;
     144             :   }
     145             : 
     146           2 :   if (GNUNET_SYSERR ==
     147           2 :       GNUNET_OS_process_wait_status (dbinit_proc,
     148             :                                      &type,
     149             :                                      &code))
     150             :   {
     151           0 :     GNUNET_OS_process_destroy (dbinit_proc);
     152           0 :     ANASTASIS_FAIL ();
     153             :   }
     154           2 :   if ( (type == GNUNET_OS_PROCESS_EXITED) &&
     155           2 :        (0 != code) )
     156             :   {
     157           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     158             :                 "Failed to setup database\n");
     159           0 :     return NULL;
     160             :   }
     161           2 :   if ( (type != GNUNET_OS_PROCESS_EXITED) ||
     162           2 :        (0 != code) )
     163             :   {
     164           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     165             :                 "Unexpected error running `anastasis-dbinit'!\n");
     166           0 :     return NULL;
     167             :   }
     168           2 :   GNUNET_OS_process_destroy (dbinit_proc);
     169           2 :   GNUNET_asprintf (&base_url,
     170             :                    "http://localhost:%llu/",
     171             :                    port);
     172           2 :   return base_url;
     173             : }

Generated by: LCOV version 1.14