LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_instance.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 64.9 % 94 61
Test Date: 2025-10-28 22:11:54 Functions: 100.0 % 5 5

            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_instance.c
      21              :  * @brief command to test GET /instance/$ID
      22              :  * @author Christian Grothoff
      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 instance" CMD.
      33              :  */
      34              : struct GetInstanceState
      35              : {
      36              : 
      37              :   /**
      38              :    * Handle for a "GET instance" request.
      39              :    */
      40              :   struct TALER_MERCHANT_InstanceGetHandle *igh;
      41              : 
      42              :   /**
      43              :    * The interpreter state.
      44              :    */
      45              :   struct TALER_TESTING_Interpreter *is;
      46              : 
      47              :   /**
      48              :    * Public key of the merchant instance.
      49              :    */
      50              :   union TALER_AccountPublicKeyP account_pub;
      51              : 
      52              :   /**
      53              :    * Base URL of the merchant serving the request.
      54              :    */
      55              :   const char *merchant_url;
      56              : 
      57              :   /**
      58              :    * ID of the instance to run GET for.
      59              :    */
      60              :   const char *instance_id;
      61              : 
      62              :   /**
      63              :    * Reference for a POST or PATCH /instances CMD (optional).
      64              :    */
      65              :   const char *instance_reference;
      66              : 
      67              :   /**
      68              :    * Expected HTTP response code.
      69              :    */
      70              :   unsigned int http_status;
      71              : 
      72              : };
      73              : 
      74              : 
      75              : /**
      76              :  * Callback for a /get/instance/$ID operation.
      77              :  *
      78              :  * @param cls closure for this function
      79              :  * @param igr response
      80              :  */
      81              : static void
      82            9 : get_instance_cb (void *cls,
      83              :                  const struct TALER_MERCHANT_InstanceGetResponse *igr)
      84              : {
      85            9 :   struct GetInstanceState *gis = cls;
      86              :   const struct TALER_TESTING_Command *instance_cmd;
      87              : 
      88            9 :   instance_cmd = TALER_TESTING_interpreter_lookup_command (
      89              :     gis->is,
      90              :     gis->instance_reference);
      91              : 
      92            9 :   gis->igh = NULL;
      93            9 :   if (gis->http_status != igr->hr.http_status)
      94              :   {
      95            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      96              :                 "Unexpected response code %u (%d) to command %s\n",
      97              :                 igr->hr.http_status,
      98              :                 (int) igr->hr.ec,
      99              :                 TALER_TESTING_interpreter_get_current_label (gis->is));
     100            0 :     TALER_TESTING_interpreter_fail (gis->is);
     101            0 :     return;
     102              :   }
     103            9 :   switch (igr->hr.http_status)
     104              :   {
     105            5 :   case MHD_HTTP_OK:
     106              :     {
     107            5 :       const struct TALER_MERCHANT_InstanceDetails *details =
     108              :         &igr->details.ok.details;
     109              : 
     110              :       gis->account_pub.merchant_pub
     111            5 :         = details->merchant_pub;
     112              :       {
     113              :         const char *name;
     114              : 
     115            5 :         if (GNUNET_OK !=
     116            5 :             TALER_TESTING_get_trait_instance_name (instance_cmd,
     117              :                                                    &name))
     118            0 :           TALER_TESTING_interpreter_fail (gis->is);
     119            5 :         if (0 != strcmp (details->name,
     120              :                          name))
     121              :         {
     122            0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     123              :                       "Instance name does not match: Got `%s', wanted `%s'\n",
     124              :                       details->name,
     125              :                       name);
     126            0 :           TALER_TESTING_interpreter_fail (gis->is);
     127            0 :           return;
     128              :         }
     129              :       }
     130              :       {
     131              :         const json_t *address;
     132              : 
     133            5 :         if (GNUNET_OK !=
     134            5 :             TALER_TESTING_get_trait_address (instance_cmd,
     135              :                                              &address))
     136            0 :           TALER_TESTING_interpreter_fail (gis->is);
     137            5 :         if (1 != json_equal (details->address,
     138              :                              address))
     139              :         {
     140            0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     141              :                       "Instance address does not match\n");
     142            0 :           TALER_TESTING_interpreter_fail (gis->is);
     143            0 :           return;
     144              :         }
     145              :       }
     146              :       {
     147              :         const json_t *jurisdiction;
     148              : 
     149            5 :         if (GNUNET_OK !=
     150            5 :             TALER_TESTING_get_trait_jurisdiction (instance_cmd,
     151              :                                                   &jurisdiction))
     152            0 :           TALER_TESTING_interpreter_fail (gis->is);
     153            5 :         if (1 != json_equal (details->jurisdiction,
     154              :                              jurisdiction))
     155              :         {
     156            0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     157              :                       "Instance jurisdiction does not match\n");
     158            0 :           TALER_TESTING_interpreter_fail (gis->is);
     159            0 :           return;
     160              :         }
     161              :       }
     162              :       {
     163              :         const bool *use_stefan;
     164              : 
     165            5 :         if (GNUNET_OK !=
     166            5 :             TALER_TESTING_get_trait_use_stefan (instance_cmd,
     167              :                                                 &use_stefan))
     168            0 :           TALER_TESTING_interpreter_fail (gis->is);
     169            5 :         if (*use_stefan != details->use_stefan)
     170              :         {
     171            0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     172              :                       "Instance use_stefan value does not match\n");
     173            0 :           TALER_TESTING_interpreter_fail (gis->is);
     174            0 :           return;
     175              :         }
     176              :       }
     177              :       {
     178              :         const struct GNUNET_TIME_Relative *default_wire_transfer_delay;
     179              : 
     180            5 :         if (GNUNET_OK !=
     181            5 :             TALER_TESTING_get_trait_wire_delay (instance_cmd,
     182              :                                                 &default_wire_transfer_delay))
     183            0 :           TALER_TESTING_interpreter_fail (gis->is);
     184            5 :         if (details->default_wire_transfer_delay.rel_value_us !=
     185            5 :             default_wire_transfer_delay->rel_value_us)
     186              :         {
     187            0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     188              :                       "Instance default wire transfer delay does not match\n");
     189            0 :           TALER_TESTING_interpreter_fail (gis->is);
     190            0 :           return;
     191              :         }
     192              :       }
     193              :       {
     194              :         const struct GNUNET_TIME_Relative *default_pay_delay;
     195              : 
     196            5 :         if (GNUNET_OK !=
     197            5 :             TALER_TESTING_get_trait_pay_delay (instance_cmd,
     198              :                                                &default_pay_delay))
     199            0 :           TALER_TESTING_interpreter_fail (gis->is);
     200            5 :         if (details->default_pay_delay.rel_value_us !=
     201            5 :             default_pay_delay->rel_value_us)
     202              :         {
     203            0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     204              :                       "Instance default pay delay does not match\n");
     205            0 :           TALER_TESTING_interpreter_fail (gis->is);
     206            0 :           return;
     207              :         }
     208              :       }
     209              :     }
     210            5 :     break;
     211            0 :   case MHD_HTTP_UNAUTHORIZED:
     212            0 :     break;
     213            4 :   case MHD_HTTP_NOT_FOUND:
     214            4 :     break;
     215            0 :   default:
     216            0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     217              :                 "Unhandled HTTP status %u for GET instance ID.\n",
     218              :                 igr->hr.http_status);
     219              :   }
     220            9 :   TALER_TESTING_interpreter_next (gis->is);
     221              : }
     222              : 
     223              : 
     224              : /**
     225              :  * Run the "GET instance" CMD.
     226              :  *
     227              :  *
     228              :  * @param cls closure.
     229              :  * @param cmd command being run now.
     230              :  * @param is interpreter state.
     231              :  */
     232              : static void
     233            9 : get_instance_run (void *cls,
     234              :                   const struct TALER_TESTING_Command *cmd,
     235              :                   struct TALER_TESTING_Interpreter *is)
     236              : {
     237            9 :   struct GetInstanceState *gis = cls;
     238              : 
     239            9 :   gis->is = is;
     240            9 :   gis->igh = TALER_MERCHANT_instance_get (
     241              :     TALER_TESTING_interpreter_get_context (is),
     242              :     gis->merchant_url,
     243              :     gis->instance_id,
     244              :     &get_instance_cb,
     245              :     gis);
     246            9 :   GNUNET_assert (NULL != gis->igh);
     247            9 : }
     248              : 
     249              : 
     250              : /**
     251              :  * Free the state of a "GET instance" CMD, and possibly
     252              :  * cancel a pending operation thereof.
     253              :  *
     254              :  * @param cls closure.
     255              :  * @param cmd command being run.
     256              :  */
     257              : static void
     258            9 : get_instance_cleanup (void *cls,
     259              :                       const struct TALER_TESTING_Command *cmd)
     260              : {
     261            9 :   struct GetInstanceState *gis = cls;
     262              : 
     263            9 :   if (NULL != gis->igh)
     264              :   {
     265            0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     266              :                 "GET /instances/$ID operation did not complete\n");
     267            0 :     TALER_MERCHANT_instance_get_cancel (gis->igh);
     268              :   }
     269            9 :   GNUNET_free (gis);
     270            9 : }
     271              : 
     272              : 
     273              : /**
     274              :  * Offers information from the GET /instance/$ID CMD state to other
     275              :  * commands.
     276              :  *
     277              :  * @param cls closure
     278              :  * @param[out] ret result (could be anything)
     279              :  * @param trait name of the trait
     280              :  * @param index index number of the object to extract.
     281              :  * @return #GNUNET_OK on success
     282              :  */
     283              : static enum GNUNET_GenericReturnValue
     284            2 : get_instance_traits (void *cls,
     285              :                      const void **ret,
     286              :                      const char *trait,
     287              :                      unsigned int index)
     288              : {
     289            2 :   struct GetInstanceState *pps = cls;
     290              :   struct TALER_TESTING_Trait traits[] = {
     291            2 :     TALER_TESTING_make_trait_merchant_base_url (
     292              :       pps->merchant_url),
     293            2 :     TALER_TESTING_make_trait_merchant_pub (
     294            2 :       &pps->account_pub.merchant_pub),
     295            2 :     TALER_TESTING_make_trait_account_pub (
     296            2 :       &pps->account_pub),
     297            2 :     TALER_TESTING_trait_end (),
     298              :   };
     299              : 
     300            2 :   return TALER_TESTING_get_trait (traits,
     301              :                                   ret,
     302              :                                   trait,
     303              :                                   index);
     304              : }
     305              : 
     306              : 
     307              : struct TALER_TESTING_Command
     308            9 : TALER_TESTING_cmd_merchant_get_instance (
     309              :   const char *label,
     310              :   const char *merchant_url,
     311              :   const char *instance_id,
     312              :   unsigned int http_status,
     313              :   const char *instance_reference)
     314              : {
     315              :   struct GetInstanceState *gis;
     316              : 
     317            9 :   gis = GNUNET_new (struct GetInstanceState);
     318            9 :   gis->merchant_url = merchant_url;
     319            9 :   gis->instance_id = instance_id;
     320            9 :   gis->http_status = http_status;
     321            9 :   gis->instance_reference = instance_reference;
     322              :   {
     323            9 :     struct TALER_TESTING_Command cmd = {
     324              :       .cls = gis,
     325              :       .label = label,
     326              :       .run = &get_instance_run,
     327              :       .cleanup = &get_instance_cleanup,
     328              :       .traits = &get_instance_traits
     329              :     };
     330              : 
     331            9 :     return cmd;
     332              :   }
     333              : }
     334              : 
     335              : 
     336              : /* end of testing_api_cmd_get_instance.c */
        

Generated by: LCOV version 2.0-1