merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_checkserver.c (7490B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2023 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 /**
     21  * @file testing/testing_api_cmd_checkserver.c
     22  * @brief Implement a CMD to run an Checkserver service for faking the legitimation service
     23  * @author Priscilla HUANG
     24  */
     25 #include "platform.h"
     26 #include "taler/taler_json_lib.h"
     27 #include <gnunet/gnunet_curl_lib.h>
     28 #include "taler/taler_testing_lib.h"
     29 #include "taler/taler_mhd_lib.h"
     30 #include "taler_merchant_testing_lib.h"
     31 #include "taler_merchant_service.h"
     32 #include <taler/taler_exchange_service.h>
     33 
     34 
     35 /**
     36  * State for a "checkserver" CMD.
     37  */
     38 struct CheckState
     39 {
     40   /**
     41    * Handle to the "testserver" service.
     42    */
     43   struct MHD_Daemon *mhd;
     44 
     45   /**
     46    * Our interpreter.
     47    */
     48   struct TALER_TESTING_Interpreter *is;
     49 
     50   /**
     51    * Index to know which web server we check.
     52    */
     53   unsigned int index;
     54 
     55   /**
     56    * Reference to command to the previous set server status operation.
     57    */
     58   const char *ref_operation;
     59 
     60   /**
     61    * Expected method of the pending webhook.
     62    */
     63   const char *expected_method;
     64 
     65   /**
     66    * Expected url of the pending webhook.
     67    */
     68   const char *expected_url;
     69 
     70   /**
     71    * Expected header of the pending webhook.
     72    */
     73   const char *expected_header;
     74 
     75   /**
     76    * Expected body of the pending webhook.
     77    */
     78   const char *expected_body;
     79 
     80 };
     81 
     82 
     83 /**
     84  * Run the command.
     85  *
     86  * @param cls closure.
     87  * @param cmd the command to execute.
     88  * @param is the interpreter state.
     89  */
     90 static void
     91 checkserver_run (void *cls,
     92                  const struct TALER_TESTING_Command *cmd,
     93                  struct TALER_TESTING_Interpreter *is)
     94 {
     95   struct CheckState *cs = cls;
     96   const struct TALER_TESTING_Command *ref;
     97   const char *url;
     98   const char *http_method;
     99   const char *header;
    100   const void *body;
    101   const size_t *body_size;
    102 
    103   (void) cmd;
    104   cs->is = is;
    105   ref = TALER_TESTING_interpreter_lookup_command (is,
    106                                                   cs->ref_operation);
    107   if (NULL == ref)
    108   {
    109     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    110                 "ref NULL\n");
    111     GNUNET_break (0);
    112     TALER_TESTING_interpreter_fail (is);
    113     return;
    114   }
    115   if (GNUNET_OK !=
    116       TALER_TESTING_get_trait_urls (ref,
    117                                     cs->index,
    118                                     &url))
    119   {
    120     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    121                 "Trait url does not work\n");
    122     GNUNET_break (0);
    123     TALER_TESTING_interpreter_fail (is);
    124     return;
    125   }
    126   if (NULL == url)
    127   {
    128     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    129                 "Trait for url is NULL!?\n");
    130     GNUNET_break (0);
    131     TALER_TESTING_interpreter_fail (is);
    132     return;
    133   }
    134   if (0 != strcmp (cs->expected_url,
    135                    url))
    136   {
    137     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    138                 "URL does not match: `%s' != `%s'\n",
    139                 cs->expected_url,
    140                 url);
    141     TALER_TESTING_interpreter_fail (is);
    142     return;
    143   }
    144   if (GNUNET_OK !=
    145       TALER_TESTING_get_trait_http_methods (ref,
    146                                             cs->index,
    147                                             &http_method))
    148     TALER_TESTING_interpreter_fail (is);
    149   if (0 != strcmp (cs->expected_method,
    150                    http_method))
    151   {
    152     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    153                 "http_method does not match\n");
    154     TALER_TESTING_interpreter_fail (is);
    155     return;
    156   }
    157   if (GNUNET_OK !=
    158       TALER_TESTING_get_trait_http_header (ref,
    159                                            cs->index,
    160                                            &header))
    161     TALER_TESTING_interpreter_fail (is);
    162   if ( ( (NULL == cs->expected_header) && (NULL != header)) ||
    163        ( (NULL != cs->expected_header) && (NULL == header)) ||
    164        ( (NULL != cs->expected_header) &&
    165          (0 != strcmp (cs->expected_header,
    166                        header)) ) )
    167   {
    168     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    169                 "header does not match: `%s' != `%s'\n",
    170                 cs->expected_header,
    171                 header);
    172     TALER_TESTING_interpreter_fail (is);
    173     return;
    174   }
    175   if (GNUNET_OK !=
    176       TALER_TESTING_get_trait_http_body (ref,
    177                                          cs->index,
    178                                          &body))
    179     TALER_TESTING_interpreter_fail (is);
    180   if (GNUNET_OK !=
    181       TALER_TESTING_get_trait_http_body_size (ref,
    182                                               cs->index,
    183                                               &body_size))
    184     TALER_TESTING_interpreter_fail (is);
    185   if ( ( (NULL == cs->expected_body) &&
    186          (NULL != body) ) ||
    187        ( (NULL != cs->expected_body) &&
    188          (NULL == body) ) ||
    189        ( (NULL != cs->expected_body) &&
    190          ( (*body_size != strlen (cs->expected_body)) ||
    191            (0 != memcmp (cs->expected_body,
    192                          body,
    193                          *body_size) ) ) ) )
    194   {
    195     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    196                 "body does not match : `%s' and `%.*s'\n",
    197                 cs->expected_body,
    198                 (int) *body_size,
    199                 (const char *) body);
    200     TALER_TESTING_interpreter_fail (is);
    201     return;
    202   }
    203   TALER_TESTING_interpreter_next (is);
    204 }
    205 
    206 
    207 /**
    208  * Free the state of a "checkserver" CMD.
    209  *
    210  * @param cls closure.
    211  * @param cmd command being run.
    212  */
    213 static void
    214 checkserver_cleanup (void *cls,
    215                      const struct TALER_TESTING_Command *cmd)
    216 {
    217   struct CheckState *cs = cls;
    218 
    219   GNUNET_free (cs);
    220 }
    221 
    222 
    223 struct TALER_TESTING_Command
    224 TALER_TESTING_cmd_checkserver2 (const char *label,
    225                                 const char *ref_operation,
    226                                 unsigned int index,
    227                                 const char *expected_url,
    228                                 const char *expected_method,
    229                                 const char *expected_header,
    230                                 const char *expected_body)
    231 {
    232   struct CheckState *cs;
    233 
    234   cs = GNUNET_new (struct CheckState);
    235   cs->ref_operation = ref_operation;
    236   cs->index = index;
    237   cs->expected_url = expected_url;
    238   cs->expected_method = expected_method;
    239   cs->expected_header = expected_header;
    240   cs->expected_body = expected_body;
    241 
    242   {
    243     struct TALER_TESTING_Command cmd = {
    244       .cls = cs,
    245       .label = label,
    246       .run = &checkserver_run,
    247       .cleanup = &checkserver_cleanup
    248     };
    249 
    250     return cmd;
    251   }
    252 }
    253 
    254 
    255 struct TALER_TESTING_Command
    256 TALER_TESTING_cmd_checkserver (const char *label,
    257                                const char *ref_operation,
    258                                unsigned int index)
    259 {
    260   return TALER_TESTING_cmd_checkserver2 (label,
    261                                          ref_operation,
    262                                          index,
    263                                          "/",
    264                                          "POST",
    265                                          "EFEHYJS-Bakery",
    266                                          "5.0 EUR");
    267 }
    268 
    269 
    270 /* end of testing_api_cmd_checkserver.c */