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;
95 GNUNET_assert (message != NULL);
96 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) message,
97 GNUNET_NO, &test_address, &empty);
98 if (GNUNET_YES == empty)
101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skipping empty HELLO address\n");
106 GNUNET_assert (daemon->phase == SP_GET_HELLO ||
107 daemon->phase == SP_START_DONE);
109 if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
110 GNUNET_SCHEDULER_cancel (daemon->task);
112 if (daemon->server != NULL)
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116 "Received `%s' from transport service of `%4s', disconnecting core!\n",
117 "HELLO", GNUNET_i2s (&daemon->id));
119 GNUNET_CORE_disconnect (daemon->server);
120 daemon->server = NULL;
123 msize = ntohs (message->size);
128 if (daemon->ghh != NULL)
130 GNUNET_TRANSPORT_get_hello_cancel (daemon->ghh);
133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
134 "Received `%s' from transport service of `%4s'\n", "HELLO",
135 GNUNET_i2s (&daemon->id));
138 GNUNET_free_non_null (daemon->hello);
139 daemon->hello = GNUNET_malloc (msize);
140 memcpy (daemon->hello, message, msize);
142 if (daemon->th != NULL)
144 GNUNET_TRANSPORT_disconnect (daemon->th);
147 daemon->phase = SP_START_DONE;
151 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
154 * Notify of a peer being up and running. Scheduled as a task
155 * so that variables which may need to be set are set before
156 * the connect callback can set up new operations.
158 * @param cls the testing daemon
159 * @param tc task scheduler context
162 notify_daemon_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
164 struct GNUNET_TESTING_Daemon *d = cls;
165 GNUNET_TESTING_NotifyDaemonRunning cb;
170 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
175 * Finite-state machine for starting GNUnet.
177 * @param cls our "struct GNUNET_TESTING_Daemon"
181 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
183 struct GNUNET_TESTING_Daemon *d = cls;
184 GNUNET_TESTING_NotifyDaemonRunning cb;
185 enum GNUNET_OS_ProcessStatusType type;
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer FSM is in phase %u.\n", d->phase);
194 d->task = GNUNET_SCHEDULER_NO_TASK;
198 /* confirm copying complete */
199 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
201 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
206 cb (d->cb_cls, NULL, d->cfg, d,
208 ("`scp' does not seem to terminate (timeout copying config).\n"));
213 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
217 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
222 cb (d->cb_cls, NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
225 GNUNET_OS_process_close (d->proc);
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Successfully copied configuration file.\n");
230 d->phase = SP_COPIED;
233 /* Start create hostkey process if we don't already know the peer identity! */
234 if (GNUNET_NO == d->have_hostkey)
236 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
237 if (d->pipe_stdout == NULL)
242 cb (d->cb_cls, NULL, d->cfg, d,
245 _("Failed to create pipe for `gnunet-peerinfo' process.\n") :
246 _("Failed to create pipe for `ssh' process.\n"));
249 if (NULL == d->hostname)
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "Starting `%s', with command `%s %s %s %s'.\n",
254 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
258 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
259 "gnunet-peerinfo", "-c", d->cfgfile, "-sq",
261 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
265 if (d->username != NULL)
266 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
268 dst = GNUNET_strdup (d->hostname);
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
273 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
276 if (d->ssh_port_str == NULL)
278 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh",
282 dst, "gnunet-peerinfo", "-c",
283 d->cfgfile, "-sq", NULL);
288 GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh", "-p",
293 dst, "gnunet-peerinfo", "-c", d->cfgfile,
296 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
302 _("Could not start `%s' process to create hostkey.\n"),
303 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
307 cb (d->cb_cls, NULL, d->cfg, d,
309 d->hostname) ? _("Failed to start `gnunet-peerinfo' process.\n")
310 : _("Failed to start `ssh' process.\n"));
311 GNUNET_DISK_pipe_close (d->pipe_stdout);
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
318 d->phase = SP_HOSTKEY_CREATE;
320 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
322 GNUNET_DISK_pipe_handle
324 GNUNET_DISK_PIPE_END_READ),
327 else /* Already have a hostkey! */
329 if (d->hostkey_callback != NULL)
331 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
332 d->hostkey_callback = NULL;
333 d->phase = SP_HOSTKEY_CREATED;
336 d->phase = SP_TOPOLOGY_SETUP;
339 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
342 case SP_HOSTKEY_CREATE:
344 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
345 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
346 &d->hostkeybuf[d->hostkeybufpos],
347 sizeof (d->hostkeybuf) - d->hostkeybufpos);
349 d->hostkeybufpos += bytes_read;
351 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
355 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
357 GNUNET_DISK_pipe_handle
359 GNUNET_DISK_PIPE_END_READ),
363 d->hostkeybuf[103] = '\0';
365 if ((bytes_read < 0) ||
367 GNUNET_CRYPTO_hash_from_string (d->hostkeybuf, &d->id.hashPubKey)))
371 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
372 _("Error reading from gnunet-peerinfo: %s\n"),
375 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
376 _("Malformed output from gnunet-peerinfo!\n"));
379 GNUNET_DISK_pipe_close (d->pipe_stdout);
380 d->pipe_stdout = NULL;
381 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
382 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
383 GNUNET_OS_process_close (d->proc);
386 cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
389 d->shortname = GNUNET_strdup (GNUNET_i2s (&d->id));
390 GNUNET_DISK_pipe_close (d->pipe_stdout);
391 d->pipe_stdout = NULL;
392 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
393 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
394 GNUNET_OS_process_close (d->proc);
396 d->have_hostkey = GNUNET_YES;
397 if (d->hostkey_callback != NULL)
399 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
400 d->hostkey_callback = NULL;
401 d->phase = SP_HOSTKEY_CREATED;
405 d->phase = SP_TOPOLOGY_SETUP;
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
411 case SP_HOSTKEY_CREATED:
412 /* wait for topology finished */
413 if ((GNUNET_YES == d->dead) ||
414 (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0))
419 cb (d->cb_cls, NULL, d->cfg, d,
420 _("`Failed while waiting for topology setup!\n"));
425 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
428 case SP_TOPOLOGY_SETUP: /* Indicates topology setup has completed! */
429 /* start GNUnet on remote host */
430 if (NULL == d->hostname)
433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
434 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
435 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-L", "DEBUG",
439 GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm", "-c",
445 GNUNET_TIME_relative_to_string
446 (GNUNET_TIME_absolute_get_remaining
447 (d->max_timeout)), NULL);
451 if (d->username != NULL)
452 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
454 dst = GNUNET_strdup (d->hostname);
457 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
458 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
459 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
460 "-L", "DEBUG", "-s", "-q");
462 if (d->ssh_port_str == NULL)
464 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
472 "-c", d->cfgfile, "-s", "-q", "-T",
473 GNUNET_TIME_relative_to_string
474 (GNUNET_TIME_absolute_get_remaining
475 (d->max_timeout)), NULL);
481 GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-p",
490 "-c", d->cfgfile, "-s", "-q", "-T",
491 GNUNET_TIME_relative_to_string
492 (GNUNET_TIME_absolute_get_remaining
493 (d->max_timeout)), NULL);
499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
500 _("Could not start `%s' process to start GNUnet.\n"),
501 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
505 cb (d->cb_cls, NULL, d->cfg, d,
507 d->hostname) ? _("Failed to start `gnunet-arm' process.\n") :
508 _("Failed to start `ssh' process.\n"));
512 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
513 "Started `%s', waiting for `%s' to be up.\n", "gnunet-arm",
514 "gnunet-service-core");
516 d->phase = SP_START_ARMING;
518 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
521 case SP_START_ARMING:
522 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
524 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
529 cb (d->cb_cls, NULL, d->cfg, d,
531 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
532 _("`ssh' does not seem to terminate.\n"));
533 GNUNET_CONFIGURATION_destroy (d->cfg);
534 GNUNET_free (d->cfgfile);
535 GNUNET_free_non_null (d->hostname);
536 GNUNET_free_non_null (d->username);
537 GNUNET_free (d->proc);
543 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
548 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Successfully started `%s'.\n",
551 GNUNET_free (d->proc);
552 d->phase = SP_START_CORE;
554 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Calling CORE_connect\n");
558 if (d->server != NULL)
559 GNUNET_CORE_disconnect (d->server);
561 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
564 if (GNUNET_YES == d->dead)
565 GNUNET_TESTING_daemon_stop (d,
566 GNUNET_TIME_absolute_get_remaining
567 (d->max_timeout), d->dead_cb,
568 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
569 else if (NULL != d->cb)
570 d->cb (d->cb_cls, &d->id, d->cfg, d,
571 _("Failed to connect to transport service!\n"));
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "Connected to transport service `%s', getting HELLO\n",
577 GNUNET_i2s (&d->id));
580 d->ghh = GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
581 /* FIXME: store task ID somewhere! */
582 GNUNET_SCHEDULER_add_now (¬ify_daemon_started, d);
586 * cb (d->cb_cls, &d->id, d->cfg, d, NULL); */
587 d->running = GNUNET_YES;
588 d->phase = SP_GET_HELLO;
591 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
593 if (d->server != NULL)
594 GNUNET_CORE_disconnect (d->server);
596 GNUNET_TRANSPORT_disconnect (d->th);
600 cb (d->cb_cls, NULL, d->cfg, d, _("Unable to get HELLO for peer!\n"));
601 GNUNET_CONFIGURATION_destroy (d->cfg);
602 GNUNET_free (d->cfgfile);
603 GNUNET_free_non_null (d->hostname);
604 GNUNET_free_non_null (d->username);
608 if (d->hello != NULL)
610 GNUNET_assert (d->task == GNUNET_SCHEDULER_NO_TASK);
612 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
613 (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
619 case SP_SERVICE_START:
620 /* confirm gnunet-arm exited */
621 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
623 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
628 cb (d->cb_cls, NULL, d->cfg, d,
630 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
631 _("`ssh' does not seem to terminate.\n"));
636 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
641 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
646 cb (d->cb_cls, NULL, d->cfg, d,
650 ("`gnunet-arm' terminated with non-zero exit status (or timed out)!\n")
651 : _("`ssh' does not seem to terminate.\n"));
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service startup complete!\n");
660 d->phase = SP_START_DONE;
662 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
664 case SP_SERVICE_SHUTDOWN_START:
665 /* confirm copying complete */
666 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
668 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
670 if (NULL != d->dead_cb)
671 d->dead_cb (d->dead_cb_cls,
673 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
678 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
683 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
685 if (NULL != d->dead_cb)
686 d->dead_cb (d->dead_cb_cls,
688 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
695 if (NULL != d->dead_cb)
696 d->dead_cb (d->dead_cb_cls, NULL);
698 case SP_SHUTDOWN_START:
699 /* confirm copying complete */
700 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
702 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
704 if (NULL != d->dead_cb)
705 d->dead_cb (d->dead_cb_cls,
707 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
710 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
712 GNUNET_TRANSPORT_disconnect (d->th);
715 GNUNET_CONFIGURATION_destroy (d->cfg);
716 GNUNET_free (d->cfgfile);
717 GNUNET_free_non_null (d->hello);
718 GNUNET_free_non_null (d->hostname);
719 GNUNET_free_non_null (d->username);
720 GNUNET_free_non_null (d->shortname);
721 GNUNET_free_non_null (d->proc);
728 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
732 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
734 if (NULL != d->dead_cb)
735 d->dead_cb (d->dead_cb_cls,
737 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
740 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
742 GNUNET_TRANSPORT_disconnect (d->th);
745 if (d->server != NULL)
747 GNUNET_CORE_disconnect (d->server);
750 GNUNET_CONFIGURATION_destroy (d->cfg);
751 GNUNET_free (d->cfgfile);
752 GNUNET_free_non_null (d->hello);
753 GNUNET_free_non_null (d->hostname);
754 GNUNET_free_non_null (d->username);
755 GNUNET_free_non_null (d->shortname);
756 GNUNET_free_non_null (d->proc);
762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
764 if (d->server != NULL)
766 GNUNET_CORE_disconnect (d->server);
772 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
774 GNUNET_TRANSPORT_disconnect (d->th);
778 if (NULL != d->dead_cb)
779 d->dead_cb (d->dead_cb_cls, NULL);
781 /* state clean up and notifications */
782 if (d->churn == GNUNET_NO)
784 GNUNET_CONFIGURATION_destroy (d->cfg);
785 GNUNET_free (d->cfgfile);
786 GNUNET_free_non_null (d->hostname);
787 GNUNET_free_non_null (d->username);
790 GNUNET_free_non_null (d->hello);
792 GNUNET_free_non_null (d->shortname);
793 GNUNET_free_non_null (d->proc);
796 if (d->churn == GNUNET_NO)
800 case SP_CONFIG_UPDATE:
801 /* confirm copying complete */
802 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
804 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
809 cb (d->cb_cls, NULL, d->cfg, d,
810 _("`scp' does not seem to terminate.\n"));
815 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
819 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
821 if (NULL != d->update_cb)
822 d->update_cb (d->update_cb_cls, _("`scp' did not complete cleanly.\n"));
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
827 "Successfully copied configuration file.\n");
829 if (NULL != d->update_cb)
830 d->update_cb (d->update_cb_cls, NULL);
831 d->phase = SP_START_DONE;
837 * Continues GNUnet daemon startup when user wanted to be notified
838 * once a hostkey was generated (for creating friends files, blacklists,
841 * @param daemon the daemon to finish starting
844 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
846 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
847 daemon->phase = SP_TOPOLOGY_SETUP;
851 * Check whether the given daemon is running.
853 * @param daemon the daemon to check
855 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
856 * daemon is down, GNUNET_SYSERR on error.
859 GNUNET_TESTING_test_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
862 return GNUNET_SYSERR;
864 if (daemon->running == GNUNET_YES)
871 * Starts a GNUnet daemon service which has been previously stopped.
873 * @param d the daemon for which the service should be started
874 * @param service the name of the service to start
875 * @param timeout how long to wait for process for shutdown to complete
876 * @param cb function called once the service starts
877 * @param cb_cls closure for cb
880 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
882 struct GNUNET_TIME_Relative
884 GNUNET_TESTING_NotifyDaemonRunning
892 GNUNET_assert (d->running == GNUNET_YES);
894 if (d->phase == SP_CONFIG_UPDATE)
896 GNUNET_SCHEDULER_cancel (d->task);
897 d->phase = SP_START_DONE;
900 if (d->churned_services == NULL)
902 d->cb (d->cb_cls, &d->id, d->cfg, d,
903 "No service has been churned off yet!!");
906 d->phase = SP_SERVICE_START;
907 GNUNET_free (d->churned_services);
908 d->churned_services = NULL;
909 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
910 /* Check if this is a local or remote process */
911 if (NULL != d->hostname)
914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
915 "Starting gnunet-arm with config `%s' on host `%s'.\n",
916 d->cfgfile, d->hostname);
919 if (d->username != NULL)
920 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
922 arg = GNUNET_strdup (d->hostname);
924 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
932 "-c", d->cfgfile, "-i", service, "-q",
934 GNUNET_TIME_relative_to_string (timeout),
936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
937 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q\n",
938 arg, "gnunet-arm", d->cfgfile, service);
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
947 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
951 "-c", d->cfgfile, "-i", service, "-q",
953 GNUNET_TIME_relative_to_string (timeout),
957 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
958 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
962 * Starts a GNUnet daemon's service.
964 * @param d the daemon for which the service should be started
965 * @param service the name of the service to start
966 * @param timeout how long to wait for process for startup
967 * @param cb function called once gnunet-arm returns
968 * @param cb_cls closure for cb
971 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
973 struct GNUNET_TIME_Relative timeout,
974 GNUNET_TESTING_NotifyDaemonRunning cb,
982 GNUNET_assert (service != NULL);
983 GNUNET_assert (d->running == GNUNET_YES);
984 GNUNET_assert (d->phase == SP_START_DONE);
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
988 _("Starting service %s for peer `%4s'\n"), service,
989 GNUNET_i2s (&d->id));
992 d->phase = SP_SERVICE_START;
993 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
994 /* Check if this is a local or remote process */
995 if (NULL != d->hostname)
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "Starting gnunet-arm with config `%s' on host `%s'.\n",
1000 d->cfgfile, d->hostname);
1003 if (d->username != NULL)
1004 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1006 arg = GNUNET_strdup (d->hostname);
1008 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1016 "-c", d->cfgfile, "-i", service, "-q",
1018 GNUNET_TIME_relative_to_string (timeout),
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q -T %s\n",
1022 arg, "gnunet-arm", d->cfgfile, service,
1023 GNUNET_TIME_relative_to_string (timeout));
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1030 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1032 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1036 "-c", d->cfgfile, "-i", service, "-q",
1038 GNUNET_TIME_relative_to_string (timeout),
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1041 "Starting gnunet-arm with command %s -c %s -i %s -q -T %s\n",
1042 "gnunet-arm", d->cfgfile, service,
1043 GNUNET_TIME_relative_to_string (timeout));
1046 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1047 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1051 * Start a peer that has previously been stopped using the daemon_stop
1052 * call (and files weren't deleted and the allow restart flag)
1054 * @param daemon the daemon to start (has been previously stopped)
1055 * @param timeout how long to wait for restart
1056 * @param cb the callback for notification when the peer is running
1057 * @param cb_cls closure for the callback
1060 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
1061 struct GNUNET_TIME_Relative timeout,
1062 GNUNET_TESTING_NotifyDaemonRunning cb,
1065 if (daemon->running == GNUNET_YES)
1067 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
1068 "Daemon already running, can't restart!");
1073 daemon->cb_cls = cb_cls;
1074 daemon->phase = SP_TOPOLOGY_SETUP;
1075 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1076 /* FIXME: why add_continuation? */
1077 GNUNET_SCHEDULER_add_continuation (&start_fsm, daemon,
1078 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1082 * Starts a GNUnet daemon. GNUnet must be installed on the target
1083 * system and available in the PATH. The machine must furthermore be
1084 * reachable via "ssh" (unless the hostname is "NULL") without the
1085 * need to enter a password.
1087 * @param cfg configuration to use
1088 * @param timeout how long to wait starting up peers
1089 * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
1090 * to really start the peer (default)
1091 * @param hostname name of the machine where to run GNUnet
1092 * (use NULL for localhost).
1093 * @param ssh_username ssh username to use when connecting to hostname
1094 * @param sshport port to pass to ssh process when connecting to hostname
1095 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
1096 * @param hostkey_callback function to call once the hostkey has been
1097 * generated for this peer, but it hasn't yet been started
1098 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
1099 * @param hostkey_cls closure for hostkey callback
1100 * @param cb function to call once peer is up, or failed to start
1101 * @param cb_cls closure for cb
1102 * @return handle to the daemon (actual start will be completed asynchronously)
1104 struct GNUNET_TESTING_Daemon *
1105 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1106 struct GNUNET_TIME_Relative timeout, int pretend,
1107 const char *hostname, const char *ssh_username,
1108 uint16_t sshport, const char *hostkey,
1109 GNUNET_TESTING_NotifyHostkeyCreated
1110 hostkey_callback, void *hostkey_cls,
1111 GNUNET_TESTING_NotifyDaemonRunning cb,
1114 struct GNUNET_TESTING_Daemon *ret;
1118 char *baseservicehome;
1121 char *temp_file_name;
1122 struct GNUNET_DISK_FileHandle *fn;
1123 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
1124 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
1126 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
1127 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
1130 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
1133 ret->ssh_port_str = NULL;
1135 /* Find service home and base service home directories, create it if it doesn't exist */
1136 GNUNET_assert (GNUNET_OK ==
1137 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
1141 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1142 GNUNET_asprintf (&temp_file_name, "%s/gnunet-testing-config", servicehome);
1143 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1144 GNUNET_free (temp_file_name);
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "Setting up peer with configuration file `%s'.\n", ret->cfgfile);
1149 if (NULL == ret->cfgfile)
1151 GNUNET_free_non_null (ret->ssh_port_str);
1152 GNUNET_free_non_null (ret->hostname);
1156 ret->hostkey_callback = hostkey_callback;
1157 ret->hostkey_cls = hostkey_cls;
1159 ret->cb_cls = cb_cls;
1160 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1161 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1162 GNUNET_CONFIGURATION_set_value_string (ret->cfg, "PATHS", "DEFAULTCONFIG",
1165 if (hostkey != NULL) /* Get the peer identity from the hostkey */
1167 private_key = GNUNET_CRYPTO_rsa_decode_key (hostkey, HOSTKEYFILESIZE);
1168 GNUNET_assert (private_key != NULL);
1169 GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
1170 GNUNET_CRYPTO_hash (&public_key,
1171 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1172 &ret->id.hashPubKey);
1173 ret->shortname = GNUNET_strdup (GNUNET_i2s (&ret->id));
1174 ret->have_hostkey = GNUNET_YES;
1175 GNUNET_CRYPTO_rsa_key_free (private_key);
1178 /* Write hostkey to file, if we were given one */
1180 if (hostkey != NULL)
1182 GNUNET_asprintf (&hostkeyfile, "%s/.hostkey", servicehome);
1183 fn = GNUNET_DISK_file_open (hostkeyfile,
1184 GNUNET_DISK_OPEN_READWRITE |
1185 GNUNET_DISK_OPEN_CREATE,
1186 GNUNET_DISK_PERM_USER_READ |
1187 GNUNET_DISK_PERM_USER_WRITE);
1188 GNUNET_assert (fn != NULL);
1189 GNUNET_assert (HOSTKEYFILESIZE ==
1190 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
1191 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
1194 /* write configuration to temporary file */
1195 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1197 if (0 != UNLINK (ret->cfgfile))
1198 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1200 GNUNET_CONFIGURATION_destroy (ret->cfg);
1201 GNUNET_free_non_null (ret->hostname);
1202 GNUNET_free (ret->cfgfile);
1206 if (ssh_username != NULL)
1207 username = GNUNET_strdup (ssh_username);
1208 if ((ssh_username == NULL) &&
1210 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "USERNAME",
1213 if (NULL != getenv ("USER"))
1214 username = GNUNET_strdup (getenv ("USER"));
1218 ret->username = username;
1220 if (GNUNET_NO == pretend) /* Copy files, enter finite state machine */
1222 /* copy directory to remote host */
1223 if (NULL != hostname)
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Copying configuration directory to host `%s'.\n", hostname);
1229 baseservicehome = GNUNET_strdup (servicehome);
1230 /* Remove trailing /'s */
1231 while (baseservicehome[strlen (baseservicehome) - 1] == '/')
1232 baseservicehome[strlen (baseservicehome) - 1] = '\0';
1233 /* Find next directory /, jump one ahead */
1234 slash = strrchr (baseservicehome, '/');
1238 ret->phase = SP_COPYING;
1239 if (NULL != username)
1240 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1242 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1243 GNUNET_free (baseservicehome);
1244 if (ret->ssh_port_str == NULL)
1246 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1250 servicehome, arg, NULL);
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "copying directory with command scp -r %s %s\n",
1260 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r", "-P",
1265 servicehome, arg, NULL);
1268 if (NULL == ret->proc)
1270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1272 ("Could not start `%s' process to copy configuration directory.\n"),
1274 if (0 != UNLINK (ret->cfgfile))
1275 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1277 GNUNET_CONFIGURATION_destroy (ret->cfg);
1278 GNUNET_free_non_null (ret->hostname);
1279 GNUNET_free_non_null (ret->username);
1280 GNUNET_free (ret->cfgfile);
1282 if ((hostkey != NULL) && (0 != UNLINK (hostkeyfile)))
1283 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1285 GNUNET_free_non_null (hostkeyfile);
1286 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1287 GNUNET_free (servicehome);
1292 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
1294 GNUNET_free_non_null (hostkeyfile);
1295 GNUNET_free (servicehome);
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300 "No need to copy configuration file since we are running locally.\n");
1302 ret->phase = SP_COPIED;
1303 /* FIXME: why add_cont? */
1304 GNUNET_SCHEDULER_add_continuation (&start_fsm, ret,
1305 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1307 GNUNET_free_non_null (hostkeyfile);
1308 GNUNET_free (servicehome);
1314 * Restart (stop and start) a GNUnet daemon.
1316 * @param d the daemon that should be restarted
1317 * @param cb function called once the daemon is (re)started
1318 * @param cb_cls closure for cb
1321 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1322 GNUNET_TESTING_NotifyDaemonRunning cb,
1331 d->dead = GNUNET_YES;
1338 if (d->phase == SP_CONFIG_UPDATE)
1340 GNUNET_SCHEDULER_cancel (d->task);
1341 d->phase = SP_START_DONE;
1343 if (d->server != NULL)
1345 GNUNET_CORE_disconnect (d->server);
1351 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1353 GNUNET_TRANSPORT_disconnect (d->th);
1356 /* state clean up and notifications */
1357 GNUNET_free_non_null (d->hello);
1360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1361 GNUNET_i2s (&d->id));
1364 d->phase = SP_START_ARMING;
1366 /* Check if this is a local or remote process */
1367 if (NULL != d->hostname)
1370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1371 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1372 d->cfgfile, d->hostname);
1375 if (d->username != NULL)
1376 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1378 arg = GNUNET_strdup (d->hostname);
1380 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1388 "-c", d->cfgfile, "-e", "-r", NULL);
1389 /* Use -r to restart arm and all services */
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1399 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1403 "-c", d->cfgfile, "-e", "-r", NULL);
1406 GNUNET_free_non_null (del_arg);
1408 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1414 * Stops a GNUnet daemon.
1416 * @param d the daemon that should be stopped
1417 * @param service the name of the service to stop
1418 * @param timeout how long to wait for process for shutdown to complete
1419 * @param cb function called once the daemon was stopped
1420 * @param cb_cls closure for cb
1423 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
1424 const char *service,
1425 struct GNUNET_TIME_Relative timeout,
1426 GNUNET_TESTING_NotifyCompletion cb,
1432 d->dead_cb_cls = cb_cls;
1434 GNUNET_assert (d->running == GNUNET_YES);
1436 if (d->phase == SP_CONFIG_UPDATE)
1438 GNUNET_SCHEDULER_cancel (d->task);
1439 d->phase = SP_START_DONE;
1443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1444 GNUNET_i2s (&d->id));
1446 if (d->churned_services != NULL)
1448 d->dead_cb (d->dead_cb_cls, "A service has already been turned off!!");
1451 d->phase = SP_SERVICE_SHUTDOWN_START;
1452 d->churned_services = GNUNET_strdup (service);
1453 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1454 /* Check if this is a local or remote process */
1455 if (NULL != d->hostname)
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1460 d->cfgfile, d->hostname);
1463 if (d->username != NULL)
1464 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1466 arg = GNUNET_strdup (d->hostname);
1468 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1476 "-c", d->cfgfile, "-k", service, "-q",
1478 GNUNET_TIME_relative_to_string (timeout),
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1481 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -k %s -q\n",
1482 arg, "gnunet-arm", d->cfgfile, service);
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1489 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1491 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1495 "-c", d->cfgfile, "-k", service, "-q",
1497 GNUNET_TIME_relative_to_string (timeout),
1501 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1502 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1507 * Stops a GNUnet daemon.
1509 * @param d the daemon that should be stopped
1510 * @param timeout how long to wait for process for shutdown to complete
1511 * @param cb function called once the daemon was stopped
1512 * @param cb_cls closure for cb
1513 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1515 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1516 * GNUNET_NO to kill off and clean up for good
1519 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1520 struct GNUNET_TIME_Relative timeout,
1521 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1522 int delete_files, int allow_restart)
1528 d->dead_cb_cls = cb_cls;
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Setting d->dead on peer `%4s'\n"),
1534 GNUNET_i2s (&d->id));
1536 d->dead = GNUNET_YES;
1540 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1542 /* Free what was left from churning! */
1543 GNUNET_assert (d->cfg != NULL);
1544 GNUNET_CONFIGURATION_destroy (d->cfg);
1545 if (delete_files == GNUNET_YES)
1547 if (0 != UNLINK (d->cfgfile))
1549 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1552 GNUNET_free (d->cfgfile);
1553 GNUNET_free_non_null (d->hostname);
1554 GNUNET_free_non_null (d->username);
1555 if (NULL != d->dead_cb)
1556 d->dead_cb (d->dead_cb_cls, NULL);
1562 if (delete_files == GNUNET_YES)
1564 GNUNET_asprintf (&del_arg, "-d");
1567 if (d->phase == SP_CONFIG_UPDATE)
1569 GNUNET_SCHEDULER_cancel (d->task);
1570 d->phase = SP_START_DONE;
1572 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1573 if (d->server != NULL)
1575 GNUNET_CORE_disconnect (d->server);
1579 /* shutdown ARM process (will terminate others) */
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1582 GNUNET_i2s (&d->id));
1584 d->phase = SP_SHUTDOWN_START;
1585 d->running = GNUNET_NO;
1586 if (allow_restart == GNUNET_YES)
1587 d->churn = GNUNET_YES;
1590 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1592 GNUNET_TRANSPORT_disconnect (d->th);
1595 /* Check if this is a local or remote process */
1596 if (NULL != d->hostname)
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1600 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1601 d->cfgfile, d->hostname);
1604 if (d->username != NULL)
1605 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1607 arg = GNUNET_strdup (d->hostname);
1609 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1617 "-c", d->cfgfile, "-e", "-q", "-T",
1618 GNUNET_TIME_relative_to_string (timeout),
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1621 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1622 arg, "gnunet-arm", d->cfgfile, del_arg);
1623 /* Use -e to end arm, and -d to remove temp files */
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1630 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1632 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1636 "-c", d->cfgfile, "-e", "-q", "-T",
1637 GNUNET_TIME_relative_to_string (timeout),
1641 GNUNET_free_non_null (del_arg);
1642 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1643 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1648 * Changes the configuration of a GNUnet daemon.
1650 * @param d the daemon that should be modified
1651 * @param cfg the new configuration for the daemon
1652 * @param cb function called once the configuration was changed
1653 * @param cb_cls closure for cb
1656 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1657 struct GNUNET_CONFIGURATION_Handle *cfg,
1658 GNUNET_TESTING_NotifyCompletion cb,
1663 if (d->phase != SP_START_DONE)
1668 ("Peer not yet running, can not change configuration at this point."));
1672 /* 1) write configuration to temporary file */
1673 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1676 cb (cb_cls, _("Failed to write new configuration to disk."));
1680 /* 2) copy file to remote host (if necessary) */
1681 if (NULL == d->hostname)
1683 /* signal success */
1689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1690 "Copying updated configuration file to remote host `%s'.\n",
1693 d->phase = SP_CONFIG_UPDATE;
1694 if (NULL != d->username)
1695 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1697 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1698 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1702 d->cfgfile, arg, NULL);
1704 if (NULL == d->proc)
1706 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1707 _("Could not start `%s' process to copy configuration file.\n"),
1710 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1711 d->phase = SP_START_DONE;
1715 d->update_cb_cls = cb_cls;
1717 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1722 * Data kept for each pair of peers that we try
1725 struct GNUNET_TESTING_ConnectContext
1728 * Testing handle to the first daemon.
1730 struct GNUNET_TESTING_Daemon *d1;
1733 * Handle to core of first daemon (to check connect)
1735 struct GNUNET_CORE_Handle *d1core;
1738 * Have we actually connected to the core of the first daemon yet?
1743 * Testing handle to the second daemon.
1745 struct GNUNET_TESTING_Daemon *d2;
1748 * Handler for the request to core to connect to this peer.
1750 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1753 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1755 struct GNUNET_TRANSPORT_Handle *d1th;
1758 * Function to call once we are done (or have timed out).
1760 GNUNET_TESTING_NotifyConnection cb;
1768 * The relative timeout from whence this connect attempt was
1769 * started. Allows for reconnect attempts.
1771 struct GNUNET_TIME_Relative relative_timeout;
1774 * Maximum number of connect attempts, will retry connection
1775 * this number of times on failures.
1777 unsigned int connect_attempts;
1780 * Hello timeout task
1782 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1785 * Connect timeout task
1787 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1790 * When should this operation be complete (or we must trigger
1793 struct GNUNET_TIME_Relative timeout_hello;
1796 * Was the connection attempt successful?
1801 * When connecting, do we need to send the HELLO?
1806 * The distance between the two connected peers
1812 /** Forward declaration **/
1814 reattempt_daemons_connect (void *cls,
1815 const struct GNUNET_SCHEDULER_TaskContext *tc);
1819 * Notify callback about success or failure of the attempt
1820 * to connect the two peers
1822 * @param cls our "struct GNUNET_TESTING_ConnectContext" (freed)
1823 * @param tc reason tells us if we succeeded or failed
1826 notify_connect_result (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1828 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1830 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1831 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1833 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1834 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1837 if (ctx->connect_request_handle != NULL)
1839 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1840 ctx->connect_request_handle = NULL;
1843 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1845 if (ctx->d1th != NULL)
1846 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1848 if (ctx->d1core != NULL)
1849 GNUNET_CORE_disconnect (ctx->d1core);
1855 if (ctx->d1th != NULL)
1856 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1858 if (ctx->d1core != NULL)
1859 GNUNET_CORE_disconnect (ctx->d1core);
1862 if (ctx->connected == GNUNET_YES)
1864 if (ctx->cb != NULL)
1866 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance,
1867 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1870 else if (ctx->connect_attempts > 0)
1872 ctx->d1core_ready = GNUNET_NO;
1873 ctx->timeout_task = GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1878 if (ctx->cb != NULL)
1880 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1881 ctx->d2->cfg, ctx->d1, ctx->d2, _("Peers failed to connect"));
1889 * Success, connection is up. Signal client our success.
1891 * @param cls our "struct GNUNET_TESTING_ConnectContext"
1892 * @param peer identity of the peer that has connected
1893 * @param atsi performance information
1897 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
1898 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1900 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1903 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Connected peer %s to peer %s\n",
1904 ctx->d1->shortname, GNUNET_i2s (peer));
1907 if (0 != memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1909 ctx->connected = GNUNET_YES;
1910 ctx->distance = 0; /* FIXME: distance */
1911 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1913 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1914 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1916 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1917 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
1921 * Task called once a core connect request has been transmitted.
1923 * @param cls struct GNUNET_TESTING_ConnectContext
1924 * @param success was the request successful?
1927 core_connect_request_cont (void *cls, int success)
1929 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1931 ctx->connect_request_handle = NULL;
1935 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1937 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1938 struct GNUNET_MessageHeader *hello;
1940 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1941 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1943 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL) &&
1944 (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)) &&
1945 (ctx->d1->phase == SP_START_DONE) && (ctx->d2->phase == SP_START_DONE))
1947 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1948 GNUNET_assert (hello != NULL);
1950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n",
1951 ctx->d2->shortname, ctx->d1->shortname);
1953 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1954 GNUNET_assert (ctx->d1core != NULL);
1955 ctx->connect_request_handle =
1956 GNUNET_CORE_peer_request_connect (ctx->d1core, &ctx->d2->id,
1957 &core_connect_request_cont, ctx);
1960 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1961 "Sending connect request to CORE of %s for peer %s\n",
1962 GNUNET_i2s (&ctx->d1->id),
1963 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1965 ctx->timeout_hello =
1966 GNUNET_TIME_relative_add (ctx->timeout_hello,
1967 GNUNET_TIME_relative_multiply
1968 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1970 ctx->hello_send_task =
1971 GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello, &send_hello, ctx);
1975 * Notify of a successful connection to the core service.
1977 * @param cls a ConnectContext
1978 * @param server handle to the core service
1979 * @param my_identity the peer identity of this peer
1980 * @param publicKey the public key of the peer
1983 core_init_notify (void *cls, struct GNUNET_CORE_Handle *server,
1984 const struct GNUNET_PeerIdentity *my_identity,
1985 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
1988 struct GNUNET_TESTING_ConnectContext *connect_ctx = cls;
1990 connect_ctx->d1core_ready = GNUNET_YES;
1992 if (connect_ctx->send_hello == GNUNET_NO)
1994 connect_ctx->connect_request_handle =
1995 GNUNET_CORE_peer_request_connect (connect_ctx->d1core,
1996 &connect_ctx->d2->id,
1997 &core_connect_request_cont,
1999 GNUNET_assert (connect_ctx->connect_request_handle != NULL);
2001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2002 "Sending connect request to CORE of %s for peer %s\n",
2003 connect_ctx->d1->shortname, connect_ctx->d2->shortname);
2011 reattempt_daemons_connect (void *cls,
2012 const struct GNUNET_SCHEDULER_TaskContext *tc)
2014 struct GNUNET_TESTING_ConnectContext *ctx = cls;
2016 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2017 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2019 #if DEBUG_TESTING_RECONNECT
2020 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2021 "re-attempting connect of peer %s to peer %s\n",
2022 ctx->d1->shortname, ctx->d2->shortname);
2024 ctx->connect_attempts--;
2025 GNUNET_assert (ctx->d1core == NULL);
2026 ctx->d1core_ready = GNUNET_NO;
2028 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2029 &connect_notify, NULL, NULL, NULL, GNUNET_NO, NULL,
2030 GNUNET_NO, no_handlers);
2031 if (ctx->d1core == NULL)
2033 if (NULL != ctx->cb)
2034 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2035 ctx->d2->cfg, ctx->d1, ctx->d2,
2036 _("Failed to connect to core service of first peer!\n"));
2041 /* Don't know reason for initial connect failure, update the HELLO for the second peer */
2042 if (NULL != ctx->d2->hello)
2044 GNUNET_free (ctx->d2->hello);
2045 ctx->d2->hello = NULL;
2046 if (NULL != ctx->d2->th)
2048 GNUNET_TRANSPORT_get_hello_cancel (ctx->d2->ghh);
2049 ctx->d2->ghh = NULL;
2050 GNUNET_TRANSPORT_disconnect (ctx->d2->th);
2053 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2055 GNUNET_assert (ctx->d2->th != NULL);
2057 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2060 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
2063 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2065 if (ctx->d2->th == NULL)
2067 GNUNET_CORE_disconnect (ctx->d1core);
2069 if (NULL != ctx->cb)
2070 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2071 ctx->d2->cfg, ctx->d1, ctx->d2,
2072 _("Failed to connect to transport service!\n"));
2076 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2079 if (ctx->send_hello == GNUNET_YES)
2082 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2084 if (ctx->d1th == NULL)
2086 GNUNET_CORE_disconnect (ctx->d1core);
2088 if (NULL != ctx->cb)
2089 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2090 ctx->d2->cfg, ctx->d1, ctx->d2,
2091 _("Failed to connect to transport service!\n"));
2094 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2095 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2099 ctx->connect_request_handle =
2100 GNUNET_CORE_peer_request_connect (ctx->d1core, &ctx->d2->id,
2101 &core_connect_request_cont, ctx);
2104 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2105 ¬ify_connect_result, ctx);
2109 * Iterator for currently known peers, to ensure
2110 * that we don't try to send duplicate connect
2113 * @param cls our "struct GNUNET_TESTING_ConnectContext"
2114 * @param peer identity of the peer that has connected,
2115 * NULL when iteration has finished
2116 * @param atsi performance information
2120 core_initial_iteration (void *cls, const struct GNUNET_PeerIdentity *peer,
2121 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2123 struct GNUNET_TESTING_ConnectContext *ctx = cls;
2125 if ((peer != NULL) &&
2126 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
2128 ctx->connected = GNUNET_YES;
2129 ctx->distance = 0; /* FIXME: distance */
2133 return; /* ignore other peers */
2134 /* peer == NULL: End of iteration over peers */
2136 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->timeout_task);
2137 if (ctx->connected == GNUNET_YES)
2140 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2144 /* Peer not already connected, need to schedule connect request! */
2145 if (ctx->d1core == NULL)
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 "Peers are NOT connected, connecting to core!\n");
2152 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2153 &connect_notify, NULL, NULL, NULL, GNUNET_NO,
2154 NULL, GNUNET_NO, no_handlers);
2157 if (ctx->d1core == NULL)
2160 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,
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
2277 if (GNUNET_SCHEDULER_NO_TASK != cc->timeout_task)
2279 GNUNET_SCHEDULER_cancel (cc->timeout_task);
2280 cc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2282 if (GNUNET_SCHEDULER_NO_TASK != cc->hello_send_task)
2284 GNUNET_SCHEDULER_cancel (cc->hello_send_task);
2285 cc->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2287 if (NULL != cc->d1core)
2289 GNUNET_CORE_disconnect (cc->d1core);
2292 if (NULL != cc->d1th)
2294 GNUNET_TRANSPORT_disconnect (cc->d1th);
2301 /* end of testing.c */