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