LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_active_legitimization_measures.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 53.8 % 91 49
Test Date: 2025-12-26 23:00:34 Functions: 80.0 % 5 4

            Line data    Source code
       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            1 : get_active_legitimization_measures_cb (
      91              :   TALER_EXCHANGE__AML_LEGITIMIZATIONS_GET_RESULT_CLOSURE *ds,
      92              :   const struct TALER_EXCHANGE_AmlLegitimizationsGetResult *result)
      93              : {
      94            1 :   const struct TALER_EXCHANGE_HttpResponse *hr = &result->hr;
      95              : 
      96            1 :   ds->dh = NULL;
      97            1 :   if (ds->expected_response != hr->http_status)
      98              :   {
      99            0 :     TALER_TESTING_unexpected_status (ds->is,
     100              :                                      hr->http_status,
     101              :                                      ds->expected_response);
     102            0 :     return;
     103              :   }
     104            1 :   if (MHD_HTTP_OK == hr->http_status)
     105              :   {
     106            1 :     if (NULL == ds->expected_measures)
     107              :     {
     108            0 :       if (0 != result->details.ok.measures_length)
     109              :       {
     110            0 :         GNUNET_break (0);
     111            0 :         TALER_TESTING_interpreter_fail (ds->is);
     112            0 :         return;
     113              :       }
     114              :     }
     115              :     else
     116              :     {
     117            1 :       if (1 != result->details.ok.measures_length)
     118              :       {
     119            0 :         GNUNET_break (0);
     120            0 :         TALER_TESTING_interpreter_fail (ds->is);
     121            0 :         return;
     122              :       }
     123            2 :       for (size_t i = 0; i<result->details.ok.measures_length; i++)
     124              :       {
     125            1 :         const struct TALER_EXCHANGE_AmlLegitimizationsGetMeasureDetails *d
     126            1 :           = &result->details.ok.measures[i];
     127              : 
     128            1 :         json_dumpf (d->measures,
     129              :                     stderr,
     130              :                     0);
     131            1 :         if (1 != json_equal (d->measures,
     132            1 :                              ds->expected_measures))
     133              :         {
     134            0 :           GNUNET_break (0);
     135            0 :           json_dumpf (d->measures,
     136              :                       stderr,
     137              :                       0);
     138            0 :           TALER_TESTING_interpreter_fail (ds->is);
     139            0 :           return;
     140              :         }
     141              :       }
     142              :     }
     143              :   }
     144            1 :   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            1 : get_active_legitimization_measures_run (
     157              :   void *cls,
     158              :   const struct TALER_TESTING_Command *cmd,
     159              :   struct TALER_TESTING_Interpreter *is)
     160              : {
     161            1 :   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            1 :   ds->is = is;
     169              :   {
     170              :     const struct TALER_TESTING_Command *exchange_cmd;
     171              : 
     172            1 :     exchange_cmd = TALER_TESTING_interpreter_get_command (is,
     173              :                                                           "exchange");
     174            1 :     if (NULL == exchange_cmd)
     175              :     {
     176            0 :       GNUNET_break (0);
     177            0 :       TALER_TESTING_interpreter_fail (is);
     178            0 :       return;
     179              :     }
     180            1 :     GNUNET_assert (GNUNET_OK ==
     181              :                    TALER_TESTING_get_trait_exchange_url (exchange_cmd,
     182              :                                                          &exchange_url));
     183              :   }
     184            1 :   ref = TALER_TESTING_interpreter_lookup_command (is,
     185              :                                                   ds->account_ref_cmd);
     186            1 :   if (NULL == ref)
     187              :   {
     188            0 :     GNUNET_break (0);
     189            0 :     TALER_TESTING_interpreter_fail (is);
     190            0 :     return;
     191              :   }
     192            1 :   if (GNUNET_OK !=
     193            1 :       TALER_TESTING_get_trait_h_normalized_payto (ref,
     194              :                                                   &h_payto))
     195              :   {
     196            0 :     GNUNET_break (0);
     197            0 :     TALER_TESTING_interpreter_fail (is);
     198            0 :     return;
     199              :   }
     200            1 :   ref = TALER_TESTING_interpreter_lookup_command (is,
     201              :                                                   ds->officer_ref_cmd);
     202            1 :   if (NULL == ref)
     203              :   {
     204            0 :     GNUNET_break (0);
     205            0 :     TALER_TESTING_interpreter_fail (is);
     206            0 :     return;
     207              :   }
     208            1 :   if (GNUNET_OK !=
     209            1 :       TALER_TESTING_get_trait_officer_priv (ref,
     210              :                                             &officer_priv))
     211              :   {
     212            0 :     GNUNET_break (0);
     213            0 :     TALER_TESTING_interpreter_fail (is);
     214            0 :     return;
     215              :   }
     216            1 :   ds->h_payto = *h_payto;
     217            1 :   ds->dh = TALER_EXCHANGE_aml_legitimizations_get_create (
     218              :     TALER_TESTING_interpreter_get_context (is),
     219              :     exchange_url,
     220              :     officer_priv);
     221            1 :   if (NULL == ds->dh)
     222              :   {
     223            0 :     GNUNET_break (0);
     224            0 :     TALER_TESTING_interpreter_fail (is);
     225            0 :     return;
     226              :   }
     227            1 :   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            1 :   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            1 : get_active_legitimization_measures_cleanup (
     252              :   void *cls,
     253              :   const struct TALER_TESTING_Command *cmd)
     254              : {
     255            1 :   struct GetLegitimizationMeasuresState *ds = cls;
     256              : 
     257            1 :   if (NULL != ds->dh)
     258              :   {
     259            0 :     TALER_TESTING_command_incomplete (ds->is,
     260              :                                       cmd->label);
     261            0 :     TALER_EXCHANGE_aml_legitimizations_get_cancel (ds->dh);
     262            0 :     ds->dh = NULL;
     263              :   }
     264            1 :   json_decref (ds->expected_measures);
     265            1 :   GNUNET_free (ds);
     266            1 : }
     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            0 : get_active_legitimization_measures_traits (void *cls,
     281              :                                            const void **ret,
     282              :                                            const char *trait,
     283              :                                            unsigned int index)
     284              : {
     285            0 :   struct GetLegitimizationMeasuresState *ws = cls;
     286              :   struct TALER_TESTING_Trait traits[] = {
     287            0 :     TALER_TESTING_make_trait_h_normalized_payto (&ws->h_payto),
     288            0 :     TALER_TESTING_trait_end ()
     289              :   };
     290              : 
     291            0 :   return TALER_TESTING_get_trait (traits,
     292              :                                   ret,
     293              :                                   trait,
     294              :                                   index);
     295              : }
     296              : 
     297              : 
     298              : struct TALER_TESTING_Command
     299            1 : 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            1 :   ds = GNUNET_new (struct GetLegitimizationMeasuresState);
     310            1 :   ds->officer_ref_cmd = ref_officer;
     311            1 :   ds->account_ref_cmd = ref_operation;
     312            1 :   ds->expected_response = expected_response;
     313            1 :   if (NULL != expected_measures)
     314              :   {
     315            1 :     ds->expected_measures = json_loads (expected_measures,
     316              :                                         JSON_DECODE_ANY,
     317              :                                         &err);
     318            1 :     if (NULL == ds->expected_measures)
     319              :     {
     320            0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     321              :                   "Invalid JSON in new rules of %s: %s\n",
     322              :                   label,
     323              :                   err.text);
     324            0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     325              :                   "Input was: `%s'\n",
     326              :                   expected_measures);
     327            0 :       GNUNET_assert (0);
     328              :     }
     329              :   }
     330              :   {
     331            1 :     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            1 :     return cmd;
     340              :   }
     341              : }
     342              : 
     343              : 
     344              : /* end of testing_api_cmd_get_active_legitimization_measures.c */
        

Generated by: LCOV version 2.0-1