testing_api_cmd_get_instance.c (9260B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2020 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_instance.c 21 * @brief command to test GET /instance/$ID 22 * @author Christian Grothoff 23 */ 24 #include "platform.h" 25 #include <taler/taler_exchange_service.h> 26 #include <taler/taler_testing_lib.h> 27 #include "taler_merchant_service.h" 28 #include "taler_merchant_testing_lib.h" 29 30 31 /** 32 * State of a "GET instance" CMD. 33 */ 34 struct GetInstanceState 35 { 36 37 /** 38 * Handle for a "GET instance" request. 39 */ 40 struct TALER_MERCHANT_InstanceGetHandle *igh; 41 42 /** 43 * The interpreter state. 44 */ 45 struct TALER_TESTING_Interpreter *is; 46 47 /** 48 * Public key of the merchant instance. 49 */ 50 union TALER_AccountPublicKeyP account_pub; 51 52 /** 53 * Base URL of the merchant serving the request. 54 */ 55 const char *merchant_url; 56 57 /** 58 * ID of the instance to run GET for. 59 */ 60 const char *instance_id; 61 62 /** 63 * Reference for a POST or PATCH /instances CMD (optional). 64 */ 65 const char *instance_reference; 66 67 /** 68 * Expected HTTP response code. 69 */ 70 unsigned int http_status; 71 72 }; 73 74 75 /** 76 * Callback for a /get/instance/$ID operation. 77 * 78 * @param cls closure for this function 79 * @param igr response 80 */ 81 static void 82 get_instance_cb (void *cls, 83 const struct TALER_MERCHANT_InstanceGetResponse *igr) 84 { 85 struct GetInstanceState *gis = cls; 86 const struct TALER_TESTING_Command *instance_cmd; 87 88 instance_cmd = TALER_TESTING_interpreter_lookup_command ( 89 gis->is, 90 gis->instance_reference); 91 92 gis->igh = NULL; 93 if (gis->http_status != igr->hr.http_status) 94 { 95 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 96 "Unexpected response code %u (%d) to command %s\n", 97 igr->hr.http_status, 98 (int) igr->hr.ec, 99 TALER_TESTING_interpreter_get_current_label (gis->is)); 100 TALER_TESTING_interpreter_fail (gis->is); 101 return; 102 } 103 switch (igr->hr.http_status) 104 { 105 case MHD_HTTP_OK: 106 { 107 const struct TALER_MERCHANT_InstanceDetails *details = 108 &igr->details.ok.details; 109 110 gis->account_pub.merchant_pub 111 = details->merchant_pub; 112 { 113 const char *name; 114 115 if (GNUNET_OK != 116 TALER_TESTING_get_trait_instance_name (instance_cmd, 117 &name)) 118 TALER_TESTING_interpreter_fail (gis->is); 119 if (0 != strcmp (details->name, 120 name)) 121 { 122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 123 "Instance name does not match: Got `%s', wanted `%s'\n", 124 details->name, 125 name); 126 TALER_TESTING_interpreter_fail (gis->is); 127 return; 128 } 129 } 130 { 131 const json_t *address; 132 133 if (GNUNET_OK != 134 TALER_TESTING_get_trait_address (instance_cmd, 135 &address)) 136 TALER_TESTING_interpreter_fail (gis->is); 137 if (1 != json_equal (details->address, 138 address)) 139 { 140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 141 "Instance address does not match\n"); 142 TALER_TESTING_interpreter_fail (gis->is); 143 return; 144 } 145 } 146 { 147 const json_t *jurisdiction; 148 149 if (GNUNET_OK != 150 TALER_TESTING_get_trait_jurisdiction (instance_cmd, 151 &jurisdiction)) 152 TALER_TESTING_interpreter_fail (gis->is); 153 if (1 != json_equal (details->jurisdiction, 154 jurisdiction)) 155 { 156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 157 "Instance jurisdiction does not match\n"); 158 TALER_TESTING_interpreter_fail (gis->is); 159 return; 160 } 161 } 162 { 163 const bool *use_stefan; 164 165 if (GNUNET_OK != 166 TALER_TESTING_get_trait_use_stefan (instance_cmd, 167 &use_stefan)) 168 TALER_TESTING_interpreter_fail (gis->is); 169 if (*use_stefan != details->use_stefan) 170 { 171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 172 "Instance use_stefan value does not match\n"); 173 TALER_TESTING_interpreter_fail (gis->is); 174 return; 175 } 176 } 177 { 178 const struct GNUNET_TIME_Relative *default_wire_transfer_delay; 179 180 if (GNUNET_OK != 181 TALER_TESTING_get_trait_wire_delay (instance_cmd, 182 &default_wire_transfer_delay)) 183 TALER_TESTING_interpreter_fail (gis->is); 184 if (details->default_wire_transfer_delay.rel_value_us != 185 default_wire_transfer_delay->rel_value_us) 186 { 187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 188 "Instance default wire transfer delay does not match\n"); 189 TALER_TESTING_interpreter_fail (gis->is); 190 return; 191 } 192 } 193 { 194 const struct GNUNET_TIME_Relative *default_pay_delay; 195 196 if (GNUNET_OK != 197 TALER_TESTING_get_trait_pay_delay (instance_cmd, 198 &default_pay_delay)) 199 TALER_TESTING_interpreter_fail (gis->is); 200 if (details->default_pay_delay.rel_value_us != 201 default_pay_delay->rel_value_us) 202 { 203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 204 "Instance default pay delay does not match\n"); 205 TALER_TESTING_interpreter_fail (gis->is); 206 return; 207 } 208 } 209 } 210 break; 211 case MHD_HTTP_UNAUTHORIZED: 212 break; 213 case MHD_HTTP_NOT_FOUND: 214 break; 215 default: 216 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 217 "Unhandled HTTP status %u for GET instance ID.\n", 218 igr->hr.http_status); 219 } 220 TALER_TESTING_interpreter_next (gis->is); 221 } 222 223 224 /** 225 * Run the "GET instance" CMD. 226 * 227 * 228 * @param cls closure. 229 * @param cmd command being run now. 230 * @param is interpreter state. 231 */ 232 static void 233 get_instance_run (void *cls, 234 const struct TALER_TESTING_Command *cmd, 235 struct TALER_TESTING_Interpreter *is) 236 { 237 struct GetInstanceState *gis = cls; 238 239 gis->is = is; 240 gis->igh = TALER_MERCHANT_instance_get ( 241 TALER_TESTING_interpreter_get_context (is), 242 gis->merchant_url, 243 gis->instance_id, 244 &get_instance_cb, 245 gis); 246 GNUNET_assert (NULL != gis->igh); 247 } 248 249 250 /** 251 * Free the state of a "GET instance" CMD, and possibly 252 * cancel a pending operation thereof. 253 * 254 * @param cls closure. 255 * @param cmd command being run. 256 */ 257 static void 258 get_instance_cleanup (void *cls, 259 const struct TALER_TESTING_Command *cmd) 260 { 261 struct GetInstanceState *gis = cls; 262 263 if (NULL != gis->igh) 264 { 265 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 266 "GET /instances/$ID operation did not complete\n"); 267 TALER_MERCHANT_instance_get_cancel (gis->igh); 268 } 269 GNUNET_free (gis); 270 } 271 272 273 /** 274 * Offers information from the GET /instance/$ID CMD state to other 275 * commands. 276 * 277 * @param cls closure 278 * @param[out] ret result (could be anything) 279 * @param trait name of the trait 280 * @param index index number of the object to extract. 281 * @return #GNUNET_OK on success 282 */ 283 static enum GNUNET_GenericReturnValue 284 get_instance_traits (void *cls, 285 const void **ret, 286 const char *trait, 287 unsigned int index) 288 { 289 struct GetInstanceState *pps = cls; 290 struct TALER_TESTING_Trait traits[] = { 291 TALER_TESTING_make_trait_merchant_base_url ( 292 pps->merchant_url), 293 TALER_TESTING_make_trait_merchant_pub ( 294 &pps->account_pub.merchant_pub), 295 TALER_TESTING_make_trait_account_pub ( 296 &pps->account_pub), 297 TALER_TESTING_trait_end (), 298 }; 299 300 return TALER_TESTING_get_trait (traits, 301 ret, 302 trait, 303 index); 304 } 305 306 307 struct TALER_TESTING_Command 308 TALER_TESTING_cmd_merchant_get_instance ( 309 const char *label, 310 const char *merchant_url, 311 const char *instance_id, 312 unsigned int http_status, 313 const char *instance_reference) 314 { 315 struct GetInstanceState *gis; 316 317 gis = GNUNET_new (struct GetInstanceState); 318 gis->merchant_url = merchant_url; 319 gis->instance_id = instance_id; 320 gis->http_status = http_status; 321 gis->instance_reference = instance_reference; 322 { 323 struct TALER_TESTING_Command cmd = { 324 .cls = gis, 325 .label = label, 326 .run = &get_instance_run, 327 .cleanup = &get_instance_cleanup, 328 .traits = &get_instance_traits 329 }; 330 331 return cmd; 332 } 333 } 334 335 336 /* end of testing_api_cmd_get_instance.c */