diff options
Diffstat (limited to 'src/testing/testing_api_cmd_netjail_start_testsystem.c')
-rw-r--r-- | src/testing/testing_api_cmd_netjail_start_testsystem.c | 610 |
1 files changed, 0 insertions, 610 deletions
diff --git a/src/testing/testing_api_cmd_netjail_start_testsystem.c b/src/testing/testing_api_cmd_netjail_start_testsystem.c deleted file mode 100644 index 531621eb5..000000000 --- a/src/testing/testing_api_cmd_netjail_start_testsystem.c +++ /dev/null | |||
@@ -1,610 +0,0 @@ | |||
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/testing_api_cmd_hello_world.c | ||
23 | * @brief Command to start the netjail peers. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_testing_ng_lib.h" | ||
28 | #include "testing_cmds.h" | ||
29 | |||
30 | #define NETJAIL_EXEC_SCRIPT "./../testing/netjail_exec.sh" | ||
31 | |||
32 | /** | ||
33 | * Struct to store messages send/received by the helper into a DLL | ||
34 | * | ||
35 | */ | ||
36 | struct HelperMessage | ||
37 | { | ||
38 | |||
39 | /** | ||
40 | * Kept in a DLL. | ||
41 | */ | ||
42 | struct HelperMessage *next; | ||
43 | |||
44 | /** | ||
45 | * Kept in a DLL. | ||
46 | */ | ||
47 | struct HelperMessage *prev; | ||
48 | |||
49 | /** | ||
50 | * Size of the original message. | ||
51 | */ | ||
52 | uint16_t bytes_msg; | ||
53 | |||
54 | /* Followed by @e bytes_msg of msg.*/ | ||
55 | }; | ||
56 | |||
57 | |||
58 | /** | ||
59 | * Struct to store information handed over to callbacks. | ||
60 | * | ||
61 | */ | ||
62 | struct NetJailState | ||
63 | { | ||
64 | /** | ||
65 | * Pointer to the return value of the test. | ||
66 | * | ||
67 | */ | ||
68 | unsigned int *rv; | ||
69 | |||
70 | /** | ||
71 | * Head of the DLL which stores messages received by the helper. | ||
72 | * | ||
73 | */ | ||
74 | struct HelperMessage *hp_messages_head; | ||
75 | |||
76 | /** | ||
77 | * Tail of the DLL which stores messages received by the helper. | ||
78 | * | ||
79 | */ | ||
80 | struct HelperMessage *hp_messages_tail; | ||
81 | |||
82 | /** | ||
83 | * Array with handles of helper processes. | ||
84 | */ | ||
85 | struct GNUNET_HELPER_Handle **helper; | ||
86 | |||
87 | /** | ||
88 | * Size of the array NetJailState#helper. | ||
89 | * | ||
90 | */ | ||
91 | unsigned int n_helper; | ||
92 | |||
93 | /** | ||
94 | * Number of nodes in a network namespace. //TODO make this a unsigned int | ||
95 | * | ||
96 | */ | ||
97 | char *local_m; | ||
98 | |||
99 | /** | ||
100 | * Number of network namespaces. //TODO make this a unsigned int | ||
101 | * | ||
102 | */ | ||
103 | char *global_n; | ||
104 | |||
105 | /** | ||
106 | * The send handle for the helper | ||
107 | */ | ||
108 | struct GNUNET_HELPER_SendHandle **shandle; | ||
109 | |||
110 | /** | ||
111 | * Size of the array NetJailState#shandle. | ||
112 | * | ||
113 | */ | ||
114 | unsigned int n_shandle; | ||
115 | |||
116 | /** | ||
117 | * The messages send to the helper. | ||
118 | */ | ||
119 | struct GNUNET_MessageHeader **msg; | ||
120 | |||
121 | /** | ||
122 | * Size of the array NetJailState#msg. | ||
123 | * | ||
124 | */ | ||
125 | unsigned int n_msg; | ||
126 | |||
127 | /** | ||
128 | * Number of test environments started. | ||
129 | * | ||
130 | */ | ||
131 | unsigned int number_of_testsystems_started; | ||
132 | |||
133 | /** | ||
134 | * Number of peers started. | ||
135 | * | ||
136 | */ | ||
137 | unsigned int number_of_peers_started; | ||
138 | |||
139 | /** | ||
140 | * Number of local tests finished. | ||
141 | * | ||
142 | */ | ||
143 | unsigned int number_of_local_test_finished; | ||
144 | |||
145 | /** | ||
146 | * Name of the test case plugin the helper will load. | ||
147 | * | ||
148 | */ | ||
149 | char *plugin_name; | ||
150 | |||
151 | /** | ||
152 | * HEAD of the DLL containing TestingSystemCount. | ||
153 | * | ||
154 | */ | ||
155 | struct TestingSystemCount *tbcs_head; | ||
156 | |||
157 | /** | ||
158 | * TAIL of the DLL containing TestingSystemCount. | ||
159 | * | ||
160 | */ | ||
161 | struct TestingSystemCount *tbcs_tail; | ||
162 | }; | ||
163 | |||
164 | /** | ||
165 | * Struct containing the number of the test environment and the NetJailState which | ||
166 | * will be handed to callbacks specific to a test environment. | ||
167 | */ | ||
168 | struct TestingSystemCount | ||
169 | { | ||
170 | /** | ||
171 | * Kept in a DLL. | ||
172 | */ | ||
173 | struct TestingSystemCount *next; | ||
174 | |||
175 | /** | ||
176 | * Kept in a DLL. | ||
177 | */ | ||
178 | struct TestingSystemCount *prev; | ||
179 | |||
180 | /** | ||
181 | * The number of the test environment. | ||
182 | * | ||
183 | */ | ||
184 | unsigned int count; | ||
185 | |||
186 | /** | ||
187 | * Struct to store information handed over to callbacks. | ||
188 | * | ||
189 | */ | ||
190 | struct NetJailState *ns; | ||
191 | }; | ||
192 | |||
193 | /** | ||
194 | * Code to clean up resource this cmd used. | ||
195 | * | ||
196 | * @param cls closure | ||
197 | * @param cmd current CMD being cleaned up. | ||
198 | */ | ||
199 | static void | ||
200 | netjail_exec_cleanup (void *cls, | ||
201 | const struct GNUNET_TESTING_Command *cmd) | ||
202 | { | ||
203 | struct NetJailState *ns = cls; | ||
204 | struct HelperMessage *message_pos; | ||
205 | struct TestingSystemCount *tbc_pos; | ||
206 | |||
207 | while (NULL != (message_pos = ns->hp_messages_head)) | ||
208 | { | ||
209 | GNUNET_CONTAINER_DLL_remove (ns->hp_messages_head, | ||
210 | ns->hp_messages_tail, | ||
211 | message_pos); | ||
212 | GNUNET_free (message_pos); | ||
213 | } | ||
214 | while (NULL != (tbc_pos = ns->tbcs_head)) | ||
215 | { | ||
216 | GNUNET_CONTAINER_DLL_remove (ns->tbcs_head, | ||
217 | ns->tbcs_tail, | ||
218 | tbc_pos); | ||
219 | GNUNET_free (tbc_pos); | ||
220 | } | ||
221 | GNUNET_free (ns); | ||
222 | } | ||
223 | |||
224 | |||
225 | /** | ||
226 | * This function prepares an array with traits. | ||
227 | * | ||
228 | */ | ||
229 | static int | ||
230 | netjail_exec_traits (void *cls, | ||
231 | const void **ret, | ||
232 | const char *trait, | ||
233 | unsigned int index) | ||
234 | { | ||
235 | struct NetJailState *ns = cls; | ||
236 | struct GNUNET_HELPER_Handle **helper = ns->helper; | ||
237 | struct HelperMessage *hp_messages_head = ns->hp_messages_head; | ||
238 | |||
239 | |||
240 | struct GNUNET_TESTING_Trait traits[] = { | ||
241 | { | ||
242 | .index = 0, | ||
243 | .trait_name = "helper_handles", | ||
244 | .ptr = (const void *) helper, | ||
245 | }, | ||
246 | { | ||
247 | .index = 1, | ||
248 | .trait_name = "hp_msgs_head", | ||
249 | .ptr = (const void *) hp_messages_head, | ||
250 | }, | ||
251 | GNUNET_TESTING_trait_end () | ||
252 | }; | ||
253 | |||
254 | return GNUNET_TESTING_get_trait (traits, | ||
255 | ret, | ||
256 | trait, | ||
257 | index); | ||
258 | } | ||
259 | |||
260 | |||
261 | /** | ||
262 | * Offer handles to testing cmd helper from trait | ||
263 | * | ||
264 | * @param cmd command to extract the message from. | ||
265 | * @param pt pointer to message. | ||
266 | * @return #GNUNET_OK on success. | ||
267 | */ | ||
268 | int | ||
269 | GNUNET_TESTING_get_trait_helper_handles (const struct | ||
270 | GNUNET_TESTING_Command *cmd, | ||
271 | struct GNUNET_HELPER_Handle ***helper) | ||
272 | { | ||
273 | return cmd->traits (cmd->cls, | ||
274 | (const void **) helper, | ||
275 | "helper_handles", | ||
276 | (unsigned int) 0); | ||
277 | } | ||
278 | |||
279 | |||
280 | /** | ||
281 | * Continuation function from GNUNET_HELPER_send() | ||
282 | * | ||
283 | * @param cls closure | ||
284 | * @param result GNUNET_OK on success, | ||
285 | * GNUNET_NO if helper process died | ||
286 | * GNUNET_SYSERR during GNUNET_HELPER_stop | ||
287 | */ | ||
288 | static void | ||
289 | clear_msg (void *cls, int result) | ||
290 | { | ||
291 | struct TestingSystemCount *tbc = cls; | ||
292 | struct NetJailState *ns = tbc->ns; | ||
293 | |||
294 | GNUNET_assert (NULL != ns->shandle[tbc->count - 1]); | ||
295 | ns->shandle[tbc->count - 1] = NULL; | ||
296 | GNUNET_free (ns->msg[tbc->count - 1]); | ||
297 | ns->msg[tbc->count - 1] = NULL; | ||
298 | } | ||
299 | |||
300 | |||
301 | /** | ||
302 | * Functions with this signature are called whenever a | ||
303 | * complete message is received by the tokenizer. | ||
304 | * | ||
305 | * Do not call GNUNET_SERVER_mst_destroy in callback | ||
306 | * | ||
307 | * @param cls closure | ||
308 | * @param client identification of the client | ||
309 | * @param message the actual message | ||
310 | * | ||
311 | * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing | ||
312 | */ | ||
313 | static int | ||
314 | helper_mst (void *cls, const struct GNUNET_MessageHeader *message) | ||
315 | { | ||
316 | struct TestingSystemCount *tbc = cls; | ||
317 | struct NetJailState *ns = tbc->ns; | ||
318 | struct HelperMessage *hp_msg; | ||
319 | |||
320 | if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_REPLY == ntohs (message->type)) | ||
321 | { | ||
322 | ns->number_of_testsystems_started++; | ||
323 | } | ||
324 | else if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_PEER_STARTED == ntohs ( | ||
325 | message->type)) | ||
326 | { | ||
327 | ns->number_of_peers_started++; | ||
328 | } | ||
329 | else if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_FINISHED == ntohs ( | ||
330 | message->type)) | ||
331 | { | ||
332 | ns->number_of_local_test_finished++; | ||
333 | } | ||
334 | else | ||
335 | { | ||
336 | hp_msg = GNUNET_new (struct HelperMessage); | ||
337 | hp_msg->bytes_msg = message->size; | ||
338 | memcpy (&hp_msg[1], message, message->size); | ||
339 | GNUNET_CONTAINER_DLL_insert (ns->hp_messages_head, ns->hp_messages_tail, | ||
340 | hp_msg); | ||
341 | } | ||
342 | |||
343 | return GNUNET_OK; | ||
344 | } | ||
345 | |||
346 | |||
347 | /** | ||
348 | * Callback called if there was an exception during execution of the helper. | ||
349 | * | ||
350 | */ | ||
351 | static void | ||
352 | exp_cb (void *cls) | ||
353 | { | ||
354 | struct NetJailState *ns = cls; | ||
355 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called exp_cb.\n"); | ||
356 | *ns->rv = 1; | ||
357 | } | ||
358 | |||
359 | |||
360 | /** | ||
361 | * Function to initialize a init message for the helper. | ||
362 | * | ||
363 | * @param m_char The actual node in a namespace. //TODO Change this to unsigned int | ||
364 | * @param n_char The actual namespace. //TODO Change this to unsigned int | ||
365 | * @param plugin_name Name of the test case plugin the helper will load. | ||
366 | * | ||
367 | */ | ||
368 | static struct GNUNET_CMDS_HelperInit * | ||
369 | create_helper_init_msg_ (char *m_char, | ||
370 | char *n_char, | ||
371 | const char *plugin_name) | ||
372 | { | ||
373 | struct GNUNET_CMDS_HelperInit *msg; | ||
374 | uint16_t plugin_name_len; | ||
375 | uint16_t msg_size; | ||
376 | |||
377 | GNUNET_assert (NULL != plugin_name); | ||
378 | plugin_name_len = strlen (plugin_name); | ||
379 | msg_size = sizeof(struct GNUNET_CMDS_HelperInit) + plugin_name_len; | ||
380 | msg = GNUNET_malloc (msg_size); | ||
381 | msg->header.size = htons (msg_size); | ||
382 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_CMDS_HELPER_INIT); | ||
383 | msg->plugin_name_size = htons (plugin_name_len); | ||
384 | GNUNET_memcpy ((char *) &msg[1], | ||
385 | plugin_name, | ||
386 | plugin_name_len); | ||
387 | return msg; | ||
388 | } | ||
389 | |||
390 | |||
391 | /** | ||
392 | * Function which start a single helper process. | ||
393 | * | ||
394 | */ | ||
395 | static void | ||
396 | start_helper (struct NetJailState *ns, struct | ||
397 | GNUNET_CONFIGURATION_Handle *config, | ||
398 | char *m_char, | ||
399 | char *n_char) | ||
400 | { | ||
401 | struct GNUNET_HELPER_Handle *helper; | ||
402 | struct GNUNET_CMDS_HelperInit *msg; | ||
403 | struct TestingSystemCount *tbc; | ||
404 | char *const script_argv[] = {NETJAIL_EXEC_SCRIPT, | ||
405 | m_char, | ||
406 | n_char, | ||
407 | GNUNET_OS_get_libexec_binary_path ( | ||
408 | HELPER_CMDS_BINARY), | ||
409 | ns->global_n, | ||
410 | ns->local_m, | ||
411 | NULL}; | ||
412 | unsigned int m = atoi (m_char); | ||
413 | unsigned int n = atoi (n_char); | ||
414 | unsigned int helper_check = GNUNET_OS_check_helper_binary ( | ||
415 | NETJAIL_EXEC_SCRIPT, | ||
416 | GNUNET_YES, | ||
417 | NULL); | ||
418 | |||
419 | tbc = GNUNET_new (struct TestingSystemCount); | ||
420 | tbc->ns = ns; | ||
421 | tbc->count = (n - 1) * atoi (ns->local_m) + m; | ||
422 | |||
423 | GNUNET_CONTAINER_DLL_insert (ns->tbcs_head, ns->tbcs_tail, | ||
424 | tbc); | ||
425 | |||
426 | |||
427 | if (GNUNET_NO == helper_check) | ||
428 | { | ||
429 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
430 | "No SUID for %s!\n", | ||
431 | NETJAIL_EXEC_SCRIPT); | ||
432 | *ns->rv = 1; | ||
433 | } | ||
434 | else if (GNUNET_NO == helper_check) | ||
435 | { | ||
436 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
437 | "%s not found!\n", | ||
438 | NETJAIL_EXEC_SCRIPT); | ||
439 | *ns->rv = 1; | ||
440 | } | ||
441 | |||
442 | GNUNET_array_append (ns->helper, ns->n_helper, GNUNET_HELPER_start ( | ||
443 | GNUNET_YES, | ||
444 | NETJAIL_EXEC_SCRIPT, | ||
445 | script_argv, | ||
446 | &helper_mst, | ||
447 | &exp_cb, | ||
448 | tbc)); | ||
449 | |||
450 | helper = ns->helper[tbc->count - 1]; | ||
451 | |||
452 | msg = create_helper_init_msg_ (m_char, | ||
453 | n_char, | ||
454 | ns->plugin_name); | ||
455 | GNUNET_array_append (ns->msg, ns->n_msg, &msg->header); | ||
456 | |||
457 | GNUNET_array_append (ns->shandle, ns->n_shandle, GNUNET_HELPER_send ( | ||
458 | helper, | ||
459 | &msg->header, | ||
460 | GNUNET_NO, | ||
461 | &clear_msg, | ||
462 | tbc)); | ||
463 | |||
464 | if (NULL == ns->shandle[tbc->count - 1]) | ||
465 | { | ||
466 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
467 | "Send handle is NULL!\n"); | ||
468 | GNUNET_free (msg); | ||
469 | *ns->rv = 1; | ||
470 | } | ||
471 | } | ||
472 | |||
473 | |||
474 | /** | ||
475 | * This function starts a helper process for each node. | ||
476 | * | ||
477 | * @param cls closure. | ||
478 | * @param cmd CMD being run. | ||
479 | * @param is interpreter state. | ||
480 | */ | ||
481 | static void | ||
482 | netjail_exec_run (void *cls, | ||
483 | const struct GNUNET_TESTING_Command *cmd, | ||
484 | struct GNUNET_TESTING_Interpreter *is) | ||
485 | { | ||
486 | char str_m[12]; | ||
487 | char str_n[12]; | ||
488 | struct NetJailState *ns = cls; | ||
489 | struct GNUNET_CONFIGURATION_Handle *config = | ||
490 | GNUNET_CONFIGURATION_create (); | ||
491 | |||
492 | for (int i = 1; i <= atoi (ns->global_n); i++) | ||
493 | { | ||
494 | for (int j = 1; j <= atoi (ns->local_m); j++) | ||
495 | { | ||
496 | sprintf (str_n, "%d", i); | ||
497 | sprintf (str_m, "%d", j); | ||
498 | start_helper (ns, config, | ||
499 | str_m, | ||
500 | str_n); | ||
501 | } | ||
502 | } | ||
503 | } | ||
504 | |||
505 | |||
506 | /** | ||
507 | * This function checks on three different information. | ||
508 | * | ||
509 | * 1. Did all helpers start. This is only logged. | ||
510 | * 2. Did all peer start. | ||
511 | * In this case a GNUNET_MESSAGE_TYPE_CMDS_HELPER_ALL_PEERS_STARTED is send to all peers. | ||
512 | * 3. Did all peers finished the test case. In this case interpreter_next will be called. | ||
513 | * | ||
514 | */ | ||
515 | static int | ||
516 | netjail_start_finish (void *cls, | ||
517 | GNUNET_SCHEDULER_TaskCallback cont, | ||
518 | void *cont_cls) | ||
519 | { | ||
520 | unsigned int ret = GNUNET_NO; | ||
521 | struct NetJailState *ns = cls; | ||
522 | unsigned int total_number = atoi (ns->local_m) * atoi (ns->global_n); | ||
523 | struct GNUNET_CMDS_ALL_PEERS_STARTED *reply; | ||
524 | size_t msg_length; | ||
525 | struct GNUNET_HELPER_Handle *helper; | ||
526 | struct TestingSystemCount *tbc; | ||
527 | |||
528 | if (ns->number_of_local_test_finished == total_number) | ||
529 | { | ||
530 | ret = GNUNET_YES; | ||
531 | cont (cont_cls); | ||
532 | } | ||
533 | |||
534 | if (ns->number_of_testsystems_started == total_number) | ||
535 | { | ||
536 | ns->number_of_testsystems_started = 0; | ||
537 | } | ||
538 | |||
539 | if (ns->number_of_peers_started == total_number) | ||
540 | { | ||
541 | for (int i = 1; i <= atoi (ns->global_n); i++) | ||
542 | { | ||
543 | for (int j = 1; j <= atoi (ns->local_m); j++) | ||
544 | { | ||
545 | tbc = GNUNET_new (struct TestingSystemCount); | ||
546 | tbc->ns = ns; | ||
547 | // TODO This needs to be more generic. As we send more messages back and forth, we can not grow the arrays again and again, because this is to error prone. | ||
548 | tbc->count = (i - 1) * atoi (ns->local_m) + j + total_number; | ||
549 | |||
550 | helper = ns->helper[tbc->count - 1 - total_number]; | ||
551 | msg_length = sizeof(struct GNUNET_CMDS_ALL_PEERS_STARTED); | ||
552 | reply = GNUNET_new (struct GNUNET_CMDS_ALL_PEERS_STARTED); | ||
553 | reply->header.type = htons ( | ||
554 | GNUNET_MESSAGE_TYPE_CMDS_HELPER_ALL_PEERS_STARTED); | ||
555 | reply->header.size = htons ((uint16_t) msg_length); | ||
556 | |||
557 | GNUNET_array_append (ns->msg, ns->n_msg, &reply->header); | ||
558 | |||
559 | struct GNUNET_HELPER_SendHandle *sh = GNUNET_HELPER_send ( | ||
560 | helper, | ||
561 | &reply->header, | ||
562 | GNUNET_NO, | ||
563 | &clear_msg, | ||
564 | tbc); | ||
565 | |||
566 | GNUNET_array_append (ns->shandle, ns->n_shandle, sh); | ||
567 | } | ||
568 | } | ||
569 | ns->number_of_peers_started = 0; | ||
570 | } | ||
571 | return ret; | ||
572 | } | ||
573 | |||
574 | |||
575 | /** | ||
576 | * Create command. | ||
577 | * | ||
578 | * @param label Name for the command. | ||
579 | * @param local_m Number of nodes in a network namespace. //TODO make this a unsigned int | ||
580 | * @param global_n Number of network namespaces. //TODO make this a unsigned int | ||
581 | * @param plugin_name Name of the test case plugin the helper will load. | ||
582 | * @param rv Pointer to the return value of the test. | ||
583 | * @return command. | ||
584 | */ | ||
585 | struct GNUNET_TESTING_Command | ||
586 | GNUNET_TESTING_cmd_netjail_start_testing_system (const char *label, | ||
587 | char *local_m, | ||
588 | char *global_n, | ||
589 | char *plugin_name, | ||
590 | unsigned int *rv) | ||
591 | { | ||
592 | struct NetJailState *ns; | ||
593 | |||
594 | ns = GNUNET_new (struct NetJailState); | ||
595 | ns->local_m = local_m; | ||
596 | ns->global_n = global_n; | ||
597 | ns->plugin_name = plugin_name; | ||
598 | ns->rv = rv; | ||
599 | |||
600 | struct GNUNET_TESTING_Command cmd = { | ||
601 | .cls = ns, | ||
602 | .label = label, | ||
603 | .run = &netjail_exec_run, | ||
604 | .finish = &netjail_start_finish, | ||
605 | .cleanup = &netjail_exec_cleanup, | ||
606 | .traits = &netjail_exec_traits | ||
607 | }; | ||
608 | |||
609 | return cmd; | ||
610 | } | ||