testing_api_cmd_get_donau.c (5952B)
1 /* 2 This file is part of TALER 3 (C) 2024 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/testing_api_cmd_get_donau.c 21 * @brief Command to get an donau handle 22 * @author Christian Grothoff 23 * @author Lukas Matyja 24 */ 25 #include <donau_config.h> 26 #include <taler/taler_json_lib.h> 27 #include <gnunet/gnunet_curl_lib.h> 28 #include <taler/taler_testing_lib.h> 29 struct GetDonauState; 30 #define DONAU_GET_KEYS_RESULT_CLOSURE struct GetDonauState 31 #include "donau_testing_lib.h" 32 #include "donau_service.h" 33 34 /** 35 * State for a "get donau" CMD. 36 */ 37 struct GetDonauState 38 { 39 40 /** 41 * Our interpreter state. 42 */ 43 struct TALER_TESTING_Interpreter *is; 44 45 /** 46 * Donau handle we produced. 47 */ 48 struct DONAU_GetKeysHandle *donau; 49 50 /** 51 * Keys of the donau. 52 */ 53 struct DONAU_Keys *keys; 54 55 /** 56 * URL of the donau. 57 */ 58 char *donau_url; 59 60 /** 61 * Are we waiting for /keys before continuing? 62 */ 63 bool wait_for_keys; 64 }; 65 66 67 /** 68 * Function called with information about what keys the donau is using. 69 * 70 * @param ges closure 71 * @param kr response from /keys 72 * @param[in] keys the keys of the donau 73 */ 74 static void 75 cert_cb (struct GetDonauState *ges, 76 const struct DONAU_KeysResponse *kr, 77 struct DONAU_Keys *keys) 78 { 79 const struct DONAU_HttpResponse *hr = &kr->hr; 80 struct TALER_TESTING_Interpreter *is = ges->is; 81 82 ges->donau = NULL; 83 DONAU_keys_decref (ges->keys); 84 ges->keys = keys; 85 switch (hr->http_status) 86 { 87 case MHD_HTTP_OK: 88 if (ges->wait_for_keys) 89 { 90 ges->wait_for_keys = false; 91 TALER_TESTING_interpreter_next (is); 92 return; 93 } 94 return; 95 default: 96 GNUNET_break (0); 97 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 98 "/keys responded with HTTP status %u\n", 99 hr->http_status); 100 if (ges->wait_for_keys) 101 { 102 ges->wait_for_keys = false; 103 TALER_TESTING_interpreter_fail (is); 104 return; 105 } 106 return; 107 } 108 109 return; 110 } 111 112 113 /** 114 * Run the "get_donau" command. 115 * 116 * @param cls closure. 117 * @param cmd the command currently being executed. 118 * @param is the interpreter state. 119 */ 120 static void 121 get_donau_run (void *cls, 122 const struct TALER_TESTING_Command *cmd, 123 struct TALER_TESTING_Interpreter *is) 124 { 125 struct GetDonauState *ges = cls; 126 127 (void) cmd; 128 if (NULL == ges->donau_url) 129 { 130 GNUNET_break (0); 131 TALER_TESTING_interpreter_fail (is); 132 return; 133 } 134 135 ges->is = is; 136 ges->donau 137 = DONAU_get_keys (TALER_TESTING_interpreter_get_context (is), 138 ges->donau_url, 139 &cert_cb, 140 ges); 141 if (NULL == ges->donau) 142 { 143 GNUNET_break (0); 144 TALER_TESTING_interpreter_fail (is); 145 return; 146 } 147 if (! ges->wait_for_keys) 148 TALER_TESTING_interpreter_next (is); 149 } 150 151 152 /** 153 * Cleanup the state. 154 * 155 * @param cls closure. 156 * @param cmd the command which is being cleaned up. 157 */ 158 static void 159 get_donau_cleanup (void *cls, 160 const struct TALER_TESTING_Command *cmd) 161 { 162 struct GetDonauState *ges = cls; 163 164 (void) cmd; 165 if (NULL != ges->donau) 166 { 167 DONAU_get_keys_cancel (ges->donau); 168 ges->donau = NULL; 169 } 170 DONAU_keys_decref (ges->keys); 171 ges->keys = NULL; 172 GNUNET_free (ges->donau_url); 173 ges->donau_url = NULL; 174 GNUNET_free (ges); 175 } 176 177 178 /** 179 * Offer internal data to a "get_donau" CMD state to other commands. 180 * 181 * @param cls closure 182 * @param[out] ret result (could be anything) 183 * @param trait name of the trait 184 * @param index index number of the object to offer. 185 * @return #GNUNET_OK on success 186 */ 187 static enum GNUNET_GenericReturnValue 188 get_donau_traits (void *cls, 189 const void **ret, 190 const char *trait, 191 unsigned int index) 192 { 193 struct GetDonauState *ges = cls; 194 195 struct TALER_TESTING_Trait traits[] = { 196 TALER_TESTING_make_trait_donau_url (ges->donau_url), 197 TALER_TESTING_make_trait_donau_keys (ges->keys), 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 /** 210 * Get the base URL of the donau from @a cfg. 211 * 212 * @param cfg configuration to evaluate 213 * @return base URL of the donau according to @a cfg 214 */ 215 static char * 216 get_donau_base_url ( 217 const struct GNUNET_CONFIGURATION_Handle *cfg) 218 { 219 char *donau_url; 220 221 if (GNUNET_OK != 222 GNUNET_CONFIGURATION_get_value_string (cfg, 223 "donau", 224 "BASE_URL", 225 &donau_url)) 226 { 227 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 228 "donau", 229 "BASE_URL"); 230 return NULL; 231 } 232 return donau_url; 233 } 234 235 236 struct TALER_TESTING_Command 237 TALER_TESTING_cmd_get_donau ( 238 const char *label, 239 const struct GNUNET_CONFIGURATION_Handle *cfg, 240 bool wait_for_keys) 241 { 242 struct GetDonauState *ges; 243 244 ges = GNUNET_new (struct GetDonauState); 245 ges->donau_url = get_donau_base_url (cfg); 246 ges->wait_for_keys = wait_for_keys; 247 { 248 struct TALER_TESTING_Command cmd = { 249 .cls = ges, 250 .label = label, 251 .run = &get_donau_run, 252 .cleanup = &get_donau_cleanup, 253 .traits = &get_donau_traits, 254 .name = "donau" 255 }; 256 257 return cmd; 258 } 259 }