testing_api_cmd_post_otp_devices.c (6934B)
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_post_otp_devices.c 21 * @brief command to test POST /otp-devices 22 * @author Christian Grothoff 23 */ 24 #include "platform.h" 25 struct PostOtpDevicesState; 26 #define TALER_MERCHANT_POST_PRIVATE_OTP_DEVICES_RESULT_CLOSURE struct PostOtpDevicesState 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/post-private-otp-devices.h> 32 33 34 /** 35 * State of a "POST /otp-devices" CMD. 36 */ 37 struct PostOtpDevicesState 38 { 39 40 /** 41 * Handle for a "GET otp_device" request. 42 */ 43 struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *iph; 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 * ID of the otp_device to run POST for. 57 */ 58 const char *otp_device_id; 59 60 /** 61 * description of the otp_device 62 */ 63 const char *otp_device_description; 64 65 /** 66 * base64-encoded key 67 */ 68 char *otp_key; 69 70 /** 71 * Option that add amount of the order 72 */ 73 enum TALER_MerchantConfirmationAlgorithm otp_alg; 74 75 /** 76 * Counter at the OTP device. 77 */ 78 uint64_t otp_ctr; 79 80 /** 81 * Expected HTTP response code. 82 */ 83 unsigned int http_status; 84 85 }; 86 87 88 /** 89 * Callback for a POST /otp-devices operation. 90 * 91 * @param cls closure for this function 92 * @param odr response being processed 93 */ 94 static void 95 post_otp_devices_cb (struct PostOtpDevicesState *tis, 96 const struct TALER_MERCHANT_PostPrivateOtpDevicesResponse * 97 odr) 98 { 99 100 tis->iph = NULL; 101 if (tis->http_status != odr->hr.http_status) 102 { 103 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 104 "Unexpected response code %u (%d) to command %s\n", 105 odr->hr.http_status, 106 (int) odr->hr.ec, 107 TALER_TESTING_interpreter_get_current_label (tis->is)); 108 TALER_TESTING_interpreter_fail (tis->is); 109 return; 110 } 111 switch (odr->hr.http_status) 112 { 113 case MHD_HTTP_NO_CONTENT: 114 break; 115 case MHD_HTTP_UNAUTHORIZED: 116 break; 117 case MHD_HTTP_FORBIDDEN: 118 break; 119 case MHD_HTTP_NOT_FOUND: 120 break; 121 case MHD_HTTP_CONFLICT: 122 break; 123 default: 124 GNUNET_break (0); 125 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 126 "Unhandled HTTP status %u for POST /otp-devices.\n", 127 odr->hr.http_status); 128 } 129 TALER_TESTING_interpreter_next (tis->is); 130 } 131 132 133 /** 134 * Run the "POST /otp-devices" CMD. 135 * 136 * 137 * @param cls closure. 138 * @param cmd command being run now. 139 * @param is interpreter state. 140 */ 141 static void 142 post_otp_devices_run (void *cls, 143 const struct TALER_TESTING_Command *cmd, 144 struct TALER_TESTING_Interpreter *is) 145 { 146 struct PostOtpDevicesState *tis = cls; 147 148 tis->is = is; 149 tis->iph = TALER_MERCHANT_post_private_otp_devices_create ( 150 TALER_TESTING_interpreter_get_context (is), 151 tis->merchant_url, 152 tis->otp_device_id, 153 tis->otp_device_description, 154 tis->otp_key, 155 tis->otp_alg); 156 GNUNET_assert (GNUNET_OK == 157 TALER_MERCHANT_post_private_otp_devices_set_options ( 158 tis->iph, 159 TALER_MERCHANT_post_private_otp_devices_option_otp_ctr ( 160 tis->otp_ctr))); 161 { 162 enum TALER_ErrorCode ec; 163 164 ec = TALER_MERCHANT_post_private_otp_devices_start ( 165 tis->iph, 166 &post_otp_devices_cb, 167 tis); 168 if (TALER_EC_NONE != ec) 169 { 170 GNUNET_break (0); 171 TALER_TESTING_interpreter_fail (tis->is); 172 return; 173 } 174 } 175 } 176 177 178 /** 179 * Offers information from the POST /otp-devices CMD state to other 180 * commands. 181 * 182 * @param cls closure 183 * @param[out] ret result (could be anything) 184 * @param trait name of the trait 185 * @param index index number of the object to extract. 186 * @return #GNUNET_OK on success 187 */ 188 static enum GNUNET_GenericReturnValue 189 post_otp_devices_traits (void *cls, 190 const void **ret, 191 const char *trait, 192 unsigned int index) 193 { 194 struct PostOtpDevicesState *pts = cls; 195 struct TALER_TESTING_Trait traits[] = { 196 TALER_TESTING_make_trait_otp_device_description (pts->otp_device_description 197 ), 198 TALER_TESTING_make_trait_otp_key (pts->otp_key), 199 TALER_TESTING_make_trait_otp_alg (&pts->otp_alg), 200 TALER_TESTING_make_trait_otp_id (pts->otp_device_id), 201 TALER_TESTING_trait_end (), 202 }; 203 204 return TALER_TESTING_get_trait (traits, 205 ret, 206 trait, 207 index); 208 } 209 210 211 /** 212 * Free the state of a "POST otp_device" CMD, and possibly 213 * cancel a pending operation thereof. 214 * 215 * @param cls closure. 216 * @param cmd command being run. 217 */ 218 static void 219 post_otp_devices_cleanup (void *cls, 220 const struct TALER_TESTING_Command *cmd) 221 { 222 struct PostOtpDevicesState *tis = cls; 223 224 if (NULL != tis->iph) 225 { 226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 227 "POST /otp-devices operation did not complete\n"); 228 TALER_MERCHANT_post_private_otp_devices_cancel (tis->iph); 229 } 230 GNUNET_free (tis->otp_key); 231 GNUNET_free (tis); 232 } 233 234 235 struct TALER_TESTING_Command 236 TALER_TESTING_cmd_merchant_post_otp_devices ( 237 const char *label, 238 const char *merchant_url, 239 const char *otp_device_id, 240 const char *otp_device_description, 241 const char *otp_key, 242 const enum TALER_MerchantConfirmationAlgorithm otp_alg, 243 uint64_t otp_ctr, 244 unsigned int http_status) 245 { 246 struct PostOtpDevicesState *tis; 247 248 tis = GNUNET_new (struct PostOtpDevicesState); 249 tis->merchant_url = merchant_url; 250 tis->otp_device_id = otp_device_id; 251 tis->http_status = http_status; 252 tis->otp_device_description = otp_device_description; 253 tis->otp_key = GNUNET_strdup (otp_key); 254 tis->otp_alg = otp_alg; 255 tis->otp_ctr = otp_ctr; 256 { 257 struct TALER_TESTING_Command cmd = { 258 .cls = tis, 259 .label = label, 260 .run = &post_otp_devices_run, 261 .cleanup = &post_otp_devices_cleanup, 262 .traits = &post_otp_devices_traits 263 }; 264 265 return cmd; 266 } 267 } 268 269 270 /* end of testing_api_cmd_post_otp_devices.c */