exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_bank_check.c (8321B)


      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 check_bank_transfer_run (void *cls,
     86                          const struct TALER_TESTING_Command *cmd,
     87                          struct TALER_TESTING_Interpreter *is)
     88 {
     89   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       = TALER_TESTING_interpreter_get_command (is,
    104                                                "fakebank");
    105     if (NULL == fakebank_cmd)
    106     {
    107       GNUNET_break (0);
    108       TALER_TESTING_interpreter_fail (is);
    109       return;
    110     }
    111     if (GNUNET_OK !=
    112         TALER_TESTING_get_trait_fakebank (fakebank_cmd,
    113                                           &fakebank))
    114     {
    115       GNUNET_break (0);
    116       TALER_TESTING_interpreter_fail (is);
    117       return;
    118     }
    119   }
    120   if (NULL == bcs->deposit_reference)
    121   {
    122     TALER_LOG_INFO ("Deposit reference NOT given\n");
    123     debit_payto = &bcs->debit_payto;
    124     credit_payto = &bcs->credit_payto;
    125     exchange_base_url = bcs->exchange_base_url;
    126 
    127     if (GNUNET_OK !=
    128         TALER_string_to_amount (bcs->amount,
    129                                 &amount))
    130     {
    131       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       TALER_TESTING_interpreter_fail (is);
    136       return;
    137     }
    138   }
    139   else
    140   {
    141     const struct TALER_TESTING_Command *deposit_cmd;
    142     const struct TALER_Amount *amount_ptr;
    143 
    144     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       = TALER_TESTING_interpreter_lookup_command (is,
    151                                                   bcs->deposit_reference);
    152     if (NULL == deposit_cmd)
    153       TALER_TESTING_FAIL (is);
    154     if ( (GNUNET_OK !=
    155           TALER_TESTING_get_trait_amount (deposit_cmd,
    156                                           &amount_ptr)) ||
    157          (GNUNET_OK !=
    158           TALER_TESTING_get_trait_debit_payto_uri (deposit_cmd,
    159                                                    &debit_payto)) ||
    160          (GNUNET_OK !=
    161           TALER_TESTING_get_trait_credit_payto_uri (deposit_cmd,
    162                                                     &credit_payto)) ||
    163          (GNUNET_OK !=
    164           TALER_TESTING_get_trait_exchange_url (deposit_cmd,
    165                                                 &exchange_base_url)) )
    166       TALER_TESTING_FAIL (is);
    167     amount = *amount_ptr;
    168   }
    169   debit_account = TALER_xtalerbank_account_from_payto (*debit_payto);
    170   credit_account = TALER_xtalerbank_account_from_payto (*credit_payto);
    171   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   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   if (GNUNET_OK !=
    180       TALER_FAKEBANK_check_debit (fakebank,
    181                                   &amount,
    182                                   debit_account,
    183                                   credit_account,
    184                                   exchange_base_url,
    185                                   &bcs->wtid))
    186   {
    187     GNUNET_break (0);
    188     GNUNET_free (credit_account);
    189     GNUNET_free (debit_account);
    190     TALER_TESTING_interpreter_fail (is);
    191     return;
    192   }
    193   GNUNET_free (credit_account);
    194   GNUNET_free (debit_account);
    195   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 check_bank_transfer_cleanup (void *cls,
    207                              const struct TALER_TESTING_Command *cmd)
    208 {
    209   struct BankCheckState *bcs = cls;
    210 
    211   (void) cmd;
    212   GNUNET_free (bcs);
    213 }
    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 check_bank_transfer_traits (void *cls,
    227                             const void **ret,
    228                             const char *trait,
    229                             unsigned int index)
    230 {
    231   struct BankCheckState *bcs = cls;
    232   struct TALER_WireTransferIdentifierRawP *wtid_ptr = &bcs->wtid;
    233   struct TALER_TESTING_Trait traits[] = {
    234     TALER_TESTING_make_trait_wtid (wtid_ptr),
    235     TALER_TESTING_make_trait_exchange_url (
    236       bcs->exchange_base_url),
    237     TALER_TESTING_trait_end ()
    238   };
    239 
    240   return TALER_TESTING_get_trait (traits,
    241                                   ret,
    242                                   trait,
    243                                   index);
    244 }
    245 
    246 
    247 struct TALER_TESTING_Command
    248 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   bcs = GNUNET_new (struct BankCheckState);
    258   bcs->exchange_base_url = exchange_base_url;
    259   bcs->amount = amount;
    260   bcs->debit_payto = debit_payto;
    261   bcs->credit_payto = credit_payto;
    262   bcs->deposit_reference = NULL;
    263   {
    264     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     return cmd;
    273   }
    274 }
    275 
    276 
    277 struct TALER_TESTING_Command
    278 TALER_TESTING_cmd_check_bank_transfer_with_ref (
    279   const char *label,
    280   const char *deposit_reference)
    281 {
    282   struct BankCheckState *bcs;
    283 
    284   bcs = GNUNET_new (struct BankCheckState);
    285   bcs->deposit_reference = deposit_reference;
    286   {
    287     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     return cmd;
    296   }
    297 }