LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_instances.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 62 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_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             :  */
      75             : static void
      76           0 : get_instances_cb (void *cls,
      77             :                   const struct TALER_MERCHANT_HttpResponse *hr,
      78             :                   unsigned int iis_length,
      79             :                   const struct TALER_MERCHANT_InstanceInformation iis[])
      80             : {
      81           0 :   struct GetInstancesState *gis = cls;
      82             : 
      83           0 :   gis->igh = NULL;
      84           0 :   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           0 :   switch (hr->http_status)
      95             :   {
      96           0 :   case MHD_HTTP_OK:
      97           0 :     if (iis_length != gis->instances_length)
      98             :     {
      99           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     100             :                   "Length of instances found does not match\n");
     101           0 :       TALER_TESTING_interpreter_fail (gis->is);
     102           0 :       return;
     103             :     }
     104           0 :     for (unsigned int i = 0; i < iis_length; ++i)
     105             :     {
     106             :       const struct TALER_TESTING_Command *instance_cmd;
     107             : 
     108           0 :       instance_cmd = TALER_TESTING_interpreter_lookup_command (
     109             :         gis->is,
     110           0 :         gis->instances[i]);
     111             : 
     112             :       {
     113             :         const char *name;
     114           0 :         if (GNUNET_OK !=
     115           0 :             TALER_TESTING_get_trait_string (instance_cmd,
     116             :                                             0,
     117             :                                             &name))
     118             :         {
     119           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     120             :                       "Could not fetch instance name\n");
     121           0 :           TALER_TESTING_interpreter_fail (gis->is);
     122           0 :           return;
     123             :         }
     124           0 :         if (0 != strcmp (iis[i].name,
     125             :                          name))
     126             :         {
     127           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     128             :                       "Instance name does not match\n");
     129           0 :           TALER_TESTING_interpreter_fail (gis->is);
     130           0 :           return;
     131             :         }
     132             :       }
     133             : 
     134             :       {
     135             :         const char *id;
     136           0 :         if (GNUNET_OK !=
     137           0 :             TALER_TESTING_get_trait_string (instance_cmd,
     138             :                                             1,
     139             :                                             &id))
     140             :         {
     141           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     142             :                       "Could not fetch instance id\n");
     143           0 :           TALER_TESTING_interpreter_fail (gis->is);
     144           0 :           return;
     145             :         }
     146           0 :         if (0 != strcmp (iis[i].id,
     147             :                          id))
     148             :         {
     149           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     150             :                       "Instance id does not match\n");
     151           0 :           TALER_TESTING_interpreter_fail (gis->is);
     152           0 :           return;
     153             :         }
     154             :       }
     155             :     }
     156             : 
     157             :     // FIXME: compare payment_targets
     158           0 :     break;
     159           0 :   default:
     160           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     161             :                 "Unhandled HTTP status.\n");
     162             :   }
     163           0 :   TALER_TESTING_interpreter_next (gis->is);
     164             : }
     165             : 
     166             : 
     167             : /**
     168             :  * Run the "GET /instances" CMD.
     169             :  *
     170             :  *
     171             :  * @param cls closure.
     172             :  * @param cmd command being run now.
     173             :  * @param is interpreter state.
     174             :  */
     175             : static void
     176           0 : get_instances_run (void *cls,
     177             :                    const struct TALER_TESTING_Command *cmd,
     178             :                    struct TALER_TESTING_Interpreter *is)
     179             : {
     180           0 :   struct GetInstancesState *gis = cls;
     181             : 
     182           0 :   gis->is = is;
     183           0 :   gis->igh = TALER_MERCHANT_instances_get (is->ctx,
     184             :                                            gis->merchant_url,
     185             :                                            &get_instances_cb,
     186             :                                            gis);
     187           0 :   GNUNET_assert (NULL != gis->igh);
     188           0 : }
     189             : 
     190             : 
     191             : /**
     192             :  * Free the state of a "GET instance" 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_instances_cleanup (void *cls,
     200             :                        const struct TALER_TESTING_Command *cmd)
     201             : {
     202           0 :   struct GetInstancesState *gis = cls;
     203             : 
     204           0 :   if (NULL != gis->igh)
     205             :   {
     206           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     207             :                 "GET /instances operation did not complete\n");
     208           0 :     TALER_MERCHANT_instances_get_cancel (gis->igh);
     209             :   }
     210           0 :   GNUNET_array_grow (gis->instances,
     211             :                      gis->instances_length,
     212             :                      0);
     213           0 :   GNUNET_free (gis);
     214           0 : }
     215             : 
     216             : 
     217             : /**
     218             :  * Define a "GET /instances" CMD.
     219             :  *
     220             :  * @param label command label.
     221             :  * @param merchant_url base URL of the merchant serving the
     222             :  *        GET /instances request.
     223             :  * @param http_status expected HTTP response code.
     224             :  * @param ... NULL-terminated list of labels (const char *) of
     225             :  *        product (commands) we expect to be returned in the list
     226             :  *        (assuming @a http_code is #MHD_HTTP_OK)
     227             :  * @return the command.
     228             :  */
     229             : struct TALER_TESTING_Command
     230           0 : TALER_TESTING_cmd_merchant_get_instances (const char *label,
     231             :                                           const char *merchant_url,
     232             :                                           unsigned int http_status,
     233             :                                           ...)
     234             : {
     235             :   struct GetInstancesState *gis;
     236             : 
     237           0 :   gis = GNUNET_new (struct GetInstancesState);
     238           0 :   gis->merchant_url = merchant_url;
     239           0 :   gis->http_status = http_status;
     240             :   {
     241             :     const char *clabel;
     242             :     va_list ap;
     243             : 
     244           0 :     va_start (ap, http_status);
     245           0 :     while (NULL != (clabel = va_arg (ap, const char *)))
     246             :     {
     247           0 :       GNUNET_array_append (gis->instances,
     248             :                            gis->instances_length,
     249             :                            clabel);
     250             :     }
     251           0 :     va_end (ap);
     252             :   }
     253             :   {
     254           0 :     struct TALER_TESTING_Command cmd = {
     255             :       .cls = gis,
     256             :       .label = label,
     257             :       .run = &get_instances_run,
     258             :       .cleanup = &get_instances_cleanup
     259             :     };
     260             : 
     261           0 :     return cmd;
     262             :   }
     263             : }
     264             : 
     265             : 
     266             : /* end of testing_api_cmd_get_instances.c */

Generated by: LCOV version 1.14