diff options
Diffstat (limited to 'src/lib/testing/testing_api_cmd_start_peer.c')
-rw-r--r-- | src/lib/testing/testing_api_cmd_start_peer.c | 298 |
1 files changed, 298 insertions, 0 deletions
diff --git a/src/lib/testing/testing_api_cmd_start_peer.c b/src/lib/testing/testing_api_cmd_start_peer.c new file mode 100644 index 000000000..e2a2c0fbf --- /dev/null +++ b/src/lib/testing/testing_api_cmd_start_peer.c | |||
@@ -0,0 +1,298 @@ | |||
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 | |||
32 | /** | ||
33 | * Generic logging shortcut | ||
34 | */ | ||
35 | #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__) | ||
36 | |||
37 | |||
38 | /** | ||
39 | * The run method of this cmd will start all services of a peer to test the transport service. | ||
40 | * | ||
41 | */ | ||
42 | static void | ||
43 | start_peer_run (void *cls, | ||
44 | struct GNUNET_TESTING_Interpreter *is) | ||
45 | { | ||
46 | struct GNUNET_TESTING_StartPeerState *sps = cls; | ||
47 | char *emsg = NULL; | ||
48 | struct GNUNET_PeerIdentity dummy; | ||
49 | const struct GNUNET_TESTING_Command *system_cmd; | ||
50 | const struct GNUNET_TESTING_System *tl_system; | ||
51 | char *home; | ||
52 | char *transport_unix_path; | ||
53 | char *tcp_communicator_unix_path; | ||
54 | char *udp_communicator_unix_path; | ||
55 | char *bindto; | ||
56 | char *bindto_udp; | ||
57 | |||
58 | if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname)) | ||
59 | { | ||
60 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
61 | "File not found: `%s'\n", | ||
62 | sps->cfgname); | ||
63 | GNUNET_TESTING_interpreter_fail (is); | ||
64 | return; | ||
65 | } | ||
66 | |||
67 | |||
68 | sps->cfg = GNUNET_CONFIGURATION_create (); | ||
69 | GNUNET_assert (GNUNET_OK == | ||
70 | GNUNET_CONFIGURATION_load (sps->cfg, sps->cfgname)); | ||
71 | |||
72 | GNUNET_asprintf (&home, | ||
73 | "$GNUNET_TMP/test-transport/api-tcp-p%u", | ||
74 | sps->no); | ||
75 | |||
76 | GNUNET_asprintf (&transport_unix_path, | ||
77 | "$GNUNET_RUNTIME_DIR/tng-p%u.sock", | ||
78 | sps->no); | ||
79 | |||
80 | GNUNET_asprintf (&tcp_communicator_unix_path, | ||
81 | "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock", | ||
82 | sps->no); | ||
83 | |||
84 | GNUNET_asprintf (&udp_communicator_unix_path, | ||
85 | "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock", | ||
86 | sps->no); | ||
87 | |||
88 | GNUNET_asprintf (&bindto, | ||
89 | "%s:60002", | ||
90 | sps->node_ip); | ||
91 | |||
92 | GNUNET_asprintf (&bindto_udp, | ||
93 | "2086"); | ||
94 | |||
95 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
96 | "node_ip %s\n", | ||
97 | bindto); | ||
98 | |||
99 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
100 | "bind_udp %s\n", | ||
101 | GNUNET_YES == sps->broadcast ? | ||
102 | bindto_udp : bindto); | ||
103 | |||
104 | GNUNET_CONFIGURATION_set_value_string (sps->cfg, "PATHS", "GNUNET_TEST_HOME", | ||
105 | home); | ||
106 | GNUNET_CONFIGURATION_set_value_string (sps->cfg, "transport", "UNIXPATH", | ||
107 | transport_unix_path); | ||
108 | GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp", | ||
109 | "BINDTO", | ||
110 | bindto); | ||
111 | GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp", | ||
112 | "BINDTO", | ||
113 | GNUNET_YES == sps->broadcast ? | ||
114 | bindto_udp : bindto); | ||
115 | GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp", | ||
116 | "UNIXPATH", | ||
117 | tcp_communicator_unix_path); | ||
118 | GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp", | ||
119 | "UNIXPATH", | ||
120 | udp_communicator_unix_path); | ||
121 | |||
122 | |||
123 | system_cmd = GNUNET_TESTING_interpreter_lookup_command (is, | ||
124 | sps->system_label); | ||
125 | GNUNET_TESTING_get_trait_test_system (system_cmd, | ||
126 | &tl_system); | ||
127 | |||
128 | sps->tl_system = tl_system; | ||
129 | |||
130 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
131 | "Creating testing library with key number %u\n", | ||
132 | sps->no); | ||
133 | |||
134 | if (GNUNET_SYSERR == | ||
135 | GNUNET_TESTING_configuration_create ((struct | ||
136 | GNUNET_TESTING_System *) tl_system, | ||
137 | sps->cfg)) | ||
138 | { | ||
139 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
140 | "Testing library failed to create unique configuration based on `%s'\n", | ||
141 | sps->cfgname); | ||
142 | GNUNET_CONFIGURATION_destroy (sps->cfg); | ||
143 | GNUNET_TESTING_interpreter_fail (is); | ||
144 | return; | ||
145 | } | ||
146 | |||
147 | sps->peer = GNUNET_TESTING_peer_configure ((struct | ||
148 | GNUNET_TESTING_System *) sps-> | ||
149 | tl_system, | ||
150 | sps->cfg, | ||
151 | sps->no, | ||
152 | NULL, | ||
153 | &emsg); | ||
154 | if (NULL == sps->peer) | ||
155 | { | ||
156 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
157 | "Testing library failed to create unique configuration based on `%s': `%s' with key number %u\n", | ||
158 | sps->cfgname, | ||
159 | emsg, | ||
160 | sps->no); | ||
161 | GNUNET_free (emsg); | ||
162 | GNUNET_TESTING_interpreter_fail (is); | ||
163 | return; | ||
164 | } | ||
165 | |||
166 | if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer)) | ||
167 | { | ||
168 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
169 | "Testing library failed to create unique configuration based on `%s'\n", | ||
170 | sps->cfgname); | ||
171 | GNUNET_free (emsg); | ||
172 | GNUNET_TESTING_interpreter_fail (is); | ||
173 | return; | ||
174 | } | ||
175 | |||
176 | memset (&dummy, | ||
177 | '\0', | ||
178 | sizeof(dummy)); | ||
179 | |||
180 | GNUNET_TESTING_peer_get_identity (sps->peer, | ||
181 | &sps->id); | ||
182 | |||
183 | if (0 == memcmp (&dummy, | ||
184 | &sps->id, | ||
185 | sizeof(struct GNUNET_PeerIdentity))) | ||
186 | { | ||
187 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
188 | "Testing library failed to obtain peer identity for peer %u\n", | ||
189 | sps->no); | ||
190 | GNUNET_free (emsg); | ||
191 | GNUNET_TESTING_interpreter_fail (is); | ||
192 | return; | ||
193 | } | ||
194 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
195 | "Peer %u configured with identity `%s'\n", | ||
196 | sps->no, | ||
197 | GNUNET_i2s_full (&sps->id)); | ||
198 | |||
199 | GNUNET_free (home); | ||
200 | GNUNET_free (transport_unix_path); | ||
201 | GNUNET_free (tcp_communicator_unix_path); | ||
202 | GNUNET_free (udp_communicator_unix_path); | ||
203 | GNUNET_free (bindto); | ||
204 | GNUNET_free (bindto_udp); | ||
205 | } | ||
206 | |||
207 | |||
208 | /** | ||
209 | * The cleanup function of this cmd frees resources the cmd allocated. | ||
210 | * | ||
211 | */ | ||
212 | static void | ||
213 | start_peer_cleanup (void *cls) | ||
214 | { | ||
215 | struct GNUNET_TESTING_StartPeerState *sps = cls; | ||
216 | |||
217 | //TODO Investigate why this caused problems during shutdown. | ||
218 | /*if (NULL != sps->cfg) | ||
219 | { | ||
220 | GNUNET_CONFIGURATION_destroy (sps->cfg); | ||
221 | sps->cfg = NULL; | ||
222 | }*/ | ||
223 | GNUNET_free (sps->cfgname); | ||
224 | GNUNET_free (sps->node_ip); | ||
225 | GNUNET_free (sps->system_label); | ||
226 | GNUNET_free (sps->hello); | ||
227 | GNUNET_free (sps->connected_peers_map); | ||
228 | GNUNET_free (sps); | ||
229 | } | ||
230 | |||
231 | |||
232 | /** | ||
233 | * This function prepares an array with traits. | ||
234 | * | ||
235 | */ | ||
236 | static int | ||
237 | start_peer_traits (void *cls, | ||
238 | const void **ret, | ||
239 | const char *trait, | ||
240 | unsigned int index) | ||
241 | { | ||
242 | struct GNUNET_TESTING_StartPeerState *sps = cls; | ||
243 | struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah; | ||
244 | struct GNUNET_PeerIdentity *id = &sps->id; | ||
245 | struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map = | ||
246 | sps->connected_peers_map; | ||
247 | char *hello = sps->hello; | ||
248 | size_t hello_size = sps->hello_size; | ||
249 | |||
250 | |||
251 | struct GNUNET_TESTING_Trait traits[] = { | ||
252 | GNUNET_TESTING_make_trait_application_handle ((const void *) ah), | ||
253 | GNUNET_TESTING_make_trait_peer_id ((const void *) id), | ||
254 | GNUNET_TESTING_make_trait_connected_peers_map ((const | ||
255 | void *) | ||
256 | connected_peers_map), | ||
257 | GNUNET_TESTING_make_trait_hello ((const void *) hello), | ||
258 | GNUNET_TESTING_make_trait_hello_size ((const void *) hello_size), | ||
259 | GNUNET_TESTING_make_trait_state ((const void *) sps), | ||
260 | GNUNET_TESTING_make_trait_broadcast ((const void *) &sps->broadcast), | ||
261 | GNUNET_TESTING_trait_end () | ||
262 | }; | ||
263 | |||
264 | return GNUNET_TESTING_get_trait (traits, | ||
265 | ret, | ||
266 | trait, | ||
267 | index); | ||
268 | } | ||
269 | |||
270 | |||
271 | struct GNUNET_TESTING_Command | ||
272 | GNUNET_TESTING_cmd_start_peer (const char *label, | ||
273 | const char *system_label, | ||
274 | uint32_t no, | ||
275 | const char *node_ip, | ||
276 | const char *cfgname, | ||
277 | unsigned int broadcast) | ||
278 | { | ||
279 | struct GNUNET_TESTING_StartPeerState *sps; | ||
280 | struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map = | ||
281 | GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO); | ||
282 | unsigned int i; | ||
283 | |||
284 | sps = GNUNET_new (struct GNUNET_TESTING_StartPeerState); | ||
285 | sps->no = no; | ||
286 | sps->system_label = GNUNET_strdup (system_label); | ||
287 | sps->connected_peers_map = connected_peers_map; | ||
288 | sps->cfgname = GNUNET_strdup (cfgname); | ||
289 | sps->node_ip = GNUNET_strdup (node_ip); | ||
290 | sps->broadcast = broadcast; | ||
291 | |||
292 | return GNUNET_TESTING_command_new (sps, | ||
293 | label, | ||
294 | &start_peer_run, | ||
295 | &start_peer_cleanup, | ||
296 | &start_peer_traits, | ||
297 | NULL); | ||
298 | } | ||