LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_deposits_get.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 58 95 61.1 %
Date: 2021-08-30 06:43:37 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-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             : /**
      21             :  * @file testing/testing_api_cmd_deposits_get.c
      22             :  * @brief Implement the testing CMDs for the /deposits/ GET operations.
      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             : 
      30             : /**
      31             :  * State for a "track transaction" CMD.
      32             :  */
      33             : struct TrackTransactionState
      34             : {
      35             : 
      36             :   /**
      37             :    * If non NULL, will provide a WTID to be compared against
      38             :    * the one returned by the "track transaction" operation.
      39             :    */
      40             :   const char *bank_transfer_reference;
      41             : 
      42             :   /**
      43             :    * The WTID associated by the transaction being tracked.
      44             :    */
      45             :   struct TALER_WireTransferIdentifierRawP wtid;
      46             : 
      47             :   /**
      48             :    * Expected HTTP response code.
      49             :    */
      50             :   unsigned int expected_response_code;
      51             : 
      52             :   /**
      53             :    * Reference to any operation that can provide a transaction.
      54             :    * Will be the transaction to track.
      55             :    */
      56             :   const char *transaction_reference;
      57             : 
      58             :   /**
      59             :    * Index of the coin involved in the transaction.  Recall:
      60             :    * at the exchange, the tracking is done _per coin_.
      61             :    */
      62             :   unsigned int coin_index;
      63             : 
      64             :   /**
      65             :    * Handle to the "track transaction" pending operation.
      66             :    */
      67             :   struct TALER_EXCHANGE_DepositGetHandle *tth;
      68             : 
      69             :   /**
      70             :    * Interpreter state.
      71             :    */
      72             :   struct TALER_TESTING_Interpreter *is;
      73             : };
      74             : 
      75             : 
      76             : /**
      77             :  * Checks what is returned by the "track transaction" operation.
      78             :  * Checks that the HTTP response code is acceptable, and - if the
      79             :  * right reference is non NULL - that the wire transfer subject
      80             :  * line matches our expectations.
      81             :  *
      82             :  * @param cls closure.
      83             :  * @param hr HTTP response details
      84             :  * @param dd data about the wire transfer associated with the deposit
      85             :  */
      86             : static void
      87           3 : deposit_wtid_cb (void *cls,
      88             :                  const struct TALER_EXCHANGE_HttpResponse *hr,
      89             :                  const struct TALER_EXCHANGE_DepositData *dd)
      90             : {
      91           3 :   struct TrackTransactionState *tts = cls;
      92           3 :   struct TALER_TESTING_Interpreter *is = tts->is;
      93           3 :   struct TALER_TESTING_Command *cmd = &is->commands[is->ip];
      94             : 
      95           3 :   tts->tth = NULL;
      96           3 :   if (tts->expected_response_code != hr->http_status)
      97             :   {
      98           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      99             :                 "Unexpected response code %u/%d to command %s in %s:%u\n",
     100             :                 hr->http_status,
     101             :                 (int) hr->ec,
     102             :                 cmd->label,
     103             :                 __FILE__,
     104             :                 __LINE__);
     105           0 :     json_dumpf (hr->reply,
     106             :                 stderr,
     107             :                 0);
     108           0 :     TALER_TESTING_interpreter_fail (is);
     109           0 :     return;
     110             :   }
     111           3 :   switch (hr->http_status)
     112             :   {
     113           1 :   case MHD_HTTP_OK:
     114           1 :     tts->wtid = dd->wtid;
     115           1 :     if (NULL != tts->bank_transfer_reference)
     116             :     {
     117             :       const struct TALER_TESTING_Command *bank_transfer_cmd;
     118             :       const struct TALER_WireTransferIdentifierRawP *wtid_want;
     119             : 
     120             :       /* _this_ wire transfer subject line.  */
     121             :       bank_transfer_cmd
     122           1 :         = TALER_TESTING_interpreter_lookup_command (is,
     123             :                                                     tts->bank_transfer_reference);
     124           1 :       if (NULL == bank_transfer_cmd)
     125             :       {
     126           0 :         GNUNET_break (0);
     127           0 :         TALER_TESTING_interpreter_fail (is);
     128           0 :         return;
     129             :       }
     130             : 
     131           1 :       if (GNUNET_OK !=
     132           1 :           TALER_TESTING_get_trait_wtid (bank_transfer_cmd,
     133             :                                         0,
     134             :                                         &wtid_want))
     135             :       {
     136           0 :         GNUNET_break (0);
     137           0 :         TALER_TESTING_interpreter_fail (is);
     138           0 :         return;
     139             :       }
     140             : 
     141             :       /* Compare that expected and gotten subjects match.  */
     142           1 :       if (0 != GNUNET_memcmp (&dd->wtid,
     143             :                               wtid_want))
     144             :       {
     145           0 :         GNUNET_break (0);
     146           0 :         TALER_TESTING_interpreter_fail (tts->is);
     147           0 :         return;
     148             :       }
     149             :     }
     150             : 
     151             : 
     152           1 :     break;
     153           1 :   case MHD_HTTP_ACCEPTED:
     154             :     /* allowed, nothing to check here */
     155           1 :     break;
     156           1 :   case MHD_HTTP_NOT_FOUND:
     157             :     /* allowed, nothing to check here */
     158           1 :     break;
     159           0 :   default:
     160           0 :     GNUNET_break (0);
     161           0 :     break;
     162             :   }
     163           3 :   TALER_TESTING_interpreter_next (tts->is);
     164             : }
     165             : 
     166             : 
     167             : /**
     168             :  * Run the command.
     169             :  *
     170             :  * @param cls closure.
     171             :  * @param cmd the command to execute.
     172             :  * @param is the interpreter state.
     173             :  */
     174             : static void
     175           3 : track_transaction_run (void *cls,
     176             :                        const struct TALER_TESTING_Command *cmd,
     177             :                        struct TALER_TESTING_Interpreter *is)
     178             : {
     179           3 :   struct TrackTransactionState *tts = cls;
     180             :   const struct TALER_TESTING_Command *transaction_cmd;
     181             :   const struct TALER_CoinSpendPrivateKeyP *coin_priv;
     182             :   struct TALER_CoinSpendPublicKeyP coin_pub;
     183             :   const json_t *contract_terms;
     184             :   const json_t *wire_details;
     185             :   struct GNUNET_HashCode h_wire_details;
     186             :   struct GNUNET_HashCode h_contract_terms;
     187             :   const struct TALER_MerchantPrivateKeyP *merchant_priv;
     188             : 
     189             :   (void) cmd;
     190           3 :   tts->is = is;
     191             :   transaction_cmd
     192           3 :     = TALER_TESTING_interpreter_lookup_command (tts->is,
     193             :                                                 tts->transaction_reference);
     194           3 :   if (NULL == transaction_cmd)
     195             :   {
     196           0 :     GNUNET_break (0);
     197           0 :     TALER_TESTING_interpreter_fail (tts->is);
     198           0 :     return;
     199             :   }
     200             : 
     201           3 :   if (GNUNET_OK !=
     202           3 :       TALER_TESTING_get_trait_coin_priv (transaction_cmd,
     203             :                                          tts->coin_index,
     204             :                                          &coin_priv))
     205             :   {
     206           0 :     GNUNET_break (0);
     207           0 :     TALER_TESTING_interpreter_fail (tts->is);
     208           0 :     return;
     209             :   }
     210             : 
     211           3 :   GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv->eddsa_priv,
     212             :                                       &coin_pub.eddsa_pub);
     213             : 
     214             :   /* Get the strings.. */
     215           3 :   if (GNUNET_OK !=
     216           3 :       TALER_TESTING_get_trait_wire_details (transaction_cmd,
     217             :                                             0,
     218             :                                             &wire_details))
     219             :   {
     220           0 :     GNUNET_break (0);
     221           0 :     TALER_TESTING_interpreter_fail (tts->is);
     222           0 :     return;
     223             :   }
     224             : 
     225           3 :   if (GNUNET_OK !=
     226           3 :       TALER_TESTING_get_trait_contract_terms (transaction_cmd,
     227             :                                               0,
     228             :                                               &contract_terms))
     229             :   {
     230           0 :     GNUNET_break (0);
     231           0 :     TALER_TESTING_interpreter_fail (tts->is);
     232           0 :     return;
     233             :   }
     234             : 
     235           3 :   if ( (NULL == wire_details) ||
     236           3 :        (NULL == contract_terms) )
     237             :   {
     238           0 :     GNUNET_break (0);
     239           0 :     TALER_TESTING_interpreter_fail (tts->is);
     240           0 :     return;
     241             :   }
     242             : 
     243             :   /* Should not fail here, json has been parsed already */
     244           3 :   GNUNET_assert
     245             :     ( (GNUNET_OK ==
     246             :        TALER_JSON_merchant_wire_signature_hash (wire_details,
     247             :                                                 &h_wire_details)) &&
     248             :     (GNUNET_OK ==
     249             :      TALER_JSON_contract_hash (contract_terms,
     250             :                                &h_contract_terms)) );
     251             : 
     252           3 :   if (GNUNET_OK !=
     253           3 :       TALER_TESTING_get_trait_merchant_priv (transaction_cmd,
     254             :                                              0,
     255             :                                              &merchant_priv))
     256             :   {
     257           0 :     GNUNET_break (0);
     258           0 :     TALER_TESTING_interpreter_fail (tts->is);
     259           0 :     return;
     260             :   }
     261             : 
     262           3 :   tts->tth = TALER_EXCHANGE_deposits_get (is->exchange,
     263             :                                           merchant_priv,
     264             :                                           &h_wire_details,
     265             :                                           &h_contract_terms,
     266             :                                           &coin_pub,
     267             :                                           &deposit_wtid_cb,
     268             :                                           tts);
     269           3 :   GNUNET_assert (NULL != tts->tth);
     270             : }
     271             : 
     272             : 
     273             : /**
     274             :  * Cleanup the state from a "track transaction" CMD, and possibly
     275             :  * cancel a operation thereof.
     276             :  *
     277             :  * @param cls closure.
     278             :  * @param cmd the command which is being cleaned up.
     279             :  */
     280             : static void
     281           3 : track_transaction_cleanup (void *cls,
     282             :                            const struct TALER_TESTING_Command *cmd)
     283             : {
     284           3 :   struct TrackTransactionState *tts = cls;
     285             : 
     286           3 :   if (NULL != tts->tth)
     287             :   {
     288           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     289             :                 "Command %u (%s) did not complete\n",
     290             :                 tts->is->ip,
     291             :                 cmd->label);
     292           0 :     TALER_EXCHANGE_deposits_get_cancel (tts->tth);
     293           0 :     tts->tth = NULL;
     294             :   }
     295           3 :   GNUNET_free (tts);
     296           3 : }
     297             : 
     298             : 
     299             : /**
     300             :  * Offer internal data from a "track transaction" CMD.
     301             :  *
     302             :  * @param cls closure.
     303             :  * @param[out] ret result (could be anything).
     304             :  * @param trait name of the trait.
     305             :  * @param index index number of the object to offer.
     306             :  * @return #GNUNET_OK on success.
     307             :  */
     308             : static int
     309          19 : track_transaction_traits (void *cls,
     310             :                           const void **ret,
     311             :                           const char *trait,
     312             :                           unsigned int index)
     313             : {
     314          19 :   struct TrackTransactionState *tts = cls;
     315             :   struct TALER_TESTING_Trait traits[] = {
     316          19 :     TALER_TESTING_make_trait_wtid (0, &tts->wtid),
     317          19 :     TALER_TESTING_trait_end ()
     318             :   };
     319             : 
     320          19 :   return TALER_TESTING_get_trait (traits,
     321             :                                   ret,
     322             :                                   trait,
     323             :                                   index);
     324             : }
     325             : 
     326             : 
     327             : /**
     328             :  * Create a "track transaction" command.
     329             :  *
     330             :  * @param label the command label.
     331             :  * @param transaction_reference reference to a deposit operation,
     332             :  *        will be used to get the input data for the track.
     333             :  * @param coin_index index of the coin involved in the transaction.
     334             :  * @param expected_response_code expected HTTP response code.
     335             :  * @param bank_transfer_reference reference to a command that
     336             :  *        can offer a WTID so as to check that against what WTID
     337             :  *        the tracked operation has.  Set as NULL if not needed.
     338             :  * @return the command.
     339             :  */
     340             : struct TALER_TESTING_Command
     341           3 : TALER_TESTING_cmd_track_transaction (const char *label,
     342             :                                      const char *transaction_reference,
     343             :                                      unsigned int coin_index,
     344             :                                      unsigned int expected_response_code,
     345             :                                      const char *bank_transfer_reference)
     346             : {
     347             :   struct TrackTransactionState *tts;
     348             : 
     349           3 :   tts = GNUNET_new (struct TrackTransactionState);
     350           3 :   tts->transaction_reference = transaction_reference;
     351           3 :   tts->expected_response_code = expected_response_code;
     352           3 :   tts->bank_transfer_reference = bank_transfer_reference;
     353           3 :   tts->coin_index = coin_index;
     354             :   {
     355           3 :     struct TALER_TESTING_Command cmd = {
     356             :       .cls = tts,
     357             :       .label = label,
     358             :       .run = &track_transaction_run,
     359             :       .cleanup = &track_transaction_cleanup,
     360             :       .traits = &track_transaction_traits
     361             :     };
     362             : 
     363           3 :     return cmd;
     364             :   }
     365             : }
     366             : 
     367             : 
     368             : /* end of testing_api_cmd_deposits_get.c */

Generated by: LCOV version 1.14