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

          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 :   GNUNET_assert (GNUNET_OK ==
     222             :                  TALER_EXCHANGE_aml_legitimizations_get_set_options (
     223             :                    ds->dh,
     224             :                    TALER_EXCHANGE_aml_legitimizations_get_option_h_payto (
     225             :                      h_payto),
     226             :                    TALER_EXCHANGE_aml_legitimizations_get_option_active (
     227             :                      TALER_EXCHANGE_YNA_YES)));
     228           1 :   GNUNET_assert (
     229             :     TALER_EXCHANGE_AML_LEGITIMIZATIONS_GET_START_OK ==
     230             :     TALER_EXCHANGE_aml_legitimizations_get_start (
     231             :       ds->dh,
     232             :       &get_active_legitimization_measures_cb,
     233             :       ds));
     234             : 
     235           1 :   if (NULL == ds->dh)
     236             :   {
     237           0 :     GNUNET_break (0);
     238           0 :     TALER_TESTING_interpreter_fail (is);
     239           0 :     return;
     240             :   }
     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           1 : get_active_legitimization_measures_cleanup (
     253             :   void *cls,
     254             :   const struct TALER_TESTING_Command *cmd)
     255             : {
     256           1 :   struct GetLegitimizationMeasuresState *ds = cls;
     257             : 
     258           1 :   if (NULL != ds->dh)
     259             :   {
     260           0 :     TALER_TESTING_command_incomplete (ds->is,
     261             :                                       cmd->label);
     262           0 :     TALER_EXCHANGE_aml_legitimizations_get_cancel (ds->dh);
     263           0 :     ds->dh = NULL;
     264             :   }
     265           1 :   json_decref (ds->expected_measures);
     266           1 :   GNUNET_free (ds);
     267           1 : }
     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           0 : get_active_legitimization_measures_traits (void *cls,
     282             :                                            const void **ret,
     283             :                                            const char *trait,
     284             :                                            unsigned int index)
     285             : {
     286           0 :   struct GetLegitimizationMeasuresState *ws = cls;
     287             :   struct TALER_TESTING_Trait traits[] = {
     288           0 :     TALER_TESTING_make_trait_h_normalized_payto (&ws->h_payto),
     289           0 :     TALER_TESTING_trait_end ()
     290             :   };
     291             : 
     292           0 :   return TALER_TESTING_get_trait (traits,
     293             :                                   ret,
     294             :                                   trait,
     295             :                                   index);
     296             : }
     297             : 
     298             : 
     299             : struct TALER_TESTING_Command
     300           1 : 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           1 :   ds = GNUNET_new (struct GetLegitimizationMeasuresState);
     311           1 :   ds->officer_ref_cmd = ref_officer;
     312           1 :   ds->account_ref_cmd = ref_operation;
     313           1 :   ds->expected_response = expected_response;
     314           1 :   if (NULL != expected_measures)
     315             :   {
     316           1 :     ds->expected_measures = json_loads (expected_measures,
     317             :                                         JSON_DECODE_ANY,
     318             :                                         &err);
     319           1 :     if (NULL == ds->expected_measures)
     320             :     {
     321           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     322             :                   "Invalid JSON in new rules of %s: %s\n",
     323             :                   label,
     324             :                   err.text);
     325           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     326             :                   "Input was: `%s'\n",
     327             :                   expected_measures);
     328           0 :       GNUNET_assert (0);
     329             :     }
     330             :   }
     331             :   {
     332           1 :     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           1 :     return cmd;
     341             :   }
     342             : }
     343             : 
     344             : 
     345             : /* end of testing_api_cmd_get_active_legitimization_measures.c */

Generated by: LCOV version 1.16