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: 17 54 31.5 %
Date: 2022-08-25 06:17:04 Functions: 1 2 50.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           0 : 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           0 :     = 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           0 :   if (NULL == merchant_proc)
      50           0 :     MERCHANT_FAIL ();
      51             : 
      52           0 :   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           0 :   fprintf (stderr,
      61             :            "Waiting for `taler-merchant-httpd' to be ready\n");
      62           0 :   iter = 0;
      63             :   do
      64             :   {
      65           0 :     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           0 :     fprintf (stderr, ".\n");
      77           0 :     sleep (1);
      78           0 :     iter++;
      79             :   }
      80           0 :   while (0 != system (wget_cmd));
      81           0 :   GNUNET_free (wget_cmd);
      82           0 :   fprintf (stderr, "\n");
      83             : 
      84           0 :   return merchant_proc;
      85             : }
      86             : 
      87             : 
      88             : char *
      89           5 : 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           5 :   cfg = GNUNET_CONFIGURATION_create ();
      99           5 :   if (GNUNET_OK !=
     100           5 :       GNUNET_CONFIGURATION_load (cfg,
     101             :                                  config_filename))
     102           0 :     MERCHANT_FAIL ();
     103           5 :   if (GNUNET_OK !=
     104           5 :       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           5 :   GNUNET_CONFIGURATION_destroy (cfg);
     117             : 
     118           5 :   if (GNUNET_OK !=
     119           5 :       GNUNET_NETWORK_test_port_free (IPPROTO_TCP,
     120           5 :                                      (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           5 :   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           5 :   if (GNUNET_SYSERR ==
     144           5 :       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           5 :   if ( (type == GNUNET_OS_PROCESS_EXITED) &&
     152           5 :        (0 != code) )
     153             :   {
     154           5 :     fprintf (stderr,
     155             :              "Failed to setup database\n");
     156           5 :     MERCHANT_FAIL ();
     157             :   }
     158           0 :   if ( (type != GNUNET_OS_PROCESS_EXITED) ||
     159           0 :        (0 != code) )
     160             :   {
     161           0 :     fprintf (stderr,
     162             :              "Unexpected error running"
     163             :              " `taler-merchant-dbinit'!\n");
     164           0 :     MERCHANT_FAIL ();
     165             :   }
     166           0 :   GNUNET_OS_process_destroy (dbinit_proc);
     167             : 
     168             : 
     169           0 :   GNUNET_asprintf (&base_url,
     170             :                    "http://localhost:%llu/",
     171             :                    port);
     172           0 :   return base_url;
     173             : }

Generated by: LCOV version 1.14