exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_get_active_legitimization_measures.c (9576B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2025 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it
      6   under the terms of the GNU General Public License as published by
      7   the Free Software Foundation; either version 3, or (at your
      8   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 GNU
     13   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/testing_api_cmd_get_active_legitimization_measures.c
     21  * @brief command for testing /aml/$OFFICER_PUB/legitimizations
     22  * @author Christian Grothoff
     23  */
     24 
     25 /**
     26  * State for a GET "active_legitimization_measures" CMD.
     27  */
     28 struct GetLegitimizationMeasuresState;
     29 #define TALER_EXCHANGE__AML_LEGITIMIZATIONS_GET_RESULT_CLOSURE  \
     30         struct GetLegitimizationMeasuresState
     31 
     32 #include "taler/platform.h"
     33 #include "taler/taler_json_lib.h"
     34 #include <gnunet/gnunet_curl_lib.h>
     35 #include "taler/taler_testing_lib.h"
     36 #include "taler/taler_signatures.h"
     37 #include "taler/backoff.h"
     38 
     39 
     40 struct GetLegitimizationMeasuresState
     41 {
     42 
     43   /**
     44    * Handle while operation is running.
     45    */
     46   struct TALER_EXCHANGE_AmlLegitimizationsGetHandle *dh;
     47 
     48   /**
     49    * Our interpreter.
     50    */
     51   struct TALER_TESTING_Interpreter *is;
     52 
     53   /**
     54    * Reference to command to previous set officer command that gives
     55    * us an officer_priv trait.
     56    */
     57   const char *officer_ref_cmd;
     58 
     59   /**
     60    * Reference to command to previous AML-triggering event that gives
     61    * us a payto-hash trait.
     62    */
     63   const char *account_ref_cmd;
     64 
     65   /**
     66    * Payto hash of the account we are manipulating the AML settings for.
     67    */
     68   struct TALER_NormalizedPaytoHashP h_payto;
     69 
     70   /**
     71    * Expected legitimization measures.
     72    */
     73   json_t *expected_measures;
     74 
     75   /**
     76    * Expected response code.
     77    */
     78   unsigned int expected_response;
     79 };
     80 
     81 
     82 /**
     83  * Callback to analyze the /aml-decision/$OFFICER_PUB response, just used to check
     84  * if the response code is acceptable.
     85  *
     86  * @param ds command state
     87  * @param result response details
     88  */
     89 static void
     90 get_active_legitimization_measures_cb (
     91   TALER_EXCHANGE__AML_LEGITIMIZATIONS_GET_RESULT_CLOSURE *ds,
     92   const struct TALER_EXCHANGE_AmlLegitimizationsGetResult *result)
     93 {
     94   const struct TALER_EXCHANGE_HttpResponse *hr = &result->hr;
     95 
     96   ds->dh = NULL;
     97   if (ds->expected_response != hr->http_status)
     98   {
     99     TALER_TESTING_unexpected_status (ds->is,
    100                                      hr->http_status,
    101                                      ds->expected_response);
    102     return;
    103   }
    104   if (MHD_HTTP_OK == hr->http_status)
    105   {
    106     if (NULL == ds->expected_measures)
    107     {
    108       if (0 != result->details.ok.measures_length)
    109       {
    110         GNUNET_break (0);
    111         TALER_TESTING_interpreter_fail (ds->is);
    112         return;
    113       }
    114     }
    115     else
    116     {
    117       if (1 != result->details.ok.measures_length)
    118       {
    119         GNUNET_break (0);
    120         TALER_TESTING_interpreter_fail (ds->is);
    121         return;
    122       }
    123       for (size_t i = 0; i<result->details.ok.measures_length; i++)
    124       {
    125         const struct TALER_EXCHANGE_AmlLegitimizationsGetMeasureDetails *d
    126           = &result->details.ok.measures[i];
    127 
    128         json_dumpf (d->measures,
    129                     stderr,
    130                     0);
    131         if (1 != json_equal (d->measures,
    132                              ds->expected_measures))
    133         {
    134           GNUNET_break (0);
    135           json_dumpf (d->measures,
    136                       stderr,
    137                       0);
    138           TALER_TESTING_interpreter_fail (ds->is);
    139           return;
    140         }
    141       }
    142     }
    143   }
    144   TALER_TESTING_interpreter_next (ds->is);
    145 }
    146 
    147 
    148 /**
    149  * Run the command.
    150  *
    151  * @param cls closure.
    152  * @param cmd the command to execute.
    153  * @param is the interpreter state.
    154  */
    155 static void
    156 get_active_legitimization_measures_run (
    157   void *cls,
    158   const struct TALER_TESTING_Command *cmd,
    159   struct TALER_TESTING_Interpreter *is)
    160 {
    161   struct GetLegitimizationMeasuresState *ds = cls;
    162   const struct TALER_NormalizedPaytoHashP *h_payto;
    163   const struct TALER_AmlOfficerPrivateKeyP *officer_priv;
    164   const struct TALER_TESTING_Command *ref;
    165   const char *exchange_url;
    166 
    167   (void) cmd;
    168   ds->is = is;
    169   {
    170     const struct TALER_TESTING_Command *exchange_cmd;
    171 
    172     exchange_cmd = TALER_TESTING_interpreter_get_command (is,
    173                                                           "exchange");
    174     if (NULL == exchange_cmd)
    175     {
    176       GNUNET_break (0);
    177       TALER_TESTING_interpreter_fail (is);
    178       return;
    179     }
    180     GNUNET_assert (GNUNET_OK ==
    181                    TALER_TESTING_get_trait_exchange_url (exchange_cmd,
    182                                                          &exchange_url));
    183   }
    184   ref = TALER_TESTING_interpreter_lookup_command (is,
    185                                                   ds->account_ref_cmd);
    186   if (NULL == ref)
    187   {
    188     GNUNET_break (0);
    189     TALER_TESTING_interpreter_fail (is);
    190     return;
    191   }
    192   if (GNUNET_OK !=
    193       TALER_TESTING_get_trait_h_normalized_payto (ref,
    194                                                   &h_payto))
    195   {
    196     GNUNET_break (0);
    197     TALER_TESTING_interpreter_fail (is);
    198     return;
    199   }
    200   ref = TALER_TESTING_interpreter_lookup_command (is,
    201                                                   ds->officer_ref_cmd);
    202   if (NULL == ref)
    203   {
    204     GNUNET_break (0);
    205     TALER_TESTING_interpreter_fail (is);
    206     return;
    207   }
    208   if (GNUNET_OK !=
    209       TALER_TESTING_get_trait_officer_priv (ref,
    210                                             &officer_priv))
    211   {
    212     GNUNET_break (0);
    213     TALER_TESTING_interpreter_fail (is);
    214     return;
    215   }
    216   ds->h_payto = *h_payto;
    217   ds->dh = TALER_EXCHANGE_aml_legitimizations_get_create (
    218     TALER_TESTING_interpreter_get_context (is),
    219     exchange_url,
    220     officer_priv);
    221   if (NULL == ds->dh)
    222   {
    223     GNUNET_break (0);
    224     TALER_TESTING_interpreter_fail (is);
    225     return;
    226   }
    227   GNUNET_assert (GNUNET_OK ==
    228                  TALER_EXCHANGE_aml_legitimizations_get_set_options (
    229                    ds->dh,
    230                    TALER_EXCHANGE_aml_legitimizations_get_option_h_payto (
    231                      h_payto),
    232                    TALER_EXCHANGE_aml_legitimizations_get_option_active (
    233                      TALER_EXCHANGE_YNA_YES)));
    234   GNUNET_assert (
    235     TALER_EXCHANGE_AML_LEGITIMIZATIONS_GET_START_OK ==
    236     TALER_EXCHANGE_aml_legitimizations_get_start (
    237       ds->dh,
    238       &get_active_legitimization_measures_cb,
    239       ds));
    240 }
    241 
    242 
    243 /**
    244  * Free the state of a "get_aml_decision" CMD, and possibly cancel a
    245  * pending operation thereof.
    246  *
    247  * @param cls closure, must be a `struct GetLegitimizationMeasuresState`.
    248  * @param cmd the command which is being cleaned up.
    249  */
    250 static void
    251 get_active_legitimization_measures_cleanup (
    252   void *cls,
    253   const struct TALER_TESTING_Command *cmd)
    254 {
    255   struct GetLegitimizationMeasuresState *ds = cls;
    256 
    257   if (NULL != ds->dh)
    258   {
    259     TALER_TESTING_command_incomplete (ds->is,
    260                                       cmd->label);
    261     TALER_EXCHANGE_aml_legitimizations_get_cancel (ds->dh);
    262     ds->dh = NULL;
    263   }
    264   json_decref (ds->expected_measures);
    265   GNUNET_free (ds);
    266 }
    267 
    268 
    269 /**
    270  * Offer internal data of a "AML decision" CMD state to other
    271  * commands.
    272  *
    273  * @param cls closure
    274  * @param[out] ret result (could be anything)
    275  * @param trait name of the trait
    276  * @param index index number of the object to offer.
    277  * @return #GNUNET_OK on success
    278  */
    279 static enum GNUNET_GenericReturnValue
    280 get_active_legitimization_measures_traits (void *cls,
    281                                            const void **ret,
    282                                            const char *trait,
    283                                            unsigned int index)
    284 {
    285   struct GetLegitimizationMeasuresState *ws = cls;
    286   struct TALER_TESTING_Trait traits[] = {
    287     TALER_TESTING_make_trait_h_normalized_payto (&ws->h_payto),
    288     TALER_TESTING_trait_end ()
    289   };
    290 
    291   return TALER_TESTING_get_trait (traits,
    292                                   ret,
    293                                   trait,
    294                                   index);
    295 }
    296 
    297 
    298 struct TALER_TESTING_Command
    299 TALER_TESTING_cmd_get_active_legitimization_measures (
    300   const char *label,
    301   const char *ref_officer,
    302   const char *ref_operation,
    303   unsigned int expected_response,
    304   const char *expected_measures)
    305 {
    306   struct GetLegitimizationMeasuresState *ds;
    307   json_error_t err;
    308 
    309   ds = GNUNET_new (struct GetLegitimizationMeasuresState);
    310   ds->officer_ref_cmd = ref_officer;
    311   ds->account_ref_cmd = ref_operation;
    312   ds->expected_response = expected_response;
    313   if (NULL != expected_measures)
    314   {
    315     ds->expected_measures = json_loads (expected_measures,
    316                                         JSON_DECODE_ANY,
    317                                         &err);
    318     if (NULL == ds->expected_measures)
    319     {
    320       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    321                   "Invalid JSON in new rules of %s: %s\n",
    322                   label,
    323                   err.text);
    324       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    325                   "Input was: `%s'\n",
    326                   expected_measures);
    327       GNUNET_assert (0);
    328     }
    329   }
    330   {
    331     struct TALER_TESTING_Command cmd = {
    332       .cls = ds,
    333       .label = label,
    334       .run = &get_active_legitimization_measures_run,
    335       .cleanup = &get_active_legitimization_measures_cleanup,
    336       .traits = &get_active_legitimization_measures_traits
    337     };
    338 
    339     return cmd;
    340   }
    341 }
    342 
    343 
    344 /* end of testing_api_cmd_get_active_legitimization_measures.c */