Line data Source code
1 : /*
2 : This file is part of SYNC
3 : Copyright (C) 2014-2019 Taler Systems SA
4 :
5 : SYNC 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 : SYNC is distributed in the hope that it will be useful, but
11 : WITHOUT ANY WARRANTY; without even the implied warranty of
12 : SYNCABILITY 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 SYNC; see the file COPYING. If not, see
17 : <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file lib/testing_api_helpers.c
21 : * @brief helper functions for test library.
22 : * @author Christian Grothoff
23 : * @author Marcello Stanisci
24 : */
25 : #include "platform.h"
26 : #include <taler/taler_testing_lib.h>
27 : #include "sync_testing_lib.h"
28 : #include <gnunet/gnunet_curl_lib.h>
29 :
30 :
31 : /**
32 : * Start the sync backend process. Assume the port
33 : * is available and the database is clean. Use the "prepare
34 : * sync" function to do such tasks.
35 : *
36 : * @param config_filename configuration filename.
37 : *
38 : * @return the process, or NULL if the process could not
39 : * be started.
40 : */
41 : struct GNUNET_OS_Process *
42 0 : SYNC_TESTING_run_sync (const char *config_filename,
43 : const char *sync_url)
44 : {
45 : struct GNUNET_OS_Process *sync_proc;
46 : unsigned int iter;
47 : char *wget_cmd;
48 :
49 : sync_proc
50 0 : = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ALL,
51 : NULL, NULL, NULL,
52 : "sync-httpd",
53 : "sync-httpd",
54 : "--log=INFO",
55 : "-c", config_filename,
56 : NULL);
57 0 : if (NULL == sync_proc)
58 : {
59 0 : GNUNET_break (0);
60 0 : return NULL;
61 : }
62 0 : GNUNET_asprintf (&wget_cmd,
63 : "wget -q -t 1 -T 1"
64 : " %s"
65 : " -o /dev/null -O /dev/null",
66 : sync_url);
67 :
68 : /* give child time to start and bind against the socket */
69 0 : fprintf (stderr,
70 : "Waiting for `sync-httpd' to be ready\n");
71 0 : iter = 0;
72 : do
73 : {
74 0 : if (10 == iter)
75 : {
76 0 : fprintf (stderr,
77 : "Failed to launch"
78 : " `sync-httpd' (or `wget')\n");
79 0 : GNUNET_OS_process_kill (sync_proc,
80 : SIGTERM);
81 0 : GNUNET_OS_process_wait (sync_proc);
82 0 : GNUNET_OS_process_destroy (sync_proc);
83 0 : GNUNET_break (0);
84 0 : return NULL;
85 : }
86 0 : fprintf (stderr, ".\n");
87 0 : sleep (1);
88 0 : iter++;
89 : }
90 0 : while (0 != system (wget_cmd));
91 0 : GNUNET_free (wget_cmd);
92 0 : fprintf (stderr, "\n");
93 0 : return sync_proc;
94 : }
95 :
96 :
97 : /**
98 : * Prepare the sync execution. Create tables and check if
99 : * the port is available.
100 : *
101 : * @param config_filename configuration filename.
102 : * @return the base url, or NULL upon errors. Must be freed
103 : * by the caller.
104 : */
105 : char *
106 0 : SYNC_TESTING_prepare_sync (const char *config_filename)
107 : {
108 : struct GNUNET_CONFIGURATION_Handle *cfg;
109 : unsigned long long port;
110 : struct GNUNET_OS_Process *dbinit_proc;
111 : enum GNUNET_OS_ProcessStatusType type;
112 : unsigned long code;
113 : char *base_url;
114 :
115 0 : cfg = GNUNET_CONFIGURATION_create ();
116 0 : if (GNUNET_OK !=
117 0 : GNUNET_CONFIGURATION_load (cfg,
118 : config_filename))
119 : {
120 0 : GNUNET_break (0);
121 0 : return NULL;
122 : }
123 0 : if (GNUNET_OK !=
124 0 : GNUNET_CONFIGURATION_get_value_number (cfg,
125 : "sync",
126 : "PORT",
127 : &port))
128 : {
129 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
130 : "sync",
131 : "PORT");
132 0 : GNUNET_CONFIGURATION_destroy (cfg);
133 0 : GNUNET_break (0);
134 0 : return NULL;
135 : }
136 0 : GNUNET_CONFIGURATION_destroy (cfg);
137 0 : if (GNUNET_OK !=
138 0 : GNUNET_NETWORK_test_port_free (IPPROTO_TCP,
139 0 : (uint16_t) port))
140 : {
141 0 : fprintf (stderr,
142 : "Required port %llu not available, skipping.\n",
143 : port);
144 0 : GNUNET_break (0);
145 0 : return NULL;
146 : }
147 :
148 : /* DB preparation */
149 0 : if (NULL == (dbinit_proc = GNUNET_OS_start_process (
150 : GNUNET_OS_INHERIT_STD_ALL,
151 : NULL, NULL, NULL,
152 : "sync-dbinit",
153 : "sync-dbinit",
154 : "-c", config_filename,
155 : "-r",
156 : NULL)))
157 : {
158 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159 : "Failed to run sync-dbinit."
160 : " Check your PATH.\n");
161 0 : GNUNET_break (0);
162 0 : return NULL;
163 : }
164 :
165 0 : if (GNUNET_SYSERR ==
166 0 : GNUNET_OS_process_wait_status (dbinit_proc,
167 : &type,
168 : &code))
169 : {
170 0 : GNUNET_OS_process_destroy (dbinit_proc);
171 0 : GNUNET_break (0);
172 0 : return NULL;
173 : }
174 0 : if ( (type == GNUNET_OS_PROCESS_EXITED) &&
175 0 : (0 != code) )
176 : {
177 0 : fprintf (stderr,
178 : "Failed to setup database\n");
179 0 : GNUNET_break (0);
180 0 : return NULL;
181 : }
182 0 : if ( (type != GNUNET_OS_PROCESS_EXITED) ||
183 0 : (0 != code) )
184 : {
185 0 : fprintf (stderr,
186 : "Unexpected error running"
187 : " `sync-dbinit'!\n");
188 0 : GNUNET_break (0);
189 0 : return NULL;
190 : }
191 0 : GNUNET_OS_process_destroy (dbinit_proc);
192 0 : GNUNET_asprintf (&base_url,
193 : "http://localhost:%llu/",
194 : port);
195 0 : return base_url;
196 : }
|