exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

testing_api_twister_helpers.c (6015B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2014-2018 Taler Systems SA
      4 
      5   TALER 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   TALER is distributed in the hope that it will be useful, but
     11   WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY 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 TALER; see the file COPYING.  If not, see
     17   <http://www.gnu.org/licenses/>
     18 */
     19 
     20 /**
     21  * @file testing_api_twister_helpers.c
     22  * @brief helper functions for test library.
     23  * @author Christian Grothoff
     24  * @author Marcello Stanisci
     25  */
     26 #include "taler/platform.h"
     27 #include <gnunet/gnunet_util_lib.h>
     28 #include <taler/taler_twister_service.h>
     29 #include "taler/taler_twister_testing_lib.h"
     30 
     31 
     32 /**
     33  * Prepare twister for execution; mainly checks whether the
     34  * HTTP port is available and construct the base URL based on it.
     35  *
     36  * @param config_filename configuration file name.
     37  * @return twister base URL, NULL upon errors.
     38  */
     39 char *
     40 TALER_TWISTER_prepare_twister (const char *config_filename)
     41 {
     42   struct GNUNET_CONFIGURATION_Handle *cfg;
     43   unsigned long long port;
     44   char *base_url;
     45 
     46   cfg = GNUNET_CONFIGURATION_create (TWISTER_project_data ());
     47   if (GNUNET_OK !=
     48       GNUNET_CONFIGURATION_load (cfg,
     49                                  config_filename))
     50     TWISTER_FAIL ();
     51 
     52   if (GNUNET_OK !=
     53       GNUNET_CONFIGURATION_get_value_number (cfg,
     54                                              "twister",
     55                                              "HTTP_PORT",
     56                                              &port))
     57   {
     58     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     59                                "twister",
     60                                "HTTP_PORT");
     61     GNUNET_CONFIGURATION_destroy (cfg);
     62     TWISTER_FAIL ();
     63   }
     64 
     65   GNUNET_CONFIGURATION_destroy (cfg);
     66 
     67   if (GNUNET_OK !=
     68       GNUNET_NETWORK_test_port_free (IPPROTO_TCP,
     69                                      (uint16_t) port))
     70   {
     71     fprintf (stderr,
     72              "Required port %llu not available, skipping.\n",
     73              port);
     74     TWISTER_FAIL ();
     75   }
     76 
     77   GNUNET_assert (0 < GNUNET_asprintf
     78                    (&base_url,
     79                    "http://localhost:%llu/",
     80                    port));
     81 
     82   return base_url;
     83 }
     84 
     85 
     86 /**
     87  * Run the twister service.
     88  *
     89  * @param config_filename configuration file name.
     90  * @return twister process handle, NULL upon errors.
     91  */
     92 struct GNUNET_Process *
     93 TALER_TWISTER_run_twister (const char *config_filename)
     94 {
     95   struct GNUNET_Process *proc;
     96   struct GNUNET_Process *client_proc;
     97   unsigned long code;
     98   enum GNUNET_OS_ProcessStatusType type;
     99 
    100   proc = GNUNET_process_create (GNUNET_OS_INHERIT_STD_ALL);
    101   if (GNUNET_OK !=
    102       GNUNET_process_run_command_va (proc,
    103                                      "taler-twister-service",
    104                                      "taler-twister-service",
    105                                      "-c", config_filename,
    106                                      NULL))
    107     TWISTER_FAIL ();
    108 
    109   client_proc = GNUNET_process_create (GNUNET_OS_INHERIT_STD_ALL);
    110   if (GNUNET_OK !=
    111       GNUNET_process_run_command_va (client_proc,
    112                                      "taler-twister",
    113                                      "taler-twister",
    114                                      "-c", config_filename,
    115                                      "-a",
    116                                      NULL))
    117   {
    118     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    119                 "Could not start the taler-twister client\n");
    120     GNUNET_break (GNUNET_OK ==
    121                   GNUNET_process_kill (proc,
    122                                        SIGTERM));
    123     GNUNET_break (GNUNET_OK ==
    124                   GNUNET_process_wait (proc,
    125                                        true,
    126                                        NULL,
    127                                        NULL));
    128     GNUNET_process_destroy (proc);
    129     TWISTER_FAIL ();
    130   }
    131 
    132   if (GNUNET_SYSERR ==
    133       GNUNET_process_wait (client_proc,
    134                            true,
    135                            &type,
    136                            &code))
    137   {
    138     GNUNET_process_destroy (client_proc);
    139     GNUNET_break (GNUNET_OK ==
    140                   GNUNET_process_kill (proc,
    141                                        SIGTERM));
    142     GNUNET_break (GNUNET_OK ==
    143                   GNUNET_process_wait (proc,
    144                                        true,
    145                                        NULL,
    146                                        NULL));
    147     GNUNET_process_destroy (proc);
    148     TWISTER_FAIL ();
    149   }
    150   if ( (type == GNUNET_OS_PROCESS_EXITED) &&
    151        (0 != code) )
    152   {
    153     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    154                 "Failed to check twister works.\n");
    155     GNUNET_process_destroy (client_proc);
    156     GNUNET_break (GNUNET_OK ==
    157                   GNUNET_process_kill (proc,
    158                                        SIGTERM));
    159     GNUNET_break (GNUNET_OK ==
    160                   GNUNET_process_wait (proc,
    161                                        true,
    162                                        NULL,
    163                                        NULL));
    164     GNUNET_process_destroy (proc);
    165     TWISTER_FAIL ();
    166   }
    167   if ( (type != GNUNET_OS_PROCESS_EXITED) ||
    168        (0 != code) )
    169   {
    170     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    171                 "Unexpected error running `taler-twister'!\n");
    172     GNUNET_process_destroy (client_proc);
    173     GNUNET_break (GNUNET_OK ==
    174                   GNUNET_process_kill (proc,
    175                                        SIGTERM));
    176     GNUNET_break (GNUNET_OK ==
    177                   GNUNET_process_wait (proc,
    178                                        true,
    179                                        NULL,
    180                                        NULL));
    181     GNUNET_process_destroy (proc);
    182     TWISTER_FAIL ();
    183   }
    184   GNUNET_process_destroy (client_proc);
    185 
    186   return proc;
    187 }