Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2018-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_bank_check.c
21 : * @brief command to check if a particular wire transfer took
22 : * place.
23 : * @author Marcello Stanisci
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 : #include "taler_fakebank_lib.h"
30 :
31 :
32 : /**
33 : * State for a "bank check" CMD.
34 : */
35 : struct BankCheckState
36 : {
37 :
38 : /**
39 : * Base URL of the exchange supposed to be
40 : * involved in the bank transaction.
41 : */
42 : const char *exchange_base_url;
43 :
44 : /**
45 : * Expected transferred amount.
46 : */
47 : const char *amount;
48 :
49 : /**
50 : * Expected debit bank account.
51 : */
52 : const char *debit_payto;
53 :
54 : /**
55 : * Expected credit bank account.
56 : */
57 : const char *credit_payto;
58 :
59 : /**
60 : * Binary form of the wire transfer subject.
61 : */
62 : struct TALER_WireTransferIdentifierRawP wtid;
63 :
64 : /**
65 : * Interpreter state.
66 : */
67 : struct TALER_TESTING_Interpreter *is;
68 :
69 : /**
70 : * Reference to a CMD that provides all the data
71 : * needed to issue the bank check. If NULL, that data
72 : * must exist here in the state.
73 : */
74 : const char *deposit_reference;
75 : };
76 :
77 :
78 : /**
79 : * Run the command.
80 : *
81 : * @param cls closure.
82 : * @param cmd the command to execute.
83 : * @param is the interpreter state.
84 : */
85 : static void
86 0 : check_bank_transfer_run (void *cls,
87 : const struct TALER_TESTING_Command *cmd,
88 : struct TALER_TESTING_Interpreter *is)
89 : {
90 0 : struct BankCheckState *bcs = cls;
91 : struct TALER_Amount amount;
92 : char *debit_account;
93 : char *credit_account;
94 : const char **exchange_base_url;
95 : const char **debit_payto;
96 : const char **credit_payto;
97 :
98 : (void) cmd;
99 0 : if (NULL == bcs->deposit_reference)
100 : {
101 0 : TALER_LOG_INFO ("Deposit reference NOT given\n");
102 0 : debit_payto = &bcs->debit_payto;
103 0 : credit_payto = &bcs->credit_payto;
104 0 : exchange_base_url = &bcs->exchange_base_url;
105 :
106 0 : if (GNUNET_OK !=
107 0 : TALER_string_to_amount (bcs->amount,
108 : &amount))
109 : {
110 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111 : "Failed to parse amount `%s' at %u\n",
112 : bcs->amount,
113 : is->ip);
114 0 : TALER_TESTING_interpreter_fail (is);
115 0 : return;
116 : }
117 : }
118 : else
119 : {
120 : const struct TALER_TESTING_Command *deposit_cmd;
121 : const struct TALER_Amount *amount_ptr;
122 :
123 0 : TALER_LOG_INFO ("`%s' uses reference (%s/%p)\n",
124 : TALER_TESTING_interpreter_get_current_label
125 : (is),
126 : bcs->deposit_reference,
127 : bcs->deposit_reference);
128 : deposit_cmd
129 0 : = TALER_TESTING_interpreter_lookup_command (is,
130 : bcs->deposit_reference);
131 0 : if (NULL == deposit_cmd)
132 0 : TALER_TESTING_FAIL (is);
133 0 : if ( (GNUNET_OK !=
134 0 : TALER_TESTING_get_trait_amount (deposit_cmd,
135 0 : &amount_ptr)) ||
136 : (GNUNET_OK !=
137 0 : TALER_TESTING_get_trait_debit_payto_uri (deposit_cmd,
138 0 : &debit_payto)) ||
139 : (GNUNET_OK !=
140 0 : TALER_TESTING_get_trait_credit_payto_uri (deposit_cmd,
141 0 : &credit_payto)) ||
142 : (GNUNET_OK !=
143 0 : TALER_TESTING_get_trait_exchange_url (deposit_cmd,
144 : &exchange_base_url)) )
145 0 : TALER_TESTING_FAIL (is);
146 0 : amount = *amount_ptr;
147 : }
148 :
149 :
150 0 : debit_account = TALER_xtalerbank_account_from_payto (*debit_payto);
151 0 : credit_account = TALER_xtalerbank_account_from_payto (*credit_payto);
152 :
153 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
154 : "converted debit_payto (%s) to debit_account (%s)\n",
155 : *debit_payto,
156 : debit_account);
157 :
158 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
159 : "converted credit_payto (%s) to credit_account (%s)\n",
160 : *credit_payto,
161 : credit_account);
162 :
163 0 : if (GNUNET_OK !=
164 0 : TALER_FAKEBANK_check_debit (is->fakebank,
165 : &amount,
166 : debit_account,
167 : credit_account,
168 : *exchange_base_url,
169 : &bcs->wtid))
170 : {
171 0 : GNUNET_break (0);
172 0 : GNUNET_free (credit_account);
173 0 : GNUNET_free (debit_account);
174 0 : TALER_TESTING_interpreter_fail (is);
175 0 : return;
176 : }
177 0 : GNUNET_free (credit_account);
178 0 : GNUNET_free (debit_account);
179 0 : TALER_TESTING_interpreter_next (is);
180 : }
181 :
182 :
183 : /**
184 : * Free the state of a "bank check" CMD.
185 : *
186 : * @param cls closure.
187 : * @param cmd the command which is being cleaned up.
188 : */
189 : static void
190 0 : check_bank_transfer_cleanup (void *cls,
191 : const struct TALER_TESTING_Command *cmd)
192 : {
193 0 : struct BankCheckState *bcs = cls;
194 :
195 : (void) cmd;
196 0 : GNUNET_free (bcs);
197 0 : }
198 :
199 :
200 : /**
201 : * Offer internal data from a "bank check" CMD state.
202 : *
203 : * @param cls closure.
204 : * @param[out] ret result.
205 : * @param trait name of the trait.
206 : * @param index index number of the object to offer.
207 : * @return #GNUNET_OK on success.
208 : */
209 : static enum GNUNET_GenericReturnValue
210 0 : check_bank_transfer_traits (void *cls,
211 : const void **ret,
212 : const char *trait,
213 : unsigned int index)
214 : {
215 0 : struct BankCheckState *bcs = cls;
216 0 : struct TALER_WireTransferIdentifierRawP *wtid_ptr = &bcs->wtid;
217 : struct TALER_TESTING_Trait traits[] = {
218 0 : TALER_TESTING_make_trait_wtid (wtid_ptr),
219 0 : TALER_TESTING_make_trait_exchange_url (
220 : &bcs->exchange_base_url),
221 0 : TALER_TESTING_trait_end ()
222 : };
223 :
224 0 : return TALER_TESTING_get_trait (traits,
225 : ret,
226 : trait,
227 : index);
228 : }
229 :
230 :
231 : struct TALER_TESTING_Command
232 0 : TALER_TESTING_cmd_check_bank_transfer (const char *label,
233 : const char *exchange_base_url,
234 : const char *amount,
235 : const char *debit_payto,
236 : const char *credit_payto)
237 : {
238 : struct BankCheckState *bcs;
239 :
240 0 : bcs = GNUNET_new (struct BankCheckState);
241 0 : bcs->exchange_base_url = exchange_base_url;
242 0 : bcs->amount = amount;
243 0 : bcs->debit_payto = debit_payto;
244 0 : bcs->credit_payto = credit_payto;
245 0 : bcs->deposit_reference = NULL;
246 : {
247 0 : struct TALER_TESTING_Command cmd = {
248 : .label = label,
249 : .cls = bcs,
250 : .run = &check_bank_transfer_run,
251 : .cleanup = &check_bank_transfer_cleanup,
252 : .traits = &check_bank_transfer_traits
253 : };
254 :
255 0 : return cmd;
256 : }
257 : }
258 :
259 :
260 : struct TALER_TESTING_Command
261 0 : TALER_TESTING_cmd_check_bank_transfer_with_ref (
262 : const char *label,
263 : const char *deposit_reference)
264 : {
265 : struct BankCheckState *bcs;
266 :
267 0 : bcs = GNUNET_new (struct BankCheckState);
268 0 : bcs->deposit_reference = deposit_reference;
269 : {
270 0 : struct TALER_TESTING_Command cmd = {
271 : .label = label,
272 : .cls = bcs,
273 : .run = &check_bank_transfer_run,
274 : .cleanup = &check_bank_transfer_cleanup,
275 : .traits = &check_bank_transfer_traits
276 : };
277 :
278 0 : return cmd;
279 : }
280 : }
|