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