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
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,
166 d, NULL, NULL, NULL);
169 if (GNUNET_YES == d->dead)
170 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
171 else if (NULL != d->cb)
172 d->cb (d->cb_cls, &d->id, d->cfg, d,
173 _("Failed to connect to transport service!\n"));
177 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
182 * Finite-state machine for starting GNUnet.
184 * @param cls our "struct GNUNET_TESTING_Daemon"
188 start_fsm (void *cls,
189 const struct GNUNET_SCHEDULER_TaskContext *tc)
191 struct GNUNET_TESTING_Daemon *d = cls;
192 GNUNET_TESTING_NotifyDaemonRunning cb;
193 enum GNUNET_OS_ProcessStatusType type;
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Peer FSM is in phase %u.\n", d->phase);
203 d->task = GNUNET_SCHEDULER_NO_TASK;
207 /* confirm copying complete */
208 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
210 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
217 d->cfg, d, _("`scp' does not seem to terminate (timeout copying config).\n"));
222 = GNUNET_SCHEDULER_add_delayed (d->sched,
223 GNUNET_CONSTANTS_EXEC_WAIT,
227 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
233 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Successfully copied configuration file.\n");
240 d->phase = SP_COPIED;
243 /* Start create hostkey process */
244 d->pipe_stdout = GNUNET_DISK_pipe(GNUNET_NO);
245 if (d->pipe_stdout == NULL)
254 (NULL == d->hostname)
255 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
256 : _("Failed to create pipe for `ssh' process.\n"));
259 if (NULL == d->hostname)
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263 "Starting `%s', with command `%s %s %s %s'.\n",
264 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
267 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
271 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
275 if (d->username != NULL)
276 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
278 dst = GNUNET_strdup (d->hostname);
281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
282 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
283 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c", d->cfgfile,
286 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
293 "-c", d->cfgfile, "-sq", NULL);
294 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
300 _("Could not start `%s' process to create hostkey.\n"),
301 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
309 (NULL == d->hostname)
310 ? _("Failed to start `gnunet-peerinfo' process.\n")
311 : _("Failed to start `ssh' process.\n"));
312 GNUNET_DISK_pipe_close(d->pipe_stdout);
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Started `%s', waiting for hostkey.\n",
320 d->phase = SP_HOSTKEY_CREATE;
322 = GNUNET_SCHEDULER_add_read_file (d->sched,
323 GNUNET_TIME_absolute_get_remaining(d->max_timeout),
324 GNUNET_DISK_pipe_handle(d->pipe_stdout,
325 GNUNET_DISK_PIPE_END_READ),
329 case SP_HOSTKEY_CREATE:
330 bytes_read = GNUNET_DISK_file_read(GNUNET_DISK_pipe_handle(d->pipe_stdout,
331 GNUNET_DISK_PIPE_END_READ),
332 &d->hostkeybuf[d->hostkeybufpos],
333 sizeof(d->hostkeybuf) - d->hostkeybufpos);
335 d->hostkeybufpos += bytes_read;
337 if ( (d->hostkeybufpos < 104) &&
342 = GNUNET_SCHEDULER_add_read_file (d->sched,
343 GNUNET_TIME_absolute_get_remaining(d->max_timeout),
344 GNUNET_DISK_pipe_handle(d->pipe_stdout,
345 GNUNET_DISK_PIPE_END_READ),
350 d->hostkeybuf[103] = '\0';
351 if ( (bytes_read < 0) ||
352 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
353 &d->id.hashPubKey)) )
357 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
358 _("Error reading from gnunet-peerinfo: %s\n"),
361 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
362 _("Malformed output from gnunet-peerinfo!\n"));
365 GNUNET_DISK_pipe_close(d->pipe_stdout);
366 d->pipe_stdout = NULL;
367 (void) PLIBC_KILL (d->pid, SIGKILL);
368 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
375 _("`Failed to get hostkey!\n"));
378 GNUNET_DISK_pipe_close(d->pipe_stdout);
379 d->pipe_stdout = NULL;
380 (void) PLIBC_KILL (d->pid, SIGKILL);
381 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Successfully got hostkey!\n");
387 if (d->hostkey_callback != NULL)
389 d->hostkey_callback(d->hostkey_cls, &d->id, d, NULL);
390 d->phase = SP_HOSTKEY_CREATED;
394 d->phase = SP_TOPOLOGY_SETUP;
397 case SP_HOSTKEY_CREATED:
398 /* wait for topology finished */
399 if ((GNUNET_YES == d->dead) || (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0))
408 _("`Failed while waiting for topology setup!\n"));
413 = GNUNET_SCHEDULER_add_delayed (d->sched,
414 GNUNET_CONSTANTS_EXEC_WAIT,
417 case SP_TOPOLOGY_SETUP:
418 /* start GNUnet on remote host */
419 if (NULL == d->hostname)
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
424 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
428 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
438 if (d->username != NULL)
439 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
441 dst = GNUNET_strdup (d->hostname);
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
446 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
447 "-L", "DEBUG", "-s", "-q");
449 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
459 "-c", d->cfgfile, "-s", "-q", NULL);
464 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
465 _("Could not start `%s' process to start GNUnet.\n"),
466 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
474 (NULL == d->hostname)
475 ? _("Failed to start `gnunet-arm' process.\n")
476 : _("Failed to start `ssh' process.\n"));
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 "Started `%s', waiting for `%s' to be up.\n",
482 "gnunet-arm", "gnunet-service-core");
484 d->phase = SP_START_ARMING;
486 = GNUNET_SCHEDULER_add_delayed (d->sched,
487 GNUNET_CONSTANTS_EXEC_WAIT,
490 case SP_START_ARMING:
491 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
493 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
502 (NULL == d->hostname)
503 ? _("`gnunet-arm' does not seem to terminate.\n")
504 : _("`ssh' does not seem to terminate.\n"));
509 = GNUNET_SCHEDULER_add_delayed (d->sched,
510 GNUNET_CONSTANTS_EXEC_WAIT,
515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
516 "Successfully started `%s'.\n", "gnunet-arm");
518 d->phase = SP_START_CORE;
519 d->server = GNUNET_CORE_connect (d->sched,
526 NULL, GNUNET_NO, no_handlers);
534 case SP_SHUTDOWN_START:
535 /* confirm copying complete */
536 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
538 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
540 if (NULL != d->dead_cb)
541 d->dead_cb (d->dead_cb_cls,
542 _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
545 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
546 GNUNET_TRANSPORT_disconnect(d->th);
549 GNUNET_CONFIGURATION_destroy (d->cfg);
550 GNUNET_free (d->cfgfile);
551 GNUNET_free_non_null(d->hello);
552 GNUNET_free_non_null (d->hostname);
553 GNUNET_free_non_null (d->username);
554 GNUNET_free_non_null (d->shortname);
560 = GNUNET_SCHEDULER_add_delayed (d->sched,
561 GNUNET_CONSTANTS_EXEC_WAIT,
565 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
567 if (NULL != d->dead_cb)
568 d->dead_cb (d->dead_cb_cls,
569 _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
572 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
573 GNUNET_TRANSPORT_disconnect(d->th);
576 GNUNET_CONFIGURATION_destroy (d->cfg);
577 GNUNET_free (d->cfgfile);
578 GNUNET_free_non_null(d->hello);
579 GNUNET_free_non_null (d->hostname);
580 GNUNET_free_non_null (d->username);
581 GNUNET_free_non_null (d->shortname);
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
590 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
591 GNUNET_TRANSPORT_disconnect(d->th);
594 /* state clean up and notifications */
595 if (d->churn == GNUNET_NO)
597 GNUNET_CONFIGURATION_destroy (d->cfg);
598 GNUNET_free (d->cfgfile);
599 GNUNET_free_non_null (d->hostname);
600 GNUNET_free_non_null (d->username);
603 GNUNET_free_non_null(d->hello);
605 GNUNET_free_non_null (d->shortname);
607 if (NULL != d->dead_cb)
608 d->dead_cb (d->dead_cb_cls, NULL);
610 if (d->churn == GNUNET_NO)
614 case SP_CONFIG_UPDATE:
615 /* confirm copying complete */
616 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
618 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0) /* FIXME: config update should take timeout parameter! */
625 d->cfg, d, _("`scp' does not seem to terminate.\n"));
630 = GNUNET_SCHEDULER_add_delayed (d->sched,
631 GNUNET_CONSTANTS_EXEC_WAIT,
635 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
637 if (NULL != d->update_cb)
638 d->update_cb (d->update_cb_cls,
639 _("`scp' did not complete cleanly.\n"));
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Successfully copied configuration file.\n");
646 if (NULL != d->update_cb)
647 d->update_cb (d->update_cb_cls, NULL);
648 d->phase = SP_START_DONE;
654 * Continues GNUnet daemon startup when user wanted to be notified
655 * once a hostkey was generated (for creating friends files, blacklists,
658 * @param daemon the daemon to finish starting
661 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon)
663 GNUNET_assert(daemon->phase == SP_HOSTKEY_CREATED);
664 daemon->phase = SP_TOPOLOGY_SETUP;
669 * Start a peer that has previously been stopped using the daemon_stop
670 * call (and files weren't deleted and the allow restart flag)
672 * @param daemon the daemon to start (has been previously stopped)
673 * @param timeout how long to wait for restart
674 * @param cb the callback for notification when the peer is running
675 * @param cb_cls closure for the callback
678 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
679 struct GNUNET_TIME_Relative timeout,
680 GNUNET_TESTING_NotifyDaemonRunning cb,
683 if (daemon->running == GNUNET_YES)
685 cb(cb_cls, &daemon->id, daemon->cfg, daemon, "Daemon already running, can't restart!");
690 daemon->cb_cls = cb_cls;
691 daemon->phase = SP_TOPOLOGY_SETUP;
692 daemon->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
694 GNUNET_SCHEDULER_add_continuation (daemon->sched,
697 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
701 * Starts a GNUnet daemon. GNUnet must be installed on the target
702 * system and available in the PATH. The machine must furthermore be
703 * reachable via "ssh" (unless the hostname is "NULL") without the
704 * need to enter a password.
706 * @param sched scheduler to use
707 * @param cfg configuration to use
708 * @param timeout how long to wait starting up peers
709 * @param hostname name of the machine where to run GNUnet
710 * (use NULL for localhost).
711 * @param hostkey_callback function to call once the hostkey has been
712 * generated for this peer, but it hasn't yet been started
713 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
714 * @param hostkey_cls closure for hostkey callback
715 * @param cb function to call once peer is up, or failed to start
716 * @param cb_cls closure for cb
717 * @return handle to the daemon (actual start will be completed asynchronously)
719 struct GNUNET_TESTING_Daemon *
720 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
721 const struct GNUNET_CONFIGURATION_Handle *cfg,
722 struct GNUNET_TIME_Relative timeout,
723 const char *hostname,
724 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
726 GNUNET_TESTING_NotifyDaemonRunning cb,
729 struct GNUNET_TESTING_Daemon *ret;
733 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
735 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
736 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
739 "Setting up peer with configuration file `%s'.\n",
742 if (NULL == ret->cfgfile)
744 GNUNET_free_non_null (ret->hostname);
748 ret->hostkey_callback = hostkey_callback;
749 ret->hostkey_cls = hostkey_cls;
751 ret->cb_cls = cb_cls;
752 ret->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
753 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
754 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
756 "DEFAULTCONFIG", ret->cfgfile);
757 /* 1) write configuration to temporary file */
758 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
760 if (0 != UNLINK (ret->cfgfile))
761 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
762 "unlink", ret->cfgfile);
763 GNUNET_CONFIGURATION_destroy (ret->cfg);
764 GNUNET_free_non_null (ret->hostname);
765 GNUNET_free (ret->cfgfile);
770 GNUNET_CONFIGURATION_get_value_string (cfg,
772 "USERNAME", &username))
774 if (NULL != getenv ("USER"))
775 username = GNUNET_strdup (getenv ("USER"));
779 ret->username = username;
781 /* 2) copy file to remote host */
782 if (NULL != hostname)
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Copying configuration file to host `%s'.\n", hostname);
788 ret->phase = SP_COPYING;
789 if (NULL != username)
790 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
792 GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
793 ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
798 ret->cfgfile, arg, NULL);
802 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
804 ("Could not start `%s' process to copy configuration file.\n"),
806 if (0 != UNLINK (ret->cfgfile))
807 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
808 "unlink", ret->cfgfile);
809 GNUNET_CONFIGURATION_destroy (ret->cfg);
810 GNUNET_free_non_null (ret->hostname);
811 GNUNET_free_non_null (ret->username);
812 GNUNET_free (ret->cfgfile);
817 = GNUNET_SCHEDULER_add_delayed (sched,
818 GNUNET_CONSTANTS_EXEC_WAIT,
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
824 "No need to copy configuration file since we are running locally.\n");
826 ret->phase = SP_COPIED;
827 GNUNET_SCHEDULER_add_continuation (sched,
830 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
836 * Restart (stop and start) a GNUnet daemon.
838 * @param d the daemon that should be restarted
839 * @param cb function called once the daemon is (re)started
840 * @param cb_cls closure for cb
843 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
844 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls)
852 d->dead = GNUNET_YES;
859 if (d->phase == SP_CONFIG_UPDATE)
861 GNUNET_SCHEDULER_cancel (d->sched, d->task);
862 d->phase = SP_START_DONE;
864 if (d->server != NULL)
866 GNUNET_CORE_disconnect (d->server);
872 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
873 GNUNET_TRANSPORT_disconnect(d->th);
876 /* state clean up and notifications */
877 GNUNET_free_non_null(d->hello);
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
884 d->phase = SP_START_ARMING;
886 /* Check if this is a local or remote process */
887 if (NULL != d->hostname)
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
894 if (d->username != NULL)
895 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
897 arg = GNUNET_strdup (d->hostname);
899 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
907 "-c", d->cfgfile, "-e", "-r", NULL);
908 /* Use -r to restart arm and all services */
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
918 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
923 "-c", d->cfgfile, "-e", "-r", NULL);
926 GNUNET_free_non_null(del_arg);
928 = GNUNET_SCHEDULER_add_delayed (d->sched,
929 GNUNET_CONSTANTS_EXEC_WAIT,
936 * Stops a GNUnet daemon.
938 * @param d the daemon that should be stopped
939 * @param timeout how long to wait for process for shutdown to complete
940 * @param cb function called once the daemon was stopped
941 * @param cb_cls closure for cb
942 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
944 * @param allow_restart GNUNET_YES to restart peer later (using this API)
945 * GNUNET_NO to kill off and clean up for good
948 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
949 struct GNUNET_TIME_Relative timeout,
950 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
957 d->dead_cb_cls = cb_cls;
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
963 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
965 d->dead = GNUNET_YES;
969 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
971 /* Free what was left from churning! */
972 GNUNET_assert(d->cfg != NULL);
973 GNUNET_CONFIGURATION_destroy (d->cfg);
974 if (delete_files == GNUNET_YES)
976 if (0 != UNLINK(d->cfgfile))
978 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "unlink");
981 GNUNET_free (d->cfgfile);
982 GNUNET_free_non_null (d->hostname);
983 GNUNET_free_non_null (d->username);
984 if (NULL != d->dead_cb)
985 d->dead_cb (d->dead_cb_cls, NULL);
990 if (delete_files == GNUNET_YES)
992 GNUNET_asprintf(&del_arg, "-d");
995 if (d->phase == SP_CONFIG_UPDATE)
997 GNUNET_SCHEDULER_cancel (d->sched, d->task);
998 d->phase = SP_START_DONE;
1000 if (d->server != NULL)
1002 GNUNET_CORE_disconnect (d->server);
1005 /* shutdown ARM process (will terminate others) */
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1010 d->phase = SP_SHUTDOWN_START;
1011 d->running = GNUNET_NO;
1012 if (allow_restart == GNUNET_YES)
1013 d->churn = GNUNET_YES;
1016 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
1017 GNUNET_TRANSPORT_disconnect(d->th);
1020 /* Check if this is a local or remote process */
1021 if (NULL != d->hostname)
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
1028 if (d->username != NULL)
1029 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1031 arg = GNUNET_strdup (d->hostname);
1033 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1041 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n", arg, "gnunet-arm", d->cfgfile, del_arg);
1044 /* Use -e to end arm, and -d to remove temp files */
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1053 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1058 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1061 GNUNET_free_non_null(del_arg);
1062 d->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
1064 = GNUNET_SCHEDULER_add_now (d->sched,
1070 * Changes the configuration of a GNUnet daemon.
1072 * @param d the daemon that should be modified
1073 * @param cfg the new configuration for the daemon
1074 * @param cb function called once the configuration was changed
1075 * @param cb_cls closure for cb
1078 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1079 struct GNUNET_CONFIGURATION_Handle *cfg,
1080 GNUNET_TESTING_NotifyCompletion cb,
1085 if (d->phase != SP_START_DONE)
1090 ("Peer not yet running, can not change configuration at this point."));
1094 /* 1) write configuration to temporary file */
1095 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1098 cb (cb_cls, _("Failed to write new configuration to disk."));
1102 /* 2) copy file to remote host (if necessary) */
1103 if (NULL == d->hostname)
1105 /* signal success */
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 "Copying updated configuration file to remote host `%s'.\n",
1115 d->phase = SP_CONFIG_UPDATE;
1116 if (NULL != d->username)
1117 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1119 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1120 d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1124 d->cfgfile, arg, NULL);
1128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1130 ("Could not start `%s' process to copy configuration file.\n"),
1133 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1134 d->phase = SP_START_DONE;
1138 d->update_cb_cls = cb_cls;
1140 = GNUNET_SCHEDULER_add_delayed (d->sched,
1141 GNUNET_CONSTANTS_EXEC_WAIT,
1147 * Data kept for each pair of peers that we try
1150 struct ConnectContext
1153 * Testing handle to the first daemon.
1155 struct GNUNET_TESTING_Daemon *d1;
1158 * Handle to core of first daemon (to check connect)
1160 struct GNUNET_CORE_Handle * d1core;
1163 * Testing handle to the second daemon.
1165 struct GNUNET_TESTING_Daemon *d2;
1168 * Handler for the request to core to connect to this peer.
1170 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1173 * Transport handle to the second daemon.
1175 struct GNUNET_TRANSPORT_Handle *d2th;
1178 * Function to call once we are done (or have timed out).
1180 GNUNET_TESTING_NotifyConnection cb;
1188 * When should this operation be complete (or we must trigger
1191 struct GNUNET_TIME_Absolute timeout;
1194 * The relative timeout from whence this connect attempt was
1195 * started. Allows for reconnect attempts.
1197 struct GNUNET_TIME_Relative relative_timeout;
1200 * Maximum number of connect attempts, will retry connection
1201 * this number of times on failures.
1203 unsigned int max_connect_attempts;
1206 * Hello timeout task
1208 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1211 * Connect timeout task
1213 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1216 * When should this operation be complete (or we must trigger
1219 struct GNUNET_TIME_Relative timeout_hello;
1222 * Was the connection attempt successful?
1227 * The distance between the two connected peers
1233 /** Forward declaration **/
1235 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1239 * Notify callback about success or failure of the attempt
1240 * to connect the two peers
1242 * @param cls our "struct ConnectContext" (freed)
1243 * @param tc reason tells us if we succeeded or failed
1246 notify_connect_result (void *cls,
1247 const struct GNUNET_SCHEDULER_TaskContext *tc)
1249 struct ConnectContext *ctx = cls;
1250 struct GNUNET_TIME_Relative remaining;
1252 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1253 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1255 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
1256 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1259 if (ctx->connect_request_handle != NULL)
1261 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1262 ctx->connect_request_handle = NULL;
1264 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1266 if (ctx->d2th != NULL)
1267 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1269 if (ctx->d1core != NULL)
1270 GNUNET_CORE_disconnect (ctx->d1core);
1272 if (ctx->d2core != NULL)
1273 GNUNET_CORE_disconnect (ctx->d2core);
1282 remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
1284 if (ctx->connected == GNUNET_YES)
1286 if (ctx->cb != NULL)
1288 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance, ctx->d1->cfg,
1289 ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1292 else if (remaining.value > 0)
1294 if (ctx->d1core != NULL)
1296 GNUNET_CORE_disconnect(ctx->d1core);
1300 if (ctx->d2core != NULL)
1302 GNUNET_CORE_disconnect(ctx->d2core);
1307 if (ctx->d2th != NULL)
1309 GNUNET_TRANSPORT_disconnect(ctx->d2th);
1312 GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
1317 if (ctx->cb != NULL)
1319 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1320 ctx->d2->cfg, ctx->d1, ctx->d2,
1321 _("Peers failed to connect"));
1325 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1327 GNUNET_CORE_disconnect (ctx->d1core);
1334 * Success, connection is up. Signal client our success.
1336 * @param cls our "struct ConnectContext"
1337 * @param peer identity of the peer that has connected
1338 * @param latency the round trip latency of the connection to this peer
1339 * @param distance distance the transport level distance to this peer
1343 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1346 struct ConnectContext *ctx = cls;
1348 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1350 ctx->connected = GNUNET_YES;
1351 ctx->distance = distance;
1352 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1353 ctx->timeout_task = GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1354 ¬ify_connect_result,
1362 * Success, connection is up. Signal client our success.
1364 * @param cls our "struct ConnectContext"
1365 * @param peer identity of the peer that has connected
1366 * @param latency the round trip latency of the connection to this peer
1367 * @param distance distance the transport level distance to this peer
1371 connect_notify_core2 (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1374 struct ConnectContext *ctx = cls;
1376 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1378 ctx->connected = GNUNET_YES;
1379 ctx->distance = distance;
1380 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1381 ctx->timeout_task = GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1382 ¬ify_connect_result,
1390 * Task called once a core connect request has been transmitted.
1392 * @param cls struct ConnectContext
1393 * @param tc context information (why was this task triggered now)
1395 void core_connect_request_cont (void *cls,
1397 GNUNET_SCHEDULER_TaskContext * tc)
1399 struct ConnectContext *ctx = cls;
1401 if (tc->reason == GNUNET_SCHEDULER_REASON_PREREQ_DONE)
1402 ctx->connect_request_handle = NULL;
1407 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1409 struct ConnectContext *ctx = cls;
1410 struct GNUNET_MessageHeader *hello;
1411 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1412 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1414 if ((ctx->d1->hello != NULL) && (NULL != GNUNET_HELLO_get_header(ctx->d1->hello)))
1416 hello = GNUNET_HELLO_get_header(ctx->d1->hello);
1417 GNUNET_assert(hello != NULL);
1418 GNUNET_TRANSPORT_offer_hello (ctx->d2th, hello);
1420 ctx->connect_request_handle = GNUNET_CORE_peer_request_connect (ctx->d1->sched,
1422 GNUNET_TIME_relative_get_forever(),
1424 &core_connect_request_cont,
1426 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending connect request to core for peer %s\n", GNUNET_i2s(&ctx->d1->id));
1427 ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
1428 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1431 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
1437 * Establish a connection between two GNUnet daemons.
1439 * @param d1 handle for the first daemon
1440 * @param d2 handle for the second daemon
1441 * @param timeout how long is the connection attempt
1443 * @param max_connect_attempts how many times should we try to reconnect
1445 * @param cb function to call at the end
1446 * @param cb_cls closure for cb
1449 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1450 struct GNUNET_TESTING_Daemon *d2,
1451 struct GNUNET_TIME_Relative timeout,
1452 unsigned int max_connect_attempts,
1453 GNUNET_TESTING_NotifyConnection cb,
1456 struct ConnectContext *ctx;
1458 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1461 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1462 _("Peers are not fully running yet, can not connect!\n"));
1465 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1468 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1469 ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
1470 ctx->relative_timeout = timeout;
1472 ctx->cb_cls = cb_cls;
1473 ctx->max_connect_attempts = max_connect_attempts;
1474 ctx->connected = GNUNET_NO;
1476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1477 "Asked to connect peer %s to peer %s\n",
1478 d1->shortname, d2->shortname);
1481 ctx->d1core = GNUNET_CORE_connect (d1->sched,
1486 &connect_notify, NULL, NULL,
1488 NULL, GNUNET_NO, no_handlers);
1489 if (ctx->d1core == NULL)
1493 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1494 _("Failed to connect to core service of first peer!\n"));
1499 ctx->d2core = GNUNET_CORE_connect (d2->sched,
1506 NULL, GNUNET_NO, no_handlers);
1507 if (ctx->d2core == NULL)
1511 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1512 _("Failed to connect to core service of second peer!\n"));
1517 #if DEBUG_TESTING > 2
1518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1519 "Asked to connect peer %s to peer %s\n",
1520 d1->shortname, d2->shortname);
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522 "Connecting to transport service of peer %s\n", d2->shortname);
1526 ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
1529 d2, NULL, NULL, NULL);
1530 if (ctx->d2th == NULL)
1532 GNUNET_CORE_disconnect(ctx->d1core);
1535 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1536 _("Failed to connect to transport service!\n"));
1540 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
1541 GNUNET_TIME_relative_divide(ctx->relative_timeout,
1542 max_connect_attempts),
1543 ¬ify_connect_result, ctx);
1545 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1549 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1552 struct ConnectContext *ctx = cls;
1553 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1557 #if DEBUG_TESTING_RECONNECT
1558 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "re-attempting connect of peer %s to peer %s\n",
1559 ctx->d1->shortname, ctx->d2->shortname);
1562 GNUNET_assert(ctx->d1core == NULL);
1564 ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1566 GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1569 &connect_notify, NULL, NULL,
1571 NULL, GNUNET_NO, no_handlers);
1572 if (ctx->d1core == NULL)
1574 if (NULL != ctx->cb)
1575 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1576 _("Failed to connect to core service of first peer!\n"));
1581 ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1584 ctx->d2, NULL, NULL, NULL);
1585 if (ctx->d2th == NULL)
1587 GNUNET_CORE_disconnect(ctx->d1core);
1589 if (NULL != ctx->cb)
1590 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1591 _("Failed to connect to transport service!\n"));
1595 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1596 GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1597 ¬ify_connect_result, ctx);
1599 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1602 /* end of testing.c */