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