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