anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

testing_api_helpers.c (5485B)


      1 /*
      2   This file is part of ANASTASIS
      3   Copyright (C) 2014-2021 Anastasis SARL
      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 anastasis/src/testing/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_Process *
     33 ANASTASIS_TESTING_run_anastasis (const char *config_filename,
     34                                  const char *anastasis_url)
     35 {
     36   struct GNUNET_Process *anastasis_proc;
     37   unsigned int iter;
     38   char *wget_cmd;
     39 
     40   anastasis_proc = GNUNET_process_create (GNUNET_OS_INHERIT_STD_ALL);
     41   if (GNUNET_OK !=
     42       GNUNET_process_run_command_va (anastasis_proc,
     43                                      "anastasis-httpd",
     44                                      "anastasis-httpd",
     45                                      "--log=INFO",
     46                                      "-c", config_filename,
     47                                      NULL))
     48   {
     49     GNUNET_process_destroy (anastasis_proc);
     50     ANASTASIS_FAIL ();
     51   }
     52 
     53   GNUNET_asprintf (&wget_cmd,
     54                    "wget -q -t 1 -T 1"
     55                    " %s"
     56                    " -o /dev/null -O /dev/null",
     57                    anastasis_url);
     58 
     59   /* give child time to start and bind against the socket */
     60   fprintf (stderr,
     61            "Waiting for `anastasis-httpd' to be ready\n");
     62   iter = 0;
     63   do
     64   {
     65     if (100 == iter)
     66     {
     67       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     68                   "Failed to launch `anastasis-httpd' (or `wget')\n");
     69       GNUNET_break (GNUNET_OK ==
     70                     GNUNET_process_kill (anastasis_proc,
     71                                          SIGTERM));
     72       GNUNET_break (GNUNET_OK ==
     73                     GNUNET_process_wait (anastasis_proc,
     74                                          true,
     75                                          NULL,
     76                                          NULL));
     77       GNUNET_process_destroy (anastasis_proc);
     78       ANASTASIS_FAIL ();
     79     }
     80     {
     81       struct timespec req = {
     82         .tv_nsec = 10000
     83       };
     84 
     85       nanosleep (&req,
     86                  NULL);
     87     }
     88     iter++;
     89   }
     90   while (0 != system (wget_cmd));
     91   GNUNET_free (wget_cmd);
     92   fprintf (stderr,
     93            "\n");
     94   return anastasis_proc;
     95 }
     96 
     97 
     98 char *
     99 ANASTASIS_TESTING_prepare_anastasis (const char *config_filename)
    100 {
    101   struct GNUNET_CONFIGURATION_Handle *cfg;
    102   unsigned long long port;
    103   struct GNUNET_Process *dbinit_proc;
    104   enum GNUNET_OS_ProcessStatusType type;
    105   unsigned long code;
    106   char *base_url;
    107 
    108   cfg = GNUNET_CONFIGURATION_create (ANASTASIS_project_data ());
    109   if (GNUNET_OK !=
    110       GNUNET_CONFIGURATION_load (cfg,
    111                                  config_filename))
    112     ANASTASIS_FAIL ();
    113   if (GNUNET_OK !=
    114       GNUNET_CONFIGURATION_get_value_number (cfg,
    115                                              "anastasis",
    116                                              "PORT",
    117                                              &port))
    118   {
    119     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
    120                                "anastasis",
    121                                "PORT");
    122     GNUNET_CONFIGURATION_destroy (cfg);
    123     return NULL;
    124   }
    125 
    126   GNUNET_CONFIGURATION_destroy (cfg);
    127 
    128   if (GNUNET_OK !=
    129       GNUNET_NETWORK_test_port_free (IPPROTO_TCP,
    130                                      (uint16_t) port))
    131   {
    132     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    133                 "Required port %llu not available, skipping.\n",
    134                 port);
    135     return NULL;
    136   }
    137 
    138   /* DB preparation */
    139   dbinit_proc = GNUNET_process_create (GNUNET_OS_INHERIT_STD_ALL);
    140   if (GNUNET_OK !=
    141       GNUNET_process_run_command_va (dbinit_proc,
    142                                      "anastasis-dbinit",
    143                                      "anastasis-dbinit",
    144                                      "-c", config_filename,
    145                                      "-r",
    146                                      NULL))
    147   {
    148     GNUNET_process_destroy (dbinit_proc);
    149     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    150                 "Failed to run anastasis-dbinit. Check your PATH.\n");
    151     return NULL;
    152   }
    153 
    154   if (GNUNET_SYSERR ==
    155       GNUNET_process_wait (dbinit_proc,
    156                            true,
    157                            &type,
    158                            &code))
    159   {
    160     GNUNET_process_destroy (dbinit_proc);
    161     ANASTASIS_FAIL ();
    162   }
    163   GNUNET_process_destroy (dbinit_proc);
    164   if ( (type == GNUNET_OS_PROCESS_EXITED) &&
    165        (0 != code) )
    166   {
    167     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    168                 "Failed to setup database\n");
    169     return NULL;
    170   }
    171   if ( (type != GNUNET_OS_PROCESS_EXITED) ||
    172        (0 != code) )
    173   {
    174     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    175                 "Unexpected error running `anastasis-dbinit'!\n");
    176     return NULL;
    177   }
    178   GNUNET_asprintf (&base_url,
    179                    "http://localhost:%llu/",
    180                    port);
    181   return base_url;
    182 }