LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_templates.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 37 58 63.8 %
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) 2022 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_templates.c
      21             :  * @brief command to test GET /templates
      22             :  * @author Priscilla HUANG
      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 templates" CMD.
      33             :  */
      34             : struct GetTemplatesState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET template" request.
      39             :    */
      40             :   struct TALER_MERCHANT_TemplatesGetHandle *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 template references.
      59             :    */
      60             :   const char **templates;
      61             : 
      62             :   /**
      63             :    * Length of @e templates.
      64             :    */
      65             :   unsigned int templates_length;
      66             : 
      67             : };
      68             : 
      69             : 
      70             : /**
      71             :  * Callback for a GET /templates operation.
      72             :  *
      73             :  * @param cls closure for this function
      74             :  * @param tgr response details
      75             :  */
      76             : static void
      77           4 : get_templates_cb (void *cls,
      78             :                   const struct TALER_MERCHANT_TemplatesGetResponse *tgr)
      79             : {
      80           4 :   struct GetTemplatesState *gis = cls;
      81             : 
      82           4 :   gis->igh = NULL;
      83           4 :   if (gis->http_status != tgr->hr.http_status)
      84             :   {
      85           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      86             :                 "Unexpected response code %u (%d) to command %s\n",
      87             :                 tgr->hr.http_status,
      88             :                 (int) tgr->hr.ec,
      89             :                 TALER_TESTING_interpreter_get_current_label (gis->is));
      90           0 :     TALER_TESTING_interpreter_fail (gis->is);
      91           0 :     return;
      92             :   }
      93           4 :   switch (tgr->hr.http_status)
      94             :   {
      95           4 :   case MHD_HTTP_OK:
      96           4 :     if (tgr->details.ok.templates_length != gis->templates_length)
      97             :     {
      98           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      99             :                   "Length of templates found does not match\n");
     100           0 :       TALER_TESTING_interpreter_fail (gis->is);
     101           0 :       return;
     102             :     }
     103           6 :     for (unsigned int i = 0; i < gis->templates_length; ++i)
     104             :     {
     105             :       const struct TALER_TESTING_Command *template_cmd;
     106             : 
     107           2 :       template_cmd = TALER_TESTING_interpreter_lookup_command (
     108             :         gis->is,
     109           2 :         gis->templates[i]);
     110             : 
     111             :       {
     112             :         const char *template_id;
     113             : 
     114           2 :         if (GNUNET_OK !=
     115           2 :             TALER_TESTING_get_trait_template_id (template_cmd,
     116             :                                                  &template_id))
     117             :         {
     118           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     119             :                       "Could not fetch template id\n");
     120           0 :           TALER_TESTING_interpreter_fail (gis->is);
     121           0 :           return;
     122             :         }
     123           2 :         if (0 != strcmp (tgr->details.ok.templates[i].template_id,
     124             :                          template_id))
     125             :         {
     126           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     127             :                       "Template id does not match\n");
     128           0 :           TALER_TESTING_interpreter_fail (gis->is);
     129           0 :           return;
     130             :         }
     131             :       }
     132             :     }
     133           4 :     break;
     134           0 :   case MHD_HTTP_UNAUTHORIZED:
     135           0 :     break;
     136           0 :   case MHD_HTTP_NOT_FOUND:
     137             :     /* instance does not exist */
     138           0 :     break;
     139           0 :   default:
     140           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     141             :                 "Unhandled HTTP status %u (%d).\n",
     142             :                 tgr->hr.http_status,
     143             :                 tgr->hr.ec);
     144           0 :     break;
     145             :   }
     146           4 :   TALER_TESTING_interpreter_next (gis->is);
     147             : }
     148             : 
     149             : 
     150             : /**
     151             :  * Run the "GET /templates" CMD.
     152             :  *
     153             :  *
     154             :  * @param cls closure.
     155             :  * @param cmd command being run now.
     156             :  * @param is interpreter state.
     157             :  */
     158             : static void
     159           4 : get_templates_run (void *cls,
     160             :                    const struct TALER_TESTING_Command *cmd,
     161             :                    struct TALER_TESTING_Interpreter *is)
     162             : {
     163           4 :   struct GetTemplatesState *gis = cls;
     164             : 
     165           4 :   gis->is = is;
     166           4 :   gis->igh = TALER_MERCHANT_templates_get (
     167             :     TALER_TESTING_interpreter_get_context (is),
     168             :     gis->merchant_url,
     169             :     &get_templates_cb,
     170             :     gis);
     171           4 :   GNUNET_assert (NULL != gis->igh);
     172           4 : }
     173             : 
     174             : 
     175             : /**
     176             :  * Free the state of a "GET template" CMD, and possibly
     177             :  * cancel a pending operation thereof.
     178             :  *
     179             :  * @param cls closure.
     180             :  * @param cmd command being run.
     181             :  */
     182             : static void
     183           4 : get_templates_cleanup (void *cls,
     184             :                        const struct TALER_TESTING_Command *cmd)
     185             : {
     186           4 :   struct GetTemplatesState *gis = cls;
     187             : 
     188           4 :   if (NULL != gis->igh)
     189             :   {
     190           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     191             :                 "GET /templates operation did not complete\n");
     192           0 :     TALER_MERCHANT_templates_get_cancel (gis->igh);
     193             :   }
     194           4 :   GNUNET_array_grow (gis->templates,
     195             :                      gis->templates_length,
     196             :                      0);
     197           4 :   GNUNET_free (gis);
     198           4 : }
     199             : 
     200             : 
     201             : struct TALER_TESTING_Command
     202           4 : TALER_TESTING_cmd_merchant_get_templates (const char *label,
     203             :                                           const char *merchant_url,
     204             :                                           unsigned int http_status,
     205             :                                           ...)
     206             : {
     207             :   struct GetTemplatesState *gis;
     208             : 
     209           4 :   gis = GNUNET_new (struct GetTemplatesState);
     210           4 :   gis->merchant_url = merchant_url;
     211           4 :   gis->http_status = http_status;
     212             :   {
     213             :     const char *clabel;
     214             :     va_list ap;
     215             : 
     216           4 :     va_start (ap, http_status);
     217           6 :     while (NULL != (clabel = va_arg (ap, const char *)))
     218             :     {
     219           2 :       GNUNET_array_append (gis->templates,
     220             :                            gis->templates_length,
     221             :                            clabel);
     222             :     }
     223           4 :     va_end (ap);
     224             :   }
     225             :   {
     226           4 :     struct TALER_TESTING_Command cmd = {
     227             :       .cls = gis,
     228             :       .label = label,
     229             :       .run = &get_templates_run,
     230             :       .cleanup = &get_templates_cleanup
     231             :     };
     232             : 
     233           4 :     return cmd;
     234             :   }
     235             : }
     236             : 
     237             : 
     238             : /* end of testing_api_cmd_get_templates.c */

Generated by: LCOV version 1.16