testing_api_cmd_patch_otp_device.c (7029B)
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_patch_otp_device.c 21 * @brief command to test PATCH /otp-device 22 * @author Christian Grothoff 23 */ 24 #include "platform.h" 25 struct PatchOtpDeviceState; 26 #define TALER_MERCHANT_PATCH_PRIVATE_OTP_DEVICE_RESULT_CLOSURE struct PatchOtpDeviceState 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/patch-private-otp-devices-DEVICE_ID.h> 32 33 34 /** 35 * State of a "PATCH /otp-device" CMD. 36 */ 37 struct PatchOtpDeviceState 38 { 39 40 /** 41 * Handle for a "GET otp_device" request. 42 */ 43 struct TALER_MERCHANT_PatchPrivateOtpDeviceHandle *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 GET 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 * Algorithm used by the OTP device 72 */ 73 enum TALER_MerchantConfirmationAlgorithm otp_alg; 74 75 /** 76 * Counter of the device (if in counter mode). 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 PATCH /otp-devices/$ID operation. 90 * 91 * @param cls closure for this function 92 * @param result response being processed 93 */ 94 static void 95 patch_otp_device_cb (struct PatchOtpDeviceState *pis, 96 const struct TALER_MERCHANT_PatchPrivateOtpDeviceResponse * 97 result) 98 { 99 const struct TALER_MERCHANT_HttpResponse *hr = &result->hr; 100 101 pis->iph = NULL; 102 if (pis->http_status != hr->http_status) 103 { 104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 105 "Unexpected response code %u (%d) to command %s\n", 106 hr->http_status, 107 (int) hr->ec, 108 TALER_TESTING_interpreter_get_current_label (pis->is)); 109 TALER_TESTING_interpreter_fail (pis->is); 110 return; 111 } 112 switch (hr->http_status) 113 { 114 case MHD_HTTP_NO_CONTENT: 115 break; 116 case MHD_HTTP_UNAUTHORIZED: 117 break; 118 case MHD_HTTP_FORBIDDEN: 119 break; 120 case MHD_HTTP_NOT_FOUND: 121 break; 122 case MHD_HTTP_CONFLICT: 123 if (TALER_EC_MERCHANT_PRIVATE_PATCH_OTP_DEVICES_CONFLICT != hr->ec) 124 TALER_TESTING_FAIL (pis->is); 125 break; 126 default: 127 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 128 "Unhandled HTTP status %u for PATCH /otp-devices/ID.\n", 129 hr->http_status); 130 } 131 TALER_TESTING_interpreter_next (pis->is); 132 } 133 134 135 /** 136 * Run the "PATCH /otp-devices/$ID" CMD. 137 * 138 * 139 * @param cls closure. 140 * @param cmd command being run now. 141 * @param is interpreter state. 142 */ 143 static void 144 patch_otp_device_run (void *cls, 145 const struct TALER_TESTING_Command *cmd, 146 struct TALER_TESTING_Interpreter *is) 147 { 148 struct PatchOtpDeviceState *pis = cls; 149 150 pis->is = is; 151 pis->iph = TALER_MERCHANT_patch_private_otp_device_create ( 152 TALER_TESTING_interpreter_get_context (is), 153 pis->merchant_url, 154 pis->otp_device_id, 155 pis->otp_device_description, 156 pis->otp_key, 157 pis->otp_alg); 158 GNUNET_assert (NULL != pis->iph); 159 if (0 != pis->otp_ctr) 160 TALER_MERCHANT_patch_private_otp_device_set_options ( 161 pis->iph, 162 TALER_MERCHANT_patch_private_otp_device_option_otp_ctr (pis->otp_ctr)); 163 { 164 enum TALER_ErrorCode ec; 165 166 ec = TALER_MERCHANT_patch_private_otp_device_start ( 167 pis->iph, 168 &patch_otp_device_cb, 169 pis); 170 GNUNET_assert (TALER_EC_NONE == ec); 171 } 172 } 173 174 175 /** 176 * Offers information from the PATCH /otp-devices CMD state to other 177 * commands. 178 * 179 * @param cls closure 180 * @param[out] ret result (could be anything) 181 * @param trait name of the trait 182 * @param index index number of the object to extract. 183 * @return #GNUNET_OK on success 184 */ 185 static enum GNUNET_GenericReturnValue 186 patch_otp_device_traits (void *cls, 187 const void **ret, 188 const char *trait, 189 unsigned int index) 190 { 191 struct PatchOtpDeviceState *pts = cls; 192 struct TALER_TESTING_Trait traits[] = { 193 TALER_TESTING_make_trait_otp_device_description (pts->otp_device_description 194 ), 195 TALER_TESTING_make_trait_otp_key (pts->otp_key), 196 TALER_TESTING_make_trait_otp_alg (&pts->otp_alg), 197 TALER_TESTING_make_trait_otp_id (pts->otp_device_id), 198 TALER_TESTING_trait_end (), 199 }; 200 201 return TALER_TESTING_get_trait (traits, 202 ret, 203 trait, 204 index); 205 } 206 207 208 /** 209 * Free the state of a "GET otp_device" CMD, and possibly 210 * cancel a pending operation thereof. 211 * 212 * @param cls closure. 213 * @param cmd command being run. 214 */ 215 static void 216 patch_otp_device_cleanup (void *cls, 217 const struct TALER_TESTING_Command *cmd) 218 { 219 struct PatchOtpDeviceState *pis = cls; 220 221 if (NULL != pis->iph) 222 { 223 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 224 "PATCH /otp-devices/$ID operation did not complete\n"); 225 TALER_MERCHANT_patch_private_otp_device_cancel (pis->iph); 226 } 227 GNUNET_free (pis->otp_key); 228 GNUNET_free (pis); 229 } 230 231 232 struct TALER_TESTING_Command 233 TALER_TESTING_cmd_merchant_patch_otp_device ( 234 const char *label, 235 const char *merchant_url, 236 const char *otp_device_id, 237 const char *otp_device_description, 238 const char *otp_key, 239 const enum TALER_MerchantConfirmationAlgorithm otp_alg, 240 uint64_t otp_ctr, 241 unsigned int http_status) 242 { 243 struct PatchOtpDeviceState *pis; 244 245 pis = GNUNET_new (struct PatchOtpDeviceState); 246 pis->merchant_url = merchant_url; 247 pis->otp_device_id = otp_device_id; 248 pis->http_status = http_status; 249 pis->otp_device_description = otp_device_description; 250 pis->otp_key = (NULL == otp_key) 251 ? NULL 252 : GNUNET_strdup (otp_key); 253 pis->otp_alg = otp_alg; 254 pis->otp_ctr = otp_ctr; 255 { 256 struct TALER_TESTING_Command cmd = { 257 .cls = pis, 258 .label = label, 259 .run = &patch_otp_device_run, 260 .cleanup = &patch_otp_device_cleanup, 261 .traits = &patch_otp_device_traits 262 }; 263 264 return cmd; 265 } 266 } 267 268 269 /* end of testing_api_cmd_patch_otp_device.c */