testing_api_helpers.c (7296B)
1 /* 2 This file is part of ANASTASIS 3 Copyright (C) 2014-2021 Anastasis SARL 4 5 ANASTASIS 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 ANASTASIS is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 ANASTASISABILITY 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 ANASTASIS; see the file COPYING. If not, see 17 <http://www.gnu.org/licenses/> 18 */ 19 20 /** 21 * @file anastasis/src/testing/testing_api_helpers.c 22 * @brief helper functions for test library. 23 * @author Christian Grothoff 24 * @author Marcello Stanisci 25 */ 26 27 #include "platform.h" 28 #include "anastasis_testing_lib.h" 29 #include <gnunet/gnunet_curl_lib.h> 30 31 32 struct GNUNET_Process * 33 ANASTASIS_TESTING_run_anastasis (const char *config_filename, 34 const char *anastasis_url) 35 { 36 struct GNUNET_Process *anastasis_proc; 37 unsigned int iter; 38 char *wget_cmd; 39 40 anastasis_proc = GNUNET_process_create (GNUNET_OS_INHERIT_STD_ALL); 41 if (GNUNET_OK != 42 GNUNET_process_run_command_va (anastasis_proc, 43 "anastasis-httpd", 44 "anastasis-httpd", 45 "--log=INFO", 46 "-c", config_filename, 47 NULL)) 48 { 49 GNUNET_process_destroy (anastasis_proc); 50 ANASTASIS_FAIL (); 51 } 52 53 GNUNET_asprintf (&wget_cmd, 54 "wget -q -t 1 -T 1" 55 " %s" 56 " -o /dev/null -O /dev/null", 57 anastasis_url); 58 59 /* give child time to start and bind against the socket */ 60 fprintf (stderr, 61 "Waiting for `anastasis-httpd' to be ready\n"); 62 iter = 0; 63 do 64 { 65 if (100 == iter) 66 { 67 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 68 "Failed to launch `anastasis-httpd' (or `wget')\n"); 69 GNUNET_break (GNUNET_OK == 70 GNUNET_process_kill (anastasis_proc, 71 SIGTERM)); 72 GNUNET_break (GNUNET_OK == 73 GNUNET_process_wait (anastasis_proc, 74 true, 75 NULL, 76 NULL)); 77 GNUNET_process_destroy (anastasis_proc); 78 ANASTASIS_FAIL (); 79 } 80 { 81 struct timespec req = { 82 /* 10 ms (was 10000 ns = 10 us, far too short on loaded CI hosts) */ 83 .tv_nsec = 10 * 1000 * 1000 84 }; 85 86 nanosleep (&req, 87 NULL); 88 } 89 iter++; 90 } 91 while (0 != system (wget_cmd)); 92 GNUNET_free (wget_cmd); 93 fprintf (stderr, 94 "\n"); 95 return anastasis_proc; 96 } 97 98 99 char * 100 ANASTASIS_TESTING_make_file_challenge_name (const char *config_filename) 101 { 102 struct GNUNET_CONFIGURATION_Handle *cfg; 103 char *dir; 104 char *tmpl; 105 char *fn; 106 107 cfg = GNUNET_CONFIGURATION_create (ANASTASIS_project_data ()); 108 if (GNUNET_OK != 109 GNUNET_CONFIGURATION_load (cfg, 110 config_filename)) 111 ANASTASIS_FAIL (); 112 if (GNUNET_OK != 113 GNUNET_CONFIGURATION_get_value_filename (cfg, 114 "authorization-file", 115 "DIRECTORY", 116 &dir)) 117 { 118 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 119 "authorization-file", 120 "DIRECTORY"); 121 GNUNET_CONFIGURATION_destroy (cfg); 122 return NULL; 123 } 124 GNUNET_CONFIGURATION_destroy (cfg); 125 if (GNUNET_OK != 126 GNUNET_DISK_directory_create (dir)) 127 { 128 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 129 "mkdir", 130 dir); 131 GNUNET_free (dir); 132 return NULL; 133 } 134 /* The plugin only writes inside DIRECTORY, so the scratch directory has to 135 live there too; a private subdirectory keeps concurrent runs apart. */ 136 GNUNET_asprintf (&tmpl, 137 "%s/test-anastasis-file-XXXXXX", 138 dir); 139 GNUNET_free (dir); 140 if (NULL == mkdtemp (tmpl)) 141 { 142 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 143 "mkdtemp", 144 tmpl); 145 GNUNET_free (tmpl); 146 return NULL; 147 } 148 GNUNET_asprintf (&fn, 149 "%s/.secret", 150 tmpl); 151 GNUNET_free (tmpl); 152 return fn; 153 } 154 155 156 char * 157 ANASTASIS_TESTING_prepare_anastasis (const char *config_filename) 158 { 159 struct GNUNET_CONFIGURATION_Handle *cfg; 160 unsigned long long port; 161 struct GNUNET_Process *dbinit_proc; 162 enum GNUNET_OS_ProcessStatusType type; 163 unsigned long code; 164 char *base_url; 165 166 cfg = GNUNET_CONFIGURATION_create (ANASTASIS_project_data ()); 167 if (GNUNET_OK != 168 GNUNET_CONFIGURATION_load (cfg, 169 config_filename)) 170 ANASTASIS_FAIL (); 171 if (GNUNET_OK != 172 GNUNET_CONFIGURATION_get_value_number (cfg, 173 "anastasis", 174 "PORT", 175 &port)) 176 { 177 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 178 "anastasis", 179 "PORT"); 180 GNUNET_CONFIGURATION_destroy (cfg); 181 return NULL; 182 } 183 184 GNUNET_CONFIGURATION_destroy (cfg); 185 186 if (GNUNET_OK != 187 GNUNET_NETWORK_test_port_free (IPPROTO_TCP, 188 (uint16_t) port)) 189 { 190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 191 "Required port %llu not available, skipping.\n", 192 port); 193 return NULL; 194 } 195 196 /* DB preparation */ 197 dbinit_proc = GNUNET_process_create (GNUNET_OS_INHERIT_STD_ALL); 198 if (GNUNET_OK != 199 GNUNET_process_run_command_va (dbinit_proc, 200 "anastasis-dbinit", 201 "anastasis-dbinit", 202 "-c", config_filename, 203 "-r", 204 NULL)) 205 { 206 GNUNET_process_destroy (dbinit_proc); 207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 208 "Failed to run anastasis-dbinit. Check your PATH.\n"); 209 return NULL; 210 } 211 212 if (GNUNET_SYSERR == 213 GNUNET_process_wait (dbinit_proc, 214 true, 215 &type, 216 &code)) 217 { 218 GNUNET_process_destroy (dbinit_proc); 219 ANASTASIS_FAIL (); 220 } 221 GNUNET_process_destroy (dbinit_proc); 222 if ( (type == GNUNET_OS_PROCESS_EXITED) && 223 (0 != code) ) 224 { 225 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 226 "Failed to setup database\n"); 227 return NULL; 228 } 229 if ( (type != GNUNET_OS_PROCESS_EXITED) || 230 (0 != code) ) 231 { 232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 233 "Unexpected error running `anastasis-dbinit'!\n"); 234 return NULL; 235 } 236 GNUNET_asprintf (&base_url, 237 "http://localhost:%llu/", 238 port); 239 return base_url; 240 }