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

Generated by: LCOV version 2.0-1