LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_instance.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 96 143 67.1 %
Date: 2021-08-30 06:54:17 Functions: 5 5 100.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 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             :    * Base URL of the merchant serving the request.
      49             :    */
      50             :   const char *merchant_url;
      51             : 
      52             :   /**
      53             :    * ID of the instance to run GET for.
      54             :    */
      55             :   const char *instance_id;
      56             : 
      57             :   /**
      58             :    * Reference for a POST or PATCH /instances CMD (optional).
      59             :    */
      60             :   const char *instance_reference;
      61             : 
      62             :   /**
      63             :    * Whether we should check the instance's accounts or not.
      64             :    */
      65             :   bool cmp_accounts;
      66             : 
      67             :   /**
      68             :    * The accounts of the merchant we expect to be active.
      69             :    */
      70             :   const char **active_accounts;
      71             : 
      72             :   /**
      73             :    * The length of @e active_accounts.
      74             :    */
      75             :   unsigned int active_accounts_length;
      76             : 
      77             :   /**
      78             :    * The accounts of the merchant we expect to be inactive.
      79             :    */
      80             :   const char **inactive_accounts;
      81             : 
      82             :   /**
      83             :    * The length of @e inactive_accounts.
      84             :    */
      85             :   unsigned int inactive_accounts_length;
      86             : 
      87             :   /**
      88             :    * Expected HTTP response code.
      89             :    */
      90             :   unsigned int http_status;
      91             : 
      92             : };
      93             : 
      94             : 
      95             : /**
      96             :  * Callback for a /get/instance/$ID operation.
      97             :  *
      98             :  * @param cls closure for this function
      99             :  * @param hr HTTP response
     100             :  * @param accounts_length how many bank accounts the instance has
     101             :  * @param accounts the list of the instance's bank accounts
     102             :  * @param details all the details related to this particular instance
     103             :  */
     104             : static void
     105           5 : get_instance_cb (void *cls,
     106             :                  const struct TALER_MERCHANT_HttpResponse *hr,
     107             :                  unsigned int accounts_length,
     108             :                  const struct TALER_MERCHANT_Account accounts[],
     109             :                  const struct TALER_MERCHANT_InstanceDetails *details)
     110             : {
     111             :   /* FIXME, deeper checks should be implemented here (for accounts). */
     112           5 :   struct GetInstanceState *gis = cls;
     113             :   const struct TALER_TESTING_Command *instance_cmd;
     114             : 
     115           5 :   instance_cmd = TALER_TESTING_interpreter_lookup_command (
     116             :     gis->is,
     117             :     gis->instance_reference);
     118             : 
     119           5 :   gis->igh = NULL;
     120           5 :   if (gis->http_status != hr->http_status)
     121             :   {
     122           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     123             :                 "Unexpected response code %u (%d) to command %s\n",
     124             :                 hr->http_status,
     125             :                 (int) hr->ec,
     126             :                 TALER_TESTING_interpreter_get_current_label (gis->is));
     127           0 :     TALER_TESTING_interpreter_fail (gis->is);
     128           0 :     return;
     129             :   }
     130           5 :   switch (hr->http_status)
     131             :   {
     132           3 :   case MHD_HTTP_OK:
     133             :     {
     134             :       const char *name;
     135             : 
     136           3 :       if (GNUNET_OK !=
     137           3 :           TALER_TESTING_get_trait_string (instance_cmd,
     138             :                                           0,
     139             :                                           &name))
     140           0 :         TALER_TESTING_interpreter_fail (gis->is);
     141           3 :       if (0 != strcmp (details->name,
     142             :                        name))
     143             :       {
     144           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     145             :                     "Instance name does not match: Got `%s', wanted `%s'\n",
     146             :                     details->name,
     147             :                     name);
     148           0 :         TALER_TESTING_interpreter_fail (gis->is);
     149           0 :         return;
     150             :       }
     151             :     }
     152             :     {
     153             :       const struct json_t *address;
     154           3 :       if (GNUNET_OK !=
     155           3 :           TALER_TESTING_get_trait_json (instance_cmd,
     156             :                                         0,
     157             :                                         &address))
     158           0 :         TALER_TESTING_interpreter_fail (gis->is);
     159           3 :       if (1 != json_equal (details->address,
     160             :                            address))
     161             :       {
     162           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     163             :                     "Instance address does not match\n");
     164           0 :         TALER_TESTING_interpreter_fail (gis->is);
     165           0 :         return;
     166             :       }
     167             :     }
     168             :     {
     169             :       const struct json_t *jurisdiction;
     170           3 :       if (GNUNET_OK !=
     171           3 :           TALER_TESTING_get_trait_json (instance_cmd,
     172             :                                         1,
     173             :                                         &jurisdiction))
     174           0 :         TALER_TESTING_interpreter_fail (gis->is);
     175           3 :       if (1 != json_equal (details->jurisdiction,
     176             :                            jurisdiction))
     177             :       {
     178           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     179             :                     "Instance jurisdiction does not match\n");
     180           0 :         TALER_TESTING_interpreter_fail (gis->is);
     181           0 :         return;
     182             :       }
     183             :     }
     184             :     {
     185             :       const struct TALER_Amount *default_max_wire_fee;
     186           3 :       if (GNUNET_OK !=
     187           3 :           TALER_TESTING_get_trait_amount_obj (instance_cmd,
     188             :                                               0,
     189             :                                               &default_max_wire_fee))
     190           0 :         TALER_TESTING_interpreter_fail (gis->is);
     191           3 :       if ((GNUNET_OK != TALER_amount_cmp_currency (
     192             :              details->default_max_wire_fee,
     193           3 :              default_max_wire_fee)) ||
     194           3 :           (0 != TALER_amount_cmp (details->default_max_wire_fee,
     195             :                                   default_max_wire_fee)))
     196             :       {
     197           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     198             :                     "Instance default max wire fee does not match\n");
     199           0 :         TALER_TESTING_interpreter_fail (gis->is);
     200           0 :         return;
     201             :       }
     202             :     }
     203             :     {
     204             :       const uint32_t *default_wire_fee_amortization;
     205           3 :       if (GNUNET_OK !=
     206           3 :           TALER_TESTING_get_trait_uint32 (instance_cmd,
     207             :                                           0,
     208             :                                           &default_wire_fee_amortization))
     209           0 :         TALER_TESTING_interpreter_fail (gis->is);
     210           3 :       if (details->default_wire_fee_amortization !=
     211           3 :           *default_wire_fee_amortization)
     212             :       {
     213           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     214             :                     "Instance default wire fee amortization does not match\n");
     215           0 :         TALER_TESTING_interpreter_fail (gis->is);
     216           0 :         return;
     217             :       }
     218             :     }
     219             :     {
     220             :       const struct TALER_Amount *default_max_deposit_fee;
     221             : 
     222           3 :       if (GNUNET_OK !=
     223           3 :           TALER_TESTING_get_trait_amount_obj (instance_cmd,
     224             :                                               1,
     225             :                                               &default_max_deposit_fee))
     226           0 :         TALER_TESTING_interpreter_fail (gis->is);
     227           3 :       if ( (GNUNET_OK !=
     228           3 :             TALER_amount_cmp_currency (
     229             :               details->default_max_deposit_fee,
     230           3 :               default_max_deposit_fee)) ||
     231           3 :            (0 != TALER_amount_cmp (details->default_max_deposit_fee,
     232             :                                    default_max_deposit_fee)) )
     233             :       {
     234           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     235             :                     "Instance default max deposit fee %s does not match\n",
     236             :                     TALER_amount2s (details->default_max_deposit_fee));
     237           0 :         TALER_TESTING_interpreter_fail (gis->is);
     238           0 :         return;
     239             :       }
     240             :     }
     241             :     {
     242             :       const struct GNUNET_TIME_Relative *default_wire_transfer_delay;
     243             : 
     244           3 :       if (GNUNET_OK !=
     245           3 :           TALER_TESTING_get_trait_relative_time (instance_cmd,
     246             :                                                  0,
     247             :                                                  &default_wire_transfer_delay))
     248           0 :         TALER_TESTING_interpreter_fail (gis->is);
     249           3 :       if (details->default_wire_transfer_delay.rel_value_us !=
     250           3 :           default_wire_transfer_delay->rel_value_us)
     251             :       {
     252           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     253             :                     "Instance default wire transfer delay does not match\n");
     254           0 :         TALER_TESTING_interpreter_fail (gis->is);
     255           0 :         return;
     256             :       }
     257             :     }
     258             :     {
     259             :       const struct GNUNET_TIME_Relative *default_pay_delay;
     260           3 :       if (GNUNET_OK !=
     261           3 :           TALER_TESTING_get_trait_relative_time (instance_cmd,
     262             :                                                  1,
     263             :                                                  &default_pay_delay))
     264           0 :         TALER_TESTING_interpreter_fail (gis->is);
     265           3 :       if (details->default_pay_delay.rel_value_us !=
     266           3 :           default_pay_delay->rel_value_us)
     267             :       {
     268           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     269             :                     "Instance default pay delay does not match\n");
     270           0 :         TALER_TESTING_interpreter_fail (gis->is);
     271           0 :         return;
     272             :       }
     273             :     }
     274             :     /* We aren't guaranteed an order for the accounts, so we just have to check
     275             :        that we can match each account returned with exactly one account
     276             :        expected. */
     277           3 :     if (gis->cmp_accounts)
     278           2 :     {
     279           2 :       unsigned int expected_accounts_length =
     280           2 :         gis->active_accounts_length + gis->inactive_accounts_length;
     281           2 :       unsigned int matches[accounts_length];
     282             : 
     283           2 :       if (accounts_length != expected_accounts_length)
     284             :       {
     285           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     286             :                     "Accounts length does not match\n");
     287           0 :         TALER_TESTING_interpreter_fail (gis->is);
     288           0 :         return;
     289             :       }
     290             : 
     291           2 :       memset (matches,
     292             :               0,
     293             :               sizeof (unsigned int) * accounts_length);
     294             : 
     295             :       // Compare the accounts
     296           6 :       for (unsigned int i = 0; i < accounts_length; ++i)
     297             :       {
     298          10 :         for (unsigned int j = 0; j < gis->active_accounts_length; ++j)
     299             :         {
     300           6 :           if ((0 == strcasecmp (accounts[i].payto_uri,
     301           6 :                                 gis->active_accounts[j])) &&
     302           3 :               (true == accounts[i].active))
     303             :           {
     304           3 :             matches[i] += 1;
     305             :           }
     306             :         }
     307           6 :         for (unsigned int j = 0; j < gis->inactive_accounts_length; ++j)
     308             :         {
     309           2 :           if ((0 == strcasecmp (accounts[i].payto_uri,
     310           2 :                                 gis->inactive_accounts[j])) &&
     311           1 :               (false == accounts[i].active))
     312             :           {
     313           1 :             matches[i] += 1;
     314             :           }
     315             :         }
     316             :       }
     317             : 
     318             :       // Each account should have exactly one match.
     319           6 :       for (unsigned int i = 0; i < accounts_length; ++i)
     320             :       {
     321           4 :         if (1 != matches[i])
     322             :         {
     323           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     324             :                       "Instance account does not match\n");
     325           0 :           TALER_TESTING_interpreter_fail (gis->is);
     326           0 :           return;
     327             :         }
     328             :       }
     329             :     }
     330           3 :     break;
     331           0 :   case MHD_HTTP_UNAUTHORIZED:
     332           0 :     break;
     333           2 :   case MHD_HTTP_NOT_FOUND:
     334           2 :     break;
     335           0 :   default:
     336           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     337             :                 "Unhandled HTTP status %u for GET instance ID.\n",
     338             :                 hr->http_status);
     339             :   }
     340           5 :   TALER_TESTING_interpreter_next (gis->is);
     341             : }
     342             : 
     343             : 
     344             : /**
     345             :  * Run the "GET instance" CMD.
     346             :  *
     347             :  *
     348             :  * @param cls closure.
     349             :  * @param cmd command being run now.
     350             :  * @param is interpreter state.
     351             :  */
     352             : static void
     353           5 : get_instance_run (void *cls,
     354             :                   const struct TALER_TESTING_Command *cmd,
     355             :                   struct TALER_TESTING_Interpreter *is)
     356             : {
     357           5 :   struct GetInstanceState *gis = cls;
     358             : 
     359           5 :   gis->is = is;
     360           5 :   gis->igh = TALER_MERCHANT_instance_get (is->ctx,
     361             :                                           gis->merchant_url,
     362             :                                           gis->instance_id,
     363             :                                           &get_instance_cb,
     364             :                                           gis);
     365           5 :   GNUNET_assert (NULL != gis->igh);
     366           5 : }
     367             : 
     368             : 
     369             : /**
     370             :  * Free the state of a "GET instance" CMD, and possibly
     371             :  * cancel a pending operation thereof.
     372             :  *
     373             :  * @param cls closure.
     374             :  * @param cmd command being run.
     375             :  */
     376             : static void
     377           5 : get_instance_cleanup (void *cls,
     378             :                       const struct TALER_TESTING_Command *cmd)
     379             : {
     380           5 :   struct GetInstanceState *gis = cls;
     381             : 
     382           5 :   if (NULL != gis->igh)
     383             :   {
     384           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     385             :                 "GET /instances/$ID operation did not complete\n");
     386           0 :     TALER_MERCHANT_instance_get_cancel (gis->igh);
     387             :   }
     388           5 :   GNUNET_free (gis);
     389           5 : }
     390             : 
     391             : 
     392             : struct TALER_TESTING_Command
     393           3 : TALER_TESTING_cmd_merchant_get_instance (const char *label,
     394             :                                          const char *merchant_url,
     395             :                                          const char *instance_id,
     396             :                                          unsigned int http_status,
     397             :                                          const char *instance_reference)
     398             : {
     399             :   struct GetInstanceState *gis;
     400             : 
     401           3 :   gis = GNUNET_new (struct GetInstanceState);
     402           3 :   gis->merchant_url = merchant_url;
     403           3 :   gis->instance_id = instance_id;
     404           3 :   gis->http_status = http_status;
     405           3 :   gis->instance_reference = instance_reference;
     406           3 :   gis->cmp_accounts = false;
     407             :   {
     408           3 :     struct TALER_TESTING_Command cmd = {
     409             :       .cls = gis,
     410             :       .label = label,
     411             :       .run = &get_instance_run,
     412             :       .cleanup = &get_instance_cleanup
     413             :     };
     414             : 
     415           3 :     return cmd;
     416             :   }
     417             : }
     418             : 
     419             : 
     420             : struct TALER_TESTING_Command
     421           2 : TALER_TESTING_cmd_merchant_get_instance2 (const char *label,
     422             :                                           const char *merchant_url,
     423             :                                           const char *instance_id,
     424             :                                           unsigned int http_status,
     425             :                                           const char *instance_reference,
     426             :                                           const char *active_accounts[],
     427             :                                           unsigned int active_accounts_length,
     428             :                                           const char *inactive_accounts[],
     429             :                                           unsigned int inactive_accounts_length)
     430             : {
     431             :   struct GetInstanceState *gis;
     432             : 
     433           2 :   gis = GNUNET_new (struct GetInstanceState);
     434           2 :   gis->merchant_url = merchant_url;
     435           2 :   gis->instance_id = instance_id;
     436           2 :   gis->http_status = http_status;
     437           2 :   gis->instance_reference = instance_reference;
     438           2 :   gis->cmp_accounts = true;
     439           2 :   gis->active_accounts = active_accounts;
     440           2 :   gis->active_accounts_length = active_accounts_length;
     441           2 :   gis->inactive_accounts = inactive_accounts;
     442           2 :   gis->inactive_accounts_length = inactive_accounts_length;
     443             :   {
     444           2 :     struct TALER_TESTING_Command cmd = {
     445             :       .cls = gis,
     446             :       .label = label,
     447             :       .run = &get_instance_run,
     448             :       .cleanup = &get_instance_cleanup
     449             :     };
     450             : 
     451           2 :     return cmd;
     452             :   }
     453             : }
     454             : 
     455             : 
     456             : /* end of testing_api_cmd_get_instance.c */

Generated by: LCOV version 1.14