Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020 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_api_cmd_get_reserves.c
21 : * @brief command to test GET /private/reserves
22 : * @author Jonathan Buchanan
23 : */
24 : #include "platform.h"
25 : #include <taler/taler_exchange_service.h>
26 : #include <taler/taler_testing_lib.h>
27 : #include "taler_merchant_service.h"
28 : #include "taler_merchant_testing_lib.h"
29 :
30 :
31 : /**
32 : * State of a "GET reserves" CMD
33 : */
34 : struct GetReservesState
35 : {
36 :
37 : /**
38 : * Handle for a "GET reserves" request.
39 : */
40 : struct TALER_MERCHANT_ReservesGetHandle *rgh;
41 :
42 : /**
43 : * The interpreter state.
44 : */
45 : struct TALER_TESTING_Interpreter *is;
46 :
47 : /**
48 : * A list of reserves to compare with.
49 : */
50 : const char **reserves;
51 :
52 : /**
53 : * Length of @e reserve_refs.
54 : */
55 : unsigned int reserves_length;
56 :
57 : /**
58 : * Base URL of the merchant serving the request.
59 : */
60 : const char *merchant_url;
61 :
62 : /**
63 : * Expected HTTP response code.
64 : */
65 : unsigned int http_status;
66 : };
67 :
68 :
69 : static void
70 0 : get_reserves_cb (void *cls,
71 : const struct TALER_MERCHANT_HttpResponse *hr,
72 : unsigned int reserves_length,
73 : const struct TALER_MERCHANT_ReserveSummary reserves[])
74 0 : {
75 : /* FIXME, deeper checks should be implemented here. */
76 0 : struct GetReservesState *grs = cls;
77 0 : bool matched[reserves_length];
78 0 : bool fail = false;
79 :
80 0 : grs->rgh = NULL;
81 0 : if (grs->http_status != hr->http_status)
82 : {
83 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
84 : "Unexpected response code %u (%d) to command %s\n",
85 : hr->http_status,
86 : (int) hr->ec,
87 : TALER_TESTING_interpreter_get_current_label (grs->is));
88 0 : TALER_TESTING_interpreter_fail (grs->is);
89 0 : return;
90 : }
91 0 : switch (hr->http_status)
92 : {
93 0 : case MHD_HTTP_OK:
94 0 : if (reserves_length != grs->reserves_length)
95 : {
96 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
97 : "Length of reserves found does not match\n");
98 0 : TALER_TESTING_interpreter_fail (grs->is);
99 0 : return;
100 : }
101 : // FIXME: check if the data returned matches that from the POST / PATCH
102 0 : memset (matched, 0, sizeof (matched));
103 0 : for (unsigned int i = 0; i < reserves_length; ++i)
104 0 : for (unsigned int j = 0; j < reserves_length; ++j)
105 : {
106 : const struct TALER_TESTING_Command *reserve_cmd;
107 0 : bool match = true;
108 :
109 0 : reserve_cmd = TALER_TESTING_interpreter_lookup_command (
110 : grs->is,
111 0 : grs->reserves[j]);
112 : {
113 : const struct TALER_ReservePublicKeyP *reserve_pub;
114 :
115 0 : if (GNUNET_OK !=
116 0 : TALER_TESTING_get_trait_reserve_pub (reserve_cmd,
117 : &reserve_pub))
118 : {
119 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
120 : "Could not fetch reserve public key\n");
121 0 : TALER_TESTING_interpreter_fail (grs->is);
122 0 : return;
123 : }
124 0 : if (0 != GNUNET_memcmp (&reserves[i].reserve_pub,
125 : reserve_pub))
126 : {
127 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
128 : "Reserve public key does not match, got %s\n",
129 : TALER_B2S (&reserves[i].reserve_pub));
130 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
131 : "Reserve public key does not match, expected %s\n",
132 : TALER_B2S (reserve_pub));
133 0 : match = false;
134 : }
135 : }
136 : {
137 : const struct TALER_Amount *initial;
138 :
139 0 : if (GNUNET_OK !=
140 0 : TALER_TESTING_get_trait_amount (reserve_cmd,
141 : &initial))
142 : {
143 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
144 : "Could not fetch reserve initial balance\n");
145 0 : TALER_TESTING_interpreter_fail (grs->is);
146 0 : return;
147 : }
148 0 : if ((GNUNET_OK !=
149 0 : TALER_amount_cmp_currency (&reserves[i].merchant_initial_amount,
150 0 : initial)) ||
151 0 : (0 != TALER_amount_cmp (&reserves[i].merchant_initial_amount,
152 : initial)))
153 : {
154 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
155 : "Reserve initial amount does not match, got %s\n",
156 : TALER_amount2s (&reserves[i].merchant_initial_amount));
157 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
158 : "Reserve initial amount does not match, wanted %s\n",
159 : TALER_amount2s (initial));
160 0 : match = false;
161 : }
162 : }
163 0 : if (match)
164 0 : matched[i] = true;
165 : }
166 0 : for (unsigned int i = 0; i < reserves_length; ++i)
167 0 : if (! matched[i])
168 0 : fail = true;
169 0 : if (fail)
170 : {
171 0 : TALER_TESTING_interpreter_fail (grs->is);
172 0 : return;
173 : }
174 0 : break;
175 0 : default:
176 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
177 : "Unhandled HTTP status.\n");
178 : }
179 0 : TALER_TESTING_interpreter_next (grs->is);
180 : }
181 :
182 :
183 : /**
184 : * Run the "GET /private/reserves" CMD.
185 : *
186 : * @param cls closure.
187 : * @param cmd command being run now.
188 : * @param is interpreter state.
189 : */
190 : static void
191 0 : get_reserves_run (void *cls,
192 : const struct TALER_TESTING_Command *cmd,
193 : struct TALER_TESTING_Interpreter *is)
194 : {
195 0 : struct GetReservesState *grs = cls;
196 :
197 0 : grs->is = is;
198 0 : grs->rgh = TALER_MERCHANT_reserves_get (is->ctx,
199 : grs->merchant_url,
200 0 : GNUNET_TIME_UNIT_ZERO_TS,
201 : TALER_EXCHANGE_YNA_ALL,
202 : TALER_EXCHANGE_YNA_ALL,
203 : &get_reserves_cb,
204 : grs);
205 :
206 0 : GNUNET_assert (NULL != grs->rgh);
207 0 : }
208 :
209 :
210 : /**
211 : * Free the state of a "GET reserves" CMD, and possibly
212 : * cancel a pending operation thereof.
213 : *
214 : * @param cls closure.
215 : * @param cmd command being run.
216 : */
217 : static void
218 0 : get_reserves_cleanup (void *cls,
219 : const struct TALER_TESTING_Command *cmd)
220 : {
221 0 : struct GetReservesState *grs = cls;
222 :
223 0 : if (NULL != grs->rgh)
224 : {
225 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
226 : "GET /private/reserves operation did not complete\n");
227 0 : TALER_MERCHANT_reserves_get_cancel (grs->rgh);
228 : }
229 0 : GNUNET_array_grow (grs->reserves,
230 : grs->reserves_length,
231 : 0);
232 0 : GNUNET_free (grs);
233 0 : }
234 :
235 :
236 : struct TALER_TESTING_Command
237 0 : TALER_TESTING_cmd_merchant_get_reserves (const char *label,
238 : const char *merchant_url,
239 : unsigned int http_status,
240 : ...)
241 : {
242 : struct GetReservesState *grs;
243 :
244 0 : grs = GNUNET_new (struct GetReservesState);
245 0 : grs->merchant_url = merchant_url;
246 0 : grs->http_status = http_status;
247 : {
248 : const char *clabel;
249 : va_list ap;
250 :
251 0 : va_start (ap, http_status);
252 0 : while (NULL != (clabel = va_arg (ap, const char *)))
253 : {
254 0 : GNUNET_array_append (grs->reserves,
255 : grs->reserves_length,
256 : clabel);
257 : }
258 0 : va_end (ap);
259 : }
260 : {
261 0 : struct TALER_TESTING_Command cmd = {
262 : .cls = grs,
263 : .label = label,
264 : .run = &get_reserves_run,
265 : .cleanup = &get_reserves_cleanup
266 : };
267 :
268 0 : return cmd;
269 : }
270 : }
271 :
272 :
273 : /* end of testing_api_cmd_get_reserves.c */
|