LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_reserves.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 65 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 4 0.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 lib/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           0 : 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             : {
      75             :   /* FIXME, deeper checks should be implemented here. */
      76           0 :   struct GetReservesState *grs = cls;
      77             : 
      78           0 :   grs->rgh = NULL;
      79           0 :   if (grs->http_status != hr->http_status)
      80             :   {
      81           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      82             :                 "Unexpected response code %u (%d) to command %s\n",
      83             :                 hr->http_status,
      84             :                 (int) hr->ec,
      85             :                 TALER_TESTING_interpreter_get_current_label (grs->is));
      86           0 :     TALER_TESTING_interpreter_fail (grs->is);
      87           0 :     return;
      88             :   }
      89           0 :   switch (hr->http_status)
      90             :   {
      91           0 :   case MHD_HTTP_OK:
      92           0 :     if (reserves_length != grs->reserves_length)
      93             :     {
      94           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      95             :                   "Length of reserves found does not match\n");
      96           0 :       TALER_TESTING_interpreter_fail (grs->is);
      97           0 :       return;
      98             :     }
      99             :     // FIXME: check if the data returned matches that from the POST / PATCH
     100           0 :     for (unsigned int i = 0; i < reserves_length; ++i)
     101             :     {
     102             :       const struct TALER_TESTING_Command *reserve_cmd;
     103             : 
     104           0 :       reserve_cmd = TALER_TESTING_interpreter_lookup_command (
     105             :         grs->is,
     106           0 :         grs->reserves[i]);
     107             :       {
     108             :         const struct TALER_ReservePublicKeyP *reserve_pub;
     109             : 
     110           0 :         if (GNUNET_OK !=
     111           0 :             TALER_TESTING_get_trait_reserve_pub (reserve_cmd,
     112             :                                                  0,
     113             :                                                  &reserve_pub))
     114             :         {
     115           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     116             :                       "Could not fetch reserve public key\n");
     117           0 :           TALER_TESTING_interpreter_fail (grs->is);
     118           0 :           return;
     119             :         }
     120           0 :         if (0 != GNUNET_memcmp (&reserves[i].reserve_pub,
     121             :                                 reserve_pub))
     122             :         {
     123           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     124             :                       "Reserve public key does not match\n");
     125           0 :           TALER_TESTING_interpreter_fail (grs->is);
     126           0 :           return;
     127             :         }
     128             :       }
     129             :       {
     130             :         const struct TALER_Amount *initial;
     131             : 
     132           0 :         if (GNUNET_OK !=
     133           0 :             TALER_TESTING_get_trait_amount_obj (reserve_cmd,
     134             :                                                 0,
     135             :                                                 &initial))
     136             :         {
     137           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     138             :                       "Could not fetch reserve initial balance\n");
     139           0 :           TALER_TESTING_interpreter_fail (grs->is);
     140           0 :           return;
     141             :         }
     142           0 :         if ((GNUNET_OK != TALER_amount_cmp_currency (
     143           0 :                &reserves[i].merchant_initial_amount,
     144           0 :                initial)) ||
     145           0 :             (0 != TALER_amount_cmp (&reserves[i].merchant_initial_amount,
     146             :                                     initial)))
     147             :         {
     148           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     149             :                       "Reserve initial amount does not match\n");
     150           0 :           TALER_TESTING_interpreter_fail (grs->is);
     151           0 :           return;
     152             :         }
     153             :       }
     154             :     }
     155           0 :     break;
     156           0 :   default:
     157           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     158             :                 "Unhandled HTTP status.\n");
     159             :   }
     160           0 :   TALER_TESTING_interpreter_next (grs->is);
     161             : }
     162             : 
     163             : 
     164             : /**
     165             :  * Run the "GET /private/reserves" CMD.
     166             :  *
     167             :  * @param cls closure.
     168             :  * @param cmd command being run now.
     169             :  * @param is interpreter state.
     170             :  */
     171             : static void
     172           0 : get_reserves_run (void *cls,
     173             :                   const struct TALER_TESTING_Command *cmd,
     174             :                   struct TALER_TESTING_Interpreter *is)
     175             : {
     176           0 :   struct GetReservesState *grs = cls;
     177             : 
     178           0 :   grs->is = is;
     179           0 :   grs->rgh = TALER_MERCHANT_reserves_get (is->ctx,
     180             :                                           grs->merchant_url,
     181             :                                           GNUNET_TIME_UNIT_ZERO_ABS,
     182             :                                           TALER_EXCHANGE_YNA_ALL,
     183             :                                           TALER_EXCHANGE_YNA_ALL,
     184             :                                           &get_reserves_cb,
     185             :                                           grs);
     186             : 
     187           0 :   GNUNET_assert (NULL != grs->rgh);
     188           0 : }
     189             : 
     190             : 
     191             : /**
     192             :  * Free the state of a "GET reserves" CMD, and possibly
     193             :  * cancel a pending operation thereof.
     194             :  *
     195             :  * @param cls closure.
     196             :  * @param cmd command being run.
     197             :  */
     198             : static void
     199           0 : get_reserves_cleanup (void *cls,
     200             :                       const struct TALER_TESTING_Command *cmd)
     201             : {
     202           0 :   struct GetReservesState *grs = cls;
     203             : 
     204           0 :   if (NULL != grs->rgh)
     205             :   {
     206           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     207             :                 "GET /private/reserves operation did not complete\n");
     208           0 :     TALER_MERCHANT_reserves_get_cancel (grs->rgh);
     209             :   }
     210           0 :   GNUNET_array_grow (grs->reserves,
     211             :                      grs->reserves_length,
     212             :                      0);
     213           0 :   GNUNET_free (grs);
     214           0 : }
     215             : 
     216             : 
     217             : /**
     218             :  * Define a "GET /reserves" CMD
     219             :  *
     220             :  * @param label command label.
     221             :  * @param merchant_url url to the merchant.
     222             :  * @param http_status expected HTTP response code.
     223             :  * @param ... NULL-terminated list of labels (const char *) of
     224             :  *        reserve (commands) we expect to be returned in the list
     225             :  *        (assuming @a http_code is #MHD_HTTP_OK)
     226             :  */
     227             : struct TALER_TESTING_Command
     228           0 : TALER_TESTING_cmd_merchant_get_reserves (const char *label,
     229             :                                          const char *merchant_url,
     230             :                                          unsigned int http_status,
     231             :                                          ...)
     232             : {
     233             :   struct GetReservesState *grs;
     234             : 
     235           0 :   grs = GNUNET_new (struct GetReservesState);
     236           0 :   grs->merchant_url = merchant_url;
     237           0 :   grs->http_status = http_status;
     238             :   {
     239             :     const char *clabel;
     240             :     va_list ap;
     241             : 
     242           0 :     va_start (ap, http_status);
     243           0 :     while (NULL != (clabel = va_arg (ap, const char *)))
     244             :     {
     245           0 :       GNUNET_array_append (grs->reserves,
     246             :                            grs->reserves_length,
     247             :                            clabel);
     248             :     }
     249           0 :     va_end (ap);
     250             :   }
     251             :   {
     252           0 :     struct TALER_TESTING_Command cmd = {
     253             :       .cls = grs,
     254             :       .label = label,
     255             :       .run = &get_reserves_run,
     256             :       .cleanup = &get_reserves_cleanup
     257             :     };
     258             : 
     259           0 :     return cmd;
     260             :   }
     261             : }
     262             : 
     263             : 
     264             : /* end of testing_api_cmd_get_reserves.c */

Generated by: LCOV version 1.14