Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2022 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU General Public License as
7 : published by the Free Software Foundation; either version 3, or
8 : (at your 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
13 : GNU 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_reserve_open.c
21 : * @brief Implement the /reserve/$RID/open test command.
22 : * @author Christian Grothoff
23 : */
24 : #include "taler/platform.h"
25 : #include "taler/taler_json_lib.h"
26 : #include <gnunet/gnunet_curl_lib.h>
27 : #include "taler/taler_testing_lib.h"
28 :
29 :
30 : /**
31 : * Information we track per coin used to pay for opening the
32 : * reserve.
33 : */
34 : struct CoinDetail
35 : {
36 : /**
37 : * Name of the command and index of the coin to use.
38 : */
39 : const char *name;
40 :
41 : /**
42 : * Amount to charge to this coin.
43 : */
44 : struct TALER_Amount amount;
45 : };
46 :
47 :
48 : /**
49 : * State for a "open" CMD.
50 : */
51 : struct OpenState
52 : {
53 : /**
54 : * Label to the command which created the reserve to check,
55 : * needed to resort the reserve key.
56 : */
57 : const char *reserve_reference;
58 :
59 : /**
60 : * Requested expiration time.
61 : */
62 : struct GNUNET_TIME_Relative req_expiration_time;
63 :
64 : /**
65 : * Requested minimum number of purses.
66 : */
67 : uint32_t min_purses;
68 :
69 : /**
70 : * Amount to pay for the opening from the reserve balance.
71 : */
72 : struct TALER_Amount reserve_pay;
73 :
74 : /**
75 : * Handle to the "reserve open" operation.
76 : */
77 : struct TALER_EXCHANGE_PostReservesOpenHandle *rsh;
78 :
79 : /**
80 : * Expected reserve balance.
81 : */
82 : const char *expected_balance;
83 :
84 : /**
85 : * Length of the @e cd array.
86 : */
87 : unsigned int cpl;
88 :
89 : /**
90 : * Coin details, array of length @e cpl.
91 : */
92 : struct CoinDetail *cd;
93 :
94 : /**
95 : * Private key of the reserve being analyzed.
96 : */
97 : const struct TALER_ReservePrivateKeyP *reserve_priv;
98 :
99 : /**
100 : * Public key of the reserve being analyzed.
101 : */
102 : struct TALER_ReservePublicKeyP reserve_pub;
103 :
104 : /**
105 : * Expected HTTP response code.
106 : */
107 : unsigned int expected_response_code;
108 :
109 : /**
110 : * Interpreter state.
111 : */
112 : struct TALER_TESTING_Interpreter *is;
113 : };
114 :
115 :
116 : /**
117 : * Check that the reserve balance and HTTP response code are
118 : * both acceptable.
119 : *
120 : * @param cls closure.
121 : * @param rs HTTP response details
122 : */
123 : static void
124 6 : reserve_open_cb (void *cls,
125 : const struct TALER_EXCHANGE_PostReservesOpenResponse *rs)
126 : {
127 6 : struct OpenState *ss = cls;
128 6 : struct TALER_TESTING_Interpreter *is = ss->is;
129 :
130 6 : ss->rsh = NULL;
131 6 : if (ss->expected_response_code != rs->hr.http_status)
132 : {
133 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
134 : "Unexpected HTTP response code: %d in %s:%u\n",
135 : rs->hr.http_status,
136 : __FILE__,
137 : __LINE__);
138 0 : json_dumpf (rs->hr.reply,
139 : stderr,
140 : JSON_INDENT (2));
141 0 : TALER_TESTING_interpreter_fail (ss->is);
142 0 : return;
143 : }
144 6 : if (MHD_HTTP_OK != rs->hr.http_status)
145 : {
146 2 : TALER_TESTING_interpreter_next (is);
147 2 : return;
148 : }
149 4 : TALER_TESTING_interpreter_next (is);
150 : }
151 :
152 :
153 : /**
154 : * Run the command.
155 : *
156 : * @param cls closure.
157 : * @param cmd the command being executed.
158 : * @param is the interpreter state.
159 : */
160 : static void
161 6 : open_run (void *cls,
162 : const struct TALER_TESTING_Command *cmd,
163 : struct TALER_TESTING_Interpreter *is)
164 6 : {
165 6 : struct OpenState *ss = cls;
166 : const struct TALER_TESTING_Command *create_reserve;
167 6 : struct TALER_EXCHANGE_PurseDeposit cp[GNUNET_NZL (ss->cpl)];
168 :
169 6 : ss->is = is;
170 : create_reserve
171 6 : = TALER_TESTING_interpreter_lookup_command (is,
172 : ss->reserve_reference);
173 :
174 6 : if (NULL == create_reserve)
175 : {
176 0 : GNUNET_break (0);
177 0 : TALER_TESTING_interpreter_fail (is);
178 0 : return;
179 : }
180 6 : if (GNUNET_OK !=
181 6 : TALER_TESTING_get_trait_reserve_priv (create_reserve,
182 : &ss->reserve_priv))
183 : {
184 0 : GNUNET_break (0);
185 0 : TALER_LOG_ERROR ("Failed to find reserve_priv for open query\n");
186 0 : TALER_TESTING_interpreter_fail (is);
187 0 : return;
188 : }
189 6 : GNUNET_CRYPTO_eddsa_key_get_public (&ss->reserve_priv->eddsa_priv,
190 : &ss->reserve_pub.eddsa_pub);
191 8 : for (unsigned int i = 0; i<ss->cpl; i++)
192 : {
193 2 : struct TALER_EXCHANGE_PurseDeposit *cpi = &cp[i];
194 : const struct TALER_TESTING_Command *cmdi;
195 : const struct TALER_AgeCommitmentProof *age_commitment_proof;
196 : const struct TALER_CoinSpendPrivateKeyP *coin_priv;
197 : const struct TALER_DenominationSignature *denom_sig;
198 : const struct TALER_EXCHANGE_DenomPublicKey *denom_pub;
199 : char *cref;
200 : unsigned int cidx;
201 :
202 2 : if (GNUNET_OK !=
203 2 : TALER_TESTING_parse_coin_reference (ss->cd[i].name,
204 : &cref,
205 : &cidx))
206 : {
207 0 : GNUNET_break (0);
208 0 : TALER_LOG_ERROR ("Failed to parse coin reference `%s'\n",
209 : ss->cd[i].name);
210 0 : TALER_TESTING_interpreter_fail (is);
211 0 : return;
212 : }
213 2 : cmdi = TALER_TESTING_interpreter_lookup_command (is,
214 : cref);
215 2 : GNUNET_free (cref);
216 2 : if (NULL == cmdi)
217 : {
218 0 : GNUNET_break (0);
219 0 : TALER_LOG_ERROR ("Command `%s' not found\n",
220 : ss->cd[i].name);
221 0 : TALER_TESTING_interpreter_fail (is);
222 0 : return;
223 : }
224 2 : if ( (GNUNET_OK !=
225 2 : TALER_TESTING_get_trait_age_commitment_proof (cmdi,
226 : cidx,
227 : &age_commitment_proof))
228 2 : ||
229 : (GNUNET_OK !=
230 2 : TALER_TESTING_get_trait_coin_priv (cmdi,
231 : cidx,
232 2 : &coin_priv)) ||
233 : (GNUNET_OK !=
234 2 : TALER_TESTING_get_trait_denom_sig (cmdi,
235 : cidx,
236 2 : &denom_sig)) ||
237 : (GNUNET_OK !=
238 2 : TALER_TESTING_get_trait_denom_pub (cmdi,
239 : cidx,
240 : &denom_pub)) )
241 : {
242 0 : GNUNET_break (0);
243 0 : TALER_LOG_ERROR ("Coin trait not found in `%s'\n",
244 : ss->cd[i].name);
245 0 : TALER_TESTING_interpreter_fail (is);
246 0 : return;
247 : }
248 2 : cpi->age_commitment_proof = age_commitment_proof;
249 2 : cpi->coin_priv = *coin_priv;
250 2 : cpi->denom_sig = *denom_sig;
251 2 : cpi->amount = ss->cd[i].amount;
252 2 : cpi->h_denom_pub = denom_pub->h_key;
253 : }
254 12 : ss->rsh = TALER_EXCHANGE_post_reserves_open_create (
255 : TALER_TESTING_interpreter_get_context (is),
256 : TALER_TESTING_get_exchange_url (is),
257 : TALER_TESTING_get_keys (is),
258 : ss->reserve_priv,
259 6 : &ss->reserve_pay,
260 : ss->cpl,
261 : cp,
262 : GNUNET_TIME_relative_to_timestamp (ss->req_expiration_time),
263 : ss->min_purses);
264 6 : if (NULL == ss->rsh)
265 : {
266 0 : GNUNET_break (0);
267 0 : TALER_TESTING_interpreter_fail (is);
268 0 : return;
269 : }
270 : {
271 : enum TALER_ErrorCode ec;
272 :
273 6 : ec = TALER_EXCHANGE_post_reserves_open_start (ss->rsh,
274 : &reserve_open_cb,
275 : ss);
276 6 : if (TALER_EC_NONE != ec)
277 : {
278 0 : GNUNET_break (0);
279 0 : ss->rsh = NULL;
280 0 : TALER_TESTING_interpreter_fail (is);
281 0 : return;
282 : }
283 : }
284 : }
285 :
286 :
287 : /**
288 : * Cleanup the state from a "reserve open" CMD, and possibly
289 : * cancel a pending operation thereof.
290 : *
291 : * @param cls closure.
292 : * @param cmd the command which is being cleaned up.
293 : */
294 : static void
295 6 : open_cleanup (void *cls,
296 : const struct TALER_TESTING_Command *cmd)
297 : {
298 6 : struct OpenState *ss = cls;
299 :
300 6 : if (NULL != ss->rsh)
301 : {
302 0 : TALER_TESTING_command_incomplete (ss->is,
303 : cmd->label);
304 0 : TALER_EXCHANGE_post_reserves_open_cancel (ss->rsh);
305 0 : ss->rsh = NULL;
306 : }
307 6 : GNUNET_free (ss->cd);
308 6 : GNUNET_free (ss);
309 6 : }
310 :
311 :
312 : struct TALER_TESTING_Command
313 6 : TALER_TESTING_cmd_reserve_open (const char *label,
314 : const char *reserve_reference,
315 : const char *reserve_pay,
316 : struct GNUNET_TIME_Relative expiration_time,
317 : uint32_t min_purses,
318 : unsigned int expected_response_code,
319 : ...)
320 : {
321 : struct OpenState *ss;
322 : va_list ap;
323 : const char *name;
324 : unsigned int i;
325 :
326 6 : GNUNET_assert (NULL != reserve_reference);
327 6 : ss = GNUNET_new (struct OpenState);
328 6 : ss->reserve_reference = reserve_reference;
329 6 : ss->req_expiration_time = expiration_time;
330 6 : ss->min_purses = min_purses;
331 6 : GNUNET_assert (GNUNET_OK ==
332 : TALER_string_to_amount (reserve_pay,
333 : &ss->reserve_pay));
334 6 : ss->expected_response_code = expected_response_code;
335 6 : va_start (ap,
336 : expected_response_code);
337 10 : while (NULL != (name = va_arg (ap, const char *)))
338 4 : ss->cpl++;
339 6 : va_end (ap);
340 6 : GNUNET_assert (0 == (ss->cpl % 2));
341 6 : ss->cpl /= 2; /* name and amount per coin */
342 6 : ss->cd = GNUNET_new_array (ss->cpl,
343 : struct CoinDetail);
344 6 : i = 0;
345 6 : va_start (ap,
346 : expected_response_code);
347 8 : while (NULL != (name = va_arg (ap, const char *)))
348 : {
349 2 : struct CoinDetail *cd = &ss->cd[i];
350 2 : cd->name = name;
351 2 : GNUNET_assert (GNUNET_OK ==
352 : TALER_string_to_amount (va_arg (ap,
353 : const char *),
354 : &cd->amount));
355 2 : i++;
356 : }
357 6 : va_end (ap);
358 : {
359 6 : struct TALER_TESTING_Command cmd = {
360 : .cls = ss,
361 : .label = label,
362 : .run = &open_run,
363 : .cleanup = &open_cleanup
364 : };
365 :
366 6 : return cmd;
367 : }
368 : }
|