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_instances.c
21 : * @brief command to test GET /instances
22 : * @author Christian Grothoff
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 instances" CMD.
33 : */
34 : struct GetInstancesState
35 : {
36 :
37 : /**
38 : * Handle for a "GET instance" request.
39 : */
40 : struct TALER_MERCHANT_InstancesGetHandle *igh;
41 :
42 : /**
43 : * The interpreter state.
44 : */
45 : struct TALER_TESTING_Interpreter *is;
46 :
47 : /**
48 : * Base URL of the merchant serving the request.
49 : */
50 : const char *merchant_url;
51 :
52 : /**
53 : * Expected HTTP response code.
54 : */
55 : unsigned int http_status;
56 :
57 : /**
58 : * The list of instance references to compare to.
59 : */
60 : const char **instances;
61 :
62 : /**
63 : * The length of @e instances.
64 : */
65 : unsigned int instances_length;
66 :
67 : };
68 :
69 :
70 : /**
71 : * Callback for a GET /instances operation.
72 : *
73 : * @param cls closure for this function
74 : * @param hr HTTP response
75 : * @param iis_length how many instances are returned
76 : * @param iis all the instances details
77 : */
78 : static void
79 0 : get_instances_cb (void *cls,
80 : const struct TALER_MERCHANT_HttpResponse *hr,
81 : unsigned int iis_length,
82 : const struct TALER_MERCHANT_InstanceInformation iis[])
83 : {
84 0 : struct GetInstancesState *gis = cls;
85 :
86 0 : gis->igh = NULL;
87 0 : 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 0 : switch (hr->http_status)
98 : {
99 0 : case MHD_HTTP_OK:
100 0 : if (iis_length != gis->instances_length)
101 : {
102 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
103 : "Length of instances found does not match\n");
104 0 : TALER_TESTING_interpreter_fail (gis->is);
105 0 : return;
106 : }
107 0 : for (unsigned int i = 0; i < iis_length; ++i)
108 : {
109 : const struct TALER_TESTING_Command *instance_cmd;
110 :
111 0 : instance_cmd = TALER_TESTING_interpreter_lookup_command (
112 : gis->is,
113 0 : gis->instances[i]);
114 :
115 : {
116 : const char **name;
117 :
118 0 : if (GNUNET_OK !=
119 0 : TALER_TESTING_get_trait_instance_name (instance_cmd,
120 : &name))
121 : {
122 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
123 : "Could not fetch instance name\n");
124 0 : TALER_TESTING_interpreter_fail (gis->is);
125 0 : return;
126 : }
127 0 : if (0 != strcmp (iis[i].name,
128 : *name))
129 : {
130 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
131 : "Instance name does not match\n");
132 0 : TALER_TESTING_interpreter_fail (gis->is);
133 0 : return;
134 : }
135 : }
136 :
137 : {
138 : const char **id;
139 :
140 0 : if (GNUNET_OK !=
141 0 : TALER_TESTING_get_trait_instance_id (instance_cmd,
142 : &id))
143 : {
144 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145 : "Could not fetch instance id\n");
146 0 : TALER_TESTING_interpreter_fail (gis->is);
147 0 : return;
148 : }
149 0 : if (0 != strcmp (iis[i].id,
150 : *id))
151 : {
152 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153 : "Instance id does not match\n");
154 0 : TALER_TESTING_interpreter_fail (gis->is);
155 0 : return;
156 : }
157 : }
158 : }
159 :
160 : // FIXME: compare payment_targets
161 0 : break;
162 0 : default:
163 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
164 : "Unhandled HTTP status %u for GET /instances.\n",
165 : hr->http_status);
166 : }
167 0 : TALER_TESTING_interpreter_next (gis->is);
168 : }
169 :
170 :
171 : /**
172 : * Run the "GET /instances" CMD.
173 : *
174 : *
175 : * @param cls closure.
176 : * @param cmd command being run now.
177 : * @param is interpreter state.
178 : */
179 : static void
180 0 : get_instances_run (void *cls,
181 : const struct TALER_TESTING_Command *cmd,
182 : struct TALER_TESTING_Interpreter *is)
183 : {
184 0 : struct GetInstancesState *gis = cls;
185 :
186 0 : gis->is = is;
187 0 : gis->igh = TALER_MERCHANT_instances_get (is->ctx,
188 : gis->merchant_url,
189 : &get_instances_cb,
190 : gis);
191 0 : GNUNET_assert (NULL != gis->igh);
192 0 : }
193 :
194 :
195 : /**
196 : * Free the state of a "GET instance" CMD, and possibly
197 : * cancel a pending operation thereof.
198 : *
199 : * @param cls closure.
200 : * @param cmd command being run.
201 : */
202 : static void
203 0 : get_instances_cleanup (void *cls,
204 : const struct TALER_TESTING_Command *cmd)
205 : {
206 0 : struct GetInstancesState *gis = cls;
207 :
208 0 : if (NULL != gis->igh)
209 : {
210 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
211 : "GET /instances operation did not complete\n");
212 0 : TALER_MERCHANT_instances_get_cancel (gis->igh);
213 : }
214 0 : GNUNET_array_grow (gis->instances,
215 : gis->instances_length,
216 : 0);
217 0 : GNUNET_free (gis);
218 0 : }
219 :
220 :
221 : struct TALER_TESTING_Command
222 0 : TALER_TESTING_cmd_merchant_get_instances (const char *label,
223 : const char *merchant_url,
224 : unsigned int http_status,
225 : ...)
226 : {
227 : struct GetInstancesState *gis;
228 :
229 0 : gis = GNUNET_new (struct GetInstancesState);
230 0 : gis->merchant_url = merchant_url;
231 0 : gis->http_status = http_status;
232 : {
233 : const char *clabel;
234 : va_list ap;
235 :
236 0 : va_start (ap, http_status);
237 0 : while (NULL != (clabel = va_arg (ap, const char *)))
238 : {
239 0 : GNUNET_array_append (gis->instances,
240 : gis->instances_length,
241 : clabel);
242 : }
243 0 : va_end (ap);
244 : }
245 : {
246 0 : struct TALER_TESTING_Command cmd = {
247 : .cls = gis,
248 : .label = label,
249 : .run = &get_instances_run,
250 : .cleanup = &get_instances_cleanup
251 : };
252 :
253 0 : return cmd;
254 : }
255 : }
256 :
257 :
258 : /* end of testing_api_cmd_get_instances.c */
|