Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2021 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 : /**
21 : * @file testing/testing_api_cmd_kyc_wallet_get.c
22 : * @brief Implement the testing CMDs for the /kyc_wallet/ GET operations.
23 : * @author Christian Grothoff
24 : */
25 : #include "platform.h"
26 : #include "taler_json_lib.h"
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include "taler_testing_lib.h"
29 :
30 : /**
31 : * State for a "/kyc-wallet" GET CMD.
32 : */
33 : struct KycWalletGetState
34 : {
35 :
36 : /**
37 : * Private key of the reserve (account).
38 : */
39 : struct TALER_ReservePrivateKeyP reserve_priv;
40 :
41 : /**
42 : * Public key of the reserve (account).
43 : */
44 : struct TALER_ReservePublicKeyP reserve_pub;
45 :
46 : /**
47 : * Payto URI of the reserve of the wallet.
48 : */
49 : char *reserve_payto_uri;
50 :
51 : /**
52 : * Command to get a reserve private key from.
53 : */
54 : const char *reserve_reference;
55 :
56 : /**
57 : * Expected HTTP response code.
58 : */
59 : unsigned int expected_response_code;
60 :
61 : /**
62 : * Set to the KYC requirement payto hash *if* the exchange replied with a
63 : * request for KYC (#MHD_HTTP_OK).
64 : */
65 : struct TALER_PaytoHashP h_payto;
66 :
67 : /**
68 : * Set to the KYC requirement row *if* the exchange replied with
69 : * a request for KYC (#MHD_HTTP_OK).
70 : */
71 : uint64_t requirement_row;
72 :
73 : /**
74 : * Handle to the "track transaction" pending operation.
75 : */
76 : struct TALER_EXCHANGE_KycWalletHandle *kwh;
77 :
78 : /**
79 : * Balance to pass to the exchange.
80 : */
81 : struct TALER_Amount balance;
82 :
83 : /**
84 : * Interpreter state.
85 : */
86 : struct TALER_TESTING_Interpreter *is;
87 : };
88 :
89 :
90 : /**
91 : * Handle response to the command.
92 : *
93 : * @param cls closure.
94 : * @param wkr GET deposit response details
95 : */
96 : static void
97 0 : wallet_kyc_cb (void *cls,
98 : const struct TALER_EXCHANGE_WalletKycResponse *wkr)
99 : {
100 0 : struct KycWalletGetState *kwg = cls;
101 0 : struct TALER_TESTING_Interpreter *is = kwg->is;
102 0 : struct TALER_TESTING_Command *cmd = &is->commands[is->ip];
103 :
104 0 : kwg->kwh = NULL;
105 0 : if (kwg->expected_response_code != wkr->http_status)
106 : {
107 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
108 : "Unexpected response code %u/%d (wanted %u) to command %s in %s:%u\n",
109 : wkr->http_status,
110 : (int) wkr->ec,
111 : kwg->expected_response_code,
112 : cmd->label,
113 : __FILE__,
114 : __LINE__);
115 0 : TALER_TESTING_interpreter_fail (is);
116 0 : return;
117 : }
118 0 : switch (wkr->http_status)
119 : {
120 0 : case MHD_HTTP_NO_CONTENT:
121 0 : break;
122 0 : case MHD_HTTP_FORBIDDEN:
123 0 : GNUNET_break (0);
124 0 : TALER_TESTING_interpreter_fail (is);
125 0 : return;
126 0 : case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
127 : kwg->requirement_row
128 0 : = wkr->details.unavailable_for_legal_reasons.requirement_row;
129 : kwg->h_payto
130 0 : = wkr->details.unavailable_for_legal_reasons.h_payto;
131 0 : break;
132 0 : default:
133 0 : GNUNET_break (0);
134 0 : break;
135 : }
136 0 : TALER_TESTING_interpreter_next (kwg->is);
137 : }
138 :
139 :
140 : /**
141 : * Run the command.
142 : *
143 : * @param cls closure.
144 : * @param cmd the command to execute.
145 : * @param is the interpreter state.
146 : */
147 : static void
148 0 : wallet_kyc_run (void *cls,
149 : const struct TALER_TESTING_Command *cmd,
150 : struct TALER_TESTING_Interpreter *is)
151 : {
152 0 : struct KycWalletGetState *kwg = cls;
153 :
154 : (void) cmd;
155 0 : kwg->is = is;
156 0 : if (NULL != kwg->reserve_reference)
157 : {
158 : const struct TALER_TESTING_Command *res_cmd;
159 : const struct TALER_ReservePrivateKeyP *reserve_priv;
160 :
161 0 : res_cmd = TALER_TESTING_interpreter_lookup_command (kwg->is,
162 : kwg->reserve_reference);
163 0 : if (NULL == res_cmd)
164 : {
165 0 : GNUNET_break (0);
166 0 : TALER_TESTING_interpreter_fail (kwg->is);
167 0 : return;
168 : }
169 :
170 0 : if (GNUNET_OK !=
171 0 : TALER_TESTING_get_trait_reserve_priv (res_cmd,
172 : &reserve_priv))
173 : {
174 0 : GNUNET_break (0);
175 0 : TALER_TESTING_interpreter_fail (kwg->is);
176 0 : return;
177 : }
178 0 : kwg->reserve_priv = *reserve_priv;
179 : }
180 : else
181 : {
182 0 : GNUNET_CRYPTO_eddsa_key_create (&kwg->reserve_priv.eddsa_priv);
183 : }
184 0 : GNUNET_CRYPTO_eddsa_key_get_public (&kwg->reserve_priv.eddsa_priv,
185 : &kwg->reserve_pub.eddsa_pub);
186 : kwg->reserve_payto_uri
187 0 : = TALER_reserve_make_payto (TALER_EXCHANGE_get_base_url (is->exchange),
188 0 : &kwg->reserve_pub);
189 0 : kwg->kwh = TALER_EXCHANGE_kyc_wallet (is->exchange,
190 0 : &kwg->reserve_priv,
191 0 : &kwg->balance,
192 : &wallet_kyc_cb,
193 : kwg);
194 0 : GNUNET_assert (NULL != kwg->kwh);
195 : }
196 :
197 :
198 : /**
199 : * Cleanup the state from a "track transaction" CMD, and possibly
200 : * cancel a operation thereof.
201 : *
202 : * @param cls closure with our `struct KycWalletGetState`
203 : * @param cmd the command which is being cleaned up.
204 : */
205 : static void
206 0 : wallet_kyc_cleanup (void *cls,
207 : const struct TALER_TESTING_Command *cmd)
208 : {
209 0 : struct KycWalletGetState *kwg = cls;
210 :
211 0 : if (NULL != kwg->kwh)
212 : {
213 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
214 : "Command %u (%s) did not complete\n",
215 : kwg->is->ip,
216 : cmd->label);
217 0 : TALER_EXCHANGE_kyc_wallet_cancel (kwg->kwh);
218 0 : kwg->kwh = NULL;
219 : }
220 0 : GNUNET_free (kwg->reserve_payto_uri);
221 0 : GNUNET_free (kwg);
222 0 : }
223 :
224 :
225 : /**
226 : * Offer internal data from a "wallet KYC" CMD.
227 : *
228 : * @param cls closure.
229 : * @param[out] ret result (could be anything).
230 : * @param trait name of the trait.
231 : * @param index index number of the object to offer.
232 : * @return #GNUNET_OK on success.
233 : */
234 : static enum GNUNET_GenericReturnValue
235 0 : wallet_kyc_traits (void *cls,
236 : const void **ret,
237 : const char *trait,
238 : unsigned int index)
239 : {
240 0 : struct KycWalletGetState *kwg = cls;
241 : struct TALER_TESTING_Trait traits[] = {
242 0 : TALER_TESTING_make_trait_reserve_priv (&kwg->reserve_priv),
243 0 : TALER_TESTING_make_trait_reserve_pub (&kwg->reserve_pub),
244 0 : TALER_TESTING_make_trait_legi_requirement_row (&kwg->requirement_row),
245 0 : TALER_TESTING_make_trait_h_payto (&kwg->h_payto),
246 0 : TALER_TESTING_make_trait_payto_uri (
247 0 : (const char **) &kwg->reserve_payto_uri),
248 0 : TALER_TESTING_trait_end ()
249 : };
250 :
251 0 : return TALER_TESTING_get_trait (traits,
252 : ret,
253 : trait,
254 : index);
255 : }
256 :
257 :
258 : struct TALER_TESTING_Command
259 0 : TALER_TESTING_cmd_wallet_kyc_get (const char *label,
260 : const char *reserve_reference,
261 : const char *threshold_balance,
262 : unsigned int expected_response_code)
263 : {
264 : struct KycWalletGetState *kwg;
265 :
266 0 : kwg = GNUNET_new (struct KycWalletGetState);
267 0 : kwg->reserve_reference = reserve_reference;
268 0 : kwg->expected_response_code = expected_response_code;
269 0 : GNUNET_assert (GNUNET_OK ==
270 : TALER_string_to_amount (threshold_balance,
271 : &kwg->balance));
272 : {
273 0 : struct TALER_TESTING_Command cmd = {
274 : .cls = kwg,
275 : .label = label,
276 : .run = &wallet_kyc_run,
277 : .cleanup = &wallet_kyc_cleanup,
278 : .traits = &wallet_kyc_traits
279 : };
280 :
281 0 : return cmd;
282 : }
283 : }
284 :
285 :
286 : /* end of testing_api_cmd_kyc_wallet_get.c */
|