diff options
Diffstat (limited to 'src/service/transport/test_transport_plugin_cmd_simple_send_broadcast.c')
-rw-r--r-- | src/service/transport/test_transport_plugin_cmd_simple_send_broadcast.c | 402 |
1 files changed, 402 insertions, 0 deletions
diff --git a/src/service/transport/test_transport_plugin_cmd_simple_send_broadcast.c b/src/service/transport/test_transport_plugin_cmd_simple_send_broadcast.c new file mode 100644 index 000000000..d2870ab34 --- /dev/null +++ b/src/service/transport/test_transport_plugin_cmd_simple_send_broadcast.c | |||
@@ -0,0 +1,402 @@ | |||
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 testbed/plugin_cmd_simple_send_broadcast.c | ||
23 | * @brief a plugin to provide the API for running test cases. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_testing_barrier.h" | ||
28 | #include "gnunet_testing_netjail_lib.h" | ||
29 | #include "gnunet_util_lib.h" | ||
30 | #include "gnunet_transport_application_service.h" | ||
31 | #include "transport-testing2.h" | ||
32 | #include "transport-testing-cmds.h" | ||
33 | #include "gnunet_testing_barrier.h" | ||
34 | |||
35 | /** | ||
36 | * Generic logging shortcut | ||
37 | */ | ||
38 | #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__) | ||
39 | |||
40 | #define BASE_DIR "testdir" | ||
41 | |||
42 | #define TOPOLOGY_CONFIG "test_transport_simple_send_topo.conf" | ||
43 | |||
44 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600) | ||
45 | |||
46 | static struct GNUNET_TESTING_Command block_send; | ||
47 | |||
48 | static struct GNUNET_TESTING_Command block_receive; | ||
49 | |||
50 | static struct GNUNET_TESTING_Command connect_peers; | ||
51 | |||
52 | static struct GNUNET_TESTING_Command local_prepared; | ||
53 | |||
54 | static struct GNUNET_TESTING_Interpreter *is; | ||
55 | |||
56 | /** | ||
57 | * Function called to check a message of type GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE being | ||
58 | * received. | ||
59 | * | ||
60 | */ | ||
61 | static int | ||
62 | check_test (void *cls, | ||
63 | const struct GNUNET_TRANSPORT_TESTING_TestMessage *message) | ||
64 | { | ||
65 | return GNUNET_OK; | ||
66 | } | ||
67 | |||
68 | |||
69 | /** | ||
70 | * Function called to handle a message of type GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE | ||
71 | * being received. | ||
72 | * | ||
73 | */ | ||
74 | static void | ||
75 | handle_test (void *cls, | ||
76 | const struct GNUNET_TRANSPORT_TESTING_TestMessage *message) | ||
77 | { | ||
78 | struct GNUNET_TESTING_AsyncContext *ac; | ||
79 | |||
80 | GNUNET_TESTING_get_trait_async_context (&block_receive, | ||
81 | &ac); | ||
82 | GNUNET_assert (NULL != ac); | ||
83 | if ((GNUNET_NO == ac->finished) && (NULL == ac->cont)) | ||
84 | GNUNET_TESTING_async_fail ((struct GNUNET_TESTING_AsyncContext *) ac); | ||
85 | else if (GNUNET_NO == ac->finished) | ||
86 | GNUNET_TESTING_async_finish ((struct GNUNET_TESTING_AsyncContext *) ac); | ||
87 | } | ||
88 | |||
89 | |||
90 | struct GNUNET_TESTING_BarrierList * | ||
91 | get_waiting_for_barriers () | ||
92 | { | ||
93 | // No Barrier | ||
94 | return GNUNET_new (struct GNUNET_TESTING_BarrierList); | ||
95 | } | ||
96 | |||
97 | |||
98 | /** | ||
99 | * Callback to set the flag indicating all peers started. Will be called via the plugin api. | ||
100 | * | ||
101 | */ | ||
102 | static void | ||
103 | all_peers_started () | ||
104 | { | ||
105 | struct GNUNET_TESTING_AsyncContext *ac; | ||
106 | |||
107 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
108 | "Received message\n"); | ||
109 | GNUNET_TESTING_get_trait_async_context (&block_send, | ||
110 | &ac); | ||
111 | GNUNET_assert (NULL != ac); | ||
112 | if (NULL == ac->cont) | ||
113 | GNUNET_TESTING_async_fail ((struct GNUNET_TESTING_AsyncContext *) ac); | ||
114 | else | ||
115 | GNUNET_TESTING_async_finish ((struct GNUNET_TESTING_AsyncContext *) ac); | ||
116 | } | ||
117 | |||
118 | |||
119 | /** | ||
120 | * Function called with the final result of the test. | ||
121 | * | ||
122 | * @param cls the `struct MainParams` | ||
123 | * @param rv #GNUNET_OK if the test passed | ||
124 | */ | ||
125 | static void | ||
126 | handle_result (void *cls, | ||
127 | enum GNUNET_GenericReturnValue rv) | ||
128 | { | ||
129 | struct TestState *ts = cls; | ||
130 | |||
131 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
132 | "Local test exits with status %d\n", | ||
133 | rv); | ||
134 | ts->finished_cb (rv); | ||
135 | GNUNET_free (ts->testdir); | ||
136 | GNUNET_free (ts->cfgname); | ||
137 | GNUNET_TESTING_free_topology (ts->topology); | ||
138 | GNUNET_free (ts); | ||
139 | } | ||
140 | |||
141 | |||
142 | /** | ||
143 | * Callback from start peer cmd for signaling a peer got connected. | ||
144 | * | ||
145 | */ | ||
146 | static void * | ||
147 | notify_connect (struct GNUNET_TESTING_Interpreter *is, | ||
148 | const struct GNUNET_PeerIdentity *peer) | ||
149 | { | ||
150 | struct GNUNET_TESTING_AsyncContext *ac; | ||
151 | void *ret = NULL; | ||
152 | const struct GNUNET_TESTING_Command *cmd; | ||
153 | struct GNUNET_TESTING_BlockState *bs; | ||
154 | |||
155 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
156 | "notify_connect\n"); | ||
157 | |||
158 | GNUNET_TESTING_get_trait_async_context (&connect_peers, | ||
159 | &ac); | ||
160 | |||
161 | if (NULL != ac->is) | ||
162 | { | ||
163 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
164 | "notify_connect running\n"); | ||
165 | GNUNET_assert (NULL != ac); | ||
166 | if (NULL == ac->cont) | ||
167 | GNUNET_TESTING_async_fail (ac); | ||
168 | else | ||
169 | GNUNET_TESTING_async_finish (ac); | ||
170 | } | ||
171 | else | ||
172 | { | ||
173 | cmd = GNUNET_TESTING_interpreter_lookup_future_command (is, | ||
174 | "connect-peers"); | ||
175 | |||
176 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
177 | "block state %s\n", | ||
178 | cmd->label); | ||
179 | GNUNET_TESTING_get_trait_block_state ( | ||
180 | cmd, | ||
181 | &bs); | ||
182 | |||
183 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
184 | "block state %u\n", | ||
185 | bs->asynchronous_finish); | ||
186 | bs->asynchronous_finish = GNUNET_YES; | ||
187 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
188 | "block state %u\n", | ||
189 | bs->asynchronous_finish); | ||
190 | } | ||
191 | |||
192 | return ret; | ||
193 | } | ||
194 | |||
195 | |||
196 | /** | ||
197 | * Callback to set the flag indicating all peers are prepared to finish. Will be called via the plugin api. | ||
198 | */ | ||
199 | static void | ||
200 | all_local_tests_prepared () | ||
201 | { | ||
202 | const struct GNUNET_TESTING_LocalPreparedState *lfs; | ||
203 | |||
204 | GNUNET_TESTING_get_trait_local_prepared_state (&local_prepared, | ||
205 | &lfs); | ||
206 | GNUNET_assert (NULL != &lfs->ac); | ||
207 | if (NULL == lfs->ac.cont) | ||
208 | GNUNET_TESTING_async_fail ((struct GNUNET_TESTING_AsyncContext *) &lfs->ac); | ||
209 | else | ||
210 | GNUNET_TESTING_async_finish ((struct | ||
211 | GNUNET_TESTING_AsyncContext *) &lfs->ac); | ||
212 | } | ||
213 | |||
214 | |||
215 | /** | ||
216 | * Function to start a local test case. | ||
217 | * | ||
218 | * @param write_message Callback to send a message to the master loop. | ||
219 | * @param router_ip Global address of the network namespace. | ||
220 | * @param node_ip The IP address of the node. | ||
221 | * @param m The number of the node in a network namespace. | ||
222 | * @param n The number of the network namespace. | ||
223 | * @param local_m The number of nodes in a network namespace. | ||
224 | */ | ||
225 | static struct GNUNET_TESTING_Interpreter * | ||
226 | start_testcase (GNUNET_TESTING_cmd_helper_write_cb write_message, | ||
227 | const char *router_ip, | ||
228 | const char *node_ip, | ||
229 | const char *m, | ||
230 | const char *n, | ||
231 | const char *local_m, | ||
232 | const char *topology_data, | ||
233 | unsigned int *read_file, | ||
234 | GNUNET_TESTING_cmd_helper_finish_cb finished_cb) | ||
235 | { | ||
236 | unsigned int n_int; | ||
237 | unsigned int m_int; | ||
238 | unsigned int local_m_int; | ||
239 | unsigned int num; | ||
240 | struct TestState *ts = GNUNET_new (struct TestState); | ||
241 | struct GNUNET_TESTING_NetjailTopology *topology; | ||
242 | unsigned int sscanf_ret = 0; | ||
243 | |||
244 | ts->finished_cb = finished_cb; | ||
245 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
246 | "n %s m %s\n", | ||
247 | n, | ||
248 | m); | ||
249 | |||
250 | if (GNUNET_YES == *read_file) | ||
251 | { | ||
252 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
253 | "read from file\n"); | ||
254 | topology = GNUNET_TESTING_get_topo_from_file (topology_data); | ||
255 | } | ||
256 | else | ||
257 | topology = GNUNET_TESTING_get_topo_from_string (topology_data); | ||
258 | |||
259 | ts->topology = topology; | ||
260 | |||
261 | errno = 0; | ||
262 | sscanf_ret = sscanf (m, "%u", &m_int); | ||
263 | if (errno != 0) | ||
264 | { | ||
265 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf"); | ||
266 | } | ||
267 | GNUNET_assert (0 < sscanf_ret); | ||
268 | errno = 0; | ||
269 | sscanf_ret = sscanf (n, "%u", &n_int); | ||
270 | if (errno != 0) | ||
271 | { | ||
272 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf"); | ||
273 | } | ||
274 | GNUNET_assert (0 < sscanf_ret); | ||
275 | errno = 0; | ||
276 | sscanf_ret = sscanf (local_m, "%u", &local_m_int); | ||
277 | if (errno != 0) | ||
278 | { | ||
279 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf"); | ||
280 | } | ||
281 | GNUNET_assert (0 < sscanf_ret); | ||
282 | |||
283 | if (0 == n_int) | ||
284 | num = m_int; | ||
285 | else | ||
286 | num = (n_int - 1) * local_m_int + m_int + topology->nodes_x; | ||
287 | |||
288 | block_send = GNUNET_TESTING_cmd_block_until_external_trigger ("block"); | ||
289 | block_receive = GNUNET_TESTING_cmd_block_until_external_trigger ( | ||
290 | "block-receive"); | ||
291 | connect_peers = GNUNET_TESTING_cmd_block_until_external_trigger ( | ||
292 | "connect-peers"); | ||
293 | local_prepared = GNUNET_TESTING_cmd_local_test_prepared ( | ||
294 | "local-test-prepared", | ||
295 | write_message); | ||
296 | |||
297 | |||
298 | GNUNET_asprintf (&ts->cfgname, | ||
299 | "test_transport_api2_tcp_node1.conf"); | ||
300 | |||
301 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
302 | "plugin cfgname: %s\n", | ||
303 | ts->cfgname); | ||
304 | |||
305 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
306 | "node ip: %s\n", | ||
307 | node_ip); | ||
308 | |||
309 | GNUNET_asprintf (&ts->testdir, | ||
310 | "%s%s%s", | ||
311 | BASE_DIR, | ||
312 | m, | ||
313 | n); | ||
314 | |||
315 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
316 | GNUNET_MQ_hd_var_size (test, | ||
317 | GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE, | ||
318 | struct GNUNET_TRANSPORT_TESTING_TestMessage, | ||
319 | ts), | ||
320 | GNUNET_MQ_handler_end () | ||
321 | }; | ||
322 | |||
323 | struct GNUNET_TESTING_Command commands[] = { | ||
324 | GNUNET_TESTING_cmd_system_create ("system-create", | ||
325 | ts->testdir), | ||
326 | GNUNET_TRANSPORT_cmd_start_peer ("start-peer", | ||
327 | "system-create", | ||
328 | num, | ||
329 | node_ip, | ||
330 | handlers, | ||
331 | ts->cfgname, | ||
332 | notify_connect, | ||
333 | GNUNET_YES), | ||
334 | GNUNET_TESTING_cmd_send_peer_ready ("send-peer-ready", | ||
335 | write_message), | ||
336 | block_send, | ||
337 | connect_peers, | ||
338 | GNUNET_TRANSPORT_cmd_send_simple ("send-simple", | ||
339 | "start-peer", | ||
340 | "system-create", | ||
341 | num, | ||
342 | topology), | ||
343 | block_receive, | ||
344 | local_prepared, | ||
345 | GNUNET_TRANSPORT_cmd_stop_peer ("stop-peer", | ||
346 | "start-peer"), | ||
347 | GNUNET_TESTING_cmd_system_destroy ("system-destroy", | ||
348 | "system-create"), | ||
349 | GNUNET_TESTING_cmd_end () | ||
350 | }; | ||
351 | |||
352 | ts->write_message = write_message; | ||
353 | |||
354 | is = GNUNET_TESTING_run (commands, | ||
355 | TIMEOUT, | ||
356 | &handle_result, | ||
357 | ts); | ||
358 | return is; | ||
359 | } | ||
360 | |||
361 | |||
362 | /** | ||
363 | * Entry point for the plugin. | ||
364 | * | ||
365 | * @param cls NULL | ||
366 | * @return the exported block API | ||
367 | */ | ||
368 | void * | ||
369 | libgnunet_test_transport_plugin_cmd_simple_send_broadcast_init (void *cls) | ||
370 | { | ||
371 | struct GNUNET_TESTING_PluginFunctions *api; | ||
372 | |||
373 | GNUNET_log_setup ("simple-send", | ||
374 | "DEBUG", | ||
375 | NULL); | ||
376 | |||
377 | api = GNUNET_new (struct GNUNET_TESTING_PluginFunctions); | ||
378 | api->start_testcase = &start_testcase; | ||
379 | api->all_peers_started = &all_peers_started; | ||
380 | api->all_local_tests_prepared = all_local_tests_prepared; | ||
381 | api->get_waiting_for_barriers = get_waiting_for_barriers; | ||
382 | return api; | ||
383 | } | ||
384 | |||
385 | |||
386 | /** | ||
387 | * Exit point from the plugin. | ||
388 | * | ||
389 | * @param cls the return value from #libgnunet_test_transport_plugin_block_test_init | ||
390 | * @return NULL | ||
391 | */ | ||
392 | void * | ||
393 | libgnunet_test_transport_plugin_cmd_simple_send_broadcast_done (void *cls) | ||
394 | { | ||
395 | struct GNUNET_TESTING_PluginFunctions *api = cls; | ||
396 | |||
397 | GNUNET_free (api); | ||
398 | return NULL; | ||
399 | } | ||
400 | |||
401 | |||
402 | /* end of plugin_cmd_simple_send_broadcast.c */ | ||