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 2, 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
41 #define DEBUG_TESTING_RECONNECT GNUNET_NO
44 * How long do we wait after starting gnunet-service-arm
45 * for the core service to be alive?
47 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
50 * How many times are we willing to try to wait for "scp" or
51 * "gnunet-service-arm" to complete (waitpid) before giving up?
53 #define MAX_EXEC_WAIT_RUNS 250
55 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
58 * Receive the HELLO from one peer, give it to the other
59 * and ask them to connect.
61 * @param cls "struct ConnectContext"
62 * @param message HELLO message of peer
65 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
67 struct GNUNET_TESTING_Daemon *daemon = cls;
71 if (daemon->server != NULL)
73 GNUNET_CORE_disconnect(daemon->server);
74 daemon->server = NULL;
77 GNUNET_assert (message != NULL);
78 if (daemon->th != NULL)
80 GNUNET_TRANSPORT_get_hello_cancel(daemon->th, &process_hello, daemon);
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84 "Received `%s' from transport service of `%4s'\n",
85 "HELLO", GNUNET_i2s (&daemon->id));
88 GNUNET_free_non_null(daemon->hello);
89 daemon->hello = GNUNET_malloc(ntohs(message->size));
90 memcpy(daemon->hello, message, ntohs(message->size));
92 if (daemon->th != NULL)
94 GNUNET_TRANSPORT_disconnect(daemon->th);
101 * Function called after GNUNET_CORE_connect has succeeded
102 * (or failed for good). Note that the private key of the
103 * peer is intentionally not exposed here; if you need it,
104 * your process should try to read the private key file
105 * directly (which should work if you are authorized...).
108 * @param server handle to the server, NULL if we failed
109 * @param my_identity ID of this peer, NULL if we failed
110 * @param publicKey public key of this peer, NULL if we failed
113 testing_init (void *cls,
114 struct GNUNET_CORE_Handle *server,
115 const struct GNUNET_PeerIdentity *my_identity,
116 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
118 struct GNUNET_TESTING_Daemon *d = cls;
119 GNUNET_TESTING_NotifyDaemonRunning cb;
121 GNUNET_assert (d->phase == SP_START_CORE);
122 d->phase = SP_START_DONE;
128 if (GNUNET_YES == d->dead)
129 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
131 cb (d->cb_cls, NULL, d->cfg, d,
132 _("Failed to connect to core service\n"));
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
139 d->id = *my_identity;
140 d->shortname = strdup (GNUNET_i2s (my_identity));
142 d->running = GNUNET_YES;
143 if (GNUNET_YES == d->dead)
144 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
146 cb (d->cb_cls, my_identity, d->cfg, d, NULL);
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
153 d->th = GNUNET_TRANSPORT_connect (d->sched,
154 d->cfg, d, NULL, NULL, NULL);
157 if (GNUNET_YES == d->dead)
158 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
159 else if (NULL != d->cb)
160 d->cb (d->cb_cls, &d->id, d->cfg, d,
161 _("Failed to connect to transport service!\n"));
165 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
170 * Finite-state machine for starting GNUnet.
172 * @param cls our "struct GNUNET_TESTING_Daemon"
176 start_fsm (void *cls,
177 const struct GNUNET_SCHEDULER_TaskContext *tc)
179 struct GNUNET_TESTING_Daemon *d = cls;
180 GNUNET_TESTING_NotifyDaemonRunning cb;
181 enum GNUNET_OS_ProcessStatusType type;
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 "Peer FSM is in phase %u.\n", d->phase);
191 d->task = GNUNET_SCHEDULER_NO_TASK;
195 /* confirm copying complete */
196 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
198 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
205 d->cfg, d, _("`scp' does not seem to terminate (timeout copying config).\n"));
210 = GNUNET_SCHEDULER_add_delayed (d->sched,
211 GNUNET_CONSTANTS_EXEC_WAIT,
215 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
221 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Successfully copied configuration file.\n");
228 d->phase = SP_COPIED;
231 /* Start create hostkey process */
232 d->pipe_stdout = GNUNET_DISK_pipe(GNUNET_NO);
233 if (d->pipe_stdout == NULL)
242 (NULL == d->hostname)
243 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
244 : _("Failed to create pipe for `ssh' process.\n"));
247 if (NULL == d->hostname)
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Starting `%s', with command `%s %s %s %s'.\n",
252 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
255 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
259 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
263 if (d->username != NULL)
264 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
266 dst = GNUNET_strdup (d->hostname);
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
271 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c", d->cfgfile,
274 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
278 "-c", d->cfgfile, "-sq", NULL);
279 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
285 _("Could not start `%s' process to create hostkey.\n"),
286 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
294 (NULL == d->hostname)
295 ? _("Failed to start `gnunet-peerinfo' process.\n")
296 : _("Failed to start `ssh' process.\n"));
297 GNUNET_DISK_pipe_close(d->pipe_stdout);
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Started `%s', waiting for hostkey.\n",
305 d->phase = SP_HOSTKEY_CREATE;
307 = GNUNET_SCHEDULER_add_read_file (d->sched,
308 GNUNET_TIME_absolute_get_remaining(d->max_timeout),
309 GNUNET_DISK_pipe_handle(d->pipe_stdout,
310 GNUNET_DISK_PIPE_END_READ),
314 case SP_HOSTKEY_CREATE:
315 bytes_read = GNUNET_DISK_file_read(GNUNET_DISK_pipe_handle(d->pipe_stdout,
316 GNUNET_DISK_PIPE_END_READ),
317 &d->hostkeybuf[d->hostkeybufpos],
318 sizeof(d->hostkeybuf) - d->hostkeybufpos);
320 d->hostkeybufpos += bytes_read;
322 if ( (d->hostkeybufpos < 104) &&
327 = GNUNET_SCHEDULER_add_read_file (d->sched,
328 GNUNET_TIME_absolute_get_remaining(d->max_timeout),
329 GNUNET_DISK_pipe_handle(d->pipe_stdout,
330 GNUNET_DISK_PIPE_END_READ),
335 d->hostkeybuf[103] = '\0';
336 if ( (bytes_read < 0) ||
337 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
338 &d->id.hashPubKey)) )
342 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
343 _("Error reading from gnunet-peerinfo: %s\n"),
346 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
347 _("Malformed output from gnunet-peerinfo!\n"));
350 GNUNET_DISK_pipe_close(d->pipe_stdout);
351 d->pipe_stdout = NULL;
352 (void) PLIBC_KILL (d->pid, SIGKILL);
353 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
360 _("`Failed to get hostkey!\n"));
363 GNUNET_DISK_pipe_close(d->pipe_stdout);
364 d->pipe_stdout = NULL;
365 (void) PLIBC_KILL (d->pid, SIGKILL);
366 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 "Successfully got hostkey!\n");
372 if (d->hostkey_callback != NULL)
374 d->hostkey_callback(d->hostkey_cls, &d->id, d, NULL);
375 d->phase = SP_HOSTKEY_CREATED;
379 d->phase = SP_TOPOLOGY_SETUP;
382 case SP_HOSTKEY_CREATED:
383 /* wait for topology finished */
384 if ((GNUNET_YES == d->dead) || (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0))
393 _("`Failed while waiting for topology setup!\n"));
398 = GNUNET_SCHEDULER_add_delayed (d->sched,
399 GNUNET_CONSTANTS_EXEC_WAIT,
402 case SP_TOPOLOGY_SETUP:
403 /* start GNUnet on remote host */
404 if (NULL == d->hostname)
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
409 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
413 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
423 if (d->username != NULL)
424 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
426 dst = GNUNET_strdup (d->hostname);
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
431 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
432 "-L", "DEBUG", "-s", "-q");
434 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
441 "-c", d->cfgfile, "-s", "-q", NULL);
446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
447 _("Could not start `%s' process to start GNUnet.\n"),
448 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
456 (NULL == d->hostname)
457 ? _("Failed to start `gnunet-arm' process.\n")
458 : _("Failed to start `ssh' process.\n"));
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "Started `%s', waiting for `%s' to be up.\n",
464 "gnunet-arm", "gnunet-service-core");
466 d->phase = SP_START_ARMING;
468 = GNUNET_SCHEDULER_add_delayed (d->sched,
469 GNUNET_CONSTANTS_EXEC_WAIT,
472 case SP_START_ARMING:
473 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
475 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
484 (NULL == d->hostname)
485 ? _("`gnunet-arm' does not seem to terminate.\n")
486 : _("`ssh' does not seem to terminate.\n"));
491 = GNUNET_SCHEDULER_add_delayed (d->sched,
492 GNUNET_CONSTANTS_EXEC_WAIT,
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Successfully started `%s'.\n", "gnunet-arm");
500 d->phase = SP_START_CORE;
501 d->server = GNUNET_CORE_connect (d->sched,
508 NULL, GNUNET_NO, no_handlers);
516 case SP_SHUTDOWN_START:
517 /* confirm copying complete */
518 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
520 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
522 d->dead_cb (d->dead_cb_cls,
523 _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
526 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
527 GNUNET_TRANSPORT_disconnect(d->th);
530 GNUNET_CONFIGURATION_destroy (d->cfg);
531 GNUNET_free (d->cfgfile);
532 GNUNET_free_non_null(d->hello);
533 GNUNET_free_non_null (d->hostname);
534 GNUNET_free_non_null (d->username);
535 GNUNET_free_non_null (d->shortname);
541 = GNUNET_SCHEDULER_add_delayed (d->sched,
542 GNUNET_CONSTANTS_EXEC_WAIT,
546 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
548 if (NULL != d->dead_cb)
549 d->dead_cb (d->dead_cb_cls,
550 _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
553 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
554 GNUNET_TRANSPORT_disconnect(d->th);
557 GNUNET_CONFIGURATION_destroy (d->cfg);
558 GNUNET_free (d->cfgfile);
559 GNUNET_free_non_null(d->hello);
560 GNUNET_free_non_null (d->hostname);
561 GNUNET_free_non_null (d->username);
562 GNUNET_free_non_null (d->shortname);
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
571 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
572 GNUNET_TRANSPORT_disconnect(d->th);
575 /* state clean up and notifications */
576 if (d->churn == GNUNET_NO)
578 GNUNET_CONFIGURATION_destroy (d->cfg);
579 GNUNET_free (d->cfgfile);
580 GNUNET_free_non_null (d->hostname);
581 GNUNET_free_non_null (d->username);
584 GNUNET_free_non_null(d->hello);
586 GNUNET_free_non_null (d->shortname);
588 if (NULL != d->dead_cb)
589 d->dead_cb (d->dead_cb_cls, NULL);
591 if (d->churn == GNUNET_NO)
595 case SP_CONFIG_UPDATE:
596 /* confirm copying complete */
597 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
599 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0) /* FIXME: config update should take timeout parameter! */
606 d->cfg, d, _("`scp' does not seem to terminate.\n"));
611 = GNUNET_SCHEDULER_add_delayed (d->sched,
612 GNUNET_CONSTANTS_EXEC_WAIT,
616 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
618 if (NULL != d->update_cb)
619 d->update_cb (d->update_cb_cls,
620 _("`scp' did not complete cleanly.\n"));
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "Successfully copied configuration file.\n");
627 if (NULL != d->update_cb)
628 d->update_cb (d->update_cb_cls, NULL);
629 d->phase = SP_START_DONE;
635 * Continues GNUnet daemon startup when user wanted to be notified
636 * once a hostkey was generated (for creating friends files, blacklists,
639 * @param daemon the daemon to finish starting
642 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon)
644 GNUNET_assert(daemon->phase == SP_HOSTKEY_CREATED);
645 daemon->phase = SP_TOPOLOGY_SETUP;
650 * Start a peer that has previously been stopped using the daemon_stop
651 * call (and files weren't deleted and the allow restart flag)
653 * @param daemon the daemon to start (has been previously stopped)
654 * @param timeout how long to wait for restart
655 * @param cb the callback for notification when the peer is running
656 * @param cb_cls closure for the callback
659 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
660 struct GNUNET_TIME_Relative timeout,
661 GNUNET_TESTING_NotifyDaemonRunning cb,
664 if (daemon->running == GNUNET_YES)
666 cb(cb_cls, &daemon->id, daemon->cfg, daemon, "Daemon already running, can't restart!");
671 daemon->cb_cls = cb_cls;
672 daemon->phase = SP_TOPOLOGY_SETUP;
673 daemon->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
675 GNUNET_SCHEDULER_add_continuation (daemon->sched,
678 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
682 * Starts a GNUnet daemon. GNUnet must be installed on the target
683 * system and available in the PATH. The machine must furthermore be
684 * reachable via "ssh" (unless the hostname is "NULL") without the
685 * need to enter a password.
687 * @param sched scheduler to use
688 * @param cfg configuration to use
689 * @param timeout how long to wait starting up peers
690 * @param hostname name of the machine where to run GNUnet
691 * (use NULL for localhost).
692 * @param hostkey_callback function to call once the hostkey has been
693 * generated for this peer, but it hasn't yet been started
694 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
695 * @param hostkey_cls closure for hostkey callback
696 * @param cb function to call with the result
697 * @param cb_cls closure for cb
698 * @return handle to the daemon (actual start will be completed asynchronously)
700 struct GNUNET_TESTING_Daemon *
701 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
702 const struct GNUNET_CONFIGURATION_Handle *cfg,
703 struct GNUNET_TIME_Relative timeout,
704 const char *hostname,
705 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
707 GNUNET_TESTING_NotifyDaemonRunning cb,
710 struct GNUNET_TESTING_Daemon *ret;
714 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
716 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
717 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "Setting up peer with configuration file `%s'.\n",
723 if (NULL == ret->cfgfile)
725 GNUNET_free_non_null (ret->hostname);
729 ret->hostkey_callback = hostkey_callback;
730 ret->hostkey_cls = hostkey_cls;
732 ret->cb_cls = cb_cls;
733 ret->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
734 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
735 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
737 "DEFAULTCONFIG", ret->cfgfile);
738 /* 1) write configuration to temporary file */
739 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
741 if (0 != UNLINK (ret->cfgfile))
742 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
743 "unlink", ret->cfgfile);
744 GNUNET_CONFIGURATION_destroy (ret->cfg);
745 GNUNET_free_non_null (ret->hostname);
746 GNUNET_free (ret->cfgfile);
751 GNUNET_CONFIGURATION_get_value_string (cfg,
753 "USERNAME", &username))
755 if (NULL != getenv ("USER"))
756 username = GNUNET_strdup (getenv ("USER"));
760 ret->username = username;
762 /* 2) copy file to remote host */
763 if (NULL != hostname)
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "Copying configuration file to host `%s'.\n", hostname);
769 ret->phase = SP_COPYING;
770 if (NULL != username)
771 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
773 GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
774 ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
775 "scp", ret->cfgfile, arg, NULL);
779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
781 ("Could not start `%s' process to copy configuration file.\n"),
783 if (0 != UNLINK (ret->cfgfile))
784 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
785 "unlink", ret->cfgfile);
786 GNUNET_CONFIGURATION_destroy (ret->cfg);
787 GNUNET_free_non_null (ret->hostname);
788 GNUNET_free_non_null (ret->username);
789 GNUNET_free (ret->cfgfile);
794 = GNUNET_SCHEDULER_add_delayed (sched,
795 GNUNET_CONSTANTS_EXEC_WAIT,
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "No need to copy configuration file since we are running locally.\n");
803 ret->phase = SP_COPIED;
804 GNUNET_SCHEDULER_add_continuation (sched,
807 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
813 * Restart (stop and start) a GNUnet daemon.
815 * @param d the daemon that should be restarted
816 * @param cb function called once the daemon is (re)started
817 * @param cb_cls closure for cb
820 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
821 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls)
829 d->dead = GNUNET_YES;
836 if (d->phase == SP_CONFIG_UPDATE)
838 GNUNET_SCHEDULER_cancel (d->sched, d->task);
839 d->phase = SP_START_DONE;
841 if (d->server != NULL)
843 GNUNET_CORE_disconnect (d->server);
849 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
850 GNUNET_TRANSPORT_disconnect(d->th);
853 /* state clean up and notifications */
854 GNUNET_free_non_null(d->hello);
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
858 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
861 d->phase = SP_START_ARMING;
863 /* Check if this is a local or remote process */
864 if (NULL != d->hostname)
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
871 if (d->username != NULL)
872 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
874 arg = GNUNET_strdup (d->hostname);
876 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
881 "-c", d->cfgfile, "-e", "-r", NULL);
882 /* Use -r to restart arm and all services */
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
890 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
892 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
897 "-c", d->cfgfile, "-e", "-r", NULL);
900 GNUNET_free_non_null(del_arg);
902 = GNUNET_SCHEDULER_add_delayed (d->sched,
903 GNUNET_CONSTANTS_EXEC_WAIT,
910 * Stops a GNUnet daemon.
912 * @param d the daemon that should be stopped
913 * @param timeout how long to wait for process for shutdown to complete
914 * @param cb function called once the daemon was stopped
915 * @param cb_cls closure for cb
916 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
918 * @param allow_restart GNUNET_YES to restart peer later (using this API)
919 * GNUNET_NO to kill off and clean up for good
922 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
923 struct GNUNET_TIME_Relative timeout,
924 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
931 d->dead_cb_cls = cb_cls;
936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
937 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
939 d->dead = GNUNET_YES;
944 if (delete_files == GNUNET_YES)
946 GNUNET_asprintf(&del_arg, "-d");
949 if (d->phase == SP_CONFIG_UPDATE)
951 GNUNET_SCHEDULER_cancel (d->sched, d->task);
952 d->phase = SP_START_DONE;
954 if (d->server != NULL)
956 GNUNET_CORE_disconnect (d->server);
959 /* shutdown ARM process (will terminate others) */
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
964 d->phase = SP_SHUTDOWN_START;
965 d->running = GNUNET_NO;
966 if (allow_restart == GNUNET_YES)
967 d->churn = GNUNET_YES;
970 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
971 GNUNET_TRANSPORT_disconnect(d->th);
974 /* Check if this is a local or remote process */
975 if (NULL != d->hostname)
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
982 if (d->username != NULL)
983 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
985 arg = GNUNET_strdup (d->hostname);
987 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
992 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
993 /* Use -e to end arm, and -d to remove temp files */
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1000 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1002 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1007 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1010 GNUNET_free_non_null(del_arg);
1011 d->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
1013 = GNUNET_SCHEDULER_add_now (d->sched,
1019 * Changes the configuration of a GNUnet daemon.
1021 * @param d the daemon that should be modified
1022 * @param cfg the new configuration for the daemon
1023 * @param cb function called once the configuration was changed
1024 * @param cb_cls closure for cb
1027 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1028 struct GNUNET_CONFIGURATION_Handle *cfg,
1029 GNUNET_TESTING_NotifyCompletion cb,
1034 if (d->phase != SP_START_DONE)
1039 ("Peer not yet running, can not change configuration at this point."));
1043 /* 1) write configuration to temporary file */
1044 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1047 cb (cb_cls, _("Failed to write new configuration to disk."));
1051 /* 2) copy file to remote host (if necessary) */
1052 if (NULL == d->hostname)
1054 /* signal success */
1060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1061 "Copying updated configuration file to remote host `%s'.\n",
1064 d->phase = SP_CONFIG_UPDATE;
1065 if (NULL != d->username)
1066 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1068 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1069 d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", d->cfgfile, arg, NULL);
1073 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1075 ("Could not start `%s' process to copy configuration file.\n"),
1078 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1079 d->phase = SP_START_DONE;
1083 d->update_cb_cls = cb_cls;
1085 = GNUNET_SCHEDULER_add_delayed (d->sched,
1086 GNUNET_CONSTANTS_EXEC_WAIT,
1092 * Data kept for each pair of peers that we try
1095 struct ConnectContext
1098 * Testing handle to the first daemon.
1100 struct GNUNET_TESTING_Daemon *d1;
1103 * Handle to core of first daemon (to check connect)
1105 struct GNUNET_CORE_Handle * d1core;
1108 * Testing handle to the second daemon.
1110 struct GNUNET_TESTING_Daemon *d2;
1113 * Transport handle to the second daemon.
1115 struct GNUNET_TRANSPORT_Handle *d2th;
1118 * Function to call once we are done (or have timed out).
1120 GNUNET_TESTING_NotifyConnection cb;
1128 * When should this operation be complete (or we must trigger
1131 struct GNUNET_TIME_Absolute timeout;
1134 * The relative timeout from whence this connect attempt was
1135 * started. Allows for reconnect attempts.
1137 struct GNUNET_TIME_Relative relative_timeout;
1140 * Maximum number of connect attempts, will retry connection
1141 * this number of times on failures.
1143 unsigned int max_connect_attempts;
1146 * Hello timeout task
1148 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1151 * Connect timeout task
1153 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1156 * When should this operation be complete (or we must trigger
1159 struct GNUNET_TIME_Relative timeout_hello;
1163 * Was the connection attempt successful?
1169 /** Forward declaration **/
1171 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1175 * Notify callback about success or failure of the attempt
1176 * to connect the two peers
1178 * @param cls our "struct ConnectContext" (freed)
1179 * @param tc reason tells us if we succeeded or failed
1182 notify_connect_result (void *cls,
1183 const struct GNUNET_SCHEDULER_TaskContext *tc)
1185 struct ConnectContext *ctx = cls;
1186 struct GNUNET_TIME_Relative remaining;
1188 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1189 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1191 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
1192 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1194 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1196 if (ctx->d2th != NULL)
1197 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1199 if (ctx->d1core != NULL)
1200 GNUNET_CORE_disconnect (ctx->d1core);
1207 remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
1209 if (ctx->connected == GNUNET_YES)
1211 if (ctx->cb != NULL)
1213 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
1214 ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1217 else if (remaining.value > 0)
1219 if (ctx->d1core != NULL)
1221 GNUNET_CORE_disconnect(ctx->d1core);
1225 if (ctx->d2th != NULL)
1227 GNUNET_TRANSPORT_disconnect(ctx->d2th);
1230 GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
1235 if (ctx->cb != NULL)
1237 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
1238 ctx->d2->cfg, ctx->d1, ctx->d2,
1239 _("Peers failed to connect"));
1243 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1245 GNUNET_CORE_disconnect (ctx->d1core);
1252 * Success, connection is up. Signal client our success.
1254 * @param cls our "struct ConnectContext"
1255 * @param peer identity of the peer that has connected
1256 * @param latency the round trip latency of the connection to this peer
1257 * @param distance distance the transport level distance to this peer
1261 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1264 struct ConnectContext *ctx = cls;
1266 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1268 ctx->connected = GNUNET_YES;
1269 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1270 ctx->timeout_task = GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1271 ¬ify_connect_result,
1278 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1280 struct ConnectContext *ctx = cls;
1282 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1283 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1285 if (ctx->d1->hello != NULL)
1287 GNUNET_TRANSPORT_offer_hello (ctx->d2th, GNUNET_HELLO_get_header(ctx->d1->hello));
1288 ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
1289 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1292 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
1298 * Establish a connection between two GNUnet daemons.
1300 * @param d1 handle for the first daemon
1301 * @param d2 handle for the second daemon
1302 * @param timeout how long is the connection attempt
1304 * @param max_connect_attempts how many times should we try to reconnect
1306 * @param cb function to call at the end
1307 * @param cb_cls closure for cb
1310 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1311 struct GNUNET_TESTING_Daemon *d2,
1312 struct GNUNET_TIME_Relative timeout,
1313 unsigned int max_connect_attempts,
1314 GNUNET_TESTING_NotifyConnection cb,
1317 struct ConnectContext *ctx;
1319 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1322 cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1323 _("Peers are not fully running yet, can not connect!\n"));
1326 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1329 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1330 ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
1331 ctx->relative_timeout = timeout;
1333 ctx->cb_cls = cb_cls;
1334 ctx->max_connect_attempts = max_connect_attempts;
1335 ctx->connected = GNUNET_NO;
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "Asked to connect peer %s to peer %s\n",
1339 d1->shortname, d2->shortname);
1342 ctx->d1core = GNUNET_CORE_connect (d1->sched,
1347 &connect_notify, NULL,
1349 NULL, GNUNET_NO, no_handlers);
1350 if (ctx->d1core == NULL)
1354 cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1355 _("Failed to connect to core service of first peer!\n"));
1359 #if DEBUG_TESTING > 2
1360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1361 "Asked to connect peer %s to peer %s\n",
1362 d1->shortname, d2->shortname);
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "Connecting to transport service of peer %s\n", d2->shortname);
1368 ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
1369 d2->cfg, d2, NULL, NULL, NULL);
1370 if (ctx->d2th == NULL)
1372 GNUNET_CORE_disconnect(ctx->d1core);
1375 cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1376 _("Failed to connect to transport service!\n"));
1380 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
1381 GNUNET_TIME_relative_divide(ctx->relative_timeout,
1382 max_connect_attempts),
1383 ¬ify_connect_result, ctx);
1385 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1389 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1392 struct ConnectContext *ctx = cls;
1393 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1397 #if DEBUG_TESTING_RECONNECT
1398 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "re-attempting connect of peer %s to peer %s\n",
1399 ctx->d1->shortname, ctx->d2->shortname);
1402 GNUNET_assert(ctx->d1core == NULL);
1404 ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1406 GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1409 &connect_notify, NULL,
1411 NULL, GNUNET_NO, no_handlers);
1412 if (ctx->d1core == NULL)
1414 if (NULL != ctx->cb)
1415 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1416 _("Failed to connect to core service of first peer!\n"));
1421 ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1422 ctx->d2->cfg, ctx->d2, NULL, NULL, NULL);
1423 if (ctx->d2th == NULL)
1425 GNUNET_CORE_disconnect(ctx->d1core);
1427 if (NULL != ctx->cb)
1428 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1429 _("Failed to connect to transport service!\n"));
1433 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1434 GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1435 ¬ify_connect_result, ctx);
1437 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1440 /* end of testing.c */