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