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

Generated by: LCOV version 1.14