Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2018 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 : /**
21 : * @file testing_api_twister_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 <gnunet/gnunet_util_lib.h>
29 : #include "taler_twister_testing_lib.h"
30 :
31 : /**
32 : * Prepare twister for execution; mainly checks whether the
33 : * HTTP port is available and construct the base URL based on it.
34 : *
35 : * @param config_filename configuration file name.
36 : * @return twister base URL, NULL upon errors.
37 : */
38 : char *
39 3 : TALER_TWISTER_prepare_twister (const char *config_filename)
40 : {
41 : struct GNUNET_CONFIGURATION_Handle *cfg;
42 : unsigned long long port;
43 : char *base_url;
44 :
45 3 : cfg = GNUNET_CONFIGURATION_create ();
46 :
47 3 : if (GNUNET_OK !=
48 3 : GNUNET_CONFIGURATION_load (cfg,
49 : config_filename))
50 0 : TWISTER_FAIL ();
51 :
52 3 : if (GNUNET_OK !=
53 3 : GNUNET_CONFIGURATION_get_value_number (cfg,
54 : "twister",
55 : "HTTP_PORT",
56 : &port))
57 : {
58 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
59 : "twister",
60 : "HTTP_PORT");
61 0 : GNUNET_CONFIGURATION_destroy (cfg);
62 0 : TWISTER_FAIL ();
63 : }
64 :
65 3 : GNUNET_CONFIGURATION_destroy (cfg);
66 :
67 3 : if (GNUNET_OK !=
68 3 : GNUNET_NETWORK_test_port_free (IPPROTO_TCP,
69 3 : (uint16_t) port))
70 : {
71 0 : fprintf (stderr,
72 : "Required port %llu not available, skipping.\n",
73 : port);
74 0 : TWISTER_FAIL ();
75 : }
76 :
77 3 : GNUNET_assert (0 < GNUNET_asprintf
78 : (&base_url,
79 : "http://localhost:%llu/",
80 : port));
81 :
82 3 : return base_url;
83 : }
84 :
85 :
86 : /**
87 : * Run the twister service.
88 : *
89 : * @param config_filename configuration file name.
90 : * @return twister process handle, NULL upon errors.
91 : */
92 : struct GNUNET_OS_Process *
93 1 : TALER_TWISTER_run_twister (const char *config_filename)
94 : {
95 : struct GNUNET_OS_Process *proc;
96 : struct GNUNET_OS_Process *client_proc;
97 : unsigned long code;
98 : enum GNUNET_OS_ProcessStatusType type;
99 :
100 1 : proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ALL,
101 : NULL, NULL, NULL,
102 : "taler-twister-service",
103 : "taler-twister-service",
104 : "-c", config_filename,
105 : NULL);
106 1 : if (NULL == proc)
107 0 : TWISTER_FAIL ();
108 :
109 1 : client_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ALL,
110 : NULL, NULL, NULL,
111 : "taler-twister",
112 : "taler-twister",
113 : "-c", config_filename,
114 : "-a", NULL);
115 1 : if (NULL == client_proc)
116 : {
117 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118 : "Could not start the taler-twister client\n");
119 0 : GNUNET_OS_process_kill (proc, SIGTERM);
120 0 : GNUNET_OS_process_wait (proc);
121 0 : GNUNET_OS_process_destroy (proc);
122 0 : TWISTER_FAIL ();
123 : }
124 :
125 :
126 1 : if (GNUNET_SYSERR ==
127 1 : GNUNET_OS_process_wait_status (client_proc,
128 : &type,
129 : &code))
130 : {
131 0 : GNUNET_OS_process_destroy (client_proc);
132 0 : GNUNET_OS_process_kill (proc, SIGTERM);
133 0 : GNUNET_OS_process_wait (proc);
134 0 : GNUNET_OS_process_destroy (proc);
135 0 : TWISTER_FAIL ();
136 : }
137 1 : if ( (type == GNUNET_OS_PROCESS_EXITED) &&
138 1 : (0 != code) )
139 : {
140 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
141 : "Failed to check twister works.\n");
142 0 : GNUNET_OS_process_destroy (client_proc);
143 0 : GNUNET_OS_process_kill (proc, SIGTERM);
144 0 : GNUNET_OS_process_wait (proc);
145 0 : GNUNET_OS_process_destroy (proc);
146 0 : TWISTER_FAIL ();
147 : }
148 1 : if ( (type != GNUNET_OS_PROCESS_EXITED) ||
149 1 : (0 != code) )
150 : {
151 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
152 : "Unexpected error running `taler-twister'!\n");
153 0 : GNUNET_OS_process_destroy (client_proc);
154 0 : GNUNET_OS_process_kill (proc, SIGTERM);
155 0 : GNUNET_OS_process_wait (proc);
156 0 : GNUNET_OS_process_destroy (proc);
157 0 : TWISTER_FAIL ();
158 : }
159 1 : GNUNET_OS_process_destroy (client_proc);
160 :
161 1 : return proc;
162 : }
|