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 src/testing/testing_api_cmd_get_instances.c
21 : * @brief command to test GET /instances
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : struct GetInstancesState;
26 : #define TALER_MERCHANT_GET_MANAGEMENT_INSTANCES_RESULT_CLOSURE struct GetInstancesState
27 : #include <taler/taler_exchange_service.h>
28 : #include <taler/taler_testing_lib.h>
29 : #include "taler/taler_merchant_service.h"
30 : #include "taler/taler_merchant_testing_lib.h"
31 : #include <taler/merchant/get-management-instances.h>
32 :
33 :
34 : /**
35 : * State of a "GET instances" CMD.
36 : */
37 : struct GetInstancesState
38 : {
39 :
40 : /**
41 : * Handle for a "GET instance" request.
42 : */
43 : struct TALER_MERCHANT_GetManagementInstancesHandle *igh;
44 :
45 : /**
46 : * The interpreter state.
47 : */
48 : struct TALER_TESTING_Interpreter *is;
49 :
50 : /**
51 : * Base URL of the merchant serving the request.
52 : */
53 : const char *merchant_url;
54 :
55 : /**
56 : * Expected HTTP response code.
57 : */
58 : unsigned int http_status;
59 :
60 : /**
61 : * The list of instance references to compare to.
62 : */
63 : const char **instances;
64 :
65 : /**
66 : * The length of @e instances.
67 : */
68 : unsigned int instances_length;
69 :
70 : };
71 :
72 :
73 : /**
74 : * Callback for a GET /instances operation.
75 : *
76 : * @param cls closure for this function
77 : * @param igr response
78 : */
79 : static void
80 5 : get_instances_cb (struct GetInstancesState *gis,
81 : const struct TALER_MERCHANT_GetManagementInstancesResponse *
82 : igr)
83 : {
84 5 : const struct TALER_MERCHANT_HttpResponse *hr = &igr->hr;
85 :
86 5 : gis->igh = NULL;
87 5 : if (gis->http_status != hr->http_status)
88 : {
89 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
90 : "Unexpected response code %u (%d) to command %s\n",
91 : hr->http_status,
92 : (int) hr->ec,
93 : TALER_TESTING_interpreter_get_current_label (gis->is));
94 0 : TALER_TESTING_interpreter_fail (gis->is);
95 0 : return;
96 : }
97 5 : switch (hr->http_status)
98 : {
99 5 : case MHD_HTTP_OK:
100 : {
101 5 : unsigned int iis_length
102 : = igr->details.ok.iis_length;
103 5 : const struct TALER_MERCHANT_GetManagementInstancesInstanceInfo *iis
104 : = igr->details.ok.iis;
105 :
106 5 : if (iis_length != gis->instances_length)
107 : {
108 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
109 : "Length of instances found does not match\n");
110 0 : TALER_TESTING_interpreter_fail (gis->is);
111 0 : return;
112 : }
113 9 : for (unsigned int i = 0; i < iis_length; ++i)
114 : {
115 : const struct TALER_TESTING_Command *instance_cmd;
116 :
117 4 : instance_cmd = TALER_TESTING_interpreter_lookup_command (
118 : gis->is,
119 4 : gis->instances[i]);
120 :
121 : {
122 : const char *name;
123 :
124 4 : if (GNUNET_OK !=
125 4 : TALER_TESTING_get_trait_instance_name (instance_cmd,
126 : &name))
127 : {
128 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
129 : "Could not fetch instance name\n");
130 0 : TALER_TESTING_interpreter_fail (gis->is);
131 0 : return;
132 : }
133 4 : if (0 != strcmp (iis[i].name,
134 : name))
135 : {
136 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
137 : "Instance name does not match\n");
138 0 : TALER_TESTING_interpreter_fail (gis->is);
139 0 : return;
140 : }
141 : }
142 :
143 : {
144 : const char *id;
145 :
146 4 : if (GNUNET_OK !=
147 4 : TALER_TESTING_get_trait_instance_id (instance_cmd,
148 : &id))
149 : {
150 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151 : "Could not fetch instance id\n");
152 0 : TALER_TESTING_interpreter_fail (gis->is);
153 0 : return;
154 : }
155 4 : if (0 != strcmp (iis[i].id,
156 : id))
157 : {
158 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159 : "Instance id does not match\n");
160 0 : TALER_TESTING_interpreter_fail (gis->is);
161 0 : return;
162 : }
163 : }
164 : }
165 :
166 : // FIXME: compare payment_targets
167 5 : break;
168 : }
169 0 : default:
170 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
171 : "Unhandled HTTP status %u for GET /instances.\n",
172 : hr->http_status);
173 : }
174 5 : TALER_TESTING_interpreter_next (gis->is);
175 : }
176 :
177 :
178 : /**
179 : * Run the "GET /instances" CMD.
180 : *
181 : *
182 : * @param cls closure.
183 : * @param cmd command being run now.
184 : * @param is interpreter state.
185 : */
186 : static void
187 5 : get_instances_run (void *cls,
188 : const struct TALER_TESTING_Command *cmd,
189 : struct TALER_TESTING_Interpreter *is)
190 : {
191 5 : struct GetInstancesState *gis = cls;
192 :
193 5 : gis->is = is;
194 5 : gis->igh = TALER_MERCHANT_get_management_instances_create (
195 : TALER_TESTING_interpreter_get_context (is),
196 : gis->merchant_url);
197 : {
198 : enum TALER_ErrorCode ec;
199 :
200 5 : ec = TALER_MERCHANT_get_management_instances_start (
201 : gis->igh,
202 : &get_instances_cb,
203 : gis);
204 5 : GNUNET_assert (TALER_EC_NONE == ec);
205 : }
206 5 : }
207 :
208 :
209 : /**
210 : * Free the state of a "GET instance" 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 5 : get_instances_cleanup (void *cls,
218 : const struct TALER_TESTING_Command *cmd)
219 : {
220 5 : struct GetInstancesState *gis = cls;
221 :
222 5 : if (NULL != gis->igh)
223 : {
224 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
225 : "GET /instances operation did not complete\n");
226 0 : TALER_MERCHANT_get_management_instances_cancel (gis->igh);
227 : }
228 5 : GNUNET_array_grow (gis->instances,
229 : gis->instances_length,
230 : 0);
231 5 : GNUNET_free (gis);
232 5 : }
233 :
234 :
235 : struct TALER_TESTING_Command
236 5 : TALER_TESTING_cmd_merchant_get_instances (const char *label,
237 : const char *merchant_url,
238 : unsigned int http_status,
239 : ...)
240 : {
241 : struct GetInstancesState *gis;
242 :
243 5 : gis = GNUNET_new (struct GetInstancesState);
244 5 : gis->merchant_url = merchant_url;
245 5 : gis->http_status = http_status;
246 : {
247 : const char *clabel;
248 : va_list ap;
249 :
250 5 : va_start (ap, http_status);
251 9 : while (NULL != (clabel = va_arg (ap, const char *)))
252 : {
253 4 : GNUNET_array_append (gis->instances,
254 : gis->instances_length,
255 : clabel);
256 : }
257 5 : va_end (ap);
258 : }
259 : {
260 5 : struct TALER_TESTING_Command cmd = {
261 : .cls = gis,
262 : .label = label,
263 : .run = &get_instances_run,
264 : .cleanup = &get_instances_cleanup
265 : };
266 :
267 5 : return cmd;
268 : }
269 : }
270 :
271 :
272 : /* end of testing_api_cmd_get_instances.c */
|