LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_recoup.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 0 118 0.0 %
Date: 2022-08-25 06:15:09 Functions: 0 5 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2018 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_recoup.c
      21             :  * @brief Implement the /recoup test command.
      22             :  * @author Marcello Stanisci
      23             :  */
      24             : #include "platform.h"
      25             : #include "taler_json_lib.h"
      26             : #include <gnunet/gnunet_curl_lib.h>
      27             : #include "taler_testing_lib.h"
      28             : 
      29             : 
      30             : /**
      31             :  * State for a "pay back" CMD.
      32             :  */
      33             : struct RecoupState
      34             : {
      35             :   /**
      36             :    * Expected HTTP status code.
      37             :    */
      38             :   unsigned int expected_response_code;
      39             : 
      40             :   /**
      41             :    * Command that offers a reserve private key,
      42             :    * plus a coin to be paid back.
      43             :    */
      44             :   const char *coin_reference;
      45             : 
      46             :   /**
      47             :    * The interpreter state.
      48             :    */
      49             :   struct TALER_TESTING_Interpreter *is;
      50             : 
      51             :   /**
      52             :    * Handle to the ongoing operation.
      53             :    */
      54             :   struct TALER_EXCHANGE_RecoupHandle *ph;
      55             : 
      56             :   /**
      57             :    * If the recoup filled a reserve, this is set to the reserve's public key.
      58             :    */
      59             :   struct TALER_ReservePublicKeyP reserve_pub;
      60             : 
      61             :   /**
      62             :    * Reserve history entry, set if this recoup actually filled up a reserve.
      63             :    * Otherwise `reserve_history.type` will be zero.
      64             :    */
      65             :   struct TALER_EXCHANGE_ReserveHistoryEntry reserve_history;
      66             : 
      67             : };
      68             : 
      69             : 
      70             : /**
      71             :  * Check the result of the recoup request: checks whether
      72             :  * the HTTP response code is good, and that the coin that
      73             :  * was paid back belonged to the right reserve.
      74             :  *
      75             :  * @param cls closure
      76             :  * @param hr HTTP response details
      77             :  * @param reserve_pub public key of the reserve receiving the recoup
      78             :  */
      79             : static void
      80           0 : recoup_cb (void *cls,
      81             :            const struct TALER_EXCHANGE_HttpResponse *hr,
      82             :            const struct TALER_ReservePublicKeyP *reserve_pub)
      83             : {
      84           0 :   struct RecoupState *ps = cls;
      85           0 :   struct TALER_TESTING_Interpreter *is = ps->is;
      86           0 :   struct TALER_TESTING_Command *cmd = &is->commands[is->ip];
      87             :   const struct TALER_TESTING_Command *reserve_cmd;
      88             :   char *cref;
      89             :   unsigned int idx;
      90             : 
      91           0 :   ps->ph = NULL;
      92           0 :   if (ps->expected_response_code != hr->http_status)
      93             :   {
      94           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      95             :                 "Unexpected response code %u/%d to command %s in %s:%u\n",
      96             :                 hr->http_status,
      97             :                 (int) hr->ec,
      98             :                 cmd->label,
      99             :                 __FILE__,
     100             :                 __LINE__);
     101           0 :     json_dumpf (hr->reply,
     102             :                 stderr,
     103             :                 0);
     104           0 :     fprintf (stderr, "\n");
     105           0 :     TALER_TESTING_interpreter_fail (is);
     106           0 :     return;
     107             :   }
     108             : 
     109           0 :   if (GNUNET_OK !=
     110           0 :       TALER_TESTING_parse_coin_reference (
     111             :         ps->coin_reference,
     112             :         &cref,
     113             :         &idx))
     114             :   {
     115           0 :     TALER_TESTING_interpreter_fail (is);
     116           0 :     return;
     117             :   }
     118             :   (void) idx; /* do NOT use! We ignore 'idx', must be 0 for melt! */
     119             : 
     120           0 :   reserve_cmd = TALER_TESTING_interpreter_lookup_command (is,
     121             :                                                           cref);
     122           0 :   GNUNET_free (cref);
     123             : 
     124           0 :   if (NULL == reserve_cmd)
     125             :   {
     126           0 :     GNUNET_break (0);
     127           0 :     TALER_TESTING_interpreter_fail (is);
     128           0 :     return;
     129             :   }
     130             : 
     131           0 :   switch (hr->http_status)
     132             :   {
     133           0 :   case MHD_HTTP_OK:
     134             :     /* check old_coin_pub or reserve_pub, respectively */
     135             :     {
     136             :       const struct TALER_ReservePrivateKeyP *reserve_priv;
     137             : 
     138           0 :       if (NULL == reserve_pub)
     139             :       {
     140           0 :         GNUNET_break (0);
     141           0 :         TALER_TESTING_interpreter_fail (is);
     142           0 :         return;
     143             :       }
     144           0 :       if (GNUNET_OK !=
     145           0 :           TALER_TESTING_get_trait_reserve_priv (reserve_cmd,
     146             :                                                 &reserve_priv))
     147             :       {
     148           0 :         GNUNET_break (0);
     149           0 :         TALER_TESTING_interpreter_fail (is);
     150           0 :         return;
     151             :       }
     152           0 :       GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv->eddsa_priv,
     153             :                                           &ps->reserve_pub.eddsa_pub);
     154           0 :       if (0 != GNUNET_memcmp (reserve_pub,
     155             :                               &ps->reserve_pub))
     156             :       {
     157           0 :         GNUNET_break (0);
     158           0 :         TALER_TESTING_interpreter_fail (is);
     159           0 :         return;
     160             :       }
     161           0 :       if (GNUNET_OK ==
     162           0 :           TALER_amount_is_valid (&ps->reserve_history.amount))
     163           0 :         ps->reserve_history.type = TALER_EXCHANGE_RTT_RECOUP;
     164             :       /* ps->reserve_history.details.recoup_details.coin_pub; // initialized earlier */
     165             :     }
     166           0 :     break;
     167           0 :   case MHD_HTTP_NOT_FOUND:
     168           0 :     break;
     169           0 :   case MHD_HTTP_CONFLICT:
     170           0 :     break;
     171           0 :   default:
     172           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     173             :                 "Unmanaged HTTP status code %u/%d.\n",
     174             :                 hr->http_status,
     175             :                 (int) hr->ec);
     176           0 :     break;
     177             :   }
     178           0 :   TALER_TESTING_interpreter_next (is);
     179             : }
     180             : 
     181             : 
     182             : /**
     183             :  * Run the command.
     184             :  *
     185             :  * @param cls closure.
     186             :  * @param cmd the command to execute.
     187             :  * @param is the interpreter state.
     188             :  */
     189             : static void
     190           0 : recoup_run (void *cls,
     191             :             const struct TALER_TESTING_Command *cmd,
     192             :             struct TALER_TESTING_Interpreter *is)
     193             : {
     194           0 :   struct RecoupState *ps = cls;
     195             :   const struct TALER_TESTING_Command *coin_cmd;
     196             :   const struct TALER_CoinSpendPrivateKeyP *coin_priv;
     197             :   const struct TALER_EXCHANGE_DenomPublicKey *denom_pub;
     198             :   const struct TALER_DenominationSignature *coin_sig;
     199             :   const struct TALER_PlanchetMasterSecretP *planchet;
     200             :   char *cref;
     201             :   unsigned int idx;
     202             :   const struct TALER_ExchangeWithdrawValues *ewv;
     203             : 
     204           0 :   ps->is = is;
     205           0 :   if (GNUNET_OK !=
     206           0 :       TALER_TESTING_parse_coin_reference (
     207             :         ps->coin_reference,
     208             :         &cref,
     209             :         &idx))
     210             :   {
     211           0 :     TALER_TESTING_interpreter_fail (is);
     212           0 :     return;
     213             :   }
     214             : 
     215           0 :   coin_cmd = TALER_TESTING_interpreter_lookup_command (is,
     216             :                                                        cref);
     217           0 :   GNUNET_free (cref);
     218             : 
     219           0 :   if (NULL == coin_cmd)
     220             :   {
     221           0 :     GNUNET_break (0);
     222           0 :     TALER_TESTING_interpreter_fail (is);
     223           0 :     return;
     224             :   }
     225           0 :   if (GNUNET_OK !=
     226           0 :       TALER_TESTING_get_trait_coin_priv (coin_cmd,
     227             :                                          idx,
     228             :                                          &coin_priv))
     229             :   {
     230           0 :     GNUNET_break (0);
     231           0 :     TALER_TESTING_interpreter_fail (is);
     232           0 :     return;
     233             :   }
     234           0 :   if (GNUNET_OK !=
     235           0 :       TALER_TESTING_get_trait_exchange_wd_value (coin_cmd,
     236             :                                                  idx,
     237             :                                                  &ewv))
     238             :   {
     239           0 :     GNUNET_break (0);
     240           0 :     TALER_TESTING_interpreter_fail (is);
     241           0 :     return;
     242             :   }
     243           0 :   if (GNUNET_OK !=
     244           0 :       TALER_TESTING_get_trait_planchet_secret (coin_cmd,
     245             :                                                &planchet))
     246             :   {
     247           0 :     GNUNET_break (0);
     248           0 :     TALER_TESTING_interpreter_fail (is);
     249           0 :     return;
     250             :   }
     251           0 :   GNUNET_CRYPTO_eddsa_key_get_public (
     252           0 :     &coin_priv->eddsa_priv,
     253             :     &ps->reserve_history.details.recoup_details.coin_pub.eddsa_pub);
     254             : 
     255           0 :   if (GNUNET_OK !=
     256           0 :       TALER_TESTING_get_trait_denom_pub (coin_cmd,
     257             :                                          idx,
     258             :                                          &denom_pub))
     259             :   {
     260           0 :     GNUNET_break (0);
     261           0 :     TALER_TESTING_interpreter_fail (is);
     262           0 :     return;
     263             :   }
     264           0 :   if (GNUNET_OK !=
     265           0 :       TALER_TESTING_get_trait_denom_sig (coin_cmd,
     266             :                                          idx,
     267             :                                          &coin_sig))
     268             :   {
     269           0 :     GNUNET_break (0);
     270           0 :     TALER_TESTING_interpreter_fail (is);
     271           0 :     return;
     272             :   }
     273           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     274             :               "Trying to recoup denomination '%s'\n",
     275             :               TALER_B2S (&denom_pub->h_key));
     276           0 :   ps->ph = TALER_EXCHANGE_recoup (is->exchange,
     277             :                                   denom_pub,
     278             :                                   coin_sig,
     279             :                                   ewv,
     280             :                                   planchet,
     281             :                                   &recoup_cb,
     282             :                                   ps);
     283           0 :   GNUNET_assert (NULL != ps->ph);
     284             : }
     285             : 
     286             : 
     287             : /**
     288             :  * Cleanup the "recoup" CMD state, and possibly cancel
     289             :  * a pending operation thereof.
     290             :  *
     291             :  * @param cls closure.
     292             :  * @param cmd the command which is being cleaned up.
     293             :  */
     294             : static void
     295           0 : recoup_cleanup (void *cls,
     296             :                 const struct TALER_TESTING_Command *cmd)
     297             : {
     298           0 :   struct RecoupState *ps = cls;
     299           0 :   if (NULL != ps->ph)
     300             :   {
     301           0 :     TALER_EXCHANGE_recoup_cancel (ps->ph);
     302           0 :     ps->ph = NULL;
     303             :   }
     304           0 :   GNUNET_free (ps);
     305           0 : }
     306             : 
     307             : 
     308             : /**
     309             :  * Offer internal data from a "recoup" CMD state to other
     310             :  * commands.
     311             :  *
     312             :  * @param cls closure
     313             :  * @param[out] ret result (could be anything)
     314             :  * @param trait name of the trait
     315             :  * @param index index number of the object to offer.
     316             :  * @return #GNUNET_OK on success
     317             :  */
     318             : static enum GNUNET_GenericReturnValue
     319           0 : recoup_traits (void *cls,
     320             :                const void **ret,
     321             :                const char *trait,
     322             :                unsigned int index)
     323             : {
     324           0 :   struct RecoupState *ps = cls;
     325             : 
     326           0 :   if (ps->reserve_history.type != TALER_EXCHANGE_RTT_RECOUP)
     327           0 :     return GNUNET_SYSERR; /* no traits */
     328             :   {
     329             :     struct TALER_TESTING_Trait traits[] = {
     330           0 :       TALER_TESTING_make_trait_reserve_pub (&ps->reserve_pub),
     331           0 :       TALER_TESTING_make_trait_reserve_history (0,
     332           0 :                                                 &ps->reserve_history),
     333           0 :       TALER_TESTING_trait_end ()
     334             :     };
     335             : 
     336           0 :     return TALER_TESTING_get_trait (traits,
     337             :                                     ret,
     338             :                                     trait,
     339             :                                     index);
     340             :   }
     341             : }
     342             : 
     343             : 
     344             : struct TALER_TESTING_Command
     345           0 : TALER_TESTING_cmd_recoup (const char *label,
     346             :                           unsigned int expected_response_code,
     347             :                           const char *coin_reference,
     348             :                           const char *amount)
     349             : {
     350             :   struct RecoupState *ps;
     351             : 
     352           0 :   ps = GNUNET_new (struct RecoupState);
     353           0 :   ps->expected_response_code = expected_response_code;
     354           0 :   ps->coin_reference = coin_reference;
     355           0 :   if (GNUNET_OK !=
     356           0 :       TALER_string_to_amount (amount,
     357             :                               &ps->reserve_history.amount))
     358             :   {
     359           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     360             :                 "Failed to parse amount `%s' at %s\n",
     361             :                 amount,
     362             :                 label);
     363           0 :     GNUNET_assert (0);
     364             :   }
     365             :   {
     366           0 :     struct TALER_TESTING_Command cmd = {
     367             :       .cls = ps,
     368             :       .label = label,
     369             :       .run = &recoup_run,
     370             :       .cleanup = &recoup_cleanup,
     371             :       .traits = &recoup_traits
     372             :     };
     373             : 
     374           0 :     return cmd;
     375             :   }
     376             : }

Generated by: LCOV version 1.14