testing_api_misc.c (6756B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2018-2023 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_misc.c 21 * @brief non-command functions useful for writing tests 22 * @author Christian Grothoff 23 */ 24 #include <donau_config.h> 25 #include <gnunet/gnunet_util_lib.h> 26 #include <taler/taler_testing_lib.h> 27 #include <taler/taler_fakebank_lib.h> 28 29 30 bool 31 DONAU_TESTING_has_in_name (const char *prog, 32 const char *marker) 33 { 34 size_t name_pos; 35 size_t pos; 36 37 if (! prog || ! marker) 38 return false; 39 40 pos = 0; 41 name_pos = 0; 42 while (prog[pos]) 43 { 44 if ('/' == prog[pos]) 45 name_pos = pos + 1; 46 pos++; 47 } 48 if (name_pos == pos) 49 return true; 50 return (NULL != strstr (prog + name_pos, 51 marker)); 52 } 53 54 55 /** 56 * Remove @a option directory from @a section in @a cfg. 57 * 58 * @return #GNUNET_OK on success 59 */ 60 static enum GNUNET_GenericReturnValue 61 remove_dir (const struct GNUNET_CONFIGURATION_Handle *cfg, 62 const char *section, 63 const char *option) 64 { 65 char *dir; 66 67 if (GNUNET_OK != 68 GNUNET_CONFIGURATION_get_value_filename (cfg, 69 section, 70 option, 71 &dir)) 72 { 73 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 74 section, 75 option); 76 return GNUNET_SYSERR; 77 } 78 if (GNUNET_YES == 79 GNUNET_DISK_directory_test (dir, 80 GNUNET_NO)) 81 GNUNET_break (GNUNET_OK == 82 GNUNET_DISK_directory_remove (dir)); 83 GNUNET_free (dir); 84 return GNUNET_OK; 85 } 86 87 88 enum GNUNET_GenericReturnValue 89 DONAU_TESTING_cleanup_files_cfg ( 90 void *cls, 91 const struct GNUNET_CONFIGURATION_Handle *cfg) 92 { 93 char *dir; 94 95 (void) cls; 96 if (GNUNET_OK != 97 GNUNET_CONFIGURATION_get_value_filename (cfg, 98 "SECM_TOFU_FILE", 99 &dir)) 100 { 101 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 102 "SECM_TOFU_FILE"); 103 return GNUNET_SYSERR; 104 } 105 if ( (0 != unlink (dir)) && 106 (ENOENT != errno) ) 107 { 108 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 109 "unlink", 110 dir); 111 GNUNET_free (dir); 112 return GNUNET_SYSERR; 113 } 114 GNUNET_free (dir); 115 if (GNUNET_OK != 116 remove_dir (cfg, 117 "donau-secmod-eddsa", 118 "KEY_DIR")) 119 return GNUNET_SYSERR; 120 if (GNUNET_OK != 121 remove_dir (cfg, 122 "donau-secmod-rsa", 123 "KEY_DIR")) 124 return GNUNET_SYSERR; 125 return GNUNET_OK; 126 } 127 128 129 const struct DONAU_DenomPublicKey * 130 DONAU_TESTING_find_pk ( 131 const struct DONAU_Keys *keys, 132 const struct TALER_Amount *amount, 133 bool age_restricted) 134 { 135 struct GNUNET_TIME_Timestamp now; 136 struct DONAU_DenomPublicKey *pk; 137 char *str; 138 139 now = GNUNET_TIME_timestamp_get (); 140 for (unsigned int i = 0; i<keys->num_denom_keys; i++) 141 { 142 pk = &keys->denom_keys[i]; 143 if ( (0 == TALER_amount_cmp (amount, 144 &pk->value)) && 145 (GNUNET_TIME_timestamp_cmp (now, 146 >=, 147 pk->valid_from)) && 148 (GNUNET_TIME_timestamp_cmp (now, 149 <, 150 pk->withdraw_valid_until)) && 151 (age_restricted == (0 != pk->key.age_mask.bits)) ) 152 return pk; 153 } 154 /* do 2nd pass to check if expiration times are to blame for 155 * failure */ 156 str = TALER_amount_to_string (amount); 157 for (unsigned int i = 0; i<keys->num_denom_keys; i++) 158 { 159 pk = &keys->denom_keys[i]; 160 if ( (0 == TALER_amount_cmp (amount, 161 &pk->value)) && 162 (GNUNET_TIME_timestamp_cmp (now, 163 <, 164 pk->valid_from) || 165 GNUNET_TIME_timestamp_cmp (now, 166 >, 167 pk->withdraw_valid_until) ) && 168 (age_restricted == (0 != pk->key.age_mask.bits)) ) 169 { 170 GNUNET_log 171 (GNUNET_ERROR_TYPE_WARNING, 172 "Have denomination key for `%s', but with wrong" 173 " expiration range %llu vs [%llu,%llu)\n", 174 str, 175 (unsigned long long) now.abs_time.abs_value_us, 176 (unsigned long long) pk->valid_from.abs_time.abs_value_us, 177 (unsigned long long) pk->withdraw_valid_until.abs_time.abs_value_us); 178 GNUNET_free (str); 179 return NULL; 180 } 181 } 182 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 183 "No denomination key for amount %s found\n", 184 str); 185 GNUNET_free (str); 186 return NULL; 187 } 188 189 190 int 191 DONAU_TESTING_wait_httpd_ready (const char *base_url) 192 { 193 char *wget_cmd; 194 unsigned int iter; 195 196 GNUNET_asprintf (&wget_cmd, 197 "wget -q -t 1 -T 1 %s -o /dev/null -O /dev/null", 198 base_url); // make sure ends with '/' 199 /* give child time to start and bind against the socket */ 200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 201 "Waiting for HTTP service to be ready (check with: %s)\n", 202 wget_cmd); 203 iter = 0; 204 do 205 { 206 if (10 == iter) 207 { 208 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 209 "Failed to launch HTTP service (or `wget')\n"); 210 GNUNET_free (wget_cmd); 211 return 77; 212 } 213 sleep (1); 214 iter++; 215 } 216 while (0 != system (wget_cmd)); 217 GNUNET_free (wget_cmd); 218 return 0; 219 } 220 221 222 enum GNUNET_GenericReturnValue 223 DONAU_TESTING_url_port_free (const char *url) 224 { 225 const char *port; 226 long pnum; 227 228 port = strrchr (url, 229 (unsigned char) ':'); 230 if (NULL == port) 231 pnum = 80; 232 else 233 pnum = strtol (port + 1, NULL, 10); 234 if (GNUNET_OK != 235 GNUNET_NETWORK_test_port_free (IPPROTO_TCP, 236 pnum)) 237 { 238 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 239 "Port %u not available.\n", 240 (unsigned int) pnum); 241 return GNUNET_SYSERR; 242 } 243 return GNUNET_OK; 244 }