2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing.c
23 * @brief convenience API for writing testcases for GNUnet
24 * Many testcases need to start and stop gnunetd,
25 * and this library is supposed to make that easier
26 * for TESTCASES. Normal programs should always
27 * use functions from gnunet_{util,arm}_lib.h. This API is
28 * ONLY for writing testcases!
29 * @author Christian Grothoff
33 #include "gnunet_arm_service.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_testing_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_hello_lib.h"
40 #define DEBUG_TESTING GNUNET_NO
42 #define DEBUG_TESTING_RECONNECT GNUNET_NO
45 * Hack to deal with initial HELLO's being often devoid of addresses.
46 * This hack causes 'process_hello' to ignore HELLOs without addresses.
47 * The correct implementation would continue with 'process_hello' until
48 * the connection could be established...
50 #define EMPTY_HACK GNUNET_YES
53 * How long do we wait after starting gnunet-service-arm
54 * for the core service to be alive?
56 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
59 * How many times are we willing to try to wait for "scp" or
60 * "gnunet-service-arm" to complete (waitpid) before giving up?
62 #define MAX_EXEC_WAIT_RUNS 250
64 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
68 test_address (void *cls, const char *tname,
69 struct GNUNET_TIME_Absolute expiration, const void *addr,
80 * Receive the HELLO from one peer, give it to the other
81 * and ask them to connect.
83 * @param cls "struct ConnectContext"
84 * @param message HELLO message of peer
87 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
89 struct GNUNET_TESTING_Daemon *daemon = cls;
96 GNUNET_assert (message != NULL);
97 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) message,
98 GNUNET_NO, &test_address, &empty);
99 if (GNUNET_YES == empty)
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skipping empty HELLO address\n");
107 GNUNET_assert (daemon->phase == SP_GET_HELLO ||
108 daemon->phase == SP_START_DONE);
110 if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
111 GNUNET_SCHEDULER_cancel (daemon->task);
113 if (daemon->server != NULL)
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117 "Received `%s' from transport service of `%4s', disconnecting core!\n",
118 "HELLO", GNUNET_i2s (&daemon->id));
120 GNUNET_CORE_disconnect (daemon->server);
121 daemon->server = NULL;
124 msize = ntohs (message->size);
129 if (daemon->ghh != NULL)
131 GNUNET_TRANSPORT_get_hello_cancel (daemon->ghh);
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Received `%s' from transport service of `%4s'\n", "HELLO",
136 GNUNET_i2s (&daemon->id));
139 GNUNET_free_non_null (daemon->hello);
140 daemon->hello = GNUNET_malloc (msize);
141 memcpy (daemon->hello, message, msize);
143 if (daemon->th != NULL)
145 GNUNET_TRANSPORT_disconnect (daemon->th);
148 daemon->phase = SP_START_DONE;
152 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
155 * Notify of a peer being up and running. Scheduled as a task
156 * so that variables which may need to be set are set before
157 * the connect callback can set up new operations.
159 * @param cls the testing daemon
160 * @param tc task scheduler context
163 notify_daemon_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 struct GNUNET_TESTING_Daemon *d = cls;
166 GNUNET_TESTING_NotifyDaemonRunning cb;
171 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
176 * Finite-state machine for starting GNUnet.
178 * @param cls our "struct GNUNET_TESTING_Daemon"
182 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
184 struct GNUNET_TESTING_Daemon *d = cls;
185 GNUNET_TESTING_NotifyDaemonRunning cb;
186 enum GNUNET_OS_ProcessStatusType type;
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer FSM is in phase %u.\n", d->phase);
195 d->task = GNUNET_SCHEDULER_NO_TASK;
199 /* confirm copying complete */
200 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
202 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
207 cb (d->cb_cls, NULL, d->cfg, d,
209 ("`scp' does not seem to terminate (timeout copying config).\n"));
214 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
218 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
223 cb (d->cb_cls, NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
226 GNUNET_OS_process_close (d->proc);
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229 "Successfully copied configuration file.\n");
231 d->phase = SP_COPIED;
234 /* Start create hostkey process if we don't already know the peer identity! */
235 if (GNUNET_NO == d->have_hostkey)
237 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
238 if (d->pipe_stdout == NULL)
243 cb (d->cb_cls, NULL, d->cfg, d,
246 _("Failed to create pipe for `gnunet-peerinfo' process.\n") :
247 _("Failed to create pipe for `ssh' process.\n"));
250 if (NULL == d->hostname)
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Starting `%s', with command `%s %s %s %s'.\n",
255 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
259 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
260 "gnunet-peerinfo", "-c", d->cfgfile, "-sq",
262 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
266 if (d->username != NULL)
267 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
269 dst = GNUNET_strdup (d->hostname);
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
274 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
277 if (d->ssh_port_str == NULL)
279 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh",
283 dst, "gnunet-peerinfo", "-c",
284 d->cfgfile, "-sq", NULL);
289 GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh", "-p",
294 dst, "gnunet-peerinfo", "-c", d->cfgfile,
297 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
303 _("Could not start `%s' process to create hostkey.\n"),
304 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
308 cb (d->cb_cls, NULL, d->cfg, d,
310 d->hostname) ? _("Failed to start `gnunet-peerinfo' process.\n")
311 : _("Failed to start `ssh' process.\n"));
312 GNUNET_DISK_pipe_close (d->pipe_stdout);
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
319 d->phase = SP_HOSTKEY_CREATE;
321 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
323 GNUNET_DISK_pipe_handle
325 GNUNET_DISK_PIPE_END_READ),
328 else /* Already have a hostkey! */
330 if (d->hostkey_callback != NULL)
332 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
333 d->hostkey_callback = NULL;
334 d->phase = SP_HOSTKEY_CREATED;
337 d->phase = SP_TOPOLOGY_SETUP;
340 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
343 case SP_HOSTKEY_CREATE:
345 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
346 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
347 &d->hostkeybuf[d->hostkeybufpos],
348 sizeof (d->hostkeybuf) - d->hostkeybufpos);
350 d->hostkeybufpos += bytes_read;
352 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
356 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
358 GNUNET_DISK_pipe_handle
360 GNUNET_DISK_PIPE_END_READ),
364 d->hostkeybuf[103] = '\0';
366 if ((bytes_read < 0) ||
368 GNUNET_CRYPTO_hash_from_string (d->hostkeybuf, &d->id.hashPubKey)))
372 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
373 _("Error reading from gnunet-peerinfo: %s\n"),
376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
377 _("Malformed output from gnunet-peerinfo!\n"));
380 GNUNET_DISK_pipe_close (d->pipe_stdout);
381 d->pipe_stdout = NULL;
382 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
383 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
384 GNUNET_OS_process_close (d->proc);
387 cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
390 d->shortname = GNUNET_strdup (GNUNET_i2s (&d->id));
391 GNUNET_DISK_pipe_close (d->pipe_stdout);
392 d->pipe_stdout = NULL;
393 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
394 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
395 GNUNET_OS_process_close (d->proc);
397 d->have_hostkey = GNUNET_YES;
398 if (d->hostkey_callback != NULL)
400 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
401 d->hostkey_callback = NULL;
402 d->phase = SP_HOSTKEY_CREATED;
406 d->phase = SP_TOPOLOGY_SETUP;
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
412 case SP_HOSTKEY_CREATED:
413 /* wait for topology finished */
414 if ((GNUNET_YES == d->dead) ||
415 (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0))
420 cb (d->cb_cls, NULL, d->cfg, d,
421 _("`Failed while waiting for topology setup!\n"));
426 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
429 case SP_TOPOLOGY_SETUP: /* Indicates topology setup has completed! */
430 /* start GNUnet on remote host */
431 if (NULL == d->hostname)
434 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
435 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
436 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-L", "DEBUG",
440 GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm", "-c",
446 GNUNET_TIME_relative_to_string
447 (GNUNET_TIME_absolute_get_remaining
448 (d->max_timeout)), NULL);
452 if (d->username != NULL)
453 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
455 dst = GNUNET_strdup (d->hostname);
458 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
459 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
460 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
461 "-L", "DEBUG", "-s", "-q");
463 if (d->ssh_port_str == NULL)
465 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
473 "-c", d->cfgfile, "-s", "-q", "-T",
474 GNUNET_TIME_relative_to_string
475 (GNUNET_TIME_absolute_get_remaining
476 (d->max_timeout)), NULL);
482 GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-p",
491 "-c", d->cfgfile, "-s", "-q", "-T",
492 GNUNET_TIME_relative_to_string
493 (GNUNET_TIME_absolute_get_remaining
494 (d->max_timeout)), NULL);
500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
501 _("Could not start `%s' process to start GNUnet.\n"),
502 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
506 cb (d->cb_cls, NULL, d->cfg, d,
508 d->hostname) ? _("Failed to start `gnunet-arm' process.\n") :
509 _("Failed to start `ssh' process.\n"));
513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
514 "Started `%s', waiting for `%s' to be up.\n", "gnunet-arm",
515 "gnunet-service-core");
517 d->phase = SP_START_ARMING;
519 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
522 case SP_START_ARMING:
523 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
525 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
530 cb (d->cb_cls, NULL, d->cfg, d,
532 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
533 _("`ssh' does not seem to terminate.\n"));
534 GNUNET_CONFIGURATION_destroy (d->cfg);
535 GNUNET_free (d->cfgfile);
536 GNUNET_free_non_null (d->hostname);
537 GNUNET_free_non_null (d->username);
538 GNUNET_free (d->proc);
544 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
549 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Successfully started `%s'.\n",
552 GNUNET_free (d->proc);
553 d->phase = SP_START_CORE;
555 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Calling CORE_connect\n");
559 if (d->server != NULL)
560 GNUNET_CORE_disconnect (d->server);
562 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
565 if (GNUNET_YES == d->dead)
566 GNUNET_TESTING_daemon_stop (d,
567 GNUNET_TIME_absolute_get_remaining
568 (d->max_timeout), d->dead_cb,
569 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
570 else if (NULL != d->cb)
571 d->cb (d->cb_cls, &d->id, d->cfg, d,
572 _("Failed to connect to transport service!\n"));
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577 "Connected to transport service `%s', getting HELLO\n",
578 GNUNET_i2s (&d->id));
581 d->ghh = GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
582 /* FIXME: store task ID somewhere! */
583 GNUNET_SCHEDULER_add_now (¬ify_daemon_started, d);
587 * cb (d->cb_cls, &d->id, d->cfg, d, NULL); */
588 d->running = GNUNET_YES;
589 d->phase = SP_GET_HELLO;
592 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
594 if (d->server != NULL)
595 GNUNET_CORE_disconnect (d->server);
597 GNUNET_TRANSPORT_disconnect (d->th);
601 cb (d->cb_cls, NULL, d->cfg, d, _("Unable to get HELLO for peer!\n"));
602 GNUNET_CONFIGURATION_destroy (d->cfg);
603 GNUNET_free (d->cfgfile);
604 GNUNET_free_non_null (d->hostname);
605 GNUNET_free_non_null (d->username);
609 if (d->hello != NULL)
611 GNUNET_assert (d->task == GNUNET_SCHEDULER_NO_TASK);
613 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
614 (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
620 case SP_SERVICE_START:
621 /* confirm gnunet-arm exited */
622 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
624 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
629 cb (d->cb_cls, NULL, d->cfg, d,
631 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
632 _("`ssh' does not seem to terminate.\n"));
637 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
642 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
647 cb (d->cb_cls, NULL, d->cfg, d,
651 ("`gnunet-arm' terminated with non-zero exit status (or timed out)!\n")
652 : _("`ssh' does not seem to terminate.\n"));
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service startup complete!\n");
661 d->phase = SP_START_DONE;
663 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
665 case SP_SERVICE_SHUTDOWN_START:
666 /* confirm copying complete */
667 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
669 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
671 if (NULL != d->dead_cb)
672 d->dead_cb (d->dead_cb_cls,
674 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
679 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
684 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
686 if (NULL != d->dead_cb)
687 d->dead_cb (d->dead_cb_cls,
689 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
696 if (NULL != d->dead_cb)
697 d->dead_cb (d->dead_cb_cls, NULL);
699 case SP_SHUTDOWN_START:
700 /* confirm copying complete */
701 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
703 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
705 if (NULL != d->dead_cb)
706 d->dead_cb (d->dead_cb_cls,
708 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
711 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
713 GNUNET_TRANSPORT_disconnect (d->th);
716 GNUNET_CONFIGURATION_destroy (d->cfg);
717 GNUNET_free (d->cfgfile);
718 GNUNET_free_non_null (d->hello);
719 GNUNET_free_non_null (d->hostname);
720 GNUNET_free_non_null (d->username);
721 GNUNET_free_non_null (d->shortname);
722 GNUNET_free_non_null (d->proc);
729 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
733 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
735 if (NULL != d->dead_cb)
736 d->dead_cb (d->dead_cb_cls,
738 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
741 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
743 GNUNET_TRANSPORT_disconnect (d->th);
746 if (d->server != NULL)
748 GNUNET_CORE_disconnect (d->server);
751 GNUNET_CONFIGURATION_destroy (d->cfg);
752 GNUNET_free (d->cfgfile);
753 GNUNET_free_non_null (d->hello);
754 GNUNET_free_non_null (d->hostname);
755 GNUNET_free_non_null (d->username);
756 GNUNET_free_non_null (d->shortname);
757 GNUNET_free_non_null (d->proc);
763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
765 if (d->server != NULL)
767 GNUNET_CORE_disconnect (d->server);
773 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
775 GNUNET_TRANSPORT_disconnect (d->th);
779 if (NULL != d->dead_cb)
780 d->dead_cb (d->dead_cb_cls, NULL);
782 /* state clean up and notifications */
783 if (d->churn == GNUNET_NO)
785 GNUNET_CONFIGURATION_destroy (d->cfg);
786 GNUNET_free (d->cfgfile);
787 GNUNET_free_non_null (d->hostname);
788 GNUNET_free_non_null (d->username);
791 GNUNET_free_non_null (d->hello);
793 GNUNET_free_non_null (d->shortname);
794 GNUNET_free_non_null (d->proc);
797 if (d->churn == GNUNET_NO)
801 case SP_CONFIG_UPDATE:
802 /* confirm copying complete */
803 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
805 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
810 cb (d->cb_cls, NULL, d->cfg, d,
811 _("`scp' does not seem to terminate.\n"));
816 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
820 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
822 if (NULL != d->update_cb)
823 d->update_cb (d->update_cb_cls, _("`scp' did not complete cleanly.\n"));
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
828 "Successfully copied configuration file.\n");
830 if (NULL != d->update_cb)
831 d->update_cb (d->update_cb_cls, NULL);
832 d->phase = SP_START_DONE;
838 * Continues GNUnet daemon startup when user wanted to be notified
839 * once a hostkey was generated (for creating friends files, blacklists,
842 * @param daemon the daemon to finish starting
845 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
847 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
848 daemon->phase = SP_TOPOLOGY_SETUP;
852 * Check whether the given daemon is running.
854 * @param daemon the daemon to check
856 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
857 * daemon is down, GNUNET_SYSERR on error.
860 GNUNET_TESTING_test_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
863 return GNUNET_SYSERR;
865 if (daemon->running == GNUNET_YES)
872 * Starts a GNUnet daemon service which has been previously stopped.
874 * @param d the daemon for which the service should be started
875 * @param service the name of the service to start
876 * @param timeout how long to wait for process for shutdown to complete
877 * @param cb function called once the service starts
878 * @param cb_cls closure for cb
881 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
883 struct GNUNET_TIME_Relative
885 GNUNET_TESTING_NotifyDaemonRunning
893 GNUNET_assert (d->running == GNUNET_YES);
895 if (d->phase == SP_CONFIG_UPDATE)
897 GNUNET_SCHEDULER_cancel (d->task);
898 d->phase = SP_START_DONE;
901 if (d->churned_services == NULL)
903 d->cb (d->cb_cls, &d->id, d->cfg, d,
904 "No service has been churned off yet!!");
907 d->phase = SP_SERVICE_START;
908 GNUNET_free (d->churned_services);
909 d->churned_services = NULL;
910 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
911 /* Check if this is a local or remote process */
912 if (NULL != d->hostname)
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "Starting gnunet-arm with config `%s' on host `%s'.\n",
917 d->cfgfile, d->hostname);
920 if (d->username != NULL)
921 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
923 arg = GNUNET_strdup (d->hostname);
925 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
933 "-c", d->cfgfile, "-i", service, "-q",
935 GNUNET_TIME_relative_to_string (timeout),
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q\n",
939 arg, "gnunet-arm", d->cfgfile, service);
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
946 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
948 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
952 "-c", d->cfgfile, "-i", service, "-q",
954 GNUNET_TIME_relative_to_string (timeout),
958 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
959 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
963 * Starts a GNUnet daemon's service.
965 * @param d the daemon for which the service should be started
966 * @param service the name of the service to start
967 * @param timeout how long to wait for process for startup
968 * @param cb function called once gnunet-arm returns
969 * @param cb_cls closure for cb
972 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
974 struct GNUNET_TIME_Relative timeout,
975 GNUNET_TESTING_NotifyDaemonRunning cb,
983 GNUNET_assert (service != NULL);
984 GNUNET_assert (d->running == GNUNET_YES);
985 GNUNET_assert (d->phase == SP_START_DONE);
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
989 _("Starting service %s for peer `%4s'\n"), service,
990 GNUNET_i2s (&d->id));
993 d->phase = SP_SERVICE_START;
994 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
995 /* Check if this is a local or remote process */
996 if (NULL != d->hostname)
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1000 "Starting gnunet-arm with config `%s' on host `%s'.\n",
1001 d->cfgfile, d->hostname);
1004 if (d->username != NULL)
1005 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1007 arg = GNUNET_strdup (d->hostname);
1009 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1017 "-c", d->cfgfile, "-i", service, "-q",
1019 GNUNET_TIME_relative_to_string (timeout),
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1022 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q -T %s\n",
1023 arg, "gnunet-arm", d->cfgfile, service,
1024 GNUNET_TIME_relative_to_string (timeout));
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1033 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1037 "-c", d->cfgfile, "-i", service, "-q",
1039 GNUNET_TIME_relative_to_string (timeout),
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042 "Starting gnunet-arm with command %s -c %s -i %s -q -T %s\n",
1043 "gnunet-arm", d->cfgfile, service,
1044 GNUNET_TIME_relative_to_string (timeout));
1047 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1048 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1052 * Start a peer that has previously been stopped using the daemon_stop
1053 * call (and files weren't deleted and the allow restart flag)
1055 * @param daemon the daemon to start (has been previously stopped)
1056 * @param timeout how long to wait for restart
1057 * @param cb the callback for notification when the peer is running
1058 * @param cb_cls closure for the callback
1061 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
1062 struct GNUNET_TIME_Relative timeout,
1063 GNUNET_TESTING_NotifyDaemonRunning cb,
1066 if (daemon->running == GNUNET_YES)
1068 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
1069 "Daemon already running, can't restart!");
1074 daemon->cb_cls = cb_cls;
1075 daemon->phase = SP_TOPOLOGY_SETUP;
1076 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1077 /* FIXME: why add_continuation? */
1078 GNUNET_SCHEDULER_add_continuation (&start_fsm, daemon,
1079 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1083 * Starts a GNUnet daemon. GNUnet must be installed on the target
1084 * system and available in the PATH. The machine must furthermore be
1085 * reachable via "ssh" (unless the hostname is "NULL") without the
1086 * need to enter a password.
1088 * @param cfg configuration to use
1089 * @param timeout how long to wait starting up peers
1090 * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
1091 * to really start the peer (default)
1092 * @param hostname name of the machine where to run GNUnet
1093 * (use NULL for localhost).
1094 * @param ssh_username ssh username to use when connecting to hostname
1095 * @param sshport port to pass to ssh process when connecting to hostname
1096 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
1097 * @param hostkey_callback function to call once the hostkey has been
1098 * generated for this peer, but it hasn't yet been started
1099 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
1100 * @param hostkey_cls closure for hostkey callback
1101 * @param cb function to call once peer is up, or failed to start
1102 * @param cb_cls closure for cb
1103 * @return handle to the daemon (actual start will be completed asynchronously)
1105 struct GNUNET_TESTING_Daemon *
1106 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1107 struct GNUNET_TIME_Relative timeout, int pretend,
1108 const char *hostname, const char *ssh_username,
1109 uint16_t sshport, const char *hostkey,
1110 GNUNET_TESTING_NotifyHostkeyCreated
1111 hostkey_callback, void *hostkey_cls,
1112 GNUNET_TESTING_NotifyDaemonRunning cb,
1115 struct GNUNET_TESTING_Daemon *ret;
1119 char *baseservicehome;
1122 char *temp_file_name;
1123 struct GNUNET_DISK_FileHandle *fn;
1124 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
1125 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
1127 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
1128 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
1131 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
1134 ret->ssh_port_str = NULL;
1136 /* Find service home and base service home directories, create it if it doesn't exist */
1137 GNUNET_assert (GNUNET_OK ==
1138 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
1142 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1143 GNUNET_asprintf (&temp_file_name, "%s/gnunet-testing-config", servicehome);
1144 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1145 GNUNET_free (temp_file_name);
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1148 "Setting up peer with configuration file `%s'.\n", ret->cfgfile);
1150 if (NULL == ret->cfgfile)
1152 GNUNET_free_non_null (ret->ssh_port_str);
1153 GNUNET_free_non_null (ret->hostname);
1157 ret->hostkey_callback = hostkey_callback;
1158 ret->hostkey_cls = hostkey_cls;
1160 ret->cb_cls = cb_cls;
1161 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1162 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1163 GNUNET_CONFIGURATION_set_value_string (ret->cfg, "PATHS", "DEFAULTCONFIG",
1166 if (hostkey != NULL) /* Get the peer identity from the hostkey */
1168 private_key = GNUNET_CRYPTO_rsa_decode_key (hostkey, HOSTKEYFILESIZE);
1169 GNUNET_assert (private_key != NULL);
1170 GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
1171 GNUNET_CRYPTO_hash (&public_key,
1172 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1173 &ret->id.hashPubKey);
1174 ret->shortname = GNUNET_strdup (GNUNET_i2s (&ret->id));
1175 ret->have_hostkey = GNUNET_YES;
1176 GNUNET_CRYPTO_rsa_key_free (private_key);
1179 /* Write hostkey to file, if we were given one */
1181 if (hostkey != NULL)
1183 GNUNET_asprintf (&hostkeyfile, "%s/.hostkey", servicehome);
1184 fn = GNUNET_DISK_file_open (hostkeyfile,
1185 GNUNET_DISK_OPEN_READWRITE |
1186 GNUNET_DISK_OPEN_CREATE,
1187 GNUNET_DISK_PERM_USER_READ |
1188 GNUNET_DISK_PERM_USER_WRITE);
1189 GNUNET_assert (fn != NULL);
1190 GNUNET_assert (HOSTKEYFILESIZE ==
1191 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
1192 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
1195 /* write configuration to temporary file */
1196 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1198 if (0 != UNLINK (ret->cfgfile))
1199 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1201 GNUNET_CONFIGURATION_destroy (ret->cfg);
1202 GNUNET_free_non_null (ret->hostname);
1203 GNUNET_free (ret->cfgfile);
1207 if (ssh_username != NULL)
1208 username = GNUNET_strdup (ssh_username);
1209 if ((ssh_username == NULL) &&
1211 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "USERNAME",
1214 if (NULL != getenv ("USER"))
1215 username = GNUNET_strdup (getenv ("USER"));
1219 ret->username = username;
1221 if (GNUNET_NO == pretend) /* Copy files, enter finite state machine */
1223 /* copy directory to remote host */
1224 if (NULL != hostname)
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Copying configuration directory to host `%s'.\n", hostname);
1230 baseservicehome = GNUNET_strdup (servicehome);
1231 /* Remove trailing /'s */
1232 while (baseservicehome[strlen (baseservicehome) - 1] == '/')
1233 baseservicehome[strlen (baseservicehome) - 1] = '\0';
1234 /* Find next directory /, jump one ahead */
1235 slash = strrchr (baseservicehome, '/');
1239 ret->phase = SP_COPYING;
1240 if (NULL != username)
1241 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1243 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1244 GNUNET_free (baseservicehome);
1245 if (ret->ssh_port_str == NULL)
1247 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1251 servicehome, arg, NULL);
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "copying directory with command scp -r %s %s\n",
1261 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r", "-P",
1266 servicehome, arg, NULL);
1269 if (NULL == ret->proc)
1271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1273 ("Could not start `%s' process to copy configuration directory.\n"),
1275 if (0 != UNLINK (ret->cfgfile))
1276 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1278 GNUNET_CONFIGURATION_destroy (ret->cfg);
1279 GNUNET_free_non_null (ret->hostname);
1280 GNUNET_free_non_null (ret->username);
1281 GNUNET_free (ret->cfgfile);
1283 if ((hostkey != NULL) && (0 != UNLINK (hostkeyfile)))
1284 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1286 GNUNET_free_non_null (hostkeyfile);
1287 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1288 GNUNET_free (servicehome);
1293 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
1295 GNUNET_free_non_null (hostkeyfile);
1296 GNUNET_free (servicehome);
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 "No need to copy configuration file since we are running locally.\n");
1303 ret->phase = SP_COPIED;
1304 /* FIXME: why add_cont? */
1305 GNUNET_SCHEDULER_add_continuation (&start_fsm, ret,
1306 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1308 GNUNET_free_non_null (hostkeyfile);
1309 GNUNET_free (servicehome);
1315 * Restart (stop and start) a GNUnet daemon.
1317 * @param d the daemon that should be restarted
1318 * @param cb function called once the daemon is (re)started
1319 * @param cb_cls closure for cb
1322 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1323 GNUNET_TESTING_NotifyDaemonRunning cb,
1332 d->dead = GNUNET_YES;
1339 if (d->phase == SP_CONFIG_UPDATE)
1341 GNUNET_SCHEDULER_cancel (d->task);
1342 d->phase = SP_START_DONE;
1344 if (d->server != NULL)
1346 GNUNET_CORE_disconnect (d->server);
1352 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1354 GNUNET_TRANSPORT_disconnect (d->th);
1357 /* state clean up and notifications */
1358 GNUNET_free_non_null (d->hello);
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1362 GNUNET_i2s (&d->id));
1365 d->phase = SP_START_ARMING;
1367 /* Check if this is a local or remote process */
1368 if (NULL != d->hostname)
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1372 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1373 d->cfgfile, d->hostname);
1376 if (d->username != NULL)
1377 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1379 arg = GNUNET_strdup (d->hostname);
1381 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1389 "-c", d->cfgfile, "-e", "-r", NULL);
1390 /* Use -r to restart arm and all services */
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1398 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1400 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1404 "-c", d->cfgfile, "-e", "-r", NULL);
1407 GNUNET_free_non_null (del_arg);
1409 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1415 * Stops a GNUnet daemon.
1417 * @param d the daemon that should be stopped
1418 * @param service the name of the service to stop
1419 * @param timeout how long to wait for process for shutdown to complete
1420 * @param cb function called once the daemon was stopped
1421 * @param cb_cls closure for cb
1424 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
1425 const char *service,
1426 struct GNUNET_TIME_Relative timeout,
1427 GNUNET_TESTING_NotifyCompletion cb,
1433 d->dead_cb_cls = cb_cls;
1435 GNUNET_assert (d->running == GNUNET_YES);
1437 if (d->phase == SP_CONFIG_UPDATE)
1439 GNUNET_SCHEDULER_cancel (d->task);
1440 d->phase = SP_START_DONE;
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1445 GNUNET_i2s (&d->id));
1447 if (d->churned_services != NULL)
1449 d->dead_cb (d->dead_cb_cls, "A service has already been turned off!!");
1452 d->phase = SP_SERVICE_SHUTDOWN_START;
1453 d->churned_services = GNUNET_strdup (service);
1454 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1455 /* Check if this is a local or remote process */
1456 if (NULL != d->hostname)
1459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1460 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1461 d->cfgfile, d->hostname);
1464 if (d->username != NULL)
1465 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1467 arg = GNUNET_strdup (d->hostname);
1469 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1477 "-c", d->cfgfile, "-k", service, "-q",
1479 GNUNET_TIME_relative_to_string (timeout),
1481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1482 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -k %s -q\n",
1483 arg, "gnunet-arm", d->cfgfile, service);
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1490 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1492 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1496 "-c", d->cfgfile, "-k", service, "-q",
1498 GNUNET_TIME_relative_to_string (timeout),
1502 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1503 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1508 * Stops a GNUnet daemon.
1510 * @param d the daemon that should be stopped
1511 * @param timeout how long to wait for process for shutdown to complete
1512 * @param cb function called once the daemon was stopped
1513 * @param cb_cls closure for cb
1514 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1516 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1517 * GNUNET_NO to kill off and clean up for good
1520 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1521 struct GNUNET_TIME_Relative timeout,
1522 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1523 int delete_files, int allow_restart)
1529 d->dead_cb_cls = cb_cls;
1534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Setting d->dead on peer `%4s'\n"),
1535 GNUNET_i2s (&d->id));
1537 d->dead = GNUNET_YES;
1541 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1543 /* Free what was left from churning! */
1544 GNUNET_assert (d->cfg != NULL);
1545 GNUNET_CONFIGURATION_destroy (d->cfg);
1546 if (delete_files == GNUNET_YES)
1548 if (0 != UNLINK (d->cfgfile))
1550 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1553 GNUNET_free (d->cfgfile);
1554 GNUNET_free_non_null (d->hostname);
1555 GNUNET_free_non_null (d->username);
1556 if (NULL != d->dead_cb)
1557 d->dead_cb (d->dead_cb_cls, NULL);
1563 if (delete_files == GNUNET_YES)
1565 GNUNET_asprintf (&del_arg, "-d");
1568 if (d->phase == SP_CONFIG_UPDATE)
1570 GNUNET_SCHEDULER_cancel (d->task);
1571 d->phase = SP_START_DONE;
1573 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1574 if (d->server != NULL)
1576 GNUNET_CORE_disconnect (d->server);
1580 /* shutdown ARM process (will terminate others) */
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1583 GNUNET_i2s (&d->id));
1585 d->phase = SP_SHUTDOWN_START;
1586 d->running = GNUNET_NO;
1587 if (allow_restart == GNUNET_YES)
1588 d->churn = GNUNET_YES;
1591 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1593 GNUNET_TRANSPORT_disconnect (d->th);
1596 /* Check if this is a local or remote process */
1597 if (NULL != d->hostname)
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1601 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1602 d->cfgfile, d->hostname);
1605 if (d->username != NULL)
1606 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1608 arg = GNUNET_strdup (d->hostname);
1610 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1618 "-c", d->cfgfile, "-e", "-q", "-T",
1619 GNUNET_TIME_relative_to_string (timeout),
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1622 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1623 arg, "gnunet-arm", d->cfgfile, del_arg);
1624 /* Use -e to end arm, and -d to remove temp files */
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1633 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1637 "-c", d->cfgfile, "-e", "-q", "-T",
1638 GNUNET_TIME_relative_to_string (timeout),
1642 GNUNET_free_non_null (del_arg);
1643 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1644 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1649 * Changes the configuration of a GNUnet daemon.
1651 * @param d the daemon that should be modified
1652 * @param cfg the new configuration for the daemon
1653 * @param cb function called once the configuration was changed
1654 * @param cb_cls closure for cb
1657 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1658 struct GNUNET_CONFIGURATION_Handle *cfg,
1659 GNUNET_TESTING_NotifyCompletion cb,
1664 if (d->phase != SP_START_DONE)
1669 ("Peer not yet running, can not change configuration at this point."));
1673 /* 1) write configuration to temporary file */
1674 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1677 cb (cb_cls, _("Failed to write new configuration to disk."));
1681 /* 2) copy file to remote host (if necessary) */
1682 if (NULL == d->hostname)
1684 /* signal success */
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1691 "Copying updated configuration file to remote host `%s'.\n",
1694 d->phase = SP_CONFIG_UPDATE;
1695 if (NULL != d->username)
1696 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1698 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1699 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1703 d->cfgfile, arg, NULL);
1705 if (NULL == d->proc)
1707 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1708 _("Could not start `%s' process to copy configuration file.\n"),
1711 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1712 d->phase = SP_START_DONE;
1716 d->update_cb_cls = cb_cls;
1718 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1723 * Data kept for each pair of peers that we try
1726 struct GNUNET_TESTING_ConnectContext
1729 * Testing handle to the first daemon.
1731 struct GNUNET_TESTING_Daemon *d1;
1734 * Handle to core of first daemon (to check connect)
1736 struct GNUNET_CORE_Handle *d1core;
1739 * Have we actually connected to the core of the first daemon yet?
1744 * Testing handle to the second daemon.
1746 struct GNUNET_TESTING_Daemon *d2;
1749 * Handler for the request to core to connect to this peer.
1751 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1754 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1756 struct GNUNET_TRANSPORT_Handle *d1th;
1759 * Function to call once we are done (or have timed out).
1761 GNUNET_TESTING_NotifyConnection cb;
1769 * The relative timeout from whence this connect attempt was
1770 * started. Allows for reconnect attempts.
1772 struct GNUNET_TIME_Relative relative_timeout;
1775 * Maximum number of connect attempts, will retry connection
1776 * this number of times on failures.
1778 unsigned int connect_attempts;
1781 * Hello timeout task
1783 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1786 * Connect timeout task
1788 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1791 * When should this operation be complete (or we must trigger
1794 struct GNUNET_TIME_Relative timeout_hello;
1797 * Was the connection attempt successful?
1802 * When connecting, do we need to send the HELLO?
1807 * The distance between the two connected peers
1813 /** Forward declaration **/
1815 reattempt_daemons_connect (void *cls,
1816 const struct GNUNET_SCHEDULER_TaskContext *tc);
1820 * Notify callback about success or failure of the attempt
1821 * to connect the two peers
1823 * @param cls our "struct GNUNET_TESTING_ConnectContext" (freed)
1824 * @param tc reason tells us if we succeeded or failed
1827 notify_connect_result (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1829 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1831 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1832 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1834 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1835 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1838 if (ctx->connect_request_handle != NULL)
1840 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1841 ctx->connect_request_handle = NULL;
1844 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1846 if (ctx->d1th != NULL)
1847 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1849 if (ctx->d1core != NULL)
1850 GNUNET_CORE_disconnect (ctx->d1core);
1856 if (ctx->d1th != NULL)
1857 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1859 if (ctx->d1core != NULL)
1860 GNUNET_CORE_disconnect (ctx->d1core);
1863 if (ctx->connected == GNUNET_YES)
1865 if (ctx->cb != NULL)
1867 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance,
1868 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1871 else if (ctx->connect_attempts > 0)
1873 ctx->d1core_ready = GNUNET_NO;
1875 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1880 if (ctx->cb != NULL)
1882 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1883 ctx->d2->cfg, ctx->d1, ctx->d2, _("Peers failed to connect"));
1891 * Success, connection is up. Signal client our success.
1893 * @param cls our "struct GNUNET_TESTING_ConnectContext"
1894 * @param peer identity of the peer that has connected
1895 * @param atsi performance information
1899 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
1900 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1902 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1905 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Connected peer %s to peer %s\n",
1906 ctx->d1->shortname, GNUNET_i2s (peer));
1909 if (0 != memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1911 ctx->connected = GNUNET_YES;
1912 ctx->distance = 0; /* FIXME: distance */
1913 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1915 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1916 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1918 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1919 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
1923 * Task called once a core connect request has been transmitted.
1925 * @param cls struct GNUNET_TESTING_ConnectContext
1926 * @param success was the request successful?
1929 core_connect_request_cont (void *cls, int success)
1931 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1933 ctx->connect_request_handle = NULL;
1937 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1939 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1940 struct GNUNET_MessageHeader *hello;
1942 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1943 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1945 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL) &&
1946 (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)) &&
1947 (ctx->d1->phase == SP_START_DONE) && (ctx->d2->phase == SP_START_DONE))
1949 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1950 GNUNET_assert (hello != NULL);
1952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n",
1953 ctx->d2->shortname, ctx->d1->shortname);
1955 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1956 GNUNET_assert (ctx->d1core != NULL);
1957 ctx->connect_request_handle =
1958 GNUNET_CORE_peer_request_connect (ctx->d1core, &ctx->d2->id,
1959 &core_connect_request_cont, ctx);
1962 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1963 "Sending connect request to CORE of %s for peer %s\n",
1964 GNUNET_i2s (&ctx->d1->id),
1965 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1967 ctx->timeout_hello =
1968 GNUNET_TIME_relative_add (ctx->timeout_hello,
1969 GNUNET_TIME_relative_multiply
1970 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1972 ctx->hello_send_task =
1973 GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello, &send_hello, ctx);
1977 * Notify of a successful connection to the core service.
1979 * @param cls a ConnectContext
1980 * @param server handle to the core service
1981 * @param my_identity the peer identity of this peer
1982 * @param publicKey the public key of the peer
1985 core_init_notify (void *cls, struct GNUNET_CORE_Handle *server,
1986 const struct GNUNET_PeerIdentity *my_identity,
1987 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
1990 struct GNUNET_TESTING_ConnectContext *connect_ctx = cls;
1992 connect_ctx->d1core_ready = GNUNET_YES;
1994 if (connect_ctx->send_hello == GNUNET_NO)
1996 connect_ctx->connect_request_handle =
1997 GNUNET_CORE_peer_request_connect (connect_ctx->d1core,
1998 &connect_ctx->d2->id,
1999 &core_connect_request_cont,
2001 GNUNET_assert (connect_ctx->connect_request_handle != NULL);
2003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2004 "Sending connect request to CORE of %s for peer %s\n",
2005 connect_ctx->d1->shortname, connect_ctx->d2->shortname);
2013 reattempt_daemons_connect (void *cls,
2014 const struct GNUNET_SCHEDULER_TaskContext *tc)
2016 struct GNUNET_TESTING_ConnectContext *ctx = cls;
2018 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2019 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2021 #if DEBUG_TESTING_RECONNECT
2022 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2023 "re-attempting connect of peer %s to peer %s\n",
2024 ctx->d1->shortname, ctx->d2->shortname);
2026 ctx->connect_attempts--;
2027 GNUNET_assert (ctx->d1core == NULL);
2028 ctx->d1core_ready = GNUNET_NO;
2030 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2031 &connect_notify, NULL, NULL, NULL, GNUNET_NO, NULL,
2032 GNUNET_NO, no_handlers);
2033 if (ctx->d1core == NULL)
2035 if (NULL != ctx->cb)
2036 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2037 ctx->d2->cfg, ctx->d1, ctx->d2,
2038 _("Failed to connect to core service of first peer!\n"));
2043 /* Don't know reason for initial connect failure, update the HELLO for the second peer */
2044 if (NULL != ctx->d2->hello)
2046 GNUNET_free (ctx->d2->hello);
2047 ctx->d2->hello = NULL;
2048 if (NULL != ctx->d2->th)
2050 GNUNET_TRANSPORT_get_hello_cancel (ctx->d2->ghh);
2051 ctx->d2->ghh = NULL;
2052 GNUNET_TRANSPORT_disconnect (ctx->d2->th);
2055 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2057 GNUNET_assert (ctx->d2->th != NULL);
2059 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2062 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
2065 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2067 if (ctx->d2->th == NULL)
2069 GNUNET_CORE_disconnect (ctx->d1core);
2071 if (NULL != ctx->cb)
2072 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2073 ctx->d2->cfg, ctx->d1, ctx->d2,
2074 _("Failed to connect to transport service!\n"));
2078 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2081 if (ctx->send_hello == GNUNET_YES)
2084 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2086 if (ctx->d1th == NULL)
2088 GNUNET_CORE_disconnect (ctx->d1core);
2090 if (NULL != ctx->cb)
2091 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2092 ctx->d2->cfg, ctx->d1, ctx->d2,
2093 _("Failed to connect to transport service!\n"));
2096 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2097 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2101 ctx->connect_request_handle =
2102 GNUNET_CORE_peer_request_connect (ctx->d1core, &ctx->d2->id,
2103 &core_connect_request_cont, ctx);
2106 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2107 ¬ify_connect_result, ctx);
2111 * Iterator for currently known peers, to ensure
2112 * that we don't try to send duplicate connect
2115 * @param cls our "struct GNUNET_TESTING_ConnectContext"
2116 * @param peer identity of the peer that has connected,
2117 * NULL when iteration has finished
2118 * @param atsi performance information
2122 core_initial_iteration (void *cls, const struct GNUNET_PeerIdentity *peer,
2123 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2125 struct GNUNET_TESTING_ConnectContext *ctx = cls;
2127 if ((peer != NULL) &&
2128 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
2130 ctx->connected = GNUNET_YES;
2131 ctx->distance = 0; /* FIXME: distance */
2135 return; /* ignore other peers */
2136 /* peer == NULL: End of iteration over peers */
2138 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->timeout_task);
2139 if (ctx->connected == GNUNET_YES)
2141 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2145 /* Peer not already connected, need to schedule connect request! */
2146 if (ctx->d1core == NULL)
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2150 "Peers are NOT connected, connecting to core!\n");
2153 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2154 &connect_notify, NULL, NULL, NULL, GNUNET_NO, NULL,
2155 GNUNET_NO, no_handlers);
2158 if (ctx->d1core == NULL)
2160 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2164 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL)) /* Do not yet have the second peer's hello, set up a task to get it */
2167 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2169 if (ctx->d2->th == NULL)
2171 GNUNET_CORE_disconnect (ctx->d1core);
2174 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2178 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2181 if (ctx->send_hello == GNUNET_YES)
2184 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2186 if (ctx->d1th == NULL)
2188 GNUNET_CORE_disconnect (ctx->d1core);
2191 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2194 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2195 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2199 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2200 ¬ify_connect_result, ctx);
2206 * Establish a connection between two GNUnet daemons. The daemons
2207 * must both be running and not be stopped until either the
2208 * 'cb' callback is called OR the connection request has been
2209 * explicitly cancelled.
2211 * @param d1 handle for the first daemon
2212 * @param d2 handle for the second daemon
2213 * @param timeout how long is the connection attempt
2215 * @param max_connect_attempts how many times should we try to reconnect
2217 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
2218 * the HELLO has already been exchanged
2219 * @param cb function to call at the end
2220 * @param cb_cls closure for cb
2221 * @return handle to cancel the request
2223 struct GNUNET_TESTING_ConnectContext *
2224 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
2225 struct GNUNET_TESTING_Daemon *d2,
2226 struct GNUNET_TIME_Relative timeout,
2227 unsigned int max_connect_attempts,
2229 GNUNET_TESTING_NotifyConnection cb,
2232 struct GNUNET_TESTING_ConnectContext *ctx;
2234 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
2237 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
2238 _("Peers are not fully running yet, can not connect!\n"));
2239 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Peers are not up!\n");
2243 ctx = GNUNET_malloc (sizeof (struct GNUNET_TESTING_ConnectContext));
2246 ctx->timeout_hello =
2247 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
2248 ctx->relative_timeout =
2249 GNUNET_TIME_relative_divide (timeout, max_connect_attempts);
2251 ctx->cb_cls = cb_cls;
2252 ctx->connect_attempts = max_connect_attempts;
2253 ctx->connected = GNUNET_NO;
2254 ctx->send_hello = send_hello;
2256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asked to connect peer %s to peer %s\n",
2257 d1->shortname, d2->shortname);
2260 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
2261 GNUNET_assert (GNUNET_OK ==
2262 GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id,
2263 &core_initial_iteration, ctx));
2269 * Cancel an attempt to connect two daemons.
2271 * @param cc connect context
2274 GNUNET_TESTING_daemons_connect_cancel (struct GNUNET_TESTING_ConnectContext *cc)
2276 if (GNUNET_SCHEDULER_NO_TASK != cc->timeout_task)
2278 GNUNET_SCHEDULER_cancel (cc->timeout_task);
2279 cc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2281 if (GNUNET_SCHEDULER_NO_TASK != cc->hello_send_task)
2283 GNUNET_SCHEDULER_cancel (cc->hello_send_task);
2284 cc->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2286 if (NULL != cc->d1core)
2288 GNUNET_CORE_disconnect (cc->d1core);
2291 if (NULL != cc->d1th)
2293 GNUNET_TRANSPORT_disconnect (cc->d1th);
2300 /* end of testing.c */