Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020-2024 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_wire_add.c
21 : * @brief command for testing POST to /management/wire
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 : #include "taler/taler_signatures.h"
29 : #include "taler/backoff.h"
30 :
31 :
32 : /**
33 : * State for a "wire_add" CMD.
34 : */
35 : struct WireAddState
36 : {
37 :
38 : /**
39 : * Wire enable handle while operation is running.
40 : */
41 : struct TALER_EXCHANGE_PostManagementWireHandle *dh;
42 :
43 : /**
44 : * Our interpreter.
45 : */
46 : struct TALER_TESTING_Interpreter *is;
47 :
48 : /**
49 : * Account to add.
50 : */
51 : struct TALER_FullPayto payto_uri;
52 :
53 : /**
54 : * Expected HTTP response code.
55 : */
56 : unsigned int expected_response_code;
57 :
58 : /**
59 : * Should we make the request with a bad master_sig signature?
60 : */
61 : bool bad_sig;
62 : };
63 :
64 :
65 : /**
66 : * Callback to analyze the /management/wire response, just used to check
67 : * if the response code is acceptable.
68 : *
69 : * @param cls closure.
70 : * @param wer response details
71 : */
72 : static void
73 8 : wire_add_cb (void *cls,
74 : const struct TALER_EXCHANGE_PostManagementWireResponse *wer)
75 : {
76 8 : struct WireAddState *ds = cls;
77 8 : const struct TALER_EXCHANGE_HttpResponse *hr = &wer->hr;
78 :
79 8 : ds->dh = NULL;
80 8 : if (ds->expected_response_code != hr->http_status)
81 : {
82 0 : TALER_TESTING_unexpected_status (ds->is,
83 : hr->http_status,
84 : ds->expected_response_code);
85 0 : return;
86 : }
87 8 : TALER_TESTING_interpreter_next (ds->is);
88 : }
89 :
90 :
91 : /**
92 : * Run the command.
93 : *
94 : * @param cls closure.
95 : * @param cmd the command to execute.
96 : * @param is the interpreter state.
97 : */
98 : static void
99 8 : wire_add_run (void *cls,
100 : const struct TALER_TESTING_Command *cmd,
101 : struct TALER_TESTING_Interpreter *is)
102 : {
103 8 : struct WireAddState *ds = cls;
104 : struct TALER_MasterSignatureP master_sig1;
105 : struct TALER_MasterSignatureP master_sig2;
106 : struct GNUNET_TIME_Timestamp now;
107 : json_t *credit_rest;
108 : json_t *debit_rest;
109 : const char *exchange_url;
110 :
111 : (void) cmd;
112 : {
113 : const struct TALER_TESTING_Command *exchange_cmd;
114 :
115 8 : exchange_cmd = TALER_TESTING_interpreter_get_command (is,
116 : "exchange");
117 8 : if (NULL == exchange_cmd)
118 : {
119 0 : GNUNET_break (0);
120 0 : TALER_TESTING_interpreter_fail (is);
121 0 : return;
122 : }
123 8 : GNUNET_assert (GNUNET_OK ==
124 : TALER_TESTING_get_trait_exchange_url (exchange_cmd,
125 : &exchange_url));
126 : }
127 8 : now = GNUNET_TIME_timestamp_get ();
128 8 : ds->is = is;
129 8 : debit_rest = json_array ();
130 8 : credit_rest = json_array ();
131 8 : if (ds->bad_sig)
132 : {
133 2 : memset (&master_sig1,
134 : 42,
135 : sizeof (master_sig1));
136 2 : memset (&master_sig2,
137 : 42,
138 : sizeof (master_sig2));
139 : }
140 : else
141 : {
142 : const struct TALER_TESTING_Command *exchange_cmd;
143 : const struct TALER_MasterPrivateKeyP *master_priv;
144 :
145 6 : exchange_cmd = TALER_TESTING_interpreter_get_command (is,
146 : "exchange");
147 6 : if (NULL == exchange_cmd)
148 : {
149 0 : GNUNET_break (0);
150 0 : TALER_TESTING_interpreter_fail (is);
151 0 : return;
152 : }
153 6 : GNUNET_assert (GNUNET_OK ==
154 : TALER_TESTING_get_trait_master_priv (exchange_cmd,
155 : &master_priv));
156 6 : TALER_exchange_offline_wire_add_sign (ds->payto_uri,
157 : NULL,
158 : debit_rest,
159 : credit_rest,
160 : now,
161 : master_priv,
162 : &master_sig1);
163 6 : TALER_exchange_wire_signature_make (ds->payto_uri,
164 : NULL,
165 : debit_rest,
166 : credit_rest,
167 : master_priv,
168 : &master_sig2);
169 : }
170 8 : ds->dh = TALER_EXCHANGE_post_management_wire_create (
171 : TALER_TESTING_interpreter_get_context (is),
172 : exchange_url,
173 : ds->payto_uri,
174 : NULL,
175 : debit_rest,
176 : credit_rest,
177 : now,
178 : &master_sig1,
179 : &master_sig2);
180 8 : json_decref (debit_rest);
181 8 : json_decref (credit_rest);
182 8 : if (NULL == ds->dh)
183 : {
184 0 : GNUNET_break (0);
185 0 : TALER_TESTING_interpreter_fail (is);
186 0 : return;
187 : }
188 8 : TALER_EXCHANGE_post_management_wire_start (ds->dh, &wire_add_cb, ds);
189 : }
190 :
191 :
192 : /**
193 : * Free the state of a "wire_add" CMD, and possibly cancel a
194 : * pending operation thereof.
195 : *
196 : * @param cls closure, must be a `struct WireAddState`.
197 : * @param cmd the command which is being cleaned up.
198 : */
199 : static void
200 8 : wire_add_cleanup (void *cls,
201 : const struct TALER_TESTING_Command *cmd)
202 : {
203 8 : struct WireAddState *ds = cls;
204 :
205 8 : if (NULL != ds->dh)
206 : {
207 0 : TALER_TESTING_command_incomplete (ds->is,
208 : cmd->label);
209 0 : TALER_EXCHANGE_post_management_wire_cancel (ds->dh);
210 0 : ds->dh = NULL;
211 : }
212 8 : GNUNET_free (ds);
213 8 : }
214 :
215 :
216 : struct TALER_TESTING_Command
217 8 : TALER_TESTING_cmd_wire_add (const char *label,
218 : struct TALER_FullPayto payto_uri,
219 : unsigned int expected_http_status,
220 : bool bad_sig)
221 : {
222 : struct WireAddState *ds;
223 :
224 8 : ds = GNUNET_new (struct WireAddState);
225 8 : ds->expected_response_code = expected_http_status;
226 8 : ds->bad_sig = bad_sig;
227 8 : ds->payto_uri = payto_uri;
228 : {
229 8 : struct TALER_TESTING_Command cmd = {
230 : .cls = ds,
231 : .label = label,
232 : .run = &wire_add_run,
233 : .cleanup = &wire_add_cleanup
234 : };
235 :
236 8 : return cmd;
237 : }
238 : }
239 :
240 :
241 : /* end of testing_api_cmd_wire_add.c */
|