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

          Line data    Source code
       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_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 <taler/taler_exchange_service.h>
      29             : #include <taler/taler_testing_lib.h>
      30             : #include "taler_merchant_testing_lib.h"
      31             : 
      32             : 
      33             : struct GNUNET_OS_Process *
      34           2 : TALER_TESTING_run_merchant (const char *config_filename,
      35             :                             const char *merchant_url)
      36             : {
      37             :   struct GNUNET_OS_Process *merchant_proc;
      38             :   unsigned int iter;
      39             :   char *wget_cmd;
      40             : 
      41             :   merchant_proc
      42           2 :     = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ALL,
      43             :                                NULL, NULL, NULL,
      44             :                                "taler-merchant-httpd",
      45             :                                "taler-merchant-httpd",
      46             :                                "--log=INFO",
      47             :                                "-c", config_filename,
      48             :                                NULL);
      49           2 :   if (NULL == merchant_proc)
      50           0 :     MERCHANT_FAIL ();
      51             : 
      52           2 :   GNUNET_asprintf (&wget_cmd,
      53             :                    "wget -q -t 1 -T 1"
      54             :                    " --header='Authorization: ApiKey sandbox'"
      55             :                    " %s"
      56             :                    " -o /dev/null -O /dev/null",
      57             :                    merchant_url);
      58             : 
      59             :   /* give child time to start and bind against the socket */
      60           2 :   fprintf (stderr,
      61             :            "Waiting for `taler-merchant-httpd' to be ready\n");
      62           2 :   iter = 0;
      63             :   do
      64             :   {
      65           2 :     if (10 == iter)
      66             :     {
      67           0 :       fprintf (stderr,
      68             :                "Failed to launch"
      69             :                " `taler-merchant-httpd' (or `wget')\n");
      70           0 :       GNUNET_OS_process_kill (merchant_proc,
      71             :                               SIGTERM);
      72           0 :       GNUNET_OS_process_wait (merchant_proc);
      73           0 :       GNUNET_OS_process_destroy (merchant_proc);
      74           0 :       MERCHANT_FAIL ();
      75             :     }
      76           2 :     fprintf (stderr, ".\n");
      77           2 :     sleep (1);
      78           2 :     iter++;
      79             :   }
      80           2 :   while (0 != system (wget_cmd));
      81           2 :   GNUNET_free (wget_cmd);
      82           2 :   fprintf (stderr, "\n");
      83             : 
      84           2 :   return merchant_proc;
      85             : }
      86             : 
      87             : 
      88             : char *
      89           2 : TALER_TESTING_prepare_merchant (const char *config_filename)
      90             : {
      91             :   struct GNUNET_CONFIGURATION_Handle *cfg;
      92             :   unsigned long long port;
      93             :   struct GNUNET_OS_Process *dbinit_proc;
      94             :   enum GNUNET_OS_ProcessStatusType type;
      95             :   unsigned long code;
      96             :   char *base_url;
      97             : 
      98           2 :   cfg = GNUNET_CONFIGURATION_create ();
      99           2 :   if (GNUNET_OK !=
     100           2 :       GNUNET_CONFIGURATION_load (cfg,
     101             :                                  config_filename))
     102           0 :     MERCHANT_FAIL ();
     103           2 :   if (GNUNET_OK !=
     104           2 :       GNUNET_CONFIGURATION_get_value_number (cfg,
     105             :                                              "merchant",
     106             :                                              "PORT",
     107             :                                              &port))
     108             :   {
     109           0 :     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     110             :                                "merchant",
     111             :                                "PORT");
     112           0 :     GNUNET_CONFIGURATION_destroy (cfg);
     113           0 :     MERCHANT_FAIL ();
     114             :   }
     115             : 
     116           2 :   GNUNET_CONFIGURATION_destroy (cfg);
     117             : 
     118           2 :   if (GNUNET_OK !=
     119           2 :       GNUNET_NETWORK_test_port_free (IPPROTO_TCP,
     120           2 :                                      (uint16_t) port))
     121             :   {
     122           0 :     fprintf (stderr,
     123             :              "Required port %llu not available, skipping.\n",
     124             :              port);
     125           0 :     MERCHANT_FAIL ();
     126             :   }
     127             : 
     128             :   /* DB preparation */
     129           2 :   if (NULL == (dbinit_proc = GNUNET_OS_start_process (
     130             :                  GNUNET_OS_INHERIT_STD_ALL,
     131             :                  NULL, NULL, NULL,
     132             :                  "taler-merchant-dbinit",
     133             :                  "taler-merchant-dbinit",
     134             :                  "-c", config_filename,
     135             :                  "-r",
     136             :                  NULL)))
     137             :   {
     138           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     139             :                 "Failed to run taler-merchant-dbinit. Check your PATH.\n");
     140           0 :     MERCHANT_FAIL ();
     141             :   }
     142             : 
     143           2 :   if (GNUNET_SYSERR ==
     144           2 :       GNUNET_OS_process_wait_status (dbinit_proc,
     145             :                                      &type,
     146             :                                      &code))
     147             :   {
     148           0 :     GNUNET_OS_process_destroy (dbinit_proc);
     149           0 :     MERCHANT_FAIL ();
     150             :   }
     151           2 :   if ( (type == GNUNET_OS_PROCESS_EXITED) &&
     152           2 :        (0 != code) )
     153             :   {
     154           0 :     fprintf (stderr,
     155             :              "Failed to setup database\n");
     156           0 :     MERCHANT_FAIL ();
     157             :   }
     158           2 :   if ( (type != GNUNET_OS_PROCESS_EXITED) ||
     159           2 :        (0 != code) )
     160             :   {
     161           0 :     fprintf (stderr,
     162             :              "Unexpected error running"
     163             :              " `taler-merchant-dbinit'!\n");
     164           0 :     MERCHANT_FAIL ();
     165             :   }
     166           2 :   GNUNET_OS_process_destroy (dbinit_proc);
     167             : 
     168             : 
     169           2 :   GNUNET_asprintf (&base_url,
     170             :                    "http://localhost:%llu/",
     171             :                    port);
     172           2 :   return base_url;
     173             : }

Generated by: LCOV version 1.14