diff options
Diffstat (limited to 'src/testing/testing_api_helpers.c')
-rw-r--r-- | src/testing/testing_api_helpers.c | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/src/testing/testing_api_helpers.c b/src/testing/testing_api_helpers.c new file mode 100644 index 0000000..66e7032 --- /dev/null +++ b/src/testing/testing_api_helpers.c | |||
@@ -0,0 +1,173 @@ | |||
1 | /* | ||
2 | This file is part of ANASTASIS | ||
3 | Copyright (C) 2014-2021 Taler Systems SA | ||
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 lib/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_OS_process_kill (anastasis_proc, | ||
68 | SIGTERM); | ||
69 | GNUNET_OS_process_wait (anastasis_proc); | ||
70 | GNUNET_OS_process_destroy (anastasis_proc); | ||
71 | ANASTASIS_FAIL (); | ||
72 | } | ||
73 | { | ||
74 | struct timespec req = { | ||
75 | .tv_nsec = 10000 | ||
76 | }; | ||
77 | |||
78 | nanosleep (&req, | ||
79 | NULL); | ||
80 | } | ||
81 | iter++; | ||
82 | } | ||
83 | while (0 != system (wget_cmd)); | ||
84 | GNUNET_free (wget_cmd); | ||
85 | fprintf (stderr, | ||
86 | "\n"); | ||
87 | return anastasis_proc; | ||
88 | } | ||
89 | |||
90 | |||
91 | char * | ||
92 | ANASTASIS_TESTING_prepare_anastasis (const char *config_filename) | ||
93 | { | ||
94 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
95 | unsigned long long port; | ||
96 | struct GNUNET_OS_Process *dbinit_proc; | ||
97 | enum GNUNET_OS_ProcessStatusType type; | ||
98 | unsigned long code; | ||
99 | char *base_url; | ||
100 | |||
101 | cfg = GNUNET_CONFIGURATION_create (); | ||
102 | if (GNUNET_OK != | ||
103 | GNUNET_CONFIGURATION_load (cfg, | ||
104 | config_filename)) | ||
105 | ANASTASIS_FAIL (); | ||
106 | if (GNUNET_OK != | ||
107 | GNUNET_CONFIGURATION_get_value_number (cfg, | ||
108 | "anastasis", | ||
109 | "PORT", | ||
110 | &port)) | ||
111 | { | ||
112 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
113 | "anastasis", | ||
114 | "PORT"); | ||
115 | GNUNET_CONFIGURATION_destroy (cfg); | ||
116 | return NULL; | ||
117 | } | ||
118 | |||
119 | GNUNET_CONFIGURATION_destroy (cfg); | ||
120 | |||
121 | if (GNUNET_OK != | ||
122 | GNUNET_NETWORK_test_port_free (IPPROTO_TCP, | ||
123 | (uint16_t) port)) | ||
124 | { | ||
125 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
126 | "Required port %llu not available, skipping.\n", | ||
127 | port); | ||
128 | return NULL; | ||
129 | } | ||
130 | |||
131 | /* DB preparation */ | ||
132 | if (NULL == (dbinit_proc = GNUNET_OS_start_process | ||
133 | (GNUNET_OS_INHERIT_STD_ALL, | ||
134 | NULL, NULL, NULL, | ||
135 | "anastasis-dbinit", | ||
136 | "anastasis-dbinit", | ||
137 | "-c", config_filename, | ||
138 | "-r", | ||
139 | NULL))) | ||
140 | { | ||
141 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
142 | "Failed to run anastasis-dbinit. Check your PATH.\n"); | ||
143 | return NULL; | ||
144 | } | ||
145 | |||
146 | if (GNUNET_SYSERR == | ||
147 | GNUNET_OS_process_wait_status (dbinit_proc, | ||
148 | &type, | ||
149 | &code)) | ||
150 | { | ||
151 | GNUNET_OS_process_destroy (dbinit_proc); | ||
152 | ANASTASIS_FAIL (); | ||
153 | } | ||
154 | if ( (type == GNUNET_OS_PROCESS_EXITED) && | ||
155 | (0 != code) ) | ||
156 | { | ||
157 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
158 | "Failed to setup database\n"); | ||
159 | return NULL; | ||
160 | } | ||
161 | if ( (type != GNUNET_OS_PROCESS_EXITED) || | ||
162 | (0 != code) ) | ||
163 | { | ||
164 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
165 | "Unexpected error running `anastasis-dbinit'!\n"); | ||
166 | return NULL; | ||
167 | } | ||
168 | GNUNET_OS_process_destroy (dbinit_proc); | ||
169 | GNUNET_asprintf (&base_url, | ||
170 | "http://localhost:%llu/", | ||
171 | port); | ||
172 | return base_url; | ||
173 | } | ||