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 0 : struct GetReservesState *grs = cls;
76 0 : bool matched[reserves_length];
77 0 : bool fail = false;
78 :
79 0 : grs->rgh = NULL;
80 0 : if (grs->http_status != hr->http_status)
81 : {
82 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
83 : "Unexpected response code %u (%d) to command %s\n",
84 : hr->http_status,
85 : (int) hr->ec,
86 : TALER_TESTING_interpreter_get_current_label (grs->is));
87 0 : TALER_TESTING_interpreter_fail (grs->is);
88 0 : return;
89 : }
90 0 : switch (hr->http_status)
91 : {
92 0 : case MHD_HTTP_OK:
93 0 : if (reserves_length != grs->reserves_length)
94 : {
95 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
96 : "Length of reserves found does not match\n");
97 0 : TALER_TESTING_interpreter_fail (grs->is);
98 0 : return;
99 : }
100 : /* check if the data returned matches that from the POST / PATCH */
101 0 : memset (matched, 0, sizeof (matched));
102 0 : for (unsigned int i = 0; i < reserves_length; ++i)
103 0 : for (unsigned int j = 0; j < reserves_length; ++j)
104 : {
105 : const struct TALER_TESTING_Command *reserve_cmd;
106 0 : bool match = true;
107 :
108 0 : reserve_cmd = TALER_TESTING_interpreter_lookup_command (
109 : grs->is,
110 0 : grs->reserves[j]);
111 : {
112 : const struct TALER_ReservePublicKeyP *reserve_pub;
113 :
114 0 : if (GNUNET_OK !=
115 0 : TALER_TESTING_get_trait_reserve_pub (reserve_cmd,
116 : &reserve_pub))
117 : {
118 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119 : "Could not fetch reserve public key\n");
120 0 : TALER_TESTING_interpreter_fail (grs->is);
121 0 : return;
122 : }
123 0 : if (0 != GNUNET_memcmp (&reserves[i].reserve_pub,
124 : reserve_pub))
125 : {
126 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
127 : "Reserve public key does not match, got %s\n",
128 : TALER_B2S (&reserves[i].reserve_pub));
129 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
130 : "Reserve public key does not match, expected %s\n",
131 : TALER_B2S (reserve_pub));
132 0 : match = false;
133 : }
134 : }
135 : {
136 : const struct TALER_Amount *initial;
137 :
138 0 : if (GNUNET_OK !=
139 0 : TALER_TESTING_get_trait_amount (reserve_cmd,
140 : &initial))
141 : {
142 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
143 : "Could not fetch reserve initial balance\n");
144 0 : TALER_TESTING_interpreter_fail (grs->is);
145 0 : return;
146 : }
147 0 : if ((GNUNET_OK !=
148 0 : TALER_amount_cmp_currency (&reserves[i].merchant_initial_amount,
149 0 : initial)) ||
150 0 : (0 != TALER_amount_cmp (&reserves[i].merchant_initial_amount,
151 : initial)))
152 : {
153 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
154 : "Reserve initial amount does not match, got %s\n",
155 : TALER_amount2s (&reserves[i].merchant_initial_amount));
156 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
157 : "Reserve initial amount does not match, wanted %s\n",
158 : TALER_amount2s (initial));
159 0 : match = false;
160 : }
161 : }
162 0 : if (match)
163 0 : matched[i] = true;
164 : }
165 0 : for (unsigned int i = 0; i < reserves_length; ++i)
166 0 : if (! matched[i])
167 0 : fail = true;
168 0 : if (fail)
169 : {
170 0 : TALER_TESTING_interpreter_fail (grs->is);
171 0 : return;
172 : }
173 0 : break;
174 0 : default:
175 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
176 : "Unhandled HTTP status.\n");
177 : }
178 0 : TALER_TESTING_interpreter_next (grs->is);
179 : }
180 :
181 :
182 : /**
183 : * Run the "GET /private/reserves" CMD.
184 : *
185 : * @param cls closure.
186 : * @param cmd command being run now.
187 : * @param is interpreter state.
188 : */
189 : static void
190 0 : get_reserves_run (void *cls,
191 : const struct TALER_TESTING_Command *cmd,
192 : struct TALER_TESTING_Interpreter *is)
193 : {
194 0 : struct GetReservesState *grs = cls;
195 :
196 0 : grs->is = is;
197 0 : grs->rgh = TALER_MERCHANT_reserves_get (is->ctx,
198 : grs->merchant_url,
199 0 : GNUNET_TIME_UNIT_ZERO_TS,
200 : TALER_EXCHANGE_YNA_ALL,
201 : TALER_EXCHANGE_YNA_ALL,
202 : &get_reserves_cb,
203 : grs);
204 :
205 0 : GNUNET_assert (NULL != grs->rgh);
206 0 : }
207 :
208 :
209 : /**
210 : * Free the state of a "GET reserves" CMD, and possibly
211 : * cancel a pending operation thereof.
212 : *
213 : * @param cls closure.
214 : * @param cmd command being run.
215 : */
216 : static void
217 0 : get_reserves_cleanup (void *cls,
218 : const struct TALER_TESTING_Command *cmd)
219 : {
220 0 : struct GetReservesState *grs = cls;
221 :
222 0 : if (NULL != grs->rgh)
223 : {
224 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225 : "GET /private/reserves operation did not complete\n");
226 0 : TALER_MERCHANT_reserves_get_cancel (grs->rgh);
227 : }
228 0 : GNUNET_array_grow (grs->reserves,
229 : grs->reserves_length,
230 : 0);
231 0 : GNUNET_free (grs);
232 0 : }
233 :
234 :
235 : struct TALER_TESTING_Command
236 0 : TALER_TESTING_cmd_merchant_get_reserves (const char *label,
237 : const char *merchant_url,
238 : unsigned int http_status,
239 : ...)
240 : {
241 : struct GetReservesState *grs;
242 :
243 0 : grs = GNUNET_new (struct GetReservesState);
244 0 : grs->merchant_url = merchant_url;
245 0 : grs->http_status = http_status;
246 : {
247 : const char *clabel;
248 : va_list ap;
249 :
250 0 : va_start (ap, http_status);
251 0 : while (NULL != (clabel = va_arg (ap, const char *)))
252 : {
253 0 : GNUNET_array_append (grs->reserves,
254 : grs->reserves_length,
255 : clabel);
256 : }
257 0 : va_end (ap);
258 : }
259 : {
260 0 : struct TALER_TESTING_Command cmd = {
261 : .cls = grs,
262 : .label = label,
263 : .run = &get_reserves_run,
264 : .cleanup = &get_reserves_cleanup
265 : };
266 :
267 0 : return cmd;
268 : }
269 : }
270 :
271 :
272 : /* end of testing_api_cmd_get_reserves.c */
|