LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_instances.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 43 65 66.2 %
Date: 2025-06-23 16:22:09 Functions: 4 4 100.0 %

          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_instances.c
      21             :  * @brief command to test GET /instances
      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 instances" CMD.
      33             :  */
      34             : struct GetInstancesState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET instance" request.
      39             :    */
      40             :   struct TALER_MERCHANT_InstancesGetHandle *igh;
      41             : 
      42             :   /**
      43             :    * The interpreter state.
      44             :    */
      45             :   struct TALER_TESTING_Interpreter *is;
      46             : 
      47             :   /**
      48             :    * Base URL of the merchant serving the request.
      49             :    */
      50             :   const char *merchant_url;
      51             : 
      52             :   /**
      53             :    * Expected HTTP response code.
      54             :    */
      55             :   unsigned int http_status;
      56             : 
      57             :   /**
      58             :    * The list of instance references to compare to.
      59             :    */
      60             :   const char **instances;
      61             : 
      62             :   /**
      63             :    * The length of @e instances.
      64             :    */
      65             :   unsigned int instances_length;
      66             : 
      67             : };
      68             : 
      69             : 
      70             : /**
      71             :  * Callback for a GET /instances operation.
      72             :  *
      73             :  * @param cls closure for this function
      74             :  * @param igr response
      75             :  */
      76             : static void
      77           4 : get_instances_cb (void *cls,
      78             :                   const struct TALER_MERCHANT_InstancesGetResponse *igr)
      79             : {
      80           4 :   const struct TALER_MERCHANT_HttpResponse *hr = &igr->hr;
      81           4 :   struct GetInstancesState *gis = cls;
      82             : 
      83           4 :   gis->igh = NULL;
      84           4 :   if (gis->http_status != hr->http_status)
      85             :   {
      86           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      87             :                 "Unexpected response code %u (%d) to command %s\n",
      88             :                 hr->http_status,
      89             :                 (int) hr->ec,
      90             :                 TALER_TESTING_interpreter_get_current_label (gis->is));
      91           0 :     TALER_TESTING_interpreter_fail (gis->is);
      92           0 :     return;
      93             :   }
      94           4 :   switch (hr->http_status)
      95             :   {
      96           4 :   case MHD_HTTP_OK:
      97             :     {
      98           4 :       unsigned int iis_length
      99             :         = igr->details.ok.iis_length;
     100           4 :       const struct TALER_MERCHANT_InstanceInformation *iis
     101             :         = igr->details.ok.iis;
     102             : 
     103           4 :       if (iis_length != gis->instances_length)
     104             :       {
     105           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     106             :                     "Length of instances found does not match\n");
     107           0 :         TALER_TESTING_interpreter_fail (gis->is);
     108           0 :         return;
     109             :       }
     110           8 :       for (unsigned int i = 0; i < iis_length; ++i)
     111             :       {
     112             :         const struct TALER_TESTING_Command *instance_cmd;
     113             : 
     114           4 :         instance_cmd = TALER_TESTING_interpreter_lookup_command (
     115             :           gis->is,
     116           4 :           gis->instances[i]);
     117             : 
     118             :         {
     119             :           const char *name;
     120             : 
     121           4 :           if (GNUNET_OK !=
     122           4 :               TALER_TESTING_get_trait_instance_name (instance_cmd,
     123             :                                                      &name))
     124             :           {
     125           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     126             :                         "Could not fetch instance name\n");
     127           0 :             TALER_TESTING_interpreter_fail (gis->is);
     128           0 :             return;
     129             :           }
     130           4 :           if (0 != strcmp (iis[i].name,
     131             :                            name))
     132             :           {
     133           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     134             :                         "Instance name does not match\n");
     135           0 :             TALER_TESTING_interpreter_fail (gis->is);
     136           0 :             return;
     137             :           }
     138             :         }
     139             : 
     140             :         {
     141             :           const char *id;
     142             : 
     143           4 :           if (GNUNET_OK !=
     144           4 :               TALER_TESTING_get_trait_instance_id (instance_cmd,
     145             :                                                    &id))
     146             :           {
     147           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     148             :                         "Could not fetch instance id\n");
     149           0 :             TALER_TESTING_interpreter_fail (gis->is);
     150           0 :             return;
     151             :           }
     152           4 :           if (0 != strcmp (iis[i].id,
     153             :                            id))
     154             :           {
     155           0 :             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     156             :                         "Instance id does not match\n");
     157           0 :             TALER_TESTING_interpreter_fail (gis->is);
     158           0 :             return;
     159             :           }
     160             :         }
     161             :       }
     162             : 
     163             :       // FIXME: compare payment_targets
     164           4 :       break;
     165             :     }
     166           0 :   default:
     167           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     168             :                 "Unhandled HTTP status %u for GET /instances.\n",
     169             :                 hr->http_status);
     170             :   }
     171           4 :   TALER_TESTING_interpreter_next (gis->is);
     172             : }
     173             : 
     174             : 
     175             : /**
     176             :  * Run the "GET /instances" CMD.
     177             :  *
     178             :  *
     179             :  * @param cls closure.
     180             :  * @param cmd command being run now.
     181             :  * @param is interpreter state.
     182             :  */
     183             : static void
     184           4 : get_instances_run (void *cls,
     185             :                    const struct TALER_TESTING_Command *cmd,
     186             :                    struct TALER_TESTING_Interpreter *is)
     187             : {
     188           4 :   struct GetInstancesState *gis = cls;
     189             : 
     190           4 :   gis->is = is;
     191           4 :   gis->igh = TALER_MERCHANT_instances_get (
     192             :     TALER_TESTING_interpreter_get_context (is),
     193             :     gis->merchant_url,
     194             :     &get_instances_cb,
     195             :     gis);
     196           4 :   GNUNET_assert (NULL != gis->igh);
     197           4 : }
     198             : 
     199             : 
     200             : /**
     201             :  * Free the state of a "GET instance" CMD, and possibly
     202             :  * cancel a pending operation thereof.
     203             :  *
     204             :  * @param cls closure.
     205             :  * @param cmd command being run.
     206             :  */
     207             : static void
     208           4 : get_instances_cleanup (void *cls,
     209             :                        const struct TALER_TESTING_Command *cmd)
     210             : {
     211           4 :   struct GetInstancesState *gis = cls;
     212             : 
     213           4 :   if (NULL != gis->igh)
     214             :   {
     215           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     216             :                 "GET /instances operation did not complete\n");
     217           0 :     TALER_MERCHANT_instances_get_cancel (gis->igh);
     218             :   }
     219           4 :   GNUNET_array_grow (gis->instances,
     220             :                      gis->instances_length,
     221             :                      0);
     222           4 :   GNUNET_free (gis);
     223           4 : }
     224             : 
     225             : 
     226             : struct TALER_TESTING_Command
     227           4 : TALER_TESTING_cmd_merchant_get_instances (const char *label,
     228             :                                           const char *merchant_url,
     229             :                                           unsigned int http_status,
     230             :                                           ...)
     231             : {
     232             :   struct GetInstancesState *gis;
     233             : 
     234           4 :   gis = GNUNET_new (struct GetInstancesState);
     235           4 :   gis->merchant_url = merchant_url;
     236           4 :   gis->http_status = http_status;
     237             :   {
     238             :     const char *clabel;
     239             :     va_list ap;
     240             : 
     241           4 :     va_start (ap, http_status);
     242           8 :     while (NULL != (clabel = va_arg (ap, const char *)))
     243             :     {
     244           4 :       GNUNET_array_append (gis->instances,
     245             :                            gis->instances_length,
     246             :                            clabel);
     247             :     }
     248           4 :     va_end (ap);
     249             :   }
     250             :   {
     251           4 :     struct TALER_TESTING_Command cmd = {
     252             :       .cls = gis,
     253             :       .label = label,
     254             :       .run = &get_instances_run,
     255             :       .cleanup = &get_instances_cleanup
     256             :     };
     257             : 
     258           4 :     return cmd;
     259             :   }
     260             : }
     261             : 
     262             : 
     263             : /* end of testing_api_cmd_get_instances.c */

Generated by: LCOV version 1.16