LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_bank_check.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 56.0 % 75 42
Test Date: 2026-05-12 15:34:29 Functions: 80.0 % 5 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2018-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_bank_check.c
      21              :  * @brief command to check if a particular wire transfer took
      22              :  *        place.
      23              :  * @author Marcello Stanisci
      24              :  */
      25              : #include "taler/taler_json_lib.h"
      26              : #include <gnunet/gnunet_curl_lib.h>
      27              : #include "taler/taler_testing_lib.h"
      28              : #include "taler/taler_fakebank_lib.h"
      29              : 
      30              : 
      31              : /**
      32              :  * State for a "bank check" CMD.
      33              :  */
      34              : struct BankCheckState
      35              : {
      36              : 
      37              :   /**
      38              :    * Base URL of the exchange supposed to be
      39              :    * involved in the bank transaction.
      40              :    */
      41              :   const char *exchange_base_url;
      42              : 
      43              :   /**
      44              :    * Expected transferred amount.
      45              :    */
      46              :   const char *amount;
      47              : 
      48              :   /**
      49              :    * Expected debit bank account.
      50              :    */
      51              :   struct TALER_FullPayto debit_payto;
      52              : 
      53              :   /**
      54              :    * Expected credit bank account.
      55              :    */
      56              :   struct TALER_FullPayto credit_payto;
      57              : 
      58              :   /**
      59              :    * Binary form of the wire transfer subject.
      60              :    */
      61              :   struct TALER_WireTransferIdentifierRawP wtid;
      62              : 
      63              :   /**
      64              :    * Interpreter state.
      65              :    */
      66              :   struct TALER_TESTING_Interpreter *is;
      67              : 
      68              :   /**
      69              :    * Reference to a CMD that provides all the data
      70              :    * needed to issue the bank check.  If NULL, that data
      71              :    * must exist here in the state.
      72              :    */
      73              :   const char *deposit_reference;
      74              : };
      75              : 
      76              : 
      77              : /**
      78              :  * Run the command.
      79              :  *
      80              :  * @param cls closure.
      81              :  * @param cmd the command to execute.
      82              :  * @param is the interpreter state.
      83              :  */
      84              : static void
      85           48 : check_bank_transfer_run (void *cls,
      86              :                          const struct TALER_TESTING_Command *cmd,
      87              :                          struct TALER_TESTING_Interpreter *is)
      88              : {
      89           48 :   struct BankCheckState *bcs = cls;
      90              :   struct TALER_Amount amount;
      91              :   char *debit_account;
      92              :   char *credit_account;
      93              :   const char *exchange_base_url;
      94              :   const struct TALER_FullPayto *debit_payto;
      95              :   const struct TALER_FullPayto *credit_payto;
      96              :   struct TALER_FAKEBANK_Handle *fakebank;
      97              : 
      98              :   (void) cmd;
      99              :   {
     100              :     const struct TALER_TESTING_Command *fakebank_cmd;
     101              : 
     102              :     fakebank_cmd
     103           48 :       = TALER_TESTING_interpreter_get_command (is,
     104              :                                                "fakebank");
     105           48 :     if (NULL == fakebank_cmd)
     106              :     {
     107            0 :       GNUNET_break (0);
     108            0 :       TALER_TESTING_interpreter_fail (is);
     109            0 :       return;
     110              :     }
     111           48 :     if (GNUNET_OK !=
     112           48 :         TALER_TESTING_get_trait_fakebank (fakebank_cmd,
     113              :                                           &fakebank))
     114              :     {
     115            0 :       GNUNET_break (0);
     116            0 :       TALER_TESTING_interpreter_fail (is);
     117            0 :       return;
     118              :     }
     119              :   }
     120           48 :   if (NULL == bcs->deposit_reference)
     121              :   {
     122           48 :     TALER_LOG_INFO ("Deposit reference NOT given\n");
     123           48 :     debit_payto = &bcs->debit_payto;
     124           48 :     credit_payto = &bcs->credit_payto;
     125           48 :     exchange_base_url = bcs->exchange_base_url;
     126              : 
     127           48 :     if (GNUNET_OK !=
     128           48 :         TALER_string_to_amount (bcs->amount,
     129              :                                 &amount))
     130              :     {
     131            0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     132              :                   "Failed to parse amount `%s' at %s\n",
     133              :                   bcs->amount,
     134              :                   TALER_TESTING_interpreter_get_current_label (is));
     135            0 :       TALER_TESTING_interpreter_fail (is);
     136            0 :       return;
     137              :     }
     138              :   }
     139              :   else
     140              :   {
     141              :     const struct TALER_TESTING_Command *deposit_cmd;
     142              :     const struct TALER_Amount *amount_ptr;
     143              : 
     144            0 :     TALER_LOG_INFO ("`%s' uses reference (%s/%p)\n",
     145              :                     TALER_TESTING_interpreter_get_current_label
     146              :                       (is),
     147              :                     bcs->deposit_reference,
     148              :                     bcs->deposit_reference);
     149              :     deposit_cmd
     150            0 :       = TALER_TESTING_interpreter_lookup_command (is,
     151              :                                                   bcs->deposit_reference);
     152            0 :     if (NULL == deposit_cmd)
     153            0 :       TALER_TESTING_FAIL (is);
     154            0 :     if ( (GNUNET_OK !=
     155            0 :           TALER_TESTING_get_trait_amount (deposit_cmd,
     156            0 :                                           &amount_ptr)) ||
     157              :          (GNUNET_OK !=
     158            0 :           TALER_TESTING_get_trait_debit_payto_uri (deposit_cmd,
     159            0 :                                                    &debit_payto)) ||
     160              :          (GNUNET_OK !=
     161            0 :           TALER_TESTING_get_trait_credit_payto_uri (deposit_cmd,
     162            0 :                                                     &credit_payto)) ||
     163              :          (GNUNET_OK !=
     164            0 :           TALER_TESTING_get_trait_exchange_url (deposit_cmd,
     165              :                                                 &exchange_base_url)) )
     166            0 :       TALER_TESTING_FAIL (is);
     167            0 :     amount = *amount_ptr;
     168              :   }
     169           48 :   debit_account = TALER_xtalerbank_account_from_payto (*debit_payto);
     170           48 :   credit_account = TALER_xtalerbank_account_from_payto (*credit_payto);
     171           48 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     172              :               "converted debit_payto (%s) to debit_account (%s)\n",
     173              :               debit_payto->full_payto,
     174              :               debit_account);
     175           48 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     176              :               "converted credit_payto (%s) to credit_account (%s)\n",
     177              :               credit_payto->full_payto,
     178              :               credit_account);
     179           48 :   if (GNUNET_OK !=
     180           48 :       TALER_FAKEBANK_check_debit (fakebank,
     181              :                                   &amount,
     182              :                                   debit_account,
     183              :                                   credit_account,
     184              :                                   exchange_base_url,
     185              :                                   &bcs->wtid))
     186              :   {
     187            0 :     GNUNET_break (0);
     188            0 :     GNUNET_free (credit_account);
     189            0 :     GNUNET_free (debit_account);
     190            0 :     TALER_TESTING_interpreter_fail (is);
     191            0 :     return;
     192              :   }
     193           48 :   GNUNET_free (credit_account);
     194           48 :   GNUNET_free (debit_account);
     195           48 :   TALER_TESTING_interpreter_next (is);
     196              : }
     197              : 
     198              : 
     199              : /**
     200              :  * Free the state of a "bank check" CMD.
     201              :  *
     202              :  * @param cls closure.
     203              :  * @param cmd the command which is being cleaned up.
     204              :  */
     205              : static void
     206           48 : check_bank_transfer_cleanup (void *cls,
     207              :                              const struct TALER_TESTING_Command *cmd)
     208              : {
     209           48 :   struct BankCheckState *bcs = cls;
     210              : 
     211              :   (void) cmd;
     212           48 :   GNUNET_free (bcs);
     213           48 : }
     214              : 
     215              : 
     216              : /**
     217              :  * Offer internal data from a "bank check" CMD state.
     218              :  *
     219              :  * @param cls closure.
     220              :  * @param[out] ret result.
     221              :  * @param trait name of the trait.
     222              :  * @param index index number of the object to offer.
     223              :  * @return #GNUNET_OK on success.
     224              :  */
     225              : static enum GNUNET_GenericReturnValue
     226           26 : check_bank_transfer_traits (void *cls,
     227              :                             const void **ret,
     228              :                             const char *trait,
     229              :                             unsigned int index)
     230              : {
     231           26 :   struct BankCheckState *bcs = cls;
     232           26 :   struct TALER_WireTransferIdentifierRawP *wtid_ptr = &bcs->wtid;
     233              :   struct TALER_TESTING_Trait traits[] = {
     234           26 :     TALER_TESTING_make_trait_wtid (wtid_ptr),
     235           26 :     TALER_TESTING_make_trait_exchange_url (
     236              :       bcs->exchange_base_url),
     237           26 :     TALER_TESTING_trait_end ()
     238              :   };
     239              : 
     240           26 :   return TALER_TESTING_get_trait (traits,
     241              :                                   ret,
     242              :                                   trait,
     243              :                                   index);
     244              : }
     245              : 
     246              : 
     247              : struct TALER_TESTING_Command
     248           48 : TALER_TESTING_cmd_check_bank_transfer (
     249              :   const char *label,
     250              :   const char *exchange_base_url,
     251              :   const char *amount,
     252              :   const struct TALER_FullPayto debit_payto,
     253              :   const struct TALER_FullPayto credit_payto)
     254              : {
     255              :   struct BankCheckState *bcs;
     256              : 
     257           48 :   bcs = GNUNET_new (struct BankCheckState);
     258           48 :   bcs->exchange_base_url = exchange_base_url;
     259           48 :   bcs->amount = amount;
     260           48 :   bcs->debit_payto = debit_payto;
     261           48 :   bcs->credit_payto = credit_payto;
     262           48 :   bcs->deposit_reference = NULL;
     263              :   {
     264           48 :     struct TALER_TESTING_Command cmd = {
     265              :       .label = label,
     266              :       .cls = bcs,
     267              :       .run = &check_bank_transfer_run,
     268              :       .cleanup = &check_bank_transfer_cleanup,
     269              :       .traits = &check_bank_transfer_traits
     270              :     };
     271              : 
     272           48 :     return cmd;
     273              :   }
     274              : }
     275              : 
     276              : 
     277              : struct TALER_TESTING_Command
     278            0 : TALER_TESTING_cmd_check_bank_transfer_with_ref (
     279              :   const char *label,
     280              :   const char *deposit_reference)
     281              : {
     282              :   struct BankCheckState *bcs;
     283              : 
     284            0 :   bcs = GNUNET_new (struct BankCheckState);
     285            0 :   bcs->deposit_reference = deposit_reference;
     286              :   {
     287            0 :     struct TALER_TESTING_Command cmd = {
     288              :       .label = label,
     289              :       .cls = bcs,
     290              :       .run = &check_bank_transfer_run,
     291              :       .cleanup = &check_bank_transfer_cleanup,
     292              :       .traits = &check_bank_transfer_traits
     293              :     };
     294              : 
     295            0 :     return cmd;
     296              :   }
     297              : }
        

Generated by: LCOV version 2.0-1