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 (9575B)


      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_GET_AML_LEGITIMIZATIONS_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_GetAmlLegitimizationsHandle *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_GET_AML_LEGITIMIZATIONS_RESULT_CLOSURE *ds,
     92   const struct TALER_EXCHANGE_GetAmlLegitimizationsResponse *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_GetAmlLegitimizationsMeasureDetails *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_get_aml_legitimizations_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_get_aml_legitimizations_set_options (
    229                    ds->dh,
    230                    TALER_EXCHANGE_get_aml_legitimizations_option_filter_h_payto
    231                    (
    232                      h_payto),
    233                    TALER_EXCHANGE_get_aml_legitimizations_option_filter_active (
    234                      TALER_EXCHANGE_YNA_YES)));
    235   GNUNET_assert (
    236     TALER_EC_NONE ==
    237     TALER_EXCHANGE_get_aml_legitimizations_start (
    238       ds->dh,
    239       &get_active_legitimization_measures_cb,
    240       ds));
    241 }
    242 
    243 
    244 /**
    245  * Free the state of a "get_aml_decision" CMD, and possibly cancel a
    246  * pending operation thereof.
    247  *
    248  * @param cls closure, must be a `struct GetLegitimizationMeasuresState`.
    249  * @param cmd the command which is being cleaned up.
    250  */
    251 static void
    252 get_active_legitimization_measures_cleanup (
    253   void *cls,
    254   const struct TALER_TESTING_Command *cmd)
    255 {
    256   struct GetLegitimizationMeasuresState *ds = cls;
    257 
    258   if (NULL != ds->dh)
    259   {
    260     TALER_TESTING_command_incomplete (ds->is,
    261                                       cmd->label);
    262     TALER_EXCHANGE_get_aml_legitimizations_cancel (ds->dh);
    263     ds->dh = NULL;
    264   }
    265   json_decref (ds->expected_measures);
    266   GNUNET_free (ds);
    267 }
    268 
    269 
    270 /**
    271  * Offer internal data of a "AML decision" CMD state to other
    272  * commands.
    273  *
    274  * @param cls closure
    275  * @param[out] ret result (could be anything)
    276  * @param trait name of the trait
    277  * @param index index number of the object to offer.
    278  * @return #GNUNET_OK on success
    279  */
    280 static enum GNUNET_GenericReturnValue
    281 get_active_legitimization_measures_traits (void *cls,
    282                                            const void **ret,
    283                                            const char *trait,
    284                                            unsigned int index)
    285 {
    286   struct GetLegitimizationMeasuresState *ws = cls;
    287   struct TALER_TESTING_Trait traits[] = {
    288     TALER_TESTING_make_trait_h_normalized_payto (&ws->h_payto),
    289     TALER_TESTING_trait_end ()
    290   };
    291 
    292   return TALER_TESTING_get_trait (traits,
    293                                   ret,
    294                                   trait,
    295                                   index);
    296 }
    297 
    298 
    299 struct TALER_TESTING_Command
    300 TALER_TESTING_cmd_get_active_legitimization_measures (
    301   const char *label,
    302   const char *ref_officer,
    303   const char *ref_operation,
    304   unsigned int expected_response,
    305   const char *expected_measures)
    306 {
    307   struct GetLegitimizationMeasuresState *ds;
    308   json_error_t err;
    309 
    310   ds = GNUNET_new (struct GetLegitimizationMeasuresState);
    311   ds->officer_ref_cmd = ref_officer;
    312   ds->account_ref_cmd = ref_operation;
    313   ds->expected_response = expected_response;
    314   if (NULL != expected_measures)
    315   {
    316     ds->expected_measures = json_loads (expected_measures,
    317                                         JSON_DECODE_ANY,
    318                                         &err);
    319     if (NULL == ds->expected_measures)
    320     {
    321       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    322                   "Invalid JSON in new rules of %s: %s\n",
    323                   label,
    324                   err.text);
    325       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    326                   "Input was: `%s'\n",
    327                   expected_measures);
    328       GNUNET_assert (0);
    329     }
    330   }
    331   {
    332     struct TALER_TESTING_Command cmd = {
    333       .cls = ds,
    334       .label = label,
    335       .run = &get_active_legitimization_measures_run,
    336       .cleanup = &get_active_legitimization_measures_cleanup,
    337       .traits = &get_active_legitimization_measures_traits
    338     };
    339 
    340     return cmd;
    341   }
    342 }
    343 
    344 
    345 /* end of testing_api_cmd_get_active_legitimization_measures.c */