LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_reserves.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 60 79 75.9 %
Date: 2021-08-30 06:54:17 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 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_api_cmd_get_reserves.c
      21             :  * @brief command to test GET /private/reserves
      22             :  * @author Jonathan Buchanan
      23             :  */
      24             : #include "platform.h"
      25             : #include <taler/taler_exchange_service.h>
      26             : #include <taler/taler_testing_lib.h>
      27             : #include "taler_merchant_service.h"
      28             : #include "taler_merchant_testing_lib.h"
      29             : 
      30             : 
      31             : /**
      32             :  * State of a "GET reserves" CMD
      33             :  */
      34             : struct GetReservesState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET reserves" request.
      39             :    */
      40             :   struct TALER_MERCHANT_ReservesGetHandle *rgh;
      41             : 
      42             :   /**
      43             :    * The interpreter state.
      44             :    */
      45             :   struct TALER_TESTING_Interpreter *is;
      46             : 
      47             :   /**
      48             :    * A list of reserves to compare with.
      49             :    */
      50             :   const char **reserves;
      51             : 
      52             :   /**
      53             :    * Length of @e reserve_refs.
      54             :    */
      55             :   unsigned int reserves_length;
      56             : 
      57             :   /**
      58             :    * Base URL of the merchant serving the request.
      59             :    */
      60             :   const char *merchant_url;
      61             : 
      62             :   /**
      63             :    * Expected HTTP response code.
      64             :    */
      65             :   unsigned int http_status;
      66             : };
      67             : 
      68             : 
      69             : static void
      70           2 : get_reserves_cb (void *cls,
      71             :                  const struct TALER_MERCHANT_HttpResponse *hr,
      72             :                  unsigned int reserves_length,
      73             :                  const struct TALER_MERCHANT_ReserveSummary reserves[])
      74           2 : {
      75             :   /* FIXME, deeper checks should be implemented here. */
      76           2 :   struct GetReservesState *grs = cls;
      77           2 :   bool matched[reserves_length];
      78           2 :   bool fail = false;
      79             : 
      80           2 :   grs->rgh = NULL;
      81           2 :   if (grs->http_status != hr->http_status)
      82             :   {
      83           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      84             :                 "Unexpected response code %u (%d) to command %s\n",
      85             :                 hr->http_status,
      86             :                 (int) hr->ec,
      87             :                 TALER_TESTING_interpreter_get_current_label (grs->is));
      88           0 :     TALER_TESTING_interpreter_fail (grs->is);
      89           0 :     return;
      90             :   }
      91           2 :   switch (hr->http_status)
      92             :   {
      93           2 :   case MHD_HTTP_OK:
      94           2 :     if (reserves_length != grs->reserves_length)
      95             :     {
      96           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      97             :                   "Length of reserves found does not match\n");
      98           0 :       TALER_TESTING_interpreter_fail (grs->is);
      99           0 :       return;
     100             :     }
     101             :     // FIXME: check if the data returned matches that from the POST / PATCH
     102           2 :     memset (matched, 0, sizeof (matched));
     103           5 :     for (unsigned int i = 0; i < reserves_length; ++i)
     104           8 :       for (unsigned int j = 0; j < reserves_length; ++j)
     105             :       {
     106             :         const struct TALER_TESTING_Command *reserve_cmd;
     107           5 :         bool match = true;
     108             : 
     109           5 :         reserve_cmd = TALER_TESTING_interpreter_lookup_command (
     110             :           grs->is,
     111           5 :           grs->reserves[j]);
     112             :         {
     113             :           const struct TALER_ReservePublicKeyP *reserve_pub;
     114             : 
     115           5 :           if (GNUNET_OK !=
     116           5 :               TALER_TESTING_get_trait_reserve_pub (reserve_cmd,
     117             :                                                    0,
     118             :                                                    &reserve_pub))
     119             :           {
     120           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     121             :                         "Could not fetch reserve public key\n");
     122           0 :             TALER_TESTING_interpreter_fail (grs->is);
     123           0 :             return;
     124             :           }
     125           5 :           if (0 != GNUNET_memcmp (&reserves[i].reserve_pub,
     126             :                                   reserve_pub))
     127             :           {
     128           2 :             GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     129             :                         "Reserve public key does not match, got %s\n",
     130             :                         TALER_B2S (&reserves[i].reserve_pub));
     131           2 :             GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     132             :                         "Reserve public key does not match, expected %s\n",
     133             :                         TALER_B2S (reserve_pub));
     134           2 :             match = false;
     135             :           }
     136             :         }
     137             :         {
     138             :           const struct TALER_Amount *initial;
     139             : 
     140           5 :           if (GNUNET_OK !=
     141           5 :               TALER_TESTING_get_trait_amount_obj (reserve_cmd,
     142             :                                                   0,
     143             :                                                   &initial))
     144             :           {
     145           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     146             :                         "Could not fetch reserve initial balance\n");
     147           0 :             TALER_TESTING_interpreter_fail (grs->is);
     148           0 :             return;
     149             :           }
     150           5 :           if ((GNUNET_OK !=
     151           5 :                TALER_amount_cmp_currency (&reserves[i].merchant_initial_amount,
     152           5 :                                           initial)) ||
     153           5 :               (0 != TALER_amount_cmp (&reserves[i].merchant_initial_amount,
     154             :                                       initial)))
     155             :           {
     156           2 :             GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     157             :                         "Reserve initial amount does not match, got %s\n",
     158             :                         TALER_amount2s (&reserves[i].merchant_initial_amount));
     159           2 :             GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     160             :                         "Reserve initial amount does not match, wanted %s\n",
     161             :                         TALER_amount2s (initial));
     162           2 :             match = false;
     163             :           }
     164             :         }
     165           5 :         if (match)
     166           3 :           matched[i] = true;
     167             :       }
     168           5 :     for (unsigned int i = 0; i < reserves_length; ++i)
     169           3 :       if (! matched[i])
     170           0 :         fail = true;
     171           2 :     if (fail)
     172             :     {
     173           0 :       TALER_TESTING_interpreter_fail (grs->is);
     174           0 :       return;
     175             :     }
     176           2 :     break;
     177           0 :   default:
     178           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     179             :                 "Unhandled HTTP status.\n");
     180             :   }
     181           2 :   TALER_TESTING_interpreter_next (grs->is);
     182             : }
     183             : 
     184             : 
     185             : /**
     186             :  * Run the "GET /private/reserves" CMD.
     187             :  *
     188             :  * @param cls closure.
     189             :  * @param cmd command being run now.
     190             :  * @param is interpreter state.
     191             :  */
     192             : static void
     193           2 : get_reserves_run (void *cls,
     194             :                   const struct TALER_TESTING_Command *cmd,
     195             :                   struct TALER_TESTING_Interpreter *is)
     196             : {
     197           2 :   struct GetReservesState *grs = cls;
     198             : 
     199           2 :   grs->is = is;
     200           2 :   grs->rgh = TALER_MERCHANT_reserves_get (is->ctx,
     201             :                                           grs->merchant_url,
     202             :                                           GNUNET_TIME_UNIT_ZERO_ABS,
     203             :                                           TALER_EXCHANGE_YNA_ALL,
     204             :                                           TALER_EXCHANGE_YNA_ALL,
     205             :                                           &get_reserves_cb,
     206             :                                           grs);
     207             : 
     208           2 :   GNUNET_assert (NULL != grs->rgh);
     209           2 : }
     210             : 
     211             : 
     212             : /**
     213             :  * Free the state of a "GET reserves" CMD, and possibly
     214             :  * cancel a pending operation thereof.
     215             :  *
     216             :  * @param cls closure.
     217             :  * @param cmd command being run.
     218             :  */
     219             : static void
     220           2 : get_reserves_cleanup (void *cls,
     221             :                       const struct TALER_TESTING_Command *cmd)
     222             : {
     223           2 :   struct GetReservesState *grs = cls;
     224             : 
     225           2 :   if (NULL != grs->rgh)
     226             :   {
     227           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     228             :                 "GET /private/reserves operation did not complete\n");
     229           0 :     TALER_MERCHANT_reserves_get_cancel (grs->rgh);
     230             :   }
     231           2 :   GNUNET_array_grow (grs->reserves,
     232             :                      grs->reserves_length,
     233             :                      0);
     234           2 :   GNUNET_free (grs);
     235           2 : }
     236             : 
     237             : 
     238             : struct TALER_TESTING_Command
     239           2 : TALER_TESTING_cmd_merchant_get_reserves (const char *label,
     240             :                                          const char *merchant_url,
     241             :                                          unsigned int http_status,
     242             :                                          ...)
     243             : {
     244             :   struct GetReservesState *grs;
     245             : 
     246           2 :   grs = GNUNET_new (struct GetReservesState);
     247           2 :   grs->merchant_url = merchant_url;
     248           2 :   grs->http_status = http_status;
     249             :   {
     250             :     const char *clabel;
     251             :     va_list ap;
     252             : 
     253           2 :     va_start (ap, http_status);
     254           5 :     while (NULL != (clabel = va_arg (ap, const char *)))
     255             :     {
     256           3 :       GNUNET_array_append (grs->reserves,
     257             :                            grs->reserves_length,
     258             :                            clabel);
     259             :     }
     260           2 :     va_end (ap);
     261             :   }
     262             :   {
     263           2 :     struct TALER_TESTING_Command cmd = {
     264             :       .cls = grs,
     265             :       .label = label,
     266             :       .run = &get_reserves_run,
     267             :       .cleanup = &get_reserves_cleanup
     268             :     };
     269             : 
     270           2 :     return cmd;
     271             :   }
     272             : }
     273             : 
     274             : 
     275             : /* end of testing_api_cmd_get_reserves.c */

Generated by: LCOV version 1.14