LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_post_reserves.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 60 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 7 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_post_reserves.c
      21             :  * @brief command to test POST /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             :  * State of a "POST /reserves" CMD.
      32             :  */
      33             : struct PostReservesState
      34             : {
      35             :   /**
      36             :    * Handle for a "POST /reserves" request.
      37             :    */
      38             :   struct TALER_MERCHANT_PostReservesHandle *prh;
      39             : 
      40             :   /**
      41             :    * The interpreter state.
      42             :    */
      43             :   struct TALER_TESTING_Interpreter *is;
      44             : 
      45             :   /**
      46             :    * Base URL of the merchant
      47             :    */
      48             :   const char *merchant_url;
      49             : 
      50             :   /**
      51             :    * Base URL of the exchange.
      52             :    */
      53             :   const char *exchange_url;
      54             : 
      55             :   /**
      56             :    * Wire method for the reserve.
      57             :    */
      58             :   const char *wire_method;
      59             : 
      60             :   /**
      61             :    * The initial balance of the reserve.
      62             :    */
      63             :   struct TALER_Amount initial_balance;
      64             : 
      65             :   /**
      66             :    * Expected HTTP response code.
      67             :    */
      68             :   unsigned int http_status;
      69             : 
      70             :   /**
      71             :    * Public key assigned to the reserve
      72             :    */
      73             :   struct TALER_ReservePublicKeyP reserve_pub;
      74             : };
      75             : 
      76             : 
      77             : /**
      78             :  * Callbacks of this type are used to work the result of submitting a
      79             :  * POST /reserves request to a merchant
      80             :  *
      81             :  * @param cls closure
      82             :  * @param hr HTTP response details
      83             :  * @param reserve_pub public key of the created reserve, NULL on error
      84             :  * @param payto_uri where to make the payment to for filling the reserve, NULL on error
      85             :  */
      86             : static void
      87           0 : post_reserves_cb (void *cls,
      88             :                   const struct TALER_MERCHANT_HttpResponse *hr,
      89             :                   const struct TALER_ReservePublicKeyP *reserve_pub,
      90             :                   const char *payto_uri)
      91             : {
      92           0 :   struct PostReservesState *prs = cls;
      93             : 
      94           0 :   prs->prh = NULL;
      95           0 :   if (prs->http_status != hr->http_status)
      96             :   {
      97           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      98             :                 "Unexpected response code %u (%d) to command %s\n",
      99             :                 hr->http_status,
     100             :                 (int) hr->ec,
     101             :                 TALER_TESTING_interpreter_get_current_label (prs->is));
     102           0 :     TALER_TESTING_interpreter_fail (prs->is);
     103           0 :     return;
     104             :   }
     105           0 :   switch (hr->http_status)
     106             :   {
     107           0 :   case MHD_HTTP_NO_CONTENT:
     108           0 :     break;
     109           0 :   case MHD_HTTP_CONFLICT:
     110           0 :     break;
     111           0 :   case MHD_HTTP_OK:
     112           0 :     break;
     113             :   // FIXME: add other legitimate states here...
     114           0 :   default:
     115           0 :     GNUNET_break (0);
     116           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     117             :                 "Unhandled HTTP status (%d).\n",
     118             :                 hr->http_status);
     119             :   }
     120           0 :   prs->reserve_pub = *reserve_pub;
     121           0 :   TALER_TESTING_interpreter_next (prs->is);
     122             : }
     123             : 
     124             : 
     125             : /**
     126             :  * Offers information from the POST /reserves CMD state to other
     127             :  * commands.
     128             :  *
     129             :  * @param cls closure
     130             :  * @param ret[out] result (could be anything)
     131             :  * @param trait name of the trait
     132             :  * @param index index number of the object to extract.
     133             :  * @return #GNUNET_OK on success
     134             :  */
     135             : static int
     136           0 : post_reserves_traits (void *cls,
     137             :                       const void **ret,
     138             :                       const char *trait,
     139             :                       unsigned int index)
     140             : {
     141           0 :   struct PostReservesState *prs = cls;
     142             :   struct TALER_TESTING_Trait traits[] = {
     143           0 :     TALER_TESTING_make_trait_reserve_pub (0, &prs->reserve_pub),
     144           0 :     TALER_TESTING_make_trait_amount_obj (0, &prs->initial_balance),
     145           0 :     TALER_TESTING_trait_end (),
     146             :   };
     147             : 
     148           0 :   return TALER_TESTING_get_trait (traits,
     149             :                                   ret,
     150             :                                   trait,
     151             :                                   index);
     152             : }
     153             : 
     154             : 
     155             : /**
     156             :  * Run the "POST /reserves" CMD.
     157             :  *
     158             :  * @param cls closure.
     159             :  * @param cmd command being run now.
     160             :  * @param is interpreter state.
     161             :  */
     162             : static void
     163           0 : post_reserves_run (void *cls,
     164             :                    const struct TALER_TESTING_Command *cmd,
     165             :                    struct TALER_TESTING_Interpreter *is)
     166             : {
     167           0 :   struct PostReservesState *prs = cls;
     168             : 
     169           0 :   prs->is = is;
     170           0 :   prs->prh = TALER_MERCHANT_reserves_post (is->ctx,
     171             :                                            prs->merchant_url,
     172           0 :                                            &prs->initial_balance,
     173             :                                            prs->exchange_url,
     174             :                                            prs->wire_method,
     175             :                                            &post_reserves_cb,
     176             :                                            prs);
     177           0 :   GNUNET_assert (NULL != prs->prh);
     178           0 : }
     179             : 
     180             : 
     181             : /**
     182             :  * Run the fake "POST /reserves" CMD.
     183             :  *
     184             :  * @param cls closure.
     185             :  * @param cmd command being run now.
     186             :  * @param is interpreter state.
     187             :  */
     188             : static void
     189           0 : post_reserves_fake_run (void *cls,
     190             :                         const struct TALER_TESTING_Command *cmd,
     191             :                         struct TALER_TESTING_Interpreter *is)
     192             : {
     193           0 :   struct PostReservesState *prs = cls;
     194             :   struct TALER_ReservePrivateKeyP reserve_priv;
     195             : 
     196           0 :   prs->is = is;
     197           0 :   GNUNET_CRYPTO_eddsa_key_create (&reserve_priv.eddsa_priv);
     198           0 :   GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv.eddsa_priv,
     199             :                                       &prs->reserve_pub.eddsa_pub);
     200             : 
     201           0 :   GNUNET_assert (GNUNET_OK == TALER_string_to_amount ("EUR:100.00",
     202             :                                                       &prs->initial_balance));
     203           0 :   TALER_TESTING_interpreter_next (prs->is);
     204           0 : }
     205             : 
     206             : 
     207             : /**
     208             :  * Free the state of a "POST /reserves" CMD, and possibly
     209             :  * cancel a pending operation thereof.
     210             :  *
     211             :  * @param cls closure.
     212             :  * @param cmd command being run.
     213             :  */
     214             : static void
     215           0 : post_reserves_cleanup (void *cls,
     216             :                        const struct TALER_TESTING_Command *cmd)
     217             : {
     218           0 :   struct PostReservesState *prs = cls;
     219             : 
     220           0 :   if (NULL != prs->prh)
     221             :   {
     222           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     223             :                 "POST /reserves operation did not complete\n");
     224           0 :     TALER_MERCHANT_reserves_post_cancel (prs->prh);
     225             :   }
     226           0 :   GNUNET_free (prs);
     227           0 : }
     228             : 
     229             : 
     230             : /**
     231             :  * Define a "POST /reserves" CMD
     232             :  *
     233             :  * @param label command label.
     234             :  * @param merchant_url url to the merchant.
     235             :  * @param initial_balance initial amount in the reserve.
     236             :  * @param exchange_url url to the exchange
     237             :  * @param wire_method wire transfer method to use for this reserve
     238             :  * @param http_status expected HTTP response code.
     239             :  * @return the command.
     240             :  */
     241             : struct TALER_TESTING_Command
     242           0 : TALER_TESTING_cmd_merchant_post_reserves (const char *label,
     243             :                                           const char *merchant_url,
     244             :                                           const char *initial_balance,
     245             :                                           const char *exchange_url,
     246             :                                           const char *wire_method,
     247             :                                           unsigned int http_status)
     248             : {
     249             :   struct PostReservesState *prs;
     250             : 
     251           0 :   prs = GNUNET_new (struct PostReservesState);
     252           0 :   prs->merchant_url = merchant_url;
     253           0 :   prs->exchange_url = exchange_url;
     254           0 :   prs->wire_method = wire_method;
     255           0 :   prs->http_status = http_status;
     256           0 :   GNUNET_assert (GNUNET_OK ==
     257             :                  TALER_string_to_amount (initial_balance,
     258             :                                          &prs->initial_balance));
     259             :   {
     260           0 :     struct TALER_TESTING_Command cmd = {
     261             :       .cls = prs,
     262             :       .label = label,
     263             :       .run = &post_reserves_run,
     264             :       .cleanup = &post_reserves_cleanup,
     265             :       .traits = &post_reserves_traits
     266             :     };
     267             : 
     268           0 :     return cmd;
     269             :   }
     270             : }
     271             : 
     272             : 
     273             : /**
     274             :  * This commands does not query the backend at all,
     275             :  * but just makes up a fake reserve.
     276             :  *
     277             :  * @param label command label.
     278             :  * @return the command.
     279             :  */
     280             : struct TALER_TESTING_Command
     281           0 : TALER_TESTING_cmd_merchant_post_reserves_fake (const char *label)
     282             : {
     283             :   struct PostReservesState *prs;
     284             : 
     285           0 :   prs = GNUNET_new (struct PostReservesState);
     286             :   {
     287           0 :     struct TALER_TESTING_Command cmd = {
     288             :       .cls = prs,
     289             :       .label = label,
     290             :       .run = &post_reserves_fake_run,
     291             :       .cleanup = &post_reserves_cleanup,
     292             :       .traits = &post_reserves_traits
     293             :     };
     294             : 
     295           0 :     return cmd;
     296             :   }
     297             : }

Generated by: LCOV version 1.14