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