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;
72 if (daemon->server != NULL)
74 GNUNET_CORE_disconnect(daemon->server);
75 daemon->server = NULL;
78 GNUNET_assert (message != NULL);
79 msize = ntohs(message->size);
84 if (daemon->th != NULL)
86 GNUNET_TRANSPORT_get_hello_cancel(daemon->th, &process_hello, daemon);
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90 "Received `%s' from transport service of `%4s'\n",
91 "HELLO", GNUNET_i2s (&daemon->id));
97 GNUNET_free_non_null(daemon->hello);
98 daemon->hello = GNUNET_malloc(msize);
99 memcpy(daemon->hello, message, msize);
101 if (daemon->th != NULL)
103 GNUNET_TRANSPORT_disconnect(daemon->th);
111 * Function called after GNUNET_CORE_connect has succeeded
112 * (or failed for good). Note that the private key of the
113 * peer is intentionally not exposed here; if you need it,
114 * your process should try to read the private key file
115 * directly (which should work if you are authorized...).
118 * @param server handle to the server, NULL if we failed
119 * @param my_identity ID of this peer, NULL if we failed
120 * @param publicKey public key of this peer, NULL if we failed
123 testing_init (void *cls,
124 struct GNUNET_CORE_Handle *server,
125 const struct GNUNET_PeerIdentity *my_identity,
126 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
128 struct GNUNET_TESTING_Daemon *d = cls;
129 GNUNET_TESTING_NotifyDaemonRunning cb;
131 GNUNET_assert (d->phase == SP_START_CORE);
132 d->phase = SP_START_DONE;
138 if (GNUNET_YES == d->dead)
139 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
141 cb (d->cb_cls, NULL, d->cfg, d,
142 _("Failed to connect to core service\n"));
146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
149 d->id = *my_identity;
150 d->shortname = strdup (GNUNET_i2s (my_identity));
152 d->running = GNUNET_YES;
153 if (GNUNET_YES == d->dead)
154 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
156 cb (d->cb_cls, my_identity, d->cfg, d, NULL);
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
163 d->th = GNUNET_TRANSPORT_connect (d->sched,
164 d->cfg, d, NULL, NULL, NULL);
167 if (GNUNET_YES == d->dead)
168 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
169 else if (NULL != d->cb)
170 d->cb (d->cb_cls, &d->id, d->cfg, d,
171 _("Failed to connect to transport service!\n"));
175 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
180 * Finite-state machine for starting GNUnet.
182 * @param cls our "struct GNUNET_TESTING_Daemon"
186 start_fsm (void *cls,
187 const struct GNUNET_SCHEDULER_TaskContext *tc)
189 struct GNUNET_TESTING_Daemon *d = cls;
190 GNUNET_TESTING_NotifyDaemonRunning cb;
191 enum GNUNET_OS_ProcessStatusType type;
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Peer FSM is in phase %u.\n", d->phase);
201 d->task = GNUNET_SCHEDULER_NO_TASK;
205 /* confirm copying complete */
206 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
208 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
215 d->cfg, d, _("`scp' does not seem to terminate (timeout copying config).\n"));
220 = GNUNET_SCHEDULER_add_delayed (d->sched,
221 GNUNET_CONSTANTS_EXEC_WAIT,
225 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
231 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Successfully copied configuration file.\n");
238 d->phase = SP_COPIED;
241 /* Start create hostkey process */
242 d->pipe_stdout = GNUNET_DISK_pipe(GNUNET_NO);
243 if (d->pipe_stdout == NULL)
252 (NULL == d->hostname)
253 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
254 : _("Failed to create pipe for `ssh' process.\n"));
257 if (NULL == d->hostname)
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Starting `%s', with command `%s %s %s %s'.\n",
262 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
265 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
269 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
273 if (d->username != NULL)
274 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
276 dst = GNUNET_strdup (d->hostname);
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
281 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c", d->cfgfile,
284 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
288 "-c", d->cfgfile, "-sq", NULL);
289 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
295 _("Could not start `%s' process to create hostkey.\n"),
296 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
304 (NULL == d->hostname)
305 ? _("Failed to start `gnunet-peerinfo' process.\n")
306 : _("Failed to start `ssh' process.\n"));
307 GNUNET_DISK_pipe_close(d->pipe_stdout);
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "Started `%s', waiting for hostkey.\n",
315 d->phase = SP_HOSTKEY_CREATE;
317 = GNUNET_SCHEDULER_add_read_file (d->sched,
318 GNUNET_TIME_absolute_get_remaining(d->max_timeout),
319 GNUNET_DISK_pipe_handle(d->pipe_stdout,
320 GNUNET_DISK_PIPE_END_READ),
324 case SP_HOSTKEY_CREATE:
325 bytes_read = GNUNET_DISK_file_read(GNUNET_DISK_pipe_handle(d->pipe_stdout,
326 GNUNET_DISK_PIPE_END_READ),
327 &d->hostkeybuf[d->hostkeybufpos],
328 sizeof(d->hostkeybuf) - d->hostkeybufpos);
330 d->hostkeybufpos += bytes_read;
332 if ( (d->hostkeybufpos < 104) &&
337 = GNUNET_SCHEDULER_add_read_file (d->sched,
338 GNUNET_TIME_absolute_get_remaining(d->max_timeout),
339 GNUNET_DISK_pipe_handle(d->pipe_stdout,
340 GNUNET_DISK_PIPE_END_READ),
345 d->hostkeybuf[103] = '\0';
346 if ( (bytes_read < 0) ||
347 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
348 &d->id.hashPubKey)) )
352 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
353 _("Error reading from gnunet-peerinfo: %s\n"),
356 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
357 _("Malformed output from gnunet-peerinfo!\n"));
360 GNUNET_DISK_pipe_close(d->pipe_stdout);
361 d->pipe_stdout = NULL;
362 (void) PLIBC_KILL (d->pid, SIGKILL);
363 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
370 _("`Failed to get hostkey!\n"));
373 GNUNET_DISK_pipe_close(d->pipe_stdout);
374 d->pipe_stdout = NULL;
375 (void) PLIBC_KILL (d->pid, SIGKILL);
376 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Successfully got hostkey!\n");
382 if (d->hostkey_callback != NULL)
384 d->hostkey_callback(d->hostkey_cls, &d->id, d, NULL);
385 d->phase = SP_HOSTKEY_CREATED;
389 d->phase = SP_TOPOLOGY_SETUP;
392 case SP_HOSTKEY_CREATED:
393 /* wait for topology finished */
394 if ((GNUNET_YES == d->dead) || (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0))
403 _("`Failed while waiting for topology setup!\n"));
408 = GNUNET_SCHEDULER_add_delayed (d->sched,
409 GNUNET_CONSTANTS_EXEC_WAIT,
412 case SP_TOPOLOGY_SETUP:
413 /* start GNUnet on remote host */
414 if (NULL == d->hostname)
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
419 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
423 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
433 if (d->username != NULL)
434 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
436 dst = GNUNET_strdup (d->hostname);
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
441 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
442 "-L", "DEBUG", "-s", "-q");
444 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
451 "-c", d->cfgfile, "-s", "-q", NULL);
456 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
457 _("Could not start `%s' process to start GNUnet.\n"),
458 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
466 (NULL == d->hostname)
467 ? _("Failed to start `gnunet-arm' process.\n")
468 : _("Failed to start `ssh' process.\n"));
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Started `%s', waiting for `%s' to be up.\n",
474 "gnunet-arm", "gnunet-service-core");
476 d->phase = SP_START_ARMING;
478 = GNUNET_SCHEDULER_add_delayed (d->sched,
479 GNUNET_CONSTANTS_EXEC_WAIT,
482 case SP_START_ARMING:
483 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
485 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
494 (NULL == d->hostname)
495 ? _("`gnunet-arm' does not seem to terminate.\n")
496 : _("`ssh' does not seem to terminate.\n"));
501 = GNUNET_SCHEDULER_add_delayed (d->sched,
502 GNUNET_CONSTANTS_EXEC_WAIT,
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "Successfully started `%s'.\n", "gnunet-arm");
510 d->phase = SP_START_CORE;
511 d->server = GNUNET_CORE_connect (d->sched,
518 NULL, GNUNET_NO, no_handlers);
526 case SP_SHUTDOWN_START:
527 /* confirm copying complete */
528 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
530 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
532 d->dead_cb (d->dead_cb_cls,
533 _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
536 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
537 GNUNET_TRANSPORT_disconnect(d->th);
540 GNUNET_CONFIGURATION_destroy (d->cfg);
541 GNUNET_free (d->cfgfile);
542 GNUNET_free_non_null(d->hello);
543 GNUNET_free_non_null (d->hostname);
544 GNUNET_free_non_null (d->username);
545 GNUNET_free_non_null (d->shortname);
551 = GNUNET_SCHEDULER_add_delayed (d->sched,
552 GNUNET_CONSTANTS_EXEC_WAIT,
556 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
558 if (NULL != d->dead_cb)
559 d->dead_cb (d->dead_cb_cls,
560 _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
563 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
564 GNUNET_TRANSPORT_disconnect(d->th);
567 GNUNET_CONFIGURATION_destroy (d->cfg);
568 GNUNET_free (d->cfgfile);
569 GNUNET_free_non_null(d->hello);
570 GNUNET_free_non_null (d->hostname);
571 GNUNET_free_non_null (d->username);
572 GNUNET_free_non_null (d->shortname);
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
581 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
582 GNUNET_TRANSPORT_disconnect(d->th);
585 /* state clean up and notifications */
586 if (d->churn == GNUNET_NO)
588 GNUNET_CONFIGURATION_destroy (d->cfg);
589 GNUNET_free (d->cfgfile);
590 GNUNET_free_non_null (d->hostname);
591 GNUNET_free_non_null (d->username);
594 GNUNET_free_non_null(d->hello);
596 GNUNET_free_non_null (d->shortname);
598 if (NULL != d->dead_cb)
599 d->dead_cb (d->dead_cb_cls, NULL);
601 if (d->churn == GNUNET_NO)
605 case SP_CONFIG_UPDATE:
606 /* confirm copying complete */
607 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
609 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0) /* FIXME: config update should take timeout parameter! */
616 d->cfg, d, _("`scp' does not seem to terminate.\n"));
621 = GNUNET_SCHEDULER_add_delayed (d->sched,
622 GNUNET_CONSTANTS_EXEC_WAIT,
626 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
628 if (NULL != d->update_cb)
629 d->update_cb (d->update_cb_cls,
630 _("`scp' did not complete cleanly.\n"));
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Successfully copied configuration file.\n");
637 if (NULL != d->update_cb)
638 d->update_cb (d->update_cb_cls, NULL);
639 d->phase = SP_START_DONE;
645 * Continues GNUnet daemon startup when user wanted to be notified
646 * once a hostkey was generated (for creating friends files, blacklists,
649 * @param daemon the daemon to finish starting
652 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon)
654 GNUNET_assert(daemon->phase == SP_HOSTKEY_CREATED);
655 daemon->phase = SP_TOPOLOGY_SETUP;
660 * Start a peer that has previously been stopped using the daemon_stop
661 * call (and files weren't deleted and the allow restart flag)
663 * @param daemon the daemon to start (has been previously stopped)
664 * @param timeout how long to wait for restart
665 * @param cb the callback for notification when the peer is running
666 * @param cb_cls closure for the callback
669 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
670 struct GNUNET_TIME_Relative timeout,
671 GNUNET_TESTING_NotifyDaemonRunning cb,
674 if (daemon->running == GNUNET_YES)
676 cb(cb_cls, &daemon->id, daemon->cfg, daemon, "Daemon already running, can't restart!");
681 daemon->cb_cls = cb_cls;
682 daemon->phase = SP_TOPOLOGY_SETUP;
683 daemon->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
685 GNUNET_SCHEDULER_add_continuation (daemon->sched,
688 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
692 * Starts a GNUnet daemon. GNUnet must be installed on the target
693 * system and available in the PATH. The machine must furthermore be
694 * reachable via "ssh" (unless the hostname is "NULL") without the
695 * need to enter a password.
697 * @param sched scheduler to use
698 * @param cfg configuration to use
699 * @param timeout how long to wait starting up peers
700 * @param hostname name of the machine where to run GNUnet
701 * (use NULL for localhost).
702 * @param hostkey_callback function to call once the hostkey has been
703 * generated for this peer, but it hasn't yet been started
704 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
705 * @param hostkey_cls closure for hostkey callback
706 * @param cb function to call with the result
707 * @param cb_cls closure for cb
708 * @return handle to the daemon (actual start will be completed asynchronously)
710 struct GNUNET_TESTING_Daemon *
711 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
712 const struct GNUNET_CONFIGURATION_Handle *cfg,
713 struct GNUNET_TIME_Relative timeout,
714 const char *hostname,
715 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
717 GNUNET_TESTING_NotifyDaemonRunning cb,
720 struct GNUNET_TESTING_Daemon *ret;
724 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
726 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
727 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "Setting up peer with configuration file `%s'.\n",
733 if (NULL == ret->cfgfile)
735 GNUNET_free_non_null (ret->hostname);
739 ret->hostkey_callback = hostkey_callback;
740 ret->hostkey_cls = hostkey_cls;
742 ret->cb_cls = cb_cls;
743 ret->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
744 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
745 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
747 "DEFAULTCONFIG", ret->cfgfile);
748 /* 1) write configuration to temporary file */
749 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
751 if (0 != UNLINK (ret->cfgfile))
752 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
753 "unlink", ret->cfgfile);
754 GNUNET_CONFIGURATION_destroy (ret->cfg);
755 GNUNET_free_non_null (ret->hostname);
756 GNUNET_free (ret->cfgfile);
761 GNUNET_CONFIGURATION_get_value_string (cfg,
763 "USERNAME", &username))
765 if (NULL != getenv ("USER"))
766 username = GNUNET_strdup (getenv ("USER"));
770 ret->username = username;
772 /* 2) copy file to remote host */
773 if (NULL != hostname)
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777 "Copying configuration file to host `%s'.\n", hostname);
779 ret->phase = SP_COPYING;
780 if (NULL != username)
781 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
783 GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
784 ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
785 "scp", ret->cfgfile, arg, NULL);
789 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
791 ("Could not start `%s' process to copy configuration file.\n"),
793 if (0 != UNLINK (ret->cfgfile))
794 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
795 "unlink", ret->cfgfile);
796 GNUNET_CONFIGURATION_destroy (ret->cfg);
797 GNUNET_free_non_null (ret->hostname);
798 GNUNET_free_non_null (ret->username);
799 GNUNET_free (ret->cfgfile);
804 = GNUNET_SCHEDULER_add_delayed (sched,
805 GNUNET_CONSTANTS_EXEC_WAIT,
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 "No need to copy configuration file since we are running locally.\n");
813 ret->phase = SP_COPIED;
814 GNUNET_SCHEDULER_add_continuation (sched,
817 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
823 * Restart (stop and start) a GNUnet daemon.
825 * @param d the daemon that should be restarted
826 * @param cb function called once the daemon is (re)started
827 * @param cb_cls closure for cb
830 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
831 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls)
839 d->dead = GNUNET_YES;
846 if (d->phase == SP_CONFIG_UPDATE)
848 GNUNET_SCHEDULER_cancel (d->sched, d->task);
849 d->phase = SP_START_DONE;
851 if (d->server != NULL)
853 GNUNET_CORE_disconnect (d->server);
859 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
860 GNUNET_TRANSPORT_disconnect(d->th);
863 /* state clean up and notifications */
864 GNUNET_free_non_null(d->hello);
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
871 d->phase = SP_START_ARMING;
873 /* Check if this is a local or remote process */
874 if (NULL != d->hostname)
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
881 if (d->username != NULL)
882 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
884 arg = GNUNET_strdup (d->hostname);
886 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
891 "-c", d->cfgfile, "-e", "-r", NULL);
892 /* Use -r to restart arm and all services */
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
902 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
907 "-c", d->cfgfile, "-e", "-r", NULL);
910 GNUNET_free_non_null(del_arg);
912 = GNUNET_SCHEDULER_add_delayed (d->sched,
913 GNUNET_CONSTANTS_EXEC_WAIT,
920 * Stops a GNUnet daemon.
922 * @param d the daemon that should be stopped
923 * @param timeout how long to wait for process for shutdown to complete
924 * @param cb function called once the daemon was stopped
925 * @param cb_cls closure for cb
926 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
928 * @param allow_restart GNUNET_YES to restart peer later (using this API)
929 * GNUNET_NO to kill off and clean up for good
932 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
933 struct GNUNET_TIME_Relative timeout,
934 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
941 d->dead_cb_cls = cb_cls;
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
949 d->dead = GNUNET_YES;
954 if (delete_files == GNUNET_YES)
956 GNUNET_asprintf(&del_arg, "-d");
959 if (d->phase == SP_CONFIG_UPDATE)
961 GNUNET_SCHEDULER_cancel (d->sched, d->task);
962 d->phase = SP_START_DONE;
964 if (d->server != NULL)
966 GNUNET_CORE_disconnect (d->server);
969 /* shutdown ARM process (will terminate others) */
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
974 d->phase = SP_SHUTDOWN_START;
975 d->running = GNUNET_NO;
976 if (allow_restart == GNUNET_YES)
977 d->churn = GNUNET_YES;
980 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
981 GNUNET_TRANSPORT_disconnect(d->th);
984 /* Check if this is a local or remote process */
985 if (NULL != d->hostname)
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
989 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
992 if (d->username != NULL)
993 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
995 arg = GNUNET_strdup (d->hostname);
997 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1002 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1003 /* Use -e to end arm, and -d to remove temp files */
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1012 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1017 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1020 GNUNET_free_non_null(del_arg);
1021 d->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
1023 = GNUNET_SCHEDULER_add_now (d->sched,
1029 * Changes the configuration of a GNUnet daemon.
1031 * @param d the daemon that should be modified
1032 * @param cfg the new configuration for the daemon
1033 * @param cb function called once the configuration was changed
1034 * @param cb_cls closure for cb
1037 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1038 struct GNUNET_CONFIGURATION_Handle *cfg,
1039 GNUNET_TESTING_NotifyCompletion cb,
1044 if (d->phase != SP_START_DONE)
1049 ("Peer not yet running, can not change configuration at this point."));
1053 /* 1) write configuration to temporary file */
1054 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1057 cb (cb_cls, _("Failed to write new configuration to disk."));
1061 /* 2) copy file to remote host (if necessary) */
1062 if (NULL == d->hostname)
1064 /* signal success */
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "Copying updated configuration file to remote host `%s'.\n",
1074 d->phase = SP_CONFIG_UPDATE;
1075 if (NULL != d->username)
1076 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1078 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1079 d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", d->cfgfile, arg, NULL);
1083 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1085 ("Could not start `%s' process to copy configuration file.\n"),
1088 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1089 d->phase = SP_START_DONE;
1093 d->update_cb_cls = cb_cls;
1095 = GNUNET_SCHEDULER_add_delayed (d->sched,
1096 GNUNET_CONSTANTS_EXEC_WAIT,
1102 * Data kept for each pair of peers that we try
1105 struct ConnectContext
1108 * Testing handle to the first daemon.
1110 struct GNUNET_TESTING_Daemon *d1;
1113 * Handle to core of first daemon (to check connect)
1115 struct GNUNET_CORE_Handle * d1core;
1118 * Testing handle to the second daemon.
1120 struct GNUNET_TESTING_Daemon *d2;
1123 * Transport handle to the second daemon.
1125 struct GNUNET_TRANSPORT_Handle *d2th;
1128 * Function to call once we are done (or have timed out).
1130 GNUNET_TESTING_NotifyConnection cb;
1138 * When should this operation be complete (or we must trigger
1141 struct GNUNET_TIME_Absolute timeout;
1144 * The relative timeout from whence this connect attempt was
1145 * started. Allows for reconnect attempts.
1147 struct GNUNET_TIME_Relative relative_timeout;
1150 * Maximum number of connect attempts, will retry connection
1151 * this number of times on failures.
1153 unsigned int max_connect_attempts;
1156 * Hello timeout task
1158 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1161 * Connect timeout task
1163 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1166 * When should this operation be complete (or we must trigger
1169 struct GNUNET_TIME_Relative timeout_hello;
1172 * Was the connection attempt successful?
1177 * The distance between the two connected peers
1183 /** Forward declaration **/
1185 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1189 * Notify callback about success or failure of the attempt
1190 * to connect the two peers
1192 * @param cls our "struct ConnectContext" (freed)
1193 * @param tc reason tells us if we succeeded or failed
1196 notify_connect_result (void *cls,
1197 const struct GNUNET_SCHEDULER_TaskContext *tc)
1199 struct ConnectContext *ctx = cls;
1200 struct GNUNET_TIME_Relative remaining;
1202 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1203 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1205 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
1206 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1208 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1210 if (ctx->d2th != NULL)
1211 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1213 if (ctx->d1core != NULL)
1214 GNUNET_CORE_disconnect (ctx->d1core);
1221 remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
1223 if (ctx->connected == GNUNET_YES)
1225 if (ctx->cb != NULL)
1227 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance, ctx->d1->cfg,
1228 ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1231 else if (remaining.value > 0)
1233 if (ctx->d1core != NULL)
1235 GNUNET_CORE_disconnect(ctx->d1core);
1239 if (ctx->d2th != NULL)
1241 GNUNET_TRANSPORT_disconnect(ctx->d2th);
1244 GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
1249 if (ctx->cb != NULL)
1251 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1252 ctx->d2->cfg, ctx->d1, ctx->d2,
1253 _("Peers failed to connect"));
1257 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1259 GNUNET_CORE_disconnect (ctx->d1core);
1266 * Success, connection is up. Signal client our success.
1268 * @param cls our "struct ConnectContext"
1269 * @param peer identity of the peer that has connected
1270 * @param latency the round trip latency of the connection to this peer
1271 * @param distance distance the transport level distance to this peer
1275 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1278 struct ConnectContext *ctx = cls;
1280 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1282 ctx->connected = GNUNET_YES;
1283 ctx->distance = distance;
1284 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1285 ctx->timeout_task = GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1286 ¬ify_connect_result,
1293 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1295 struct ConnectContext *ctx = cls;
1297 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1298 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1300 if (ctx->d1->hello != NULL)
1302 GNUNET_TRANSPORT_offer_hello (ctx->d2th, GNUNET_HELLO_get_header(ctx->d1->hello));
1303 ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
1304 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1307 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
1313 * Establish a connection between two GNUnet daemons.
1315 * @param d1 handle for the first daemon
1316 * @param d2 handle for the second daemon
1317 * @param timeout how long is the connection attempt
1319 * @param max_connect_attempts how many times should we try to reconnect
1321 * @param cb function to call at the end
1322 * @param cb_cls closure for cb
1325 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1326 struct GNUNET_TESTING_Daemon *d2,
1327 struct GNUNET_TIME_Relative timeout,
1328 unsigned int max_connect_attempts,
1329 GNUNET_TESTING_NotifyConnection cb,
1332 struct ConnectContext *ctx;
1334 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1337 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1338 _("Peers are not fully running yet, can not connect!\n"));
1341 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1344 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1345 ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
1346 ctx->relative_timeout = timeout;
1348 ctx->cb_cls = cb_cls;
1349 ctx->max_connect_attempts = max_connect_attempts;
1350 ctx->connected = GNUNET_NO;
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Asked to connect peer %s to peer %s\n",
1354 d1->shortname, d2->shortname);
1357 ctx->d1core = GNUNET_CORE_connect (d1->sched,
1362 &connect_notify, NULL,
1364 NULL, GNUNET_NO, no_handlers);
1365 if (ctx->d1core == NULL)
1369 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1370 _("Failed to connect to core service of first peer!\n"));
1374 #if DEBUG_TESTING > 2
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 "Asked to connect peer %s to peer %s\n",
1377 d1->shortname, d2->shortname);
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1379 "Connecting to transport service of peer %s\n", d2->shortname);
1383 ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
1384 d2->cfg, d2, NULL, NULL, NULL);
1385 if (ctx->d2th == NULL)
1387 GNUNET_CORE_disconnect(ctx->d1core);
1390 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1391 _("Failed to connect to transport service!\n"));
1395 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
1396 GNUNET_TIME_relative_divide(ctx->relative_timeout,
1397 max_connect_attempts),
1398 ¬ify_connect_result, ctx);
1400 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1404 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1407 struct ConnectContext *ctx = cls;
1408 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1412 #if DEBUG_TESTING_RECONNECT
1413 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "re-attempting connect of peer %s to peer %s\n",
1414 ctx->d1->shortname, ctx->d2->shortname);
1417 GNUNET_assert(ctx->d1core == NULL);
1419 ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1421 GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1424 &connect_notify, NULL,
1426 NULL, GNUNET_NO, no_handlers);
1427 if (ctx->d1core == NULL)
1429 if (NULL != ctx->cb)
1430 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1431 _("Failed to connect to core service of first peer!\n"));
1436 ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1437 ctx->d2->cfg, ctx->d2, NULL, NULL, NULL);
1438 if (ctx->d2th == NULL)
1440 GNUNET_CORE_disconnect(ctx->d1core);
1442 if (NULL != ctx->cb)
1443 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1444 _("Failed to connect to transport service!\n"));
1448 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1449 GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1450 ¬ify_connect_result, ctx);
1452 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1455 /* end of testing.c */