aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing_api_cmd_start_peer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testing/testing_api_cmd_start_peer.c')
-rw-r--r--src/testing/testing_api_cmd_start_peer.c380
1 files changed, 380 insertions, 0 deletions
diff --git a/src/testing/testing_api_cmd_start_peer.c b/src/testing/testing_api_cmd_start_peer.c
new file mode 100644
index 000000000..f6b471be4
--- /dev/null
+++ b/src/testing/testing_api_cmd_start_peer.c
@@ -0,0 +1,380 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet 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 GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file testing_api_cmd_start_peer.c
23 * @brief cmd to start a peer.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "testing_cmds.h"
29#include "gnunet_testing_ng_lib.h"
30#include "gnunet_testing_netjail_lib.h"
31#include "gnunet_peerstore_service.h"
32#include "gnunet_transport_core_service.h"
33#include "gnunet_transport_application_service.h"
34
35/**
36 * Generic logging shortcut
37 */
38#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
39
40
41static void
42retrieve_hello (void *cls);
43
44
45/**
46 * Callback delivering the hello of this peer from peerstore.
47 *
48 */
49static void
50hello_iter_cb (void *cb_cls,
51 const struct GNUNET_PEERSTORE_Record *record,
52 const char *emsg)
53{
54 struct GNUNET_TESTING_StartPeerState *sps = cb_cls;
55 if (NULL == record)
56 {
57 sps->pic = NULL;
58 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
59 return;
60 }
61 // Check record type et al?
62 sps->hello_size = record->value_size;
63 sps->hello = GNUNET_malloc (sps->hello_size);
64 memcpy (sps->hello, record->value, sps->hello_size);
65 sps->hello[sps->hello_size - 1] = '\0';
66
67 LOG (GNUNET_ERROR_TYPE_DEBUG,
68 "Our hello %s\n",
69 sps->hello);
70 GNUNET_PEERSTORE_iterate_cancel (sps->pic);
71 sps->pic = NULL;
72 GNUNET_TESTING_async_finish (&sps->ac);
73}
74
75
76/**
77 * Function to start the retrieval task to retrieve the hello of this peer
78 * from the peerstore.
79 *
80 */
81static void
82retrieve_hello (void *cls)
83{
84 struct GNUNET_TESTING_StartPeerState *sps = cls;
85 sps->rh_task = NULL;
86 sps->pic = GNUNET_PEERSTORE_iterate (sps->ph,
87 "transport",
88 &sps->id,
89 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
90 hello_iter_cb,
91 sps);
92
93}
94
95
96/**
97 * The run method of this cmd will start all services of a peer to test the transport service.
98 *
99 */
100static void
101start_peer_run (void *cls,
102 struct GNUNET_TESTING_Interpreter *is)
103{
104 struct GNUNET_TESTING_StartPeerState *sps = cls;
105 char *emsg = NULL;
106 struct GNUNET_PeerIdentity dummy;
107 const struct GNUNET_TESTING_Command *system_cmd;
108 const struct GNUNET_TESTING_System *tl_system;
109 char *home;
110 char *transport_unix_path;
111 char *tcp_communicator_unix_path;
112 char *udp_communicator_unix_path;
113 char *bindto;
114 char *bindto_udp;
115
116 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname))
117 {
118 LOG (GNUNET_ERROR_TYPE_ERROR,
119 "File not found: `%s'\n",
120 sps->cfgname);
121 GNUNET_TESTING_interpreter_fail (is);
122 return;
123 }
124
125
126 sps->cfg = GNUNET_CONFIGURATION_create ();
127 GNUNET_assert (GNUNET_OK ==
128 GNUNET_CONFIGURATION_load (sps->cfg, sps->cfgname));
129
130 GNUNET_asprintf (&home,
131 "$GNUNET_TMP/test-transport/api-tcp-p%u",
132 sps->no);
133
134 GNUNET_asprintf (&transport_unix_path,
135 "$GNUNET_RUNTIME_DIR/tng-p%u.sock",
136 sps->no);
137
138 GNUNET_asprintf (&tcp_communicator_unix_path,
139 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
140 sps->no);
141
142 GNUNET_asprintf (&udp_communicator_unix_path,
143 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
144 sps->no);
145
146 GNUNET_asprintf (&bindto,
147 "%s:60002",
148 sps->node_ip);
149
150 GNUNET_asprintf (&bindto_udp,
151 "2086");
152
153 LOG (GNUNET_ERROR_TYPE_DEBUG,
154 "node_ip %s\n",
155 bindto);
156
157 LOG (GNUNET_ERROR_TYPE_DEBUG,
158 "bind_udp %s\n",
159 GNUNET_YES == sps->broadcast ?
160 bindto_udp : bindto);
161
162 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "PATHS", "GNUNET_TEST_HOME",
163 home);
164 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "transport", "UNIXPATH",
165 transport_unix_path);
166 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
167 "BINDTO",
168 bindto);
169 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
170 "BINDTO",
171 GNUNET_YES == sps->broadcast ?
172 bindto_udp : bindto);
173 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
174 "UNIXPATH",
175 tcp_communicator_unix_path);
176 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
177 "UNIXPATH",
178 udp_communicator_unix_path);
179
180
181 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
182 sps->system_label);
183 GNUNET_TESTING_get_trait_test_system (system_cmd,
184 &tl_system);
185
186 sps->tl_system = tl_system;
187
188 LOG (GNUNET_ERROR_TYPE_DEBUG,
189 "Creating testing library with key number %u\n",
190 sps->no);
191
192 if (GNUNET_SYSERR ==
193 GNUNET_TESTING_configuration_create ((struct
194 GNUNET_TESTING_System *) tl_system,
195 sps->cfg))
196 {
197 LOG (GNUNET_ERROR_TYPE_DEBUG,
198 "Testing library failed to create unique configuration based on `%s'\n",
199 sps->cfgname);
200 GNUNET_CONFIGURATION_destroy (sps->cfg);
201 GNUNET_TESTING_interpreter_fail (is);
202 return;
203 }
204
205 sps->peer = GNUNET_TESTING_peer_configure ((struct
206 GNUNET_TESTING_System *) sps->
207 tl_system,
208 sps->cfg,
209 sps->no,
210 NULL,
211 &emsg);
212 if (NULL == sps->peer)
213 {
214 LOG (GNUNET_ERROR_TYPE_ERROR,
215 "Testing library failed to create unique configuration based on `%s': `%s' with key number %u\n",
216 sps->cfgname,
217 emsg,
218 sps->no);
219 GNUNET_free (emsg);
220 GNUNET_TESTING_interpreter_fail (is);
221 return;
222 }
223
224 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer))
225 {
226 LOG (GNUNET_ERROR_TYPE_ERROR,
227 "Testing library failed to create unique configuration based on `%s'\n",
228 sps->cfgname);
229 GNUNET_free (emsg);
230 GNUNET_TESTING_interpreter_fail (is);
231 return;
232 }
233
234 memset (&dummy,
235 '\0',
236 sizeof(dummy));
237
238 GNUNET_TESTING_peer_get_identity (sps->peer,
239 &sps->id);
240
241 if (0 == memcmp (&dummy,
242 &sps->id,
243 sizeof(struct GNUNET_PeerIdentity)))
244 {
245 LOG (GNUNET_ERROR_TYPE_ERROR,
246 "Testing library failed to obtain peer identity for peer %u\n",
247 sps->no);
248 GNUNET_free (emsg);
249 GNUNET_TESTING_interpreter_fail (is);
250 return;
251 }
252 LOG (GNUNET_ERROR_TYPE_DEBUG,
253 "Peer %u configured with identity `%s'\n",
254 sps->no,
255 GNUNET_i2s_full (&sps->id));
256
257 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg);
258 if (NULL == sps->ph)
259 {
260 LOG (GNUNET_ERROR_TYPE_ERROR,
261 "Failed to connect to peerstore service for peer `%s': `%s'\n",
262 sps->cfgname,
263 emsg);
264 GNUNET_free (emsg);
265 GNUNET_TESTING_interpreter_fail (is);
266 return;
267 }
268
269 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg);
270 if (NULL == sps->ah)
271 {
272 LOG (GNUNET_ERROR_TYPE_ERROR,
273 "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n",
274 sps->cfgname,
275 emsg);
276 GNUNET_free (emsg);
277 GNUNET_TESTING_interpreter_fail (is);
278 return;
279 }
280 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
281 GNUNET_free (home);
282 GNUNET_free (transport_unix_path);
283 GNUNET_free (tcp_communicator_unix_path);
284 GNUNET_free (udp_communicator_unix_path);
285 GNUNET_free (bindto);
286 GNUNET_free (bindto_udp);
287}
288
289
290/**
291 * The cleanup function of this cmd frees resources the cmd allocated.
292 *
293 */
294static void
295start_peer_cleanup (void *cls)
296{
297 struct GNUNET_TESTING_StartPeerState *sps = cls;
298
299 //TODO Investigate why this caused problems during shutdown.
300 /*if (NULL != sps->cfg)
301 {
302 GNUNET_CONFIGURATION_destroy (sps->cfg);
303 sps->cfg = NULL;
304 }*/
305 GNUNET_free (sps->cfgname);
306 GNUNET_free (sps->node_ip);
307 GNUNET_free (sps->system_label);
308 GNUNET_free (sps->hello);
309 GNUNET_free (sps->connected_peers_map);
310 GNUNET_free (sps);
311}
312
313
314/**
315 * This function prepares an array with traits.
316 *
317 */
318static int
319start_peer_traits (void *cls,
320 const void **ret,
321 const char *trait,
322 unsigned int index)
323{
324 struct GNUNET_TESTING_StartPeerState *sps = cls;
325 struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah;
326 struct GNUNET_PeerIdentity *id = &sps->id;
327 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
328 sps->connected_peers_map;
329 char *hello = sps->hello;
330 size_t hello_size = sps->hello_size;
331
332
333 struct GNUNET_TESTING_Trait traits[] = {
334 GNUNET_TESTING_make_trait_application_handle ((const void *) ah),
335 GNUNET_TESTING_make_trait_peer_id ((const void *) id),
336 GNUNET_TESTING_make_trait_connected_peers_map ((const
337 void *)
338 connected_peers_map),
339 GNUNET_TESTING_make_trait_hello ((const void *) hello),
340 GNUNET_TESTING_make_trait_hello_size ((const void *) hello_size),
341 GNUNET_TESTING_make_trait_state ((const void *) sps),
342 GNUNET_TESTING_make_trait_broadcast ((const void *) &sps->broadcast),
343 GNUNET_TESTING_trait_end ()
344 };
345
346 return GNUNET_TESTING_get_trait (traits,
347 ret,
348 trait,
349 index);
350}
351
352
353struct GNUNET_TESTING_Command
354GNUNET_TESTING_cmd_start_peer (const char *label,
355 const char *system_label,
356 uint32_t no,
357 const char *node_ip,
358 const char *cfgname,
359 unsigned int broadcast)
360{
361 struct GNUNET_TESTING_StartPeerState *sps;
362 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
363 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO);
364 unsigned int i;
365
366 sps = GNUNET_new (struct GNUNET_TESTING_StartPeerState);
367 sps->no = no;
368 sps->system_label = GNUNET_strdup (system_label);
369 sps->connected_peers_map = connected_peers_map;
370 sps->cfgname = GNUNET_strdup (cfgname);
371 sps->node_ip = GNUNET_strdup (node_ip);
372 sps->broadcast = broadcast;
373
374 return GNUNET_TESTING_command_new (sps,
375 label,
376 &start_peer_run,
377 &start_peer_cleanup,
378 &start_peer_traits,
379 &sps->ac);
380}