Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 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 src/testing/testing_api_cmd_get_otp_devices.c
21 : * @brief command to test GET /otp-devices
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : struct GetOtpDevicesState;
26 : #define TALER_MERCHANT_GET_PRIVATE_OTP_DEVICES_RESULT_CLOSURE struct GetOtpDevicesState
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-private-otp-devices.h>
32 :
33 :
34 : /**
35 : * State of a "GET /otp-devices" CMD.
36 : */
37 : struct GetOtpDevicesState
38 : {
39 :
40 : /**
41 : * Handle for a "GET /otp-devices" request.
42 : */
43 : struct TALER_MERCHANT_GetPrivateOtpDevicesHandle *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 otp_device references.
62 : */
63 : const char **otp_devices;
64 :
65 : /**
66 : * Length of @e otp_devices.
67 : */
68 : unsigned int otp_devices_length;
69 :
70 : };
71 :
72 :
73 : /**
74 : * Callback for a GET /otp-devices operation.
75 : *
76 : * @param cls closure for this function
77 : * @param tgr response details
78 : */
79 : static void
80 0 : get_otp_devices_cb (struct GetOtpDevicesState *gis,
81 : const struct TALER_MERCHANT_GetPrivateOtpDevicesResponse *
82 : tgr)
83 : {
84 :
85 0 : gis->igh = NULL;
86 0 : if (gis->http_status != tgr->hr.http_status)
87 : {
88 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
89 : "Unexpected response code %u (%d) to command %s\n",
90 : tgr->hr.http_status,
91 : (int) tgr->hr.ec,
92 : TALER_TESTING_interpreter_get_current_label (gis->is));
93 0 : TALER_TESTING_interpreter_fail (gis->is);
94 0 : return;
95 : }
96 0 : switch (tgr->hr.http_status)
97 : {
98 0 : case MHD_HTTP_OK:
99 0 : if (tgr->details.ok.otp_devices_length != gis->otp_devices_length)
100 : {
101 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
102 : "Length of otp_devices found does not match\n");
103 0 : TALER_TESTING_interpreter_fail (gis->is);
104 0 : return;
105 : }
106 0 : for (unsigned int i = 0; i < gis->otp_devices_length; ++i)
107 : {
108 : const struct TALER_TESTING_Command *otp_device_cmd;
109 :
110 0 : otp_device_cmd = TALER_TESTING_interpreter_lookup_command (
111 : gis->is,
112 0 : gis->otp_devices[i]);
113 :
114 : {
115 : const char *otp_device_id;
116 :
117 0 : if (GNUNET_OK !=
118 0 : TALER_TESTING_get_trait_otp_id (otp_device_cmd,
119 : &otp_device_id))
120 : {
121 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
122 : "Could not fetch otp_device id\n");
123 0 : TALER_TESTING_interpreter_fail (gis->is);
124 0 : return;
125 : }
126 0 : if (0 != strcmp (tgr->details.ok.otp_devices[i].otp_device_id,
127 : otp_device_id))
128 : {
129 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130 : "OtpDevice id does not match\n");
131 0 : TALER_TESTING_interpreter_fail (gis->is);
132 0 : return;
133 : }
134 : }
135 : }
136 0 : break;
137 0 : case MHD_HTTP_UNAUTHORIZED:
138 0 : break;
139 0 : case MHD_HTTP_NOT_FOUND:
140 : /* instance does not exist */
141 0 : break;
142 0 : default:
143 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
144 : "Unhandled HTTP status %u (%d).\n",
145 : tgr->hr.http_status,
146 : tgr->hr.ec);
147 0 : break;
148 : }
149 0 : TALER_TESTING_interpreter_next (gis->is);
150 : }
151 :
152 :
153 : /**
154 : * Run the "GET /otp-devices" CMD.
155 : *
156 : *
157 : * @param cls closure.
158 : * @param cmd command being run now.
159 : * @param is interpreter state.
160 : */
161 : static void
162 0 : get_otp_devices_run (void *cls,
163 : const struct TALER_TESTING_Command *cmd,
164 : struct TALER_TESTING_Interpreter *is)
165 : {
166 0 : struct GetOtpDevicesState *gis = cls;
167 :
168 0 : gis->is = is;
169 0 : gis->igh = TALER_MERCHANT_get_private_otp_devices_create (
170 : TALER_TESTING_interpreter_get_context (is),
171 : gis->merchant_url);
172 : {
173 : enum TALER_ErrorCode ec;
174 :
175 0 : ec = TALER_MERCHANT_get_private_otp_devices_start (
176 : gis->igh,
177 : &get_otp_devices_cb,
178 : gis);
179 0 : GNUNET_assert (TALER_EC_NONE == ec);
180 : }
181 0 : }
182 :
183 :
184 : /**
185 : * Free the state of a "GET otp_device" CMD, and possibly
186 : * cancel a pending operation thereof.
187 : *
188 : * @param cls closure.
189 : * @param cmd command being run.
190 : */
191 : static void
192 0 : get_otp_devices_cleanup (void *cls,
193 : const struct TALER_TESTING_Command *cmd)
194 : {
195 0 : struct GetOtpDevicesState *gis = cls;
196 :
197 0 : if (NULL != gis->igh)
198 : {
199 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
200 : "GET /otp-devices operation did not complete\n");
201 0 : TALER_MERCHANT_get_private_otp_devices_cancel (gis->igh);
202 : }
203 0 : GNUNET_array_grow (gis->otp_devices,
204 : gis->otp_devices_length,
205 : 0);
206 0 : GNUNET_free (gis);
207 0 : }
208 :
209 :
210 : struct TALER_TESTING_Command
211 0 : TALER_TESTING_cmd_merchant_get_otp_devices (const char *label,
212 : const char *merchant_url,
213 : unsigned int http_status,
214 : ...)
215 : {
216 : struct GetOtpDevicesState *gis;
217 :
218 0 : gis = GNUNET_new (struct GetOtpDevicesState);
219 0 : gis->merchant_url = merchant_url;
220 0 : gis->http_status = http_status;
221 : {
222 : const char *clabel;
223 : va_list ap;
224 :
225 0 : va_start (ap, http_status);
226 0 : while (NULL != (clabel = va_arg (ap, const char *)))
227 : {
228 0 : GNUNET_array_append (gis->otp_devices,
229 : gis->otp_devices_length,
230 : clabel);
231 : }
232 0 : va_end (ap);
233 : }
234 : {
235 0 : struct TALER_TESTING_Command cmd = {
236 : .cls = gis,
237 : .label = label,
238 : .run = &get_otp_devices_run,
239 : .cleanup = &get_otp_devices_cleanup
240 : };
241 :
242 0 : return cmd;
243 : }
244 : }
245 :
246 :
247 : /* end of testing_api_cmd_get_otp_devices.c */
|