LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_run_fakebank.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 85.1 % 47 40
Test Date: 2025-12-26 23:00:34 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   (C) 2023, 2024 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              :  * @file testing/testing_api_cmd_run_fakebank.c
      21              :  * @brief Command to run fakebank in-process
      22              :  * @author Christian Grothoff
      23              :  */
      24              : #include "taler/platform.h"
      25              : #include "taler/taler_json_lib.h"
      26              : #include <gnunet/gnunet_curl_lib.h>
      27              : #include "taler/taler_testing_lib.h"
      28              : 
      29              : /**
      30              :  * State for a "run fakebank" CMD.
      31              :  */
      32              : struct RunFakebankState
      33              : {
      34              : 
      35              :   /**
      36              :    * Our interpreter state.
      37              :    */
      38              :   struct TALER_TESTING_Interpreter *is;
      39              : 
      40              :   /**
      41              :    * Handle to the fakebank we are running.
      42              :    */
      43              :   struct TALER_FAKEBANK_Handle *fakebank;
      44              : 
      45              :   /**
      46              :    * URL of the bank.
      47              :    */
      48              :   char *bank_url;
      49              : 
      50              :   /**
      51              :    * Currency to use.
      52              :    */
      53              :   char *currency;
      54              : 
      55              :   /**
      56              :    * Data for access control.
      57              :    */
      58              :   struct TALER_BANK_AuthenticationData ba;
      59              : 
      60              :   /**
      61              :    * Port to use.
      62              :    */
      63              :   uint16_t port;
      64              : };
      65              : 
      66              : 
      67              : /**
      68              :  * Run the "get_exchange" command.
      69              :  *
      70              :  * @param cls closure.
      71              :  * @param cmd the command currently being executed.
      72              :  * @param is the interpreter state.
      73              :  */
      74              : static void
      75           13 : run_fakebank_run (void *cls,
      76              :                   const struct TALER_TESTING_Command *cmd,
      77              :                   struct TALER_TESTING_Interpreter *is)
      78              : {
      79           13 :   struct RunFakebankState *rfs = cls;
      80              : 
      81              :   (void) cmd;
      82           26 :   rfs->fakebank = TALER_FAKEBANK_start (rfs->port,
      83           13 :                                         rfs->currency);
      84           13 :   if (NULL == rfs->fakebank)
      85              :   {
      86            0 :     GNUNET_break (0);
      87            0 :     TALER_TESTING_interpreter_fail (is);
      88            0 :     return;
      89              :   }
      90           13 :   TALER_TESTING_interpreter_next (is);
      91              : }
      92              : 
      93              : 
      94              : /**
      95              :  * Cleanup the state.
      96              :  *
      97              :  * @param cls closure.
      98              :  * @param cmd the command which is being cleaned up.
      99              :  */
     100              : static void
     101           13 : run_fakebank_cleanup (void *cls,
     102              :                       const struct TALER_TESTING_Command *cmd)
     103              : {
     104           13 :   struct RunFakebankState *rfs = cls;
     105              : 
     106           13 :   if (NULL != rfs->fakebank)
     107              :   {
     108           13 :     TALER_FAKEBANK_stop (rfs->fakebank);
     109           13 :     rfs->fakebank = NULL;
     110              :   }
     111           13 :   GNUNET_free (rfs->ba.wire_gateway_url);
     112           13 :   GNUNET_free (rfs->bank_url);
     113           13 :   GNUNET_free (rfs->currency);
     114           13 :   GNUNET_free (rfs);
     115           13 : }
     116              : 
     117              : 
     118              : /**
     119              :  * Offer internal data to a "run_fakebank" CMD state to other commands.
     120              :  *
     121              :  * @param cls closure
     122              :  * @param[out] ret result (could be anything)
     123              :  * @param trait name of the trait
     124              :  * @param index index number of the object to offer.
     125              :  * @return #GNUNET_OK on success
     126              :  */
     127              : static enum GNUNET_GenericReturnValue
     128          145 : run_fakebank_traits (void *cls,
     129              :                      const void **ret,
     130              :                      const char *trait,
     131              :                      unsigned int index)
     132              : {
     133          145 :   struct RunFakebankState *rfs = cls;
     134              :   struct TALER_TESTING_Trait traits[] = {
     135          145 :     TALER_TESTING_make_trait_bank_auth_data (&rfs->ba),
     136          145 :     TALER_TESTING_make_trait_fakebank (rfs->fakebank),
     137          145 :     TALER_TESTING_trait_end ()
     138              :   };
     139              : 
     140          145 :   return TALER_TESTING_get_trait (traits,
     141              :                                   ret,
     142              :                                   trait,
     143              :                                   index);
     144              : }
     145              : 
     146              : 
     147              : struct TALER_TESTING_Command
     148           13 : TALER_TESTING_cmd_run_fakebank (
     149              :   const char *label,
     150              :   const struct GNUNET_CONFIGURATION_Handle *cfg,
     151              :   const char *exchange_account_section)
     152              : {
     153              :   struct RunFakebankState *rfs;
     154              :   unsigned long long fakebank_port;
     155              :   struct TALER_FullPayto exchange_payto_uri;
     156              : 
     157           13 :   if (GNUNET_OK !=
     158           13 :       GNUNET_CONFIGURATION_get_value_number (cfg,
     159              :                                              "BANK",
     160              :                                              "HTTP_PORT",
     161              :                                              &fakebank_port))
     162              :   {
     163            0 :     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     164              :                                "BANK",
     165              :                                "HTTP_PORT");
     166            0 :     GNUNET_assert (0);
     167              :   }
     168           13 :   if (GNUNET_OK !=
     169           13 :       GNUNET_CONFIGURATION_get_value_string (cfg,
     170              :                                              exchange_account_section,
     171              :                                              "PAYTO_URI",
     172              :                                              &exchange_payto_uri.full_payto))
     173              :   {
     174            0 :     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     175              :                                exchange_account_section,
     176              :                                "PAYTO_URI");
     177            0 :     GNUNET_assert (0);
     178              :   }
     179           13 :   rfs = GNUNET_new (struct RunFakebankState);
     180           13 :   rfs->port = (uint16_t) fakebank_port;
     181           13 :   GNUNET_asprintf (&rfs->bank_url,
     182              :                    "http://localhost:%u/",
     183           13 :                    (unsigned int) rfs->port);
     184           13 :   GNUNET_assert (GNUNET_OK ==
     185              :                  TALER_config_get_currency (cfg,
     186              :                                             "exchange",
     187              :                                             &rfs->currency));
     188              :   {
     189              :     char *exchange_xtalerbank_account;
     190              : 
     191              :     exchange_xtalerbank_account
     192           13 :       = TALER_xtalerbank_account_from_payto (exchange_payto_uri);
     193           13 :     GNUNET_assert (NULL != exchange_xtalerbank_account);
     194           13 :     GNUNET_asprintf (&rfs->ba.wire_gateway_url,
     195              :                      "http://localhost:%u/%s/",
     196              :                      (unsigned int) fakebank_port,
     197              :                      exchange_xtalerbank_account);
     198           13 :     GNUNET_free (exchange_xtalerbank_account);
     199              :   }
     200           13 :   GNUNET_free (exchange_payto_uri.full_payto);
     201           13 :   rfs->ba.method = TALER_BANK_AUTH_NONE;
     202              :   {
     203           13 :     struct TALER_TESTING_Command cmd = {
     204              :       .cls = rfs,
     205              :       .label = label,
     206              :       .run = &run_fakebank_run,
     207              :       .cleanup = &run_fakebank_cleanup,
     208              :       .traits = &run_fakebank_traits,
     209              :       .name = "fakebank"
     210              :     };
     211              : 
     212           13 :     return cmd;
     213              :   }
     214              : }
        

Generated by: LCOV version 2.0-1