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