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)
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
76 "Received `%s' from transport service of `%4s', disconnecting core!\n",
77 "HELLO", GNUNET_i2s (&daemon->id));
79 GNUNET_CORE_disconnect (daemon->server);
80 daemon->server = NULL;
83 GNUNET_assert (message != NULL);
84 msize = ntohs (message->size);
89 if (daemon->th != NULL)
91 GNUNET_TRANSPORT_get_hello_cancel (daemon->th, &process_hello, daemon);
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95 "Received `%s' from transport service of `%4s'\n",
96 "HELLO", GNUNET_i2s (&daemon->id));
99 GNUNET_free_non_null (daemon->hello);
100 daemon->hello = GNUNET_malloc (msize);
101 memcpy (daemon->hello, message, msize);
103 if (daemon->th != NULL)
105 GNUNET_TRANSPORT_disconnect (daemon->th);
112 * Function called after GNUNET_CORE_connect has succeeded
113 * (or failed for good). Note that the private key of the
114 * peer is intentionally not exposed here; if you need it,
115 * your process should try to read the private key file
116 * directly (which should work if you are authorized...).
119 * @param server handle to the server, NULL if we failed
120 * @param my_identity ID of this peer, NULL if we failed
121 * @param publicKey public key of this peer, NULL if we failed
124 testing_init (void *cls,
125 struct GNUNET_CORE_Handle *server,
126 const struct GNUNET_PeerIdentity *my_identity,
127 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
129 struct GNUNET_TESTING_Daemon *d = cls;
130 GNUNET_TESTING_NotifyDaemonRunning cb;
132 GNUNET_assert (d->phase == SP_START_CORE);
133 d->phase = SP_START_DONE;
139 if (GNUNET_YES == d->dead)
140 GNUNET_TESTING_daemon_stop (d,
141 GNUNET_TIME_absolute_get_remaining
142 (d->max_timeout), d->dead_cb,
143 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
145 cb (d->cb_cls, NULL, d->cfg, d,
146 _("Failed to connect to core service\n"));
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
153 d->id = *my_identity;
154 d->shortname = strdup (GNUNET_i2s (my_identity));
156 d->running = GNUNET_YES;
158 if (NULL != cb) /* FIXME: what happens when this callback calls GNUNET_TESTING_daemon_stop? */
159 cb (d->cb_cls, my_identity, d->cfg, d, NULL);
161 if (GNUNET_NO == d->running)
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "Peer is dead (d->running == GNUNET_NO)\n");
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "Successfully started peer `%4s', connecting to transport service.\n",
172 GNUNET_i2s (my_identity));
175 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
178 if (GNUNET_YES == d->dead)
179 GNUNET_TESTING_daemon_stop (d,
180 GNUNET_TIME_absolute_get_remaining
181 (d->max_timeout), d->dead_cb,
182 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
183 else if (NULL != d->cb)
184 d->cb (d->cb_cls, &d->id, d->cfg, d,
185 _("Failed to connect to transport service!\n"));
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190 "Connected to transport service `%s', getting HELLO\n",
191 GNUNET_i2s (my_identity));
194 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
199 * Finite-state machine for starting GNUnet.
201 * @param cls our "struct GNUNET_TESTING_Daemon"
205 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
207 struct GNUNET_TESTING_Daemon *d = cls;
208 GNUNET_TESTING_NotifyDaemonRunning cb;
209 enum GNUNET_OS_ProcessStatusType type;
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Peer FSM is in phase %u.\n", d->phase);
219 d->task = GNUNET_SCHEDULER_NO_TASK;
223 /* confirm copying complete */
224 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
226 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
236 ("`scp' does not seem to terminate (timeout copying config).\n"));
241 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
245 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
251 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256 "Successfully copied configuration file.\n");
258 d->phase = SP_COPIED;
261 /* Start create hostkey process */
262 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
263 if (d->pipe_stdout == NULL)
272 (NULL == d->hostname)
273 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
274 : _("Failed to create pipe for `ssh' process.\n"));
277 if (NULL == d->hostname)
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Starting `%s', with command `%s %s %s %s'.\n",
282 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
286 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
287 "gnunet-peerinfo", "-c", d->cfgfile,
289 GNUNET_DISK_pipe_close_end (d->pipe_stdout,
290 GNUNET_DISK_PIPE_END_WRITE);
294 if (d->username != NULL)
295 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
297 dst = GNUNET_strdup (d->hostname);
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
302 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
305 if (d->ssh_port_str == NULL)
307 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
314 "-c", d->cfgfile, "-sq",
319 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
320 "ssh", "-p", d->ssh_port_str,
326 "-c", d->cfgfile, "-sq",
329 GNUNET_DISK_pipe_close_end (d->pipe_stdout,
330 GNUNET_DISK_PIPE_END_WRITE);
335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
336 _("Could not start `%s' process to create hostkey.\n"),
337 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
345 (NULL == d->hostname)
346 ? _("Failed to start `gnunet-peerinfo' process.\n")
347 : _("Failed to start `ssh' process.\n"));
348 GNUNET_DISK_pipe_close (d->pipe_stdout);
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
355 d->phase = SP_HOSTKEY_CREATE;
358 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
360 GNUNET_DISK_pipe_handle
362 GNUNET_DISK_PIPE_END_READ),
365 case SP_HOSTKEY_CREATE:
367 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
368 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
369 &d->hostkeybuf[d->hostkeybufpos],
370 sizeof (d->hostkeybuf) - d->hostkeybufpos);
372 d->hostkeybufpos += bytes_read;
374 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
379 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
381 GNUNET_DISK_pipe_handle
383 GNUNET_DISK_PIPE_END_READ),
387 d->hostkeybuf[103] = '\0';
388 if ((bytes_read < 0) ||
389 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
394 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
395 _("Error reading from gnunet-peerinfo: %s\n"),
398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
399 _("Malformed output from gnunet-peerinfo!\n"));
402 GNUNET_DISK_pipe_close (d->pipe_stdout);
403 d->pipe_stdout = NULL;
404 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
405 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
406 GNUNET_OS_process_close (d->proc);
409 cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
412 GNUNET_DISK_pipe_close (d->pipe_stdout);
413 d->pipe_stdout = NULL;
414 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
415 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
416 GNUNET_OS_process_close (d->proc);
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
421 if (d->hostkey_callback != NULL)
423 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
424 d->phase = SP_HOSTKEY_CREATED;
428 d->phase = SP_TOPOLOGY_SETUP;
431 case SP_HOSTKEY_CREATED:
432 /* wait for topology finished */
433 if ((GNUNET_YES == d->dead)
434 || (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
442 d->cfg, d, _("`Failed while waiting for topology setup!\n"));
447 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
450 case SP_TOPOLOGY_SETUP:
451 /* start GNUnet on remote host */
452 if (NULL == d->hostname)
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
456 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
457 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
458 "-L", "DEBUG", "-s");
460 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
461 "gnunet-arm", "-c", d->cfgfile,
469 if (d->username != NULL)
470 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
472 dst = GNUNET_strdup (d->hostname);
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
477 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c",
478 d->cfgfile, "-L", "DEBUG", "-s", "-q");
480 if (d->ssh_port_str == NULL)
482 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
490 "-c", d->cfgfile, "-s", "-q",
496 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
497 "ssh", "-p", d->ssh_port_str,
505 "-c", d->cfgfile, "-s", "-q",
512 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
513 _("Could not start `%s' process to start GNUnet.\n"),
514 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
522 (NULL == d->hostname)
523 ? _("Failed to start `gnunet-arm' process.\n")
524 : _("Failed to start `ssh' process.\n"));
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529 "Started `%s', waiting for `%s' to be up.\n",
530 "gnunet-arm", "gnunet-service-core");
532 d->phase = SP_START_ARMING;
534 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
537 case SP_START_ARMING:
538 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
540 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
550 (NULL == d->hostname)
551 ? _("`gnunet-arm' does not seem to terminate.\n")
552 : _("`ssh' does not seem to terminate.\n"));
557 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
563 "Successfully started `%s'.\n", "gnunet-arm");
565 d->phase = SP_START_CORE;
566 d->server = GNUNET_CORE_connect (d->cfg, 1,
567 #if NO_MORE_TIMEOUT_FIXME
574 NULL, GNUNET_NO, no_handlers);
582 case SP_SHUTDOWN_START:
583 /* confirm copying complete */
584 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
586 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
589 if (NULL != d->dead_cb)
590 d->dead_cb (d->dead_cb_cls,
592 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
595 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello,
597 GNUNET_TRANSPORT_disconnect (d->th);
600 GNUNET_CONFIGURATION_destroy (d->cfg);
601 GNUNET_free (d->cfgfile);
602 GNUNET_free_non_null (d->hello);
603 GNUNET_free_non_null (d->hostname);
604 GNUNET_free_non_null (d->username);
605 GNUNET_free_non_null (d->shortname);
611 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
615 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
617 if (NULL != d->dead_cb)
618 d->dead_cb (d->dead_cb_cls,
620 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
623 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
624 GNUNET_TRANSPORT_disconnect (d->th);
627 if (d->server != NULL)
629 GNUNET_CORE_disconnect (d->server);
632 GNUNET_CONFIGURATION_destroy (d->cfg);
633 GNUNET_free (d->cfgfile);
634 GNUNET_free_non_null (d->hello);
635 GNUNET_free_non_null (d->hostname);
636 GNUNET_free_non_null (d->username);
637 GNUNET_free_non_null (d->shortname);
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
644 if (d->server != NULL)
646 GNUNET_CORE_disconnect (d->server);
652 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
653 GNUNET_TRANSPORT_disconnect (d->th);
656 /* state clean up and notifications */
657 if (d->churn == GNUNET_NO)
659 GNUNET_CONFIGURATION_destroy (d->cfg);
660 GNUNET_free (d->cfgfile);
661 GNUNET_free_non_null (d->hostname);
662 GNUNET_free_non_null (d->username);
665 GNUNET_free_non_null (d->hello);
667 GNUNET_free_non_null (d->shortname);
669 if (NULL != d->dead_cb)
670 d->dead_cb (d->dead_cb_cls, NULL);
672 if (d->churn == GNUNET_NO)
676 case SP_CONFIG_UPDATE:
677 /* confirm copying complete */
678 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
680 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
687 d->cfg, d, _("`scp' does not seem to terminate.\n"));
692 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
696 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
698 if (NULL != d->update_cb)
699 d->update_cb (d->update_cb_cls,
700 _("`scp' did not complete cleanly.\n"));
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Successfully copied configuration file.\n");
707 if (NULL != d->update_cb)
708 d->update_cb (d->update_cb_cls, NULL);
709 d->phase = SP_START_DONE;
715 * Continues GNUnet daemon startup when user wanted to be notified
716 * once a hostkey was generated (for creating friends files, blacklists,
719 * @param daemon the daemon to finish starting
722 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
724 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
725 daemon->phase = SP_TOPOLOGY_SETUP;
729 * Check whether the given daemon is running.
731 * @param daemon the daemon to check
733 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
734 * daemon is down, GNUNET_SYSERR on error.
737 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
740 return GNUNET_SYSERR;
742 if (daemon->running == GNUNET_YES)
749 * Start a peer that has previously been stopped using the daemon_stop
750 * call (and files weren't deleted and the allow restart flag)
752 * @param daemon the daemon to start (has been previously stopped)
753 * @param timeout how long to wait for restart
754 * @param cb the callback for notification when the peer is running
755 * @param cb_cls closure for the callback
758 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
759 struct GNUNET_TIME_Relative timeout,
760 GNUNET_TESTING_NotifyDaemonRunning cb,
763 if (daemon->running == GNUNET_YES)
765 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
766 "Daemon already running, can't restart!");
771 daemon->cb_cls = cb_cls;
772 daemon->phase = SP_TOPOLOGY_SETUP;
773 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
775 GNUNET_SCHEDULER_add_continuation (&start_fsm,
777 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
781 * Starts a GNUnet daemon. GNUnet must be installed on the target
782 * system and available in the PATH. The machine must furthermore be
783 * reachable via "ssh" (unless the hostname is "NULL") without the
784 * need to enter a password.
786 * @param cfg configuration to use
787 * @param timeout how long to wait starting up peers
788 * @param hostname name of the machine where to run GNUnet
789 * (use NULL for localhost).
790 * @param ssh_username ssh username to use when connecting to hostname
791 * @param sshport port to pass to ssh process when connecting to hostname
792 * @param hostkey_callback function to call once the hostkey has been
793 * generated for this peer, but it hasn't yet been started
794 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
795 * @param hostkey_cls closure for hostkey callback
796 * @param cb function to call once peer is up, or failed to start
797 * @param cb_cls closure for cb
798 * @return handle to the daemon (actual start will be completed asynchronously)
800 struct GNUNET_TESTING_Daemon *
801 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
802 struct GNUNET_TIME_Relative timeout,
803 const char *hostname,
804 const char *ssh_username,
806 GNUNET_TESTING_NotifyHostkeyCreated
807 hostkey_callback, void *hostkey_cls,
808 GNUNET_TESTING_NotifyDaemonRunning cb,
811 struct GNUNET_TESTING_Daemon *ret;
815 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
816 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
819 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
822 ret->ssh_port_str = NULL;
823 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
826 "Setting up peer with configuration file `%s'.\n",
829 if (NULL == ret->cfgfile)
831 GNUNET_free_non_null (ret->ssh_port_str);
832 GNUNET_free_non_null (ret->hostname);
836 ret->hostkey_callback = hostkey_callback;
837 ret->hostkey_cls = hostkey_cls;
839 ret->cb_cls = cb_cls;
840 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
841 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
842 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
844 "DEFAULTCONFIG", ret->cfgfile);
845 /* 1) write configuration to temporary file */
846 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
848 if (0 != UNLINK (ret->cfgfile))
849 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
850 "unlink", ret->cfgfile);
851 GNUNET_CONFIGURATION_destroy (ret->cfg);
852 GNUNET_free_non_null (ret->hostname);
853 GNUNET_free (ret->cfgfile);
857 if (ssh_username != NULL)
858 username = GNUNET_strdup (ssh_username);
859 if ((ssh_username == NULL) && (GNUNET_OK !=
860 GNUNET_CONFIGURATION_get_value_string (cfg,
865 if (NULL != getenv ("USER"))
866 username = GNUNET_strdup (getenv ("USER"));
870 ret->username = username;
872 /* 2) copy file to remote host */
873 if (NULL != hostname)
876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
877 "Copying configuration file to host `%s'.\n", hostname);
879 ret->phase = SP_COPYING;
880 if (NULL != username)
881 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
883 GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
885 if (ret->ssh_port_str == NULL)
887 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
891 ret->cfgfile, arg, NULL);
895 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
896 "scp", "-P", ret->ssh_port_str,
900 ret->cfgfile, arg, NULL);
903 if (NULL == ret->proc)
905 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
907 ("Could not start `%s' process to copy configuration file.\n"),
909 if (0 != UNLINK (ret->cfgfile))
910 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
911 "unlink", ret->cfgfile);
912 GNUNET_CONFIGURATION_destroy (ret->cfg);
913 GNUNET_free_non_null (ret->hostname);
914 GNUNET_free_non_null (ret->username);
915 GNUNET_free (ret->cfgfile);
920 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "No need to copy configuration file since we are running locally.\n");
928 ret->phase = SP_COPIED;
929 GNUNET_SCHEDULER_add_continuation (&start_fsm,
931 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
937 * Restart (stop and start) a GNUnet daemon.
939 * @param d the daemon that should be restarted
940 * @param cb function called once the daemon is (re)started
941 * @param cb_cls closure for cb
944 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
945 GNUNET_TESTING_NotifyDaemonRunning cb,
954 d->dead = GNUNET_YES;
961 if (d->phase == SP_CONFIG_UPDATE)
963 GNUNET_SCHEDULER_cancel (d->task);
964 d->phase = SP_START_DONE;
966 if (d->server != NULL)
968 GNUNET_CORE_disconnect (d->server);
974 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
975 GNUNET_TRANSPORT_disconnect (d->th);
978 /* state clean up and notifications */
979 GNUNET_free_non_null (d->hello);
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
986 d->phase = SP_START_ARMING;
988 /* Check if this is a local or remote process */
989 if (NULL != d->hostname)
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
994 d->cfgfile, d->hostname);
997 if (d->username != NULL)
998 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1000 arg = GNUNET_strdup (d->hostname);
1002 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1010 "-c", d->cfgfile, "-e", "-r", NULL);
1011 /* Use -r to restart arm and all services */
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 "Stopping gnunet-arm with config `%s' locally.\n",
1022 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1027 "-c", d->cfgfile, "-e", "-r", NULL);
1030 GNUNET_free_non_null (del_arg);
1032 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1039 * Stops a GNUnet daemon.
1041 * @param d the daemon that should be stopped
1042 * @param timeout how long to wait for process for shutdown to complete
1043 * @param cb function called once the daemon was stopped
1044 * @param cb_cls closure for cb
1045 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1047 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1048 * GNUNET_NO to kill off and clean up for good
1051 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1052 struct GNUNET_TIME_Relative timeout,
1053 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1054 int delete_files, int allow_restart)
1059 d->dead_cb_cls = cb_cls;
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1065 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
1067 d->dead = GNUNET_YES;
1071 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1073 /* Free what was left from churning! */
1074 GNUNET_assert (d->cfg != NULL);
1075 GNUNET_CONFIGURATION_destroy (d->cfg);
1076 if (delete_files == GNUNET_YES)
1078 if (0 != UNLINK (d->cfgfile))
1080 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1083 GNUNET_free (d->cfgfile);
1084 GNUNET_free_non_null (d->hostname);
1085 GNUNET_free_non_null (d->username);
1086 if (NULL != d->dead_cb)
1087 d->dead_cb (d->dead_cb_cls, NULL);
1093 if (delete_files == GNUNET_YES)
1095 GNUNET_asprintf (&del_arg, "-d");
1098 if (d->phase == SP_CONFIG_UPDATE)
1100 GNUNET_SCHEDULER_cancel (d->task);
1101 d->phase = SP_START_DONE;
1103 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1104 if (d->server != NULL)
1106 GNUNET_CORE_disconnect (d->server);
1110 /* shutdown ARM process (will terminate others) */
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1115 d->phase = SP_SHUTDOWN_START;
1116 d->running = GNUNET_NO;
1117 if (allow_restart == GNUNET_YES)
1118 d->churn = GNUNET_YES;
1121 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1122 GNUNET_TRANSPORT_disconnect (d->th);
1125 /* Check if this is a local or remote process */
1126 if (NULL != d->hostname)
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1131 d->cfgfile, d->hostname);
1134 if (d->username != NULL)
1135 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1137 arg = GNUNET_strdup (d->hostname);
1139 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1147 "-c", d->cfgfile, "-e", "-q",
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1150 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1151 arg, "gnunet-arm", d->cfgfile, del_arg);
1152 /* Use -e to end arm, and -d to remove temp files */
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159 "Stopping gnunet-arm with config `%s' locally.\n",
1162 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1167 "-c", d->cfgfile, "-e", "-q",
1171 GNUNET_free_non_null (del_arg);
1172 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1173 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1178 * Changes the configuration of a GNUnet daemon.
1180 * @param d the daemon that should be modified
1181 * @param cfg the new configuration for the daemon
1182 * @param cb function called once the configuration was changed
1183 * @param cb_cls closure for cb
1186 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1187 struct GNUNET_CONFIGURATION_Handle *cfg,
1188 GNUNET_TESTING_NotifyCompletion cb,
1193 if (d->phase != SP_START_DONE)
1198 ("Peer not yet running, can not change configuration at this point."));
1202 /* 1) write configuration to temporary file */
1203 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1206 cb (cb_cls, _("Failed to write new configuration to disk."));
1210 /* 2) copy file to remote host (if necessary) */
1211 if (NULL == d->hostname)
1213 /* signal success */
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1220 "Copying updated configuration file to remote host `%s'.\n",
1223 d->phase = SP_CONFIG_UPDATE;
1224 if (NULL != d->username)
1225 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1227 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1228 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1232 d->cfgfile, arg, NULL);
1234 if (NULL == d->proc)
1236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1238 ("Could not start `%s' process to copy configuration file.\n"),
1241 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1242 d->phase = SP_START_DONE;
1246 d->update_cb_cls = cb_cls;
1248 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1254 * Data kept for each pair of peers that we try
1257 struct ConnectContext
1260 * Testing handle to the first daemon.
1262 struct GNUNET_TESTING_Daemon *d1;
1265 * Handle to core of first daemon (to check connect)
1267 struct GNUNET_CORE_Handle *d1core;
1270 * Testing handle to the second daemon.
1272 struct GNUNET_TESTING_Daemon *d2;
1275 * Handler for the request to core to connect to this peer.
1277 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1280 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1282 struct GNUNET_TRANSPORT_Handle *d1th;
1285 * Function to call once we are done (or have timed out).
1287 GNUNET_TESTING_NotifyConnection cb;
1295 * When should this operation be complete (or we must trigger
1298 struct GNUNET_TIME_Absolute timeout;
1301 * The relative timeout from whence this connect attempt was
1302 * started. Allows for reconnect attempts.
1304 struct GNUNET_TIME_Relative relative_timeout;
1307 * Maximum number of connect attempts, will retry connection
1308 * this number of times on failures.
1310 unsigned int max_connect_attempts;
1313 * Hello timeout task
1315 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1318 * Connect timeout task
1320 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1323 * When should this operation be complete (or we must trigger
1326 struct GNUNET_TIME_Relative timeout_hello;
1329 * Was the connection attempt successful?
1334 * The distance between the two connected peers
1340 /** Forward declaration **/
1342 reattempt_daemons_connect (void *cls,
1343 const struct GNUNET_SCHEDULER_TaskContext *tc);
1347 * Notify callback about success or failure of the attempt
1348 * to connect the two peers
1350 * @param cls our "struct ConnectContext" (freed)
1351 * @param tc reason tells us if we succeeded or failed
1354 notify_connect_result (void *cls,
1355 const struct GNUNET_SCHEDULER_TaskContext *tc)
1357 struct ConnectContext *ctx = cls;
1358 struct GNUNET_TIME_Relative remaining;
1360 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1361 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1363 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1364 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1367 if (ctx->connect_request_handle != NULL)
1369 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1370 ctx->connect_request_handle = NULL;
1372 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1374 if (ctx->d1th != NULL)
1375 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1377 if (ctx->d1core != NULL)
1378 GNUNET_CORE_disconnect (ctx->d1core);
1380 if (ctx->d2core != NULL)
1381 GNUNET_CORE_disconnect (ctx->d2core);
1390 remaining = GNUNET_TIME_absolute_get_remaining (ctx->timeout);
1392 if (ctx->connected == GNUNET_YES)
1394 if (ctx->cb != NULL)
1396 ctx->cb (ctx->cb_cls,
1400 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1403 else if (remaining.rel_value > 0)
1405 if (ctx->d1core != NULL)
1407 GNUNET_CORE_disconnect (ctx->d1core);
1411 if (ctx->d2core != NULL)
1413 GNUNET_CORE_disconnect (ctx->d2core);
1418 if (ctx->d1th != NULL)
1420 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1423 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1428 if (ctx->cb != NULL)
1430 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1431 ctx->d2->cfg, ctx->d1, ctx->d2,
1432 _("Peers failed to connect"));
1436 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1438 GNUNET_CORE_disconnect (ctx->d1core);
1445 * Success, connection is up. Signal client our success.
1447 * @param cls our "struct ConnectContext"
1448 * @param peer identity of the peer that has connected
1449 * @param atsi performance information
1453 connect_notify (void *cls,
1454 const struct GNUNET_PeerIdentity *peer,
1455 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1457 struct ConnectContext *ctx = cls;
1459 if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
1461 ctx->connected = GNUNET_YES;
1462 ctx->distance = 0; /* FIXME: distance */
1463 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1464 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1471 * Success, connection is up. Signal client our success.
1473 * @param cls our "struct ConnectContext"
1474 * @param peer identity of the peer that has connected
1475 * @param atsi performance information
1479 connect_notify_core2 (void *cls,
1480 const struct GNUNET_PeerIdentity *peer,
1481 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1483 struct ConnectContext *ctx = cls;
1485 if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
1487 ctx->connected = GNUNET_YES;
1488 ctx->distance = 0; /* FIXME: distance */
1489 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1490 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1498 * Task called once a core connect request has been transmitted.
1500 * @param cls struct ConnectContext
1501 * @param tc context information (why was this task triggered now)
1504 core_connect_request_cont (void *cls,
1505 const struct GNUNET_SCHEDULER_TaskContext *tc)
1507 struct ConnectContext *ctx = cls;
1509 if (tc->reason == GNUNET_SCHEDULER_REASON_PREREQ_DONE)
1510 ctx->connect_request_handle = NULL;
1515 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1517 struct ConnectContext *ctx = cls;
1518 struct GNUNET_MessageHeader *hello;
1519 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1520 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1522 if ((ctx->d2->hello != NULL)
1523 && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)))
1525 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1526 GNUNET_assert (hello != NULL);
1527 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello);
1528 GNUNET_assert (ctx->d1core != NULL);
1529 ctx->connect_request_handle =
1530 GNUNET_CORE_peer_request_connect (ctx->d1core,
1531 GNUNET_TIME_relative_divide
1532 (ctx->relative_timeout,
1533 ctx->max_connect_attempts + 1),
1535 &core_connect_request_cont, ctx);
1537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1538 "Sending connect request to CORE of %s for peer %s\n",
1539 GNUNET_i2s (&ctx->d1->id),
1540 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1542 ctx->timeout_hello =
1543 GNUNET_TIME_relative_add (ctx->timeout_hello,
1544 GNUNET_TIME_relative_multiply
1545 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1547 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello,
1552 * Establish a connection between two GNUnet daemons.
1554 * @param d1 handle for the first daemon
1555 * @param d2 handle for the second daemon
1556 * @param timeout how long is the connection attempt
1558 * @param max_connect_attempts how many times should we try to reconnect
1560 * @param cb function to call at the end
1561 * @param cb_cls closure for cb
1564 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1565 struct GNUNET_TESTING_Daemon *d2,
1566 struct GNUNET_TIME_Relative timeout,
1567 unsigned int max_connect_attempts,
1568 GNUNET_TESTING_NotifyConnection cb,
1571 struct ConnectContext *ctx;
1573 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1576 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1577 _("Peers are not fully running yet, can not connect!\n"));
1580 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1583 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1584 ctx->timeout_hello =
1585 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
1586 ctx->relative_timeout = timeout;
1588 ctx->cb_cls = cb_cls;
1589 ctx->max_connect_attempts = max_connect_attempts;
1590 ctx->connected = GNUNET_NO;
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1593 "Asked to connect peer %s to peer %s\n",
1594 d1->shortname, d2->shortname);
1597 ctx->d1core = GNUNET_CORE_connect (d1->cfg, 1,
1600 &connect_notify, NULL, NULL,
1602 NULL, GNUNET_NO, no_handlers);
1603 if (ctx->d1core == NULL)
1607 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1608 _("Failed to connect to core service of first peer!\n"));
1613 ctx->d2core = GNUNET_CORE_connect (d2->cfg, 1,
1614 #if NO_MORE_TIMEOUT_FIXME
1621 NULL, GNUNET_NO, no_handlers);
1622 if (ctx->d2core == NULL)
1626 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1627 _("Failed to connect to core service of second peer!\n"));
1632 #if DEBUG_TESTING > 2
1633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1634 "Asked to connect peer %s to peer %s\n",
1635 d1->shortname, d2->shortname);
1636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1637 "Connecting to transport service of peer %s\n", d2->shortname);
1641 ctx->d1th = GNUNET_TRANSPORT_connect (d1->cfg,
1642 &d1->id, d1, NULL, NULL, NULL);
1643 if (ctx->d1th == NULL)
1645 GNUNET_CORE_disconnect (ctx->d1core);
1648 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1649 _("Failed to connect to transport service!\n"));
1654 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
1655 (ctx->relative_timeout,
1656 ctx->max_connect_attempts),
1657 ¬ify_connect_result, ctx);
1659 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1663 reattempt_daemons_connect (void *cls,
1664 const struct GNUNET_SCHEDULER_TaskContext *tc)
1667 struct ConnectContext *ctx = cls;
1668 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1672 #if DEBUG_TESTING_RECONNECT
1673 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1674 "re-attempting connect of peer %s to peer %s\n",
1675 ctx->d1->shortname, ctx->d2->shortname);
1678 GNUNET_assert (ctx->d1core == NULL);
1680 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1681 #if NO_MORE_TIMEOUT_FIXME
1682 GNUNET_TIME_absolute_get_remaining
1687 &connect_notify, NULL, NULL,
1689 NULL, GNUNET_NO, no_handlers);
1690 if (ctx->d1core == NULL)
1692 if (NULL != ctx->cb)
1693 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1694 ctx->d2->cfg, ctx->d1, ctx->d2,
1695 _("Failed to connect to core service of first peer!\n"));
1700 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1702 ctx->d1, NULL, NULL, NULL);
1703 if (ctx->d1th == NULL)
1705 GNUNET_CORE_disconnect (ctx->d1core);
1707 if (NULL != ctx->cb)
1708 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1709 ctx->d2->cfg, ctx->d1, ctx->d2,
1710 _("Failed to connect to transport service!\n"));
1715 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
1716 (ctx->relative_timeout,
1717 ctx->max_connect_attempts),
1718 ¬ify_connect_result, ctx);
1720 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1723 /* end of testing.c */