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_get_attestable.c
21 : * @brief Implement the /reserve/$RID/get_attestable test command.
22 : * @author Christian Grothoff
23 : */
24 : #include "taler/taler_json_lib.h"
25 : #include <gnunet/gnunet_curl_lib.h>
26 : struct GetAttestableState;
27 : #define TALER_EXCHANGE_GET_RESERVES_ATTEST_RESULT_CLOSURE struct \
28 : GetAttestableState
29 : #include "taler/exchange/get-reserves-attest-RESERVE_PUB.h"
30 : #include "taler/taler_testing_lib.h"
31 :
32 :
33 : /**
34 : * State for a "get_attestable" CMD.
35 : */
36 : struct GetAttestableState
37 : {
38 : /**
39 : * Label to the command which created the reserve to check,
40 : * needed to resort the reserve key.
41 : */
42 : const char *reserve_reference;
43 :
44 : /**
45 : * Handle to the "reserve get_attestable" operation.
46 : */
47 : struct TALER_EXCHANGE_GetReservesAttestHandle *rgah;
48 :
49 : /**
50 : * Expected attestable attributes.
51 : */
52 : const char **expected_attestables;
53 :
54 : /**
55 : * Length of the @e expected_attestables array.
56 : */
57 : unsigned int expected_attestables_length;
58 :
59 : /**
60 : * Public key of the reserve being analyzed.
61 : */
62 : struct TALER_ReservePublicKeyP reserve_pub;
63 :
64 : /**
65 : * Expected HTTP response code.
66 : */
67 : unsigned int expected_response_code;
68 :
69 : /**
70 : * Interpreter state.
71 : */
72 : struct TALER_TESTING_Interpreter *is;
73 : };
74 :
75 :
76 : /**
77 : * Check that the reserve balance and HTTP response code are
78 : * both acceptable.
79 : *
80 : * @param ss closure.
81 : * @param rs HTTP response details
82 : */
83 : static void
84 6 : reserve_get_attestable_cb (
85 : struct GetAttestableState *ss,
86 : const struct TALER_EXCHANGE_GetReservesAttestResponse *rs)
87 : {
88 6 : struct TALER_TESTING_Interpreter *is = ss->is;
89 :
90 6 : ss->rgah = NULL;
91 6 : if (ss->expected_response_code != rs->hr.http_status)
92 : {
93 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
94 : "Unexpected HTTP response code: %d in %s:%u\n",
95 : rs->hr.http_status,
96 : __FILE__,
97 : __LINE__);
98 0 : json_dumpf (rs->hr.reply,
99 : stderr,
100 : JSON_INDENT (2));
101 0 : TALER_TESTING_interpreter_fail (ss->is);
102 0 : return;
103 : }
104 6 : if (MHD_HTTP_OK != rs->hr.http_status)
105 : {
106 5 : TALER_TESTING_interpreter_next (is);
107 5 : return;
108 : }
109 : // FIXME: check returned list matches expectations!
110 1 : TALER_TESTING_interpreter_next (is);
111 : }
112 :
113 :
114 : /**
115 : * Run the command.
116 : *
117 : * @param cls closure.
118 : * @param cmd the command being executed.
119 : * @param is the interpreter state.
120 : */
121 : static void
122 6 : get_attestable_run (void *cls,
123 : const struct TALER_TESTING_Command *cmd,
124 : struct TALER_TESTING_Interpreter *is)
125 : {
126 6 : struct GetAttestableState *ss = cls;
127 : const struct TALER_TESTING_Command *ref_reserve;
128 : const struct TALER_ReservePrivateKeyP *reserve_priv;
129 : const struct TALER_ReservePublicKeyP *reserve_pub;
130 : const char *exchange_url;
131 :
132 6 : ss->is = is;
133 6 : exchange_url = TALER_TESTING_get_exchange_url (is);
134 6 : if (NULL == exchange_url)
135 : {
136 0 : GNUNET_break (0);
137 0 : return;
138 : }
139 : ref_reserve
140 6 : = TALER_TESTING_interpreter_lookup_command (is,
141 : ss->reserve_reference);
142 :
143 6 : if (NULL == ref_reserve)
144 : {
145 0 : GNUNET_break (0);
146 0 : TALER_TESTING_interpreter_fail (is);
147 0 : return;
148 : }
149 6 : if (GNUNET_OK ==
150 6 : TALER_TESTING_get_trait_reserve_priv (ref_reserve,
151 : &reserve_priv))
152 : {
153 6 : GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv->eddsa_priv,
154 : &ss->reserve_pub.eddsa_pub);
155 : }
156 : else
157 : {
158 0 : if (GNUNET_OK !=
159 0 : TALER_TESTING_get_trait_reserve_pub (ref_reserve,
160 : &reserve_pub))
161 : {
162 0 : GNUNET_break (0);
163 0 : TALER_LOG_ERROR (
164 : "Failed to find reserve_priv for get_attestable query\n");
165 0 : TALER_TESTING_interpreter_fail (is);
166 0 : return;
167 : }
168 0 : ss->reserve_pub = *reserve_pub;
169 : }
170 6 : ss->rgah = TALER_EXCHANGE_get_reserves_attest_create (
171 : TALER_TESTING_interpreter_get_context (is),
172 : exchange_url,
173 6 : &ss->reserve_pub);
174 6 : if (NULL == ss->rgah)
175 : {
176 0 : GNUNET_break (0);
177 0 : TALER_TESTING_interpreter_fail (is);
178 0 : return;
179 : }
180 6 : if (TALER_EC_NONE !=
181 6 : TALER_EXCHANGE_get_reserves_attest_start (ss->rgah,
182 : &reserve_get_attestable_cb,
183 : ss))
184 : {
185 0 : GNUNET_break (0);
186 0 : TALER_EXCHANGE_get_reserves_attest_cancel (ss->rgah);
187 0 : ss->rgah = NULL;
188 0 : TALER_TESTING_interpreter_fail (is);
189 0 : return;
190 : }
191 : }
192 :
193 :
194 : /**
195 : * Cleanup the state from a "reserve get_attestable" CMD, and possibly
196 : * cancel a pending operation thereof.
197 : *
198 : * @param cls closure.
199 : * @param cmd the command which is being cleaned up.
200 : */
201 : static void
202 6 : get_attestable_cleanup (void *cls,
203 : const struct TALER_TESTING_Command *cmd)
204 : {
205 6 : struct GetAttestableState *ss = cls;
206 :
207 6 : if (NULL != ss->rgah)
208 : {
209 0 : TALER_TESTING_command_incomplete (ss->is,
210 : cmd->label);
211 0 : TALER_EXCHANGE_get_reserves_attest_cancel (ss->rgah);
212 0 : ss->rgah = NULL;
213 : }
214 6 : GNUNET_free (ss->expected_attestables);
215 6 : GNUNET_free (ss);
216 6 : }
217 :
218 :
219 : struct TALER_TESTING_Command
220 6 : TALER_TESTING_cmd_reserve_get_attestable (const char *label,
221 : const char *reserve_reference,
222 : unsigned int expected_response_code,
223 : ...)
224 : {
225 : struct GetAttestableState *ss;
226 : va_list ap;
227 : unsigned int num_expected;
228 : const char *ea;
229 :
230 6 : num_expected = 0;
231 6 : va_start (ap, expected_response_code);
232 9 : while (NULL != va_arg (ap, const char *))
233 3 : num_expected++;
234 6 : va_end (ap);
235 :
236 6 : GNUNET_assert (NULL != reserve_reference);
237 6 : ss = GNUNET_new (struct GetAttestableState);
238 6 : ss->reserve_reference = reserve_reference;
239 6 : ss->expected_response_code = expected_response_code;
240 6 : ss->expected_attestables_length = num_expected;
241 6 : ss->expected_attestables = GNUNET_new_array (num_expected,
242 : const char *);
243 6 : num_expected = 0;
244 6 : va_start (ap, expected_response_code);
245 9 : while (NULL != (ea = va_arg (ap, const char *)))
246 3 : ss->expected_attestables[num_expected++] = ea;
247 6 : va_end (ap);
248 :
249 : {
250 6 : struct TALER_TESTING_Command cmd = {
251 : .cls = ss,
252 : .label = label,
253 : .run = &get_attestable_run,
254 : .cleanup = &get_attestable_cleanup
255 : };
256 :
257 6 : return cmd;
258 : }
259 : }
|