LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_bank_check.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 38 65 58.5 %
Date: 2021-08-30 06:43:37 Functions: 4 5 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2018-2020 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 "platform.h"
      26             : #include "taler_json_lib.h"
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_testing_lib.h"
      29             : #include "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             :   const char *debit_payto;
      53             : 
      54             :   /**
      55             :    * Expected credit bank account.
      56             :    */
      57             :   const char *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             :  * 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          33 : check_bank_transfer_run (void *cls,
      86             :                          const struct TALER_TESTING_Command *cmd,
      87             :                          struct TALER_TESTING_Interpreter *is)
      88             : {
      89          33 :   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 char *debit_payto;
      95             :   const char *credit_payto;
      96             : 
      97             :   (void) cmd;
      98          33 :   if (NULL == bcs->deposit_reference)
      99             :   {
     100          33 :     TALER_LOG_INFO ("Deposit reference NOT given\n");
     101          33 :     debit_payto = bcs->debit_payto;
     102          33 :     credit_payto = bcs->credit_payto;
     103          33 :     exchange_base_url = bcs->exchange_base_url;
     104             : 
     105          33 :     if (GNUNET_OK !=
     106          33 :         TALER_string_to_amount (bcs->amount,
     107             :                                 &amount))
     108             :     {
     109           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     110             :                   "Failed to parse amount `%s' at %u\n",
     111             :                   bcs->amount,
     112             :                   is->ip);
     113           0 :       TALER_TESTING_interpreter_fail (is);
     114           0 :       return;
     115             :     }
     116             :   }
     117             :   else
     118             :   {
     119             :     const struct TALER_TESTING_Command *deposit_cmd;
     120             :     const struct TALER_Amount *amount_ptr;
     121             : 
     122           0 :     TALER_LOG_INFO ("`%s' uses reference (%s/%p)\n",
     123             :                     TALER_TESTING_interpreter_get_current_label
     124             :                       (is),
     125             :                     bcs->deposit_reference,
     126             :                     bcs->deposit_reference);
     127             :     deposit_cmd
     128           0 :       = TALER_TESTING_interpreter_lookup_command (is,
     129             :                                                   bcs->deposit_reference);
     130           0 :     if (NULL == deposit_cmd)
     131           0 :       TALER_TESTING_FAIL (is);
     132           0 :     if ( (GNUNET_OK !=
     133           0 :           TALER_TESTING_get_trait_amount_obj (deposit_cmd,
     134             :                                               0,
     135           0 :                                               &amount_ptr)) ||
     136             :          (GNUNET_OK !=
     137           0 :           TALER_TESTING_get_trait_payto (deposit_cmd,
     138             :                                          TALER_TESTING_PT_DEBIT,
     139           0 :                                          &debit_payto)) ||
     140             :          (GNUNET_OK !=
     141           0 :           TALER_TESTING_get_trait_payto (deposit_cmd,
     142             :                                          TALER_TESTING_PT_CREDIT,
     143           0 :                                          &credit_payto)) ||
     144             :          (GNUNET_OK !=
     145           0 :           TALER_TESTING_get_trait_url (deposit_cmd,
     146             :                                        TALER_TESTING_UT_EXCHANGE_BASE_URL,
     147             :                                        &exchange_base_url)) )
     148           0 :       TALER_TESTING_FAIL (is);
     149           0 :     amount = *amount_ptr;
     150             :   }
     151             : 
     152             : 
     153          33 :   debit_account = TALER_xtalerbank_account_from_payto (debit_payto);
     154          33 :   credit_account = TALER_xtalerbank_account_from_payto (credit_payto);
     155             : 
     156          33 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     157             :               "converted debit_payto (%s) to debit_account (%s)\n",
     158             :               debit_payto,
     159             :               debit_account);
     160             : 
     161          33 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     162             :               "converted credit_payto (%s) to credit_account (%s)\n",
     163             :               credit_payto,
     164             :               credit_account);
     165             : 
     166          33 :   if (GNUNET_OK !=
     167          33 :       TALER_FAKEBANK_check_debit (is->fakebank,
     168             :                                   &amount,
     169             :                                   debit_account,
     170             :                                   credit_account,
     171             :                                   exchange_base_url,
     172             :                                   &bcs->wtid))
     173             :   {
     174           0 :     GNUNET_break (0);
     175           0 :     GNUNET_free (credit_account);
     176           0 :     GNUNET_free (debit_account);
     177           0 :     TALER_TESTING_interpreter_fail (is);
     178           0 :     return;
     179             :   }
     180          33 :   GNUNET_free (credit_account);
     181          33 :   GNUNET_free (debit_account);
     182          33 :   TALER_TESTING_interpreter_next (is);
     183             : }
     184             : 
     185             : 
     186             : /**
     187             :  * Free the state of a "bank check" CMD.
     188             :  *
     189             :  * @param cls closure.
     190             :  * @param cmd the command which is being cleaned up.
     191             :  */
     192             : static void
     193          33 : check_bank_transfer_cleanup (void *cls,
     194             :                              const struct TALER_TESTING_Command *cmd)
     195             : {
     196          33 :   struct BankCheckState *bcs = cls;
     197             : 
     198             :   (void) cmd;
     199          33 :   GNUNET_free (bcs);
     200          33 : }
     201             : 
     202             : 
     203             : /**
     204             :  * Offer internal data from a "bank check" CMD state.
     205             :  *
     206             :  * @param cls closure.
     207             :  * @param[out] ret result.
     208             :  * @param trait name of the trait.
     209             :  * @param index index number of the object to offer.
     210             :  * @return #GNUNET_OK on success.
     211             :  */
     212             : static int
     213          41 : check_bank_transfer_traits (void *cls,
     214             :                             const void **ret,
     215             :                             const char *trait,
     216             :                             unsigned int index)
     217             : {
     218          41 :   struct BankCheckState *bcs = cls;
     219          41 :   struct TALER_WireTransferIdentifierRawP *wtid_ptr = &bcs->wtid;
     220             :   struct TALER_TESTING_Trait traits[] = {
     221          41 :     TALER_TESTING_make_trait_wtid (0,
     222             :                                    wtid_ptr),
     223          41 :     TALER_TESTING_make_trait_url (TALER_TESTING_UT_EXCHANGE_BASE_URL,
     224             :                                   bcs->exchange_base_url),
     225          41 :     TALER_TESTING_trait_end ()
     226             :   };
     227             : 
     228          41 :   return TALER_TESTING_get_trait (traits,
     229             :                                   ret,
     230             :                                   trait,
     231             :                                   index);
     232             : }
     233             : 
     234             : 
     235             : /**
     236             :  * Make a "bank check" CMD.  It checks whether a
     237             :  * particular wire transfer has been made or not.
     238             :  *
     239             :  * @param label the command label.
     240             :  * @param exchange_base_url base url of the exchange involved in
     241             :  *        the wire transfer.
     242             :  * @param amount the amount expected to be transferred.
     243             :  * @param debit_payto the account that gave money.
     244             :  * @param credit_payto the account that received money.
     245             :  *
     246             :  * @return the command
     247             :  */
     248             : struct TALER_TESTING_Command
     249          33 : TALER_TESTING_cmd_check_bank_transfer (const char *label,
     250             :                                        const char *exchange_base_url,
     251             :                                        const char *amount,
     252             :                                        const char *debit_payto,
     253             :                                        const char *credit_payto)
     254             : {
     255             :   struct BankCheckState *bcs;
     256             : 
     257          33 :   bcs = GNUNET_new (struct BankCheckState);
     258          33 :   bcs->exchange_base_url = exchange_base_url;
     259          33 :   bcs->amount = amount;
     260          33 :   bcs->debit_payto = debit_payto;
     261          33 :   bcs->credit_payto = credit_payto;
     262          33 :   bcs->deposit_reference = NULL;
     263             :   {
     264          33 :     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          33 :     return cmd;
     273             :   }
     274             : }
     275             : 
     276             : 
     277             : /**
     278             :  * Define a "bank check" CMD that takes the input
     279             :  * data from another CMD that offers it.
     280             :  *
     281             :  * @param label command label.
     282             :  * @param deposit_reference reference to a CMD that is
     283             :  *        able to provide the "check bank transfer" operation
     284             :  *        input data.
     285             :  * @return the command.
     286             :  */
     287             : struct TALER_TESTING_Command
     288           0 : TALER_TESTING_cmd_check_bank_transfer_with_ref
     289             :   (const char *label,
     290             :   const char *deposit_reference)
     291             : {
     292             :   struct BankCheckState *bcs;
     293             : 
     294           0 :   bcs = GNUNET_new (struct BankCheckState);
     295           0 :   bcs->deposit_reference = deposit_reference;
     296             :   {
     297           0 :     struct TALER_TESTING_Command cmd = {
     298             :       .label = label,
     299             :       .cls = bcs,
     300             :       .run = &check_bank_transfer_run,
     301             :       .cleanup = &check_bank_transfer_cleanup,
     302             :       .traits = &check_bank_transfer_traits
     303             :     };
     304             : 
     305           0 :     return cmd;
     306             :   }
     307             : }

Generated by: LCOV version 1.14