LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_status.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 102 134 76.1 %
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-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_status.c
      21             :  * @brief Implement the /reserve/status 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 "status" CMD.
      32             :  */
      33             : struct StatusState
      34             : {
      35             :   /**
      36             :    * Label to the command which created the reserve to check,
      37             :    * needed to resort the reserve key.
      38             :    */
      39             :   const char *reserve_reference;
      40             : 
      41             :   /**
      42             :    * Handle to the "reserve status" operation.
      43             :    */
      44             :   struct TALER_EXCHANGE_ReservesGetHandle *rsh;
      45             : 
      46             :   /**
      47             :    * Expected reserve balance.
      48             :    */
      49             :   const char *expected_balance;
      50             : 
      51             :   /**
      52             :    * Public key of the reserve being analyzed.
      53             :    */
      54             :   const struct TALER_ReservePublicKeyP *reserve_pubp;
      55             : 
      56             :   /**
      57             :    * Expected HTTP response code.
      58             :    */
      59             :   unsigned int expected_response_code;
      60             : 
      61             :   /**
      62             :    * Interpreter state.
      63             :    */
      64             :   struct TALER_TESTING_Interpreter *is;
      65             : };
      66             : 
      67             : 
      68             : /**
      69             :  * Compare @a h1 and @a h2.
      70             :  *
      71             :  * @param h1 a history entry
      72             :  * @param h2 a history entry
      73             :  * @return 0 if @a h1 and @a h2 are equal
      74             :  */
      75             : static int
      76          30 : history_entry_cmp (const struct TALER_EXCHANGE_ReserveHistory *h1,
      77             :                    const struct TALER_EXCHANGE_ReserveHistory *h2)
      78             : {
      79          30 :   if (h1->type != h2->type)
      80           1 :     return 1;
      81          29 :   switch (h1->type)
      82             :   {
      83           8 :   case TALER_EXCHANGE_RTT_CREDIT:
      84           8 :     if ( (0 ==
      85           8 :           TALER_amount_cmp (&h1->amount,
      86           8 :                             &h2->amount)) &&
      87           8 :          (0 == strcasecmp (h1->details.in_details.sender_url,
      88           8 :                            h2->details.in_details.sender_url)) &&
      89           8 :          (h1->details.in_details.wire_reference ==
      90           8 :           h2->details.in_details.wire_reference) &&
      91           8 :          (h1->details.in_details.timestamp.abs_value_us ==
      92           8 :           h2->details.in_details.timestamp.abs_value_us) )
      93           8 :       return 0;
      94           0 :     return 1;
      95          14 :   case TALER_EXCHANGE_RTT_WITHDRAWAL:
      96          14 :     if ( (0 ==
      97          14 :           TALER_amount_cmp (&h1->amount,
      98           9 :                             &h2->amount)) &&
      99             :          (0 ==
     100           9 :           TALER_amount_cmp (&h1->details.withdraw.fee,
     101             :                             &h2->details.withdraw.fee)) )
     102             :       /* testing_api_cmd_withdraw doesn't set the out_authorization_sig,
     103             :          so we cannot test for it here. but if the amount matches,
     104             :          that should be good enough. */
     105           9 :       return 0;
     106           5 :     return 1;
     107           3 :   case TALER_EXCHANGE_RTT_RECOUP:
     108             :     /* exchange_sig, exchange_pub and timestamp are NOT available
     109             :        from the original recoup response, hence here NOT check(able/ed) */
     110           3 :     if ( (0 ==
     111           3 :           TALER_amount_cmp (&h1->amount,
     112           3 :                             &h2->amount)) &&
     113             :          (0 ==
     114           3 :           GNUNET_memcmp (&h1->details.recoup_details.coin_pub,
     115             :                          &h2->details.recoup_details.coin_pub)) )
     116           3 :       return 0;
     117           0 :     return 1;
     118           4 :   case TALER_EXCHANGE_RTT_CLOSE:
     119             :     /* testing_api_cmd_exec_closer doesn't set the
     120             :        receiver_account_details, exchange_sig, exchange_pub or wtid or timestamp
     121             :        so we cannot test for it here. but if the amount matches,
     122             :        that should be good enough. */
     123           4 :     if ( (0 ==
     124           4 :           TALER_amount_cmp (&h1->amount,
     125           4 :                             &h2->amount)) &&
     126             :          (0 ==
     127           4 :           TALER_amount_cmp (&h1->details.close_details.fee,
     128             :                             &h2->details.close_details.fee)) )
     129           4 :       return 0;
     130           0 :     return 1;
     131             :   }
     132           0 :   GNUNET_assert (0);
     133             :   return 1;
     134             : }
     135             : 
     136             : 
     137             : /**
     138             :  * Check if @a cmd changed the reserve, if so, find the
     139             :  * entry in @a history and set the respective index in @a found
     140             :  * to #GNUNET_YES. If the entry is not found, return #GNUNET_SYSERR.
     141             :  *
     142             :  * @param reserve_pub public key of the reserve for which we have the @a history
     143             :  * @param cmd command to analyze for impact on history
     144             :  * @param history_length number of entries in @a history and @a found
     145             :  * @param history history to check
     146             :  * @param[in,out] found array to update
     147             :  * @return #GNUNET_OK if @a cmd action on reserve was found in @a history
     148             :  */
     149             : static int
     150         899 : analyze_command (const struct TALER_ReservePublicKeyP *reserve_pub,
     151             :                  const struct TALER_TESTING_Command *cmd,
     152             :                  unsigned int history_length,
     153             :                  const struct TALER_EXCHANGE_ReserveHistory *history,
     154             :                  int *found)
     155             : {
     156         899 :   if (TALER_TESTING_cmd_is_batch (cmd))
     157             :   {
     158             : #define BATCH_INDEX 1
     159             :     struct TALER_TESTING_Command *cur;
     160             :     struct TALER_TESTING_Command *bcmd;
     161             : 
     162          59 :     cur = TALER_TESTING_cmd_batch_get_current (cmd);
     163          59 :     if (GNUNET_OK !=
     164          59 :         TALER_TESTING_get_trait_cmd (cmd,
     165             :                                      BATCH_INDEX,
     166             :                                      &bcmd))
     167             :     {
     168           0 :       GNUNET_break (0);
     169           0 :       return GNUNET_SYSERR;
     170             :     }
     171         833 :     for (unsigned int i = 0; NULL != bcmd[i].label; i++)
     172             :     {
     173         781 :       struct TALER_TESTING_Command *step = &bcmd[i];
     174             : 
     175         781 :       if (step == cur)
     176           7 :         break; /* if *we* are in a batch, make sure not to analyze commands past 'now' */
     177         774 :       if (GNUNET_OK !=
     178         774 :           analyze_command (reserve_pub,
     179             :                            step,
     180             :                            history_length,
     181             :                            history,
     182             :                            found))
     183           0 :         return GNUNET_SYSERR;
     184             :     }
     185          59 :     return GNUNET_OK;
     186             :   }
     187             :   else
     188             :   {
     189             :     const struct TALER_ReservePublicKeyP *rp;
     190             :     const struct TALER_EXCHANGE_ReserveHistory *he;
     191             : 
     192         840 :     if (GNUNET_OK !=
     193         840 :         TALER_TESTING_get_trait_reserve_pub (cmd,
     194             :                                              0,
     195             :                                              &rp))
     196         674 :       return GNUNET_OK; /* command does nothing for reserves */
     197         166 :     if (0 !=
     198         166 :         GNUNET_memcmp (rp,
     199             :                        reserve_pub))
     200         141 :       return GNUNET_OK; /* command affects some _other_ reserve */
     201          25 :     if (GNUNET_OK !=
     202          25 :         TALER_TESTING_get_trait_reserve_history (cmd,
     203             :                                                  0,
     204             :                                                  &he))
     205             :     {
     206             :       /* NOTE: only for debugging... */
     207           1 :       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     208             :                   "Command `%s' has the reserve_pub trait, but does not reserve history trait\n",
     209             :                   cmd->label);
     210           1 :       return GNUNET_OK; /* command does nothing for reserves */
     211             :     }
     212          53 :     for (unsigned int i = 0; i<history_length; i++)
     213             :     {
     214          53 :       if (found[i])
     215          23 :         continue; /* already found, skip */
     216          30 :       if (0 ==
     217          30 :           history_entry_cmp (he,
     218          30 :                              &history[i]))
     219             :       {
     220          24 :         found[i] = GNUNET_YES;
     221          24 :         return GNUNET_OK;
     222             :       }
     223             :     }
     224           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     225             :                 "Command `%s' reserve history entry not found\n",
     226             :                 cmd->label);
     227           0 :     return GNUNET_SYSERR;
     228             :   }
     229             : }
     230             : 
     231             : 
     232             : /**
     233             :  * Check that the reserve balance and HTTP response code are
     234             :  * both acceptable.
     235             :  *
     236             :  * @param cls closure.
     237             :  * @param hr HTTP response details
     238             :  * @param balance current balance in the reserve, NULL on error.
     239             :  * @param history_length number of entries in the transaction
     240             :  *        history, 0 on error.
     241             :  * @param history detailed transaction history, NULL on error.
     242             :  */
     243             : static void
     244           8 : reserve_status_cb (void *cls,
     245             :                    const struct TALER_EXCHANGE_HttpResponse *hr,
     246             :                    const struct TALER_Amount *balance,
     247             :                    unsigned int history_length,
     248             :                    const struct TALER_EXCHANGE_ReserveHistory *history)
     249             : {
     250           8 :   struct StatusState *ss = cls;
     251           8 :   struct TALER_TESTING_Interpreter *is = ss->is;
     252             :   struct TALER_Amount eb;
     253             : 
     254           8 :   ss->rsh = NULL;
     255           8 :   if (ss->expected_response_code != hr->http_status)
     256             :   {
     257           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     258             :                 "Unexpected HTTP response code: %d in %s:%u\n",
     259             :                 hr->http_status,
     260             :                 __FILE__,
     261             :                 __LINE__);
     262           0 :     json_dumpf (hr->reply,
     263             :                 stderr,
     264             :                 0);
     265           0 :     TALER_TESTING_interpreter_fail (ss->is);
     266           0 :     return;
     267             :   }
     268             : 
     269           8 :   GNUNET_assert (GNUNET_OK ==
     270             :                  TALER_string_to_amount (ss->expected_balance,
     271             :                                          &eb));
     272             : 
     273           8 :   if (0 != TALER_amount_cmp (&eb,
     274             :                              balance))
     275             :   {
     276           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     277             :                 "Unexpected amount in reserve: %s\n",
     278             :                 TALER_amount_to_string (balance));
     279           0 :     TALER_TESTING_interpreter_fail (ss->is);
     280           0 :     return;
     281             :   }
     282           8 :   {
     283           8 :     int found[history_length];
     284             : 
     285           8 :     memset (found,
     286             :             0,
     287             :             sizeof (found));
     288         133 :     for (unsigned int i = 0; i<=is->ip; i++)
     289             :     {
     290         125 :       struct TALER_TESTING_Command *cmd = &is->commands[i];
     291             : 
     292         125 :       if (GNUNET_OK !=
     293         125 :           analyze_command (ss->reserve_pubp,
     294             :                            cmd,
     295             :                            history_length,
     296             :                            history,
     297             :                            found))
     298             :       {
     299           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     300             :                     "Entry for command `%s' missing in history\n",
     301             :                     cmd->label);
     302           0 :         TALER_TESTING_interpreter_fail (ss->is);
     303           0 :         return;
     304             :       }
     305             :     }
     306          32 :     for (unsigned int i = 0; i<history_length; i++)
     307          24 :       if (! found[i])
     308             :       {
     309           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     310             :                     "History entry at index %u of type %d not justified by command history\n",
     311             :                     i,
     312             :                     history[i].type);
     313           0 :         TALER_TESTING_interpreter_fail (ss->is);
     314           0 :         return;
     315             :       }
     316             :   }
     317           8 :   TALER_TESTING_interpreter_next (is);
     318             : }
     319             : 
     320             : 
     321             : /**
     322             :  * Run the command.
     323             :  *
     324             :  * @param cls closure.
     325             :  * @param cmd the command being executed.
     326             :  * @param is the interpreter state.
     327             :  */
     328             : static void
     329           8 : status_run (void *cls,
     330             :             const struct TALER_TESTING_Command *cmd,
     331             :             struct TALER_TESTING_Interpreter *is)
     332             : {
     333           8 :   struct StatusState *ss = cls;
     334             :   const struct TALER_TESTING_Command *create_reserve;
     335             : 
     336           8 :   ss->is = is;
     337             :   create_reserve
     338           8 :     = TALER_TESTING_interpreter_lookup_command (is,
     339             :                                                 ss->reserve_reference);
     340             : 
     341           8 :   if (NULL == create_reserve)
     342             :   {
     343           0 :     GNUNET_break (0);
     344           0 :     TALER_TESTING_interpreter_fail (is);
     345           0 :     return;
     346             :   }
     347           8 :   if (GNUNET_OK !=
     348           8 :       TALER_TESTING_get_trait_reserve_pub (create_reserve,
     349             :                                            0,
     350             :                                            &ss->reserve_pubp))
     351             :   {
     352           0 :     GNUNET_break (0);
     353           0 :     TALER_LOG_ERROR ("Failed to find reserve_pub for status query\n");
     354           0 :     TALER_TESTING_interpreter_fail (is);
     355           0 :     return;
     356             :   }
     357           8 :   ss->rsh = TALER_EXCHANGE_reserves_get (is->exchange,
     358             :                                          ss->reserve_pubp,
     359             :                                          GNUNET_TIME_UNIT_ZERO,
     360             :                                          &reserve_status_cb,
     361             :                                          ss);
     362             : }
     363             : 
     364             : 
     365             : /**
     366             :  * Cleanup the state from a "reserve status" CMD, and possibly
     367             :  * cancel a pending operation thereof.
     368             :  *
     369             :  * @param cls closure.
     370             :  * @param cmd the command which is being cleaned up.
     371             :  */
     372             : static void
     373           8 : status_cleanup (void *cls,
     374             :                 const struct TALER_TESTING_Command *cmd)
     375             : {
     376           8 :   struct StatusState *ss = cls;
     377             : 
     378           8 :   if (NULL != ss->rsh)
     379             :   {
     380           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     381             :                 "Command %u (%s) did not complete\n",
     382             :                 ss->is->ip,
     383             :                 cmd->label);
     384           0 :     TALER_EXCHANGE_reserves_get_cancel (ss->rsh);
     385           0 :     ss->rsh = NULL;
     386             :   }
     387           8 :   GNUNET_free (ss);
     388           8 : }
     389             : 
     390             : 
     391             : /**
     392             :  * Create a "reserve status" command.
     393             :  *
     394             :  * @param label the command label.
     395             :  * @param reserve_reference reference to the reserve to check.
     396             :  * @param expected_balance expected balance for the reserve.
     397             :  * @param expected_response_code expected HTTP response code.
     398             :  *
     399             :  * @return the command.
     400             :  */
     401             : struct TALER_TESTING_Command
     402           8 : TALER_TESTING_cmd_status (const char *label,
     403             :                           const char *reserve_reference,
     404             :                           const char *expected_balance,
     405             :                           unsigned int expected_response_code)
     406             : {
     407             :   struct StatusState *ss;
     408             : 
     409           8 :   GNUNET_assert (NULL != reserve_reference);
     410           8 :   ss = GNUNET_new (struct StatusState);
     411           8 :   ss->reserve_reference = reserve_reference;
     412           8 :   ss->expected_balance = expected_balance;
     413           8 :   ss->expected_response_code = expected_response_code;
     414             :   {
     415           8 :     struct TALER_TESTING_Command cmd = {
     416             :       .cls = ss,
     417             :       .label = label,
     418             :       .run = &status_run,
     419             :       .cleanup = &status_cleanup
     420             :     };
     421             : 
     422           8 :     return cmd;
     423             :   }
     424             : }

Generated by: LCOV version 1.14