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

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2022 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_reserve_get.c
      21             :  * @brief Implement the GET /reserve/$RID 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 "poll" CMD.
      32             :  */
      33             : struct PollState
      34             : {
      35             : 
      36             :   /**
      37             :    * How long do we give the exchange to respond?
      38             :    */
      39             :   struct GNUNET_TIME_Relative timeout;
      40             : 
      41             :   /**
      42             :    * Label to the command which created the reserve to check,
      43             :    * needed to resort the reserve key.
      44             :    */
      45             :   const char *poll_reference;
      46             : 
      47             :   /**
      48             :    * Timeout to wait for at most.
      49             :    */
      50             :   struct GNUNET_SCHEDULER_Task *tt;
      51             : 
      52             :   /**
      53             :    * The interpreter we are using.
      54             :    */
      55             :   struct TALER_TESTING_Interpreter *is;
      56             : };
      57             : 
      58             : 
      59             : /**
      60             :  * State for a "status" CMD.
      61             :  */
      62             : struct StatusState
      63             : {
      64             : 
      65             :   /**
      66             :    * How long do we give the exchange to respond?
      67             :    */
      68             :   struct GNUNET_TIME_Relative timeout;
      69             : 
      70             :   /**
      71             :    * Poller waiting for us.
      72             :    */
      73             :   struct PollState *ps;
      74             : 
      75             :   /**
      76             :    * Label to the command which created the reserve to check,
      77             :    * needed to resort the reserve key.
      78             :    */
      79             :   const char *reserve_reference;
      80             : 
      81             :   /**
      82             :    * Handle to the "reserve status" operation.
      83             :    */
      84             :   struct TALER_EXCHANGE_ReservesGetHandle *rsh;
      85             : 
      86             :   /**
      87             :    * Expected reserve balance.
      88             :    */
      89             :   const char *expected_balance;
      90             : 
      91             :   /**
      92             :    * Public key of the reserve being analyzed.
      93             :    */
      94             :   const struct TALER_ReservePublicKeyP *reserve_pubp;
      95             : 
      96             :   /**
      97             :    * Expected HTTP response code.
      98             :    */
      99             :   unsigned int expected_response_code;
     100             : 
     101             :   /**
     102             :    * Interpreter state.
     103             :    */
     104             :   struct TALER_TESTING_Interpreter *is;
     105             : 
     106             : };
     107             : 
     108             : 
     109             : /**
     110             :  * Check that the reserve balance and HTTP response code are
     111             :  * both acceptable.
     112             :  *
     113             :  * @param cls closure.
     114             :  * @param rs HTTP response details
     115             :  */
     116             : static void
     117           0 : reserve_status_cb (void *cls,
     118             :                    const struct TALER_EXCHANGE_ReserveSummary *rs)
     119             : {
     120           0 :   struct StatusState *ss = cls;
     121           0 :   struct TALER_TESTING_Interpreter *is = ss->is;
     122             :   struct TALER_Amount eb;
     123             : 
     124           0 :   ss->rsh = NULL;
     125           0 :   if (ss->expected_response_code != rs->hr.http_status)
     126             :   {
     127           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     128             :                 "Unexpected HTTP response code: %d in %s:%u\n",
     129             :                 rs->hr.http_status,
     130             :                 __FILE__,
     131             :                 __LINE__);
     132           0 :     json_dumpf (rs->hr.reply,
     133             :                 stderr,
     134             :                 0);
     135           0 :     TALER_TESTING_interpreter_fail (ss->is);
     136           0 :     return;
     137             :   }
     138           0 :   if (MHD_HTTP_OK == ss->expected_response_code)
     139             :   {
     140           0 :     GNUNET_assert (GNUNET_OK ==
     141             :                    TALER_string_to_amount (ss->expected_balance,
     142             :                                            &eb));
     143           0 :     if (0 != TALER_amount_cmp (&eb,
     144             :                                &rs->details.ok.balance))
     145             :     {
     146           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     147             :                   "Unexpected amount %s in reserve, wanted %s\n",
     148             :                   TALER_amount_to_string (&rs->details.ok.balance),
     149             :                   ss->expected_balance);
     150           0 :       TALER_TESTING_interpreter_fail (ss->is);
     151           0 :       return;
     152             :     }
     153             :   }
     154           0 :   if (NULL != ss->ps)
     155             :   {
     156             :     /* force continuation on long poller */
     157           0 :     GNUNET_SCHEDULER_cancel (ss->ps->tt);
     158           0 :     ss->ps->tt = NULL;
     159           0 :     TALER_TESTING_interpreter_next (is);
     160           0 :     return;
     161             :   }
     162           0 :   if (GNUNET_TIME_relative_is_zero (ss->timeout))
     163           0 :     TALER_TESTING_interpreter_next (is);
     164             : }
     165             : 
     166             : 
     167             : /**
     168             :  * Run the command.
     169             :  *
     170             :  * @param cls closure.
     171             :  * @param cmd the command being executed.
     172             :  * @param is the interpreter state.
     173             :  */
     174             : static void
     175           0 : status_run (void *cls,
     176             :             const struct TALER_TESTING_Command *cmd,
     177             :             struct TALER_TESTING_Interpreter *is)
     178             : {
     179           0 :   struct StatusState *ss = cls;
     180             :   const struct TALER_TESTING_Command *create_reserve;
     181             : 
     182           0 :   ss->is = is;
     183             :   create_reserve
     184           0 :     = TALER_TESTING_interpreter_lookup_command (is,
     185             :                                                 ss->reserve_reference);
     186             : 
     187           0 :   if (NULL == create_reserve)
     188             :   {
     189           0 :     GNUNET_break (0);
     190           0 :     TALER_TESTING_interpreter_fail (is);
     191           0 :     return;
     192             :   }
     193           0 :   if (GNUNET_OK !=
     194           0 :       TALER_TESTING_get_trait_reserve_pub (create_reserve,
     195             :                                            &ss->reserve_pubp))
     196             :   {
     197           0 :     GNUNET_break (0);
     198           0 :     TALER_LOG_ERROR ("Failed to find reserve_pub for status query\n");
     199           0 :     TALER_TESTING_interpreter_fail (is);
     200           0 :     return;
     201             :   }
     202           0 :   ss->rsh = TALER_EXCHANGE_reserves_get (is->exchange,
     203             :                                          ss->reserve_pubp,
     204             :                                          ss->timeout,
     205             :                                          &reserve_status_cb,
     206             :                                          ss);
     207           0 :   if (! GNUNET_TIME_relative_is_zero (ss->timeout))
     208             :   {
     209           0 :     TALER_TESTING_interpreter_next (is);
     210           0 :     return;
     211             :   }
     212             : }
     213             : 
     214             : 
     215             : /**
     216             :  * Cleanup the state from a "reserve status" CMD, and possibly
     217             :  * cancel a pending operation thereof.
     218             :  *
     219             :  * @param cls closure.
     220             :  * @param cmd the command which is being cleaned up.
     221             :  */
     222             : static void
     223           0 : status_cleanup (void *cls,
     224             :                 const struct TALER_TESTING_Command *cmd)
     225             : {
     226           0 :   struct StatusState *ss = cls;
     227             : 
     228           0 :   if (NULL != ss->rsh)
     229             :   {
     230           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     231             :                 "Command %u (%s) did not complete\n",
     232             :                 ss->is->ip,
     233             :                 cmd->label);
     234           0 :     TALER_EXCHANGE_reserves_get_cancel (ss->rsh);
     235           0 :     ss->rsh = NULL;
     236             :   }
     237           0 :   GNUNET_free (ss);
     238           0 : }
     239             : 
     240             : 
     241             : struct TALER_TESTING_Command
     242           0 : TALER_TESTING_cmd_status (const char *label,
     243             :                           const char *reserve_reference,
     244             :                           const char *expected_balance,
     245             :                           unsigned int expected_response_code)
     246             : {
     247             :   struct StatusState *ss;
     248             : 
     249           0 :   GNUNET_assert (NULL != reserve_reference);
     250           0 :   ss = GNUNET_new (struct StatusState);
     251           0 :   ss->reserve_reference = reserve_reference;
     252           0 :   ss->expected_balance = expected_balance;
     253           0 :   ss->expected_response_code = expected_response_code;
     254             :   {
     255           0 :     struct TALER_TESTING_Command cmd = {
     256             :       .cls = ss,
     257             :       .label = label,
     258             :       .run = &status_run,
     259             :       .cleanup = &status_cleanup
     260             :     };
     261             : 
     262           0 :     return cmd;
     263             :   }
     264             : }
     265             : 
     266             : 
     267             : struct TALER_TESTING_Command
     268           0 : TALER_TESTING_cmd_reserve_poll (const char *label,
     269             :                                 const char *reserve_reference,
     270             :                                 const char *expected_balance,
     271             :                                 struct GNUNET_TIME_Relative timeout,
     272             :                                 unsigned int expected_response_code)
     273             : {
     274             :   struct StatusState *ss;
     275             : 
     276           0 :   GNUNET_assert (NULL != reserve_reference);
     277           0 :   ss = GNUNET_new (struct StatusState);
     278           0 :   ss->reserve_reference = reserve_reference;
     279           0 :   ss->expected_balance = expected_balance;
     280           0 :   ss->expected_response_code = expected_response_code;
     281           0 :   ss->timeout = timeout;
     282             :   {
     283           0 :     struct TALER_TESTING_Command cmd = {
     284             :       .cls = ss,
     285             :       .label = label,
     286             :       .run = &status_run,
     287             :       .cleanup = &status_cleanup
     288             :     };
     289             : 
     290           0 :     return cmd;
     291             :   }
     292             : }
     293             : 
     294             : 
     295             : /**
     296             :  * Long poller timed out. Fail the test.
     297             :  *
     298             :  * @param cls a `struct PollState`
     299             :  */
     300             : static void
     301           0 : finish_timeout (void *cls)
     302             : {
     303           0 :   struct PollState *ps = cls;
     304             : 
     305           0 :   ps->tt = NULL;
     306           0 :   GNUNET_break (0);
     307           0 :   TALER_TESTING_interpreter_fail (ps->is);
     308           0 : }
     309             : 
     310             : 
     311             : /**
     312             :  * Run the command.
     313             :  *
     314             :  * @param cls closure.
     315             :  * @param cmd the command being executed.
     316             :  * @param is the interpreter state.
     317             :  */
     318             : static void
     319           0 : finish_run (void *cls,
     320             :             const struct TALER_TESTING_Command *cmd,
     321             :             struct TALER_TESTING_Interpreter *is)
     322             : {
     323           0 :   struct PollState *ps = cls;
     324             :   const struct TALER_TESTING_Command *poll_reserve;
     325             :   struct StatusState *ss;
     326             : 
     327           0 :   ps->is = is;
     328             :   poll_reserve
     329           0 :     = TALER_TESTING_interpreter_lookup_command (is,
     330             :                                                 ps->poll_reference);
     331           0 :   GNUNET_assert (poll_reserve->run == &status_run);
     332           0 :   ss = poll_reserve->cls;
     333           0 :   if (NULL == ss->rsh)
     334             :   {
     335           0 :     TALER_TESTING_interpreter_next (is);
     336           0 :     return;
     337             :   }
     338           0 :   GNUNET_assert (NULL == ss->ps);
     339           0 :   ss->ps = ps;
     340           0 :   ps->tt = GNUNET_SCHEDULER_add_delayed (ps->timeout,
     341             :                                          &finish_timeout,
     342             :                                          ps);
     343             : }
     344             : 
     345             : 
     346             : /**
     347             :  * Cleanup the state from a "reserve finish" CMD.
     348             :  *
     349             :  * @param cls closure.
     350             :  * @param cmd the command which is being cleaned up.
     351             :  */
     352             : static void
     353           0 : finish_cleanup (void *cls,
     354             :                 const struct TALER_TESTING_Command *cmd)
     355             : {
     356           0 :   struct PollState *ps = cls;
     357             : 
     358           0 :   if (NULL != ps->tt)
     359             :   {
     360           0 :     GNUNET_SCHEDULER_cancel (ps->tt);
     361           0 :     ps->tt = NULL;
     362             :   }
     363           0 :   GNUNET_free (ps);
     364           0 : }
     365             : 
     366             : 
     367             : struct TALER_TESTING_Command
     368           0 : TALER_TESTING_cmd_reserve_poll_finish (const char *label,
     369             :                                        struct GNUNET_TIME_Relative timeout,
     370             :                                        const char *poll_reference)
     371             : {
     372             :   struct PollState *ps;
     373             : 
     374           0 :   GNUNET_assert (NULL != poll_reference);
     375           0 :   ps = GNUNET_new (struct PollState);
     376           0 :   ps->timeout = timeout;
     377           0 :   ps->poll_reference = poll_reference;
     378             :   {
     379           0 :     struct TALER_TESTING_Command cmd = {
     380             :       .cls = ps,
     381             :       .label = label,
     382             :       .run = &finish_run,
     383             :       .cleanup = &finish_cleanup
     384             :     };
     385             : 
     386           0 :     return cmd;
     387             :   }
     388             : }

Generated by: LCOV version 1.14