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: 92 147 62.6 %
Date: 2021-08-30 06:43:37 Functions: 6 6 100.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             :    * NULL if coin was not refreshed, otherwise reference
      58             :    * to the melt operation underlying @a coin_reference.
      59             :    */
      60             :   const char *melt_reference;
      61             : 
      62             :   /**
      63             :    * If the recoup filled a reserve, this is set to the reserve's public key.
      64             :    */
      65             :   struct TALER_ReservePublicKeyP reserve_pub;
      66             : 
      67             :   /**
      68             :    * Reserve history entry, set if this recoup actually filled up a reserve.
      69             :    * Otherwise `reserve_history.type` will be zero.
      70             :    */
      71             :   struct TALER_EXCHANGE_ReserveHistory reserve_history;
      72             : 
      73             : };
      74             : 
      75             : 
      76             : /**
      77             :  * Parser reference to a coin.
      78             :  *
      79             :  * @param coin_reference of format $LABEL['#' $INDEX]?
      80             :  * @param[out] cref where we return a copy of $LABEL
      81             :  * @param[out] idx where we set $INDEX
      82             :  * @return #GNUNET_SYSERR if $INDEX is present but not numeric
      83             :  */
      84             : static int
      85          36 : parse_coin_reference (const char *coin_reference,
      86             :                       char **cref,
      87             :                       unsigned int *idx)
      88             : {
      89             :   const char *index;
      90             : 
      91             :   /* We allow command references of the form "$LABEL#$INDEX" or
      92             :      just "$LABEL", which implies the index is 0. Figure out
      93             :      which one it is. */
      94          36 :   index = strchr (coin_reference, '#');
      95          36 :   if (NULL == index)
      96             :   {
      97          20 :     *idx = 0;
      98          20 :     *cref = GNUNET_strdup (coin_reference);
      99          20 :     return GNUNET_OK;
     100             :   }
     101          16 :   *cref = GNUNET_strndup (coin_reference,
     102             :                           index - coin_reference);
     103          16 :   if (1 != sscanf (index + 1,
     104             :                    "%u",
     105             :                    idx))
     106             :   {
     107           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     108             :                 "Numeric index (not `%s') required after `#' in command reference of command in %s:%u\n",
     109             :                 index,
     110             :                 __FILE__,
     111             :                 __LINE__);
     112           0 :     GNUNET_free (*cref);
     113           0 :     *cref = NULL;
     114           0 :     return GNUNET_SYSERR;
     115             :   }
     116          16 :   return GNUNET_OK;
     117             : }
     118             : 
     119             : 
     120             : /**
     121             :  * Check the result of the recoup request: checks whether
     122             :  * the HTTP response code is good, and that the coin that
     123             :  * was paid back belonged to the right reserve.
     124             :  *
     125             :  * @param cls closure
     126             :  * @param hr HTTP response details
     127             :  * @param reserve_pub public key of the reserve receiving the recoup, NULL if refreshed or on error
     128             :  * @param old_coin_pub public key of the dirty coin, NULL if not refreshed or on error
     129             :  */
     130             : static void
     131          18 : recoup_cb (void *cls,
     132             :            const struct TALER_EXCHANGE_HttpResponse *hr,
     133             :            const struct TALER_ReservePublicKeyP *reserve_pub,
     134             :            const struct TALER_CoinSpendPublicKeyP *old_coin_pub)
     135             : {
     136          18 :   struct RecoupState *ps = cls;
     137          18 :   struct TALER_TESTING_Interpreter *is = ps->is;
     138          18 :   struct TALER_TESTING_Command *cmd = &is->commands[is->ip];
     139             :   const struct TALER_TESTING_Command *reserve_cmd;
     140             :   char *cref;
     141             :   unsigned int idx;
     142             : 
     143          18 :   ps->ph = NULL;
     144          18 :   if (ps->expected_response_code != hr->http_status)
     145             :   {
     146           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     147             :                 "Unexpected response code %u/%d to command %s in %s:%u\n",
     148             :                 hr->http_status,
     149             :                 (int) hr->ec,
     150             :                 cmd->label,
     151             :                 __FILE__,
     152             :                 __LINE__);
     153           0 :     json_dumpf (hr->reply,
     154             :                 stderr,
     155             :                 0);
     156           0 :     fprintf (stderr, "\n");
     157           0 :     TALER_TESTING_interpreter_fail (is);
     158           0 :     return;
     159             :   }
     160             : 
     161          18 :   if (GNUNET_OK !=
     162          18 :       parse_coin_reference (ps->coin_reference,
     163             :                             &cref,
     164             :                             &idx))
     165             :   {
     166           0 :     TALER_TESTING_interpreter_fail (is);
     167           0 :     return;
     168             :   }
     169             : 
     170          18 :   reserve_cmd = TALER_TESTING_interpreter_lookup_command (is,
     171             :                                                           cref);
     172          18 :   GNUNET_free (cref);
     173             : 
     174          18 :   if (NULL == reserve_cmd)
     175             :   {
     176           0 :     GNUNET_break (0);
     177           0 :     TALER_TESTING_interpreter_fail (is);
     178           0 :     return;
     179             :   }
     180             : 
     181          18 :   switch (hr->http_status)
     182             :   {
     183          15 :   case MHD_HTTP_OK:
     184             :     /* check old_coin_pub or reserve_pub, respectively */
     185          15 :     if (NULL != ps->melt_reference)
     186             :     {
     187             :       const struct TALER_TESTING_Command *melt_cmd;
     188             :       const struct TALER_CoinSpendPrivateKeyP *dirty_priv;
     189             :       struct TALER_CoinSpendPublicKeyP oc;
     190             : 
     191           9 :       melt_cmd = TALER_TESTING_interpreter_lookup_command (is,
     192             :                                                            ps->melt_reference);
     193           9 :       if (NULL == melt_cmd)
     194             :       {
     195           0 :         GNUNET_break (0);
     196           0 :         TALER_TESTING_interpreter_fail (is);
     197           0 :         return;
     198             :       }
     199           9 :       if (GNUNET_OK !=
     200           9 :           TALER_TESTING_get_trait_coin_priv (melt_cmd,
     201             :                                              0,
     202             :                                              &dirty_priv))
     203             :       {
     204           0 :         GNUNET_break (0);
     205           0 :         TALER_TESTING_interpreter_fail (is);
     206           0 :         return;
     207             :       }
     208           9 :       GNUNET_CRYPTO_eddsa_key_get_public (&dirty_priv->eddsa_priv,
     209             :                                           &oc.eddsa_pub);
     210           9 :       if (0 != GNUNET_memcmp (&oc,
     211             :                               old_coin_pub))
     212             :       {
     213           0 :         GNUNET_break (0);
     214           0 :         TALER_TESTING_interpreter_fail (is);
     215           0 :         return;
     216             :       }
     217             :     }
     218             :     else
     219             :     {
     220             :       const struct TALER_ReservePrivateKeyP *reserve_priv;
     221             : 
     222           6 :       if (NULL == reserve_pub)
     223             :       {
     224           0 :         GNUNET_break (0);
     225           0 :         TALER_TESTING_interpreter_fail (is);
     226           0 :         return;
     227             :       }
     228           6 :       if (GNUNET_OK !=
     229           6 :           TALER_TESTING_get_trait_reserve_priv (reserve_cmd,
     230             :                                                 idx,
     231             :                                                 &reserve_priv))
     232             :       {
     233           0 :         GNUNET_break (0);
     234           0 :         TALER_TESTING_interpreter_fail (is);
     235           0 :         return;
     236             :       }
     237           6 :       GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv->eddsa_priv,
     238             :                                           &ps->reserve_pub.eddsa_pub);
     239           6 :       if (0 != GNUNET_memcmp (reserve_pub,
     240             :                               &ps->reserve_pub))
     241             :       {
     242           0 :         GNUNET_break (0);
     243           0 :         TALER_TESTING_interpreter_fail (is);
     244           0 :         return;
     245             :       }
     246           6 :       if (GNUNET_OK ==
     247           6 :           TALER_amount_is_valid (&ps->reserve_history.amount))
     248           5 :         ps->reserve_history.type = TALER_EXCHANGE_RTT_RECOUP;
     249             :       /* ps->reserve_history.details.recoup_details.coin_pub; // initialized earlier */
     250             :     }
     251          15 :     break;
     252           0 :   case MHD_HTTP_NOT_FOUND:
     253           0 :     break;
     254           2 :   case MHD_HTTP_CONFLICT:
     255           2 :     break;
     256           1 :   default:
     257           1 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     258             :                 "Unmanaged HTTP status code %u/%d.\n",
     259             :                 hr->http_status,
     260             :                 (int) hr->ec);
     261           1 :     break;
     262             :   }
     263          18 :   TALER_TESTING_interpreter_next (is);
     264             : }
     265             : 
     266             : 
     267             : /**
     268             :  * Run the command.
     269             :  *
     270             :  * @param cls closure.
     271             :  * @param cmd the command to execute.
     272             :  * @param is the interpreter state.
     273             :  */
     274             : static void
     275          18 : recoup_run (void *cls,
     276             :             const struct TALER_TESTING_Command *cmd,
     277             :             struct TALER_TESTING_Interpreter *is)
     278             : {
     279          18 :   struct RecoupState *ps = cls;
     280             :   const struct TALER_TESTING_Command *coin_cmd;
     281             :   const struct TALER_CoinSpendPrivateKeyP *coin_priv;
     282             :   const struct TALER_DenominationBlindingKeyP *blinding_key;
     283             :   const struct TALER_EXCHANGE_DenomPublicKey *denom_pub;
     284             :   const struct TALER_DenominationSignature *coin_sig;
     285             :   struct TALER_PlanchetSecretsP planchet;
     286             :   char *cref;
     287             :   unsigned int idx;
     288             : 
     289          18 :   ps->is = is;
     290          18 :   if (GNUNET_OK !=
     291          18 :       parse_coin_reference (ps->coin_reference,
     292             :                             &cref,
     293             :                             &idx))
     294             :   {
     295           0 :     TALER_TESTING_interpreter_fail (is);
     296           0 :     return;
     297             :   }
     298             : 
     299          18 :   coin_cmd = TALER_TESTING_interpreter_lookup_command (is,
     300             :                                                        cref);
     301          18 :   GNUNET_free (cref);
     302             : 
     303          18 :   if (NULL == coin_cmd)
     304             :   {
     305           0 :     GNUNET_break (0);
     306           0 :     TALER_TESTING_interpreter_fail (is);
     307           0 :     return;
     308             :   }
     309             : 
     310          18 :   if (GNUNET_OK !=
     311          18 :       TALER_TESTING_get_trait_coin_priv (coin_cmd,
     312             :                                          idx,
     313             :                                          &coin_priv))
     314             :   {
     315           0 :     GNUNET_break (0);
     316           0 :     TALER_TESTING_interpreter_fail (is);
     317           0 :     return;
     318             :   }
     319             : 
     320          18 :   if (GNUNET_OK !=
     321          18 :       TALER_TESTING_get_trait_blinding_key (coin_cmd,
     322             :                                             idx,
     323             :                                             &blinding_key))
     324             :   {
     325           0 :     GNUNET_break (0);
     326           0 :     TALER_TESTING_interpreter_fail (is);
     327           0 :     return;
     328             :   }
     329          18 :   planchet.coin_priv = *coin_priv;
     330          18 :   planchet.blinding_key = *blinding_key;
     331          18 :   GNUNET_CRYPTO_eddsa_key_get_public (
     332          18 :     &coin_priv->eddsa_priv,
     333             :     &ps->reserve_history.details.recoup_details.coin_pub.eddsa_pub);
     334             : 
     335          18 :   if (GNUNET_OK !=
     336          18 :       TALER_TESTING_get_trait_denom_pub (coin_cmd,
     337             :                                          idx,
     338             :                                          &denom_pub))
     339             :   {
     340           0 :     GNUNET_break (0);
     341           0 :     TALER_TESTING_interpreter_fail (is);
     342           0 :     return;
     343             :   }
     344             : 
     345          18 :   if (GNUNET_OK !=
     346          18 :       TALER_TESTING_get_trait_denom_sig (coin_cmd,
     347             :                                          idx,
     348             :                                          &coin_sig))
     349             :   {
     350           0 :     GNUNET_break (0);
     351           0 :     TALER_TESTING_interpreter_fail (is);
     352           0 :     return;
     353             :   }
     354             : 
     355          18 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     356             :               "Trying to recoup denomination '%s'\n",
     357             :               TALER_B2S (&denom_pub->h_key));
     358             : 
     359          36 :   ps->ph = TALER_EXCHANGE_recoup (is->exchange,
     360             :                                   denom_pub,
     361             :                                   coin_sig,
     362             :                                   &planchet,
     363          18 :                                   NULL != ps->melt_reference,
     364             :                                   recoup_cb,
     365             :                                   ps);
     366          18 :   GNUNET_assert (NULL != ps->ph);
     367             : }
     368             : 
     369             : 
     370             : /**
     371             :  * Cleanup the "recoup" CMD state, and possibly cancel
     372             :  * a pending operation thereof.
     373             :  *
     374             :  * @param cls closure.
     375             :  * @param cmd the command which is being cleaned up.
     376             :  */
     377             : static void
     378          18 : recoup_cleanup (void *cls,
     379             :                 const struct TALER_TESTING_Command *cmd)
     380             : {
     381          18 :   struct RecoupState *ps = cls;
     382          18 :   if (NULL != ps->ph)
     383             :   {
     384           0 :     TALER_EXCHANGE_recoup_cancel (ps->ph);
     385           0 :     ps->ph = NULL;
     386             :   }
     387          18 :   GNUNET_free (ps);
     388          18 : }
     389             : 
     390             : 
     391             : /**
     392             :  * Offer internal data from a "recoup" CMD state to other
     393             :  * commands.
     394             :  *
     395             :  * @param cls closure
     396             :  * @param[out] ret result (could be anything)
     397             :  * @param trait name of the trait
     398             :  * @param index index number of the object to offer.
     399             :  * @return #GNUNET_OK on success
     400             :  */
     401             : static int
     402          34 : recoup_traits (void *cls,
     403             :                const void **ret,
     404             :                const char *trait,
     405             :                unsigned int index)
     406             : {
     407          34 :   struct RecoupState *ps = cls;
     408             : 
     409          34 :   if (ps->reserve_history.type != TALER_EXCHANGE_RTT_RECOUP)
     410          21 :     return GNUNET_SYSERR; /* no traits */
     411             :   {
     412             :     struct TALER_TESTING_Trait traits[] = {
     413          13 :       TALER_TESTING_make_trait_reserve_pub (0,
     414          13 :                                             &ps->reserve_pub),
     415          13 :       TALER_TESTING_make_trait_reserve_history (0,
     416          13 :                                                 &ps->reserve_history),
     417          13 :       TALER_TESTING_trait_end ()
     418             :     };
     419             : 
     420          13 :     return TALER_TESTING_get_trait (traits,
     421             :                                     ret,
     422             :                                     trait,
     423             :                                     index);
     424             :   }
     425             : }
     426             : 
     427             : 
     428             : /**
     429             :  * Make a "recoup" command.
     430             :  *
     431             :  * @param label the command label
     432             :  * @param expected_response_code expected HTTP status code
     433             :  * @param coin_reference reference to any command which
     434             :  *        offers a coin & reserve private key.
     435             :  * @param melt_reference NULL if coin was not refreshed
     436             :  * @param amount how much do we expect to recoup?
     437             :  * @return the command.
     438             :  */
     439             : struct TALER_TESTING_Command
     440          18 : TALER_TESTING_cmd_recoup (const char *label,
     441             :                           unsigned int expected_response_code,
     442             :                           const char *coin_reference,
     443             :                           const char *melt_reference,
     444             :                           const char *amount)
     445             : {
     446             :   struct RecoupState *ps;
     447             : 
     448          18 :   ps = GNUNET_new (struct RecoupState);
     449          18 :   ps->expected_response_code = expected_response_code;
     450          18 :   ps->coin_reference = coin_reference;
     451          18 :   ps->melt_reference = melt_reference;
     452          25 :   if ( (NULL != amount) &&
     453             :        (GNUNET_OK !=
     454           7 :         TALER_string_to_amount (amount,
     455             :                                 &ps->reserve_history.amount)) )
     456             :   {
     457           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     458             :                 "Failed to parse amount `%s' at %s\n",
     459             :                 amount,
     460             :                 label);
     461           0 :     GNUNET_assert (0);
     462             :   }
     463             :   {
     464          18 :     struct TALER_TESTING_Command cmd = {
     465             :       .cls = ps,
     466             :       .label = label,
     467             :       .run = &recoup_run,
     468             :       .cleanup = &recoup_cleanup,
     469             :       .traits = &recoup_traits
     470             :     };
     471             : 
     472          18 :     return cmd;
     473             :   }
     474             : }

Generated by: LCOV version 1.14