testing_api_helpers.c (5195B)
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_OS_Process * 33 ANASTASIS_TESTING_run_anastasis (const char *config_filename, 34 const char *anastasis_url) 35 { 36 struct GNUNET_OS_Process *anastasis_proc; 37 unsigned int iter; 38 char *wget_cmd; 39 40 anastasis_proc 41 = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ALL, 42 NULL, NULL, NULL, 43 "anastasis-httpd", 44 "anastasis-httpd", 45 "--log=INFO", 46 "-c", config_filename, 47 NULL); 48 if (NULL == anastasis_proc) 49 ANASTASIS_FAIL (); 50 51 GNUNET_asprintf (&wget_cmd, 52 "wget -q -t 1 -T 1" 53 " %s" 54 " -o /dev/null -O /dev/null", 55 anastasis_url); 56 57 /* give child time to start and bind against the socket */ 58 fprintf (stderr, 59 "Waiting for `anastasis-httpd' to be ready\n"); 60 iter = 0; 61 do 62 { 63 if (100 == iter) 64 { 65 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 66 "Failed to launch `anastasis-httpd' (or `wget')\n"); 67 GNUNET_break (0 == 68 GNUNET_OS_process_kill (anastasis_proc, 69 SIGTERM)); 70 GNUNET_OS_process_wait (anastasis_proc); 71 GNUNET_OS_process_destroy (anastasis_proc); 72 ANASTASIS_FAIL (); 73 } 74 { 75 struct timespec req = { 76 .tv_nsec = 10000 77 }; 78 79 nanosleep (&req, 80 NULL); 81 } 82 iter++; 83 } 84 while (0 != system (wget_cmd)); 85 GNUNET_free (wget_cmd); 86 fprintf (stderr, 87 "\n"); 88 return anastasis_proc; 89 } 90 91 92 char * 93 ANASTASIS_TESTING_prepare_anastasis (const char *config_filename) 94 { 95 struct GNUNET_CONFIGURATION_Handle *cfg; 96 unsigned long long port; 97 struct GNUNET_OS_Process *dbinit_proc; 98 enum GNUNET_OS_ProcessStatusType type; 99 unsigned long code; 100 char *base_url; 101 102 cfg = GNUNET_CONFIGURATION_create (ANASTASIS_project_data ()); 103 if (GNUNET_OK != 104 GNUNET_CONFIGURATION_load (cfg, 105 config_filename)) 106 ANASTASIS_FAIL (); 107 if (GNUNET_OK != 108 GNUNET_CONFIGURATION_get_value_number (cfg, 109 "anastasis", 110 "PORT", 111 &port)) 112 { 113 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 114 "anastasis", 115 "PORT"); 116 GNUNET_CONFIGURATION_destroy (cfg); 117 return NULL; 118 } 119 120 GNUNET_CONFIGURATION_destroy (cfg); 121 122 if (GNUNET_OK != 123 GNUNET_NETWORK_test_port_free (IPPROTO_TCP, 124 (uint16_t) port)) 125 { 126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 127 "Required port %llu not available, skipping.\n", 128 port); 129 return NULL; 130 } 131 132 /* DB preparation */ 133 if (NULL == (dbinit_proc = GNUNET_OS_start_process 134 (GNUNET_OS_INHERIT_STD_ALL, 135 NULL, NULL, NULL, 136 "anastasis-dbinit", 137 "anastasis-dbinit", 138 "-c", config_filename, 139 "-r", 140 NULL))) 141 { 142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 143 "Failed to run anastasis-dbinit. Check your PATH.\n"); 144 return NULL; 145 } 146 147 if (GNUNET_SYSERR == 148 GNUNET_OS_process_wait_status (dbinit_proc, 149 &type, 150 &code)) 151 { 152 GNUNET_OS_process_destroy (dbinit_proc); 153 ANASTASIS_FAIL (); 154 } 155 if ( (type == GNUNET_OS_PROCESS_EXITED) && 156 (0 != code) ) 157 { 158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 159 "Failed to setup database\n"); 160 return NULL; 161 } 162 if ( (type != GNUNET_OS_PROCESS_EXITED) || 163 (0 != code) ) 164 { 165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 166 "Unexpected error running `anastasis-dbinit'!\n"); 167 return NULL; 168 } 169 GNUNET_OS_process_destroy (dbinit_proc); 170 GNUNET_asprintf (&base_url, 171 "http://localhost:%llu/", 172 port); 173 return base_url; 174 }