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->cfg,
165 d, NULL, NULL, NULL);
168 if (GNUNET_YES == d->dead)
169 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
170 else if (NULL != d->cb)
171 d->cb (d->cb_cls, &d->id, d->cfg, d,
172 _("Failed to connect to transport service!\n"));
176 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
181 * Finite-state machine for starting GNUnet.
183 * @param cls our "struct GNUNET_TESTING_Daemon"
187 start_fsm (void *cls,
188 const struct GNUNET_SCHEDULER_TaskContext *tc)
190 struct GNUNET_TESTING_Daemon *d = cls;
191 GNUNET_TESTING_NotifyDaemonRunning cb;
192 enum GNUNET_OS_ProcessStatusType type;
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199 "Peer FSM is in phase %u.\n", d->phase);
202 d->task = GNUNET_SCHEDULER_NO_TASK;
206 /* confirm copying complete */
207 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
209 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).rel_value == 0)
216 d->cfg, d, _("`scp' does not seem to terminate (timeout copying config).\n"));
221 = GNUNET_SCHEDULER_add_delayed (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, GNUNET_NO, GNUNET_YES);
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->proc = 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 if (d->ssh_port_str == NULL)
286 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
293 "-c", d->cfgfile, "-sq", NULL);
297 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
298 "ssh", "-p", d->ssh_port_str,
304 "-c", d->cfgfile, "-sq", NULL);
306 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
312 _("Could not start `%s' process to create hostkey.\n"),
313 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
321 (NULL == d->hostname)
322 ? _("Failed to start `gnunet-peerinfo' process.\n")
323 : _("Failed to start `ssh' process.\n"));
324 GNUNET_DISK_pipe_close(d->pipe_stdout);
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329 "Started `%s', waiting for hostkey.\n",
332 d->phase = SP_HOSTKEY_CREATE;
334 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining(d->max_timeout),
335 GNUNET_DISK_pipe_handle(d->pipe_stdout,
336 GNUNET_DISK_PIPE_END_READ),
340 case SP_HOSTKEY_CREATE:
341 bytes_read = GNUNET_DISK_file_read(GNUNET_DISK_pipe_handle(d->pipe_stdout,
342 GNUNET_DISK_PIPE_END_READ),
343 &d->hostkeybuf[d->hostkeybufpos],
344 sizeof(d->hostkeybuf) - d->hostkeybufpos);
346 d->hostkeybufpos += bytes_read;
348 if ( (d->hostkeybufpos < 104) &&
353 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining(d->max_timeout),
354 GNUNET_DISK_pipe_handle(d->pipe_stdout,
355 GNUNET_DISK_PIPE_END_READ),
360 d->hostkeybuf[103] = '\0';
361 if ( (bytes_read < 0) ||
362 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
363 &d->id.hashPubKey)) )
367 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
368 _("Error reading from gnunet-peerinfo: %s\n"),
371 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
372 _("Malformed output from gnunet-peerinfo!\n"));
375 GNUNET_DISK_pipe_close(d->pipe_stdout);
376 d->pipe_stdout = NULL;
377 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
378 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
379 GNUNET_OS_process_close (d->proc);
386 _("`Failed to get hostkey!\n"));
389 GNUNET_DISK_pipe_close(d->pipe_stdout);
390 d->pipe_stdout = NULL;
391 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
392 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
393 GNUNET_OS_process_close (d->proc);
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "Successfully got hostkey!\n");
399 if (d->hostkey_callback != NULL)
401 d->hostkey_callback(d->hostkey_cls, &d->id, d, NULL);
402 d->phase = SP_HOSTKEY_CREATED;
406 d->phase = SP_TOPOLOGY_SETUP;
409 case SP_HOSTKEY_CREATED:
410 /* wait for topology finished */
411 if ((GNUNET_YES == d->dead) || (GNUNET_TIME_absolute_get_remaining(d->max_timeout).rel_value == 0))
420 _("`Failed while waiting for topology setup!\n"));
425 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
428 case SP_TOPOLOGY_SETUP:
429 /* start GNUnet on remote host */
430 if (NULL == d->hostname)
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
435 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
439 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
449 if (d->username != NULL)
450 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
452 dst = GNUNET_strdup (d->hostname);
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
456 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
457 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
458 "-L", "DEBUG", "-s", "-q");
460 if (d->ssh_port_str == NULL)
462 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
472 "-c", d->cfgfile, "-s", "-q", NULL);
477 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
478 "ssh", "-p", d->ssh_port_str,
487 "-c", d->cfgfile, "-s", "-q", NULL);
493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
494 _("Could not start `%s' process to start GNUnet.\n"),
495 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
503 (NULL == d->hostname)
504 ? _("Failed to start `gnunet-arm' process.\n")
505 : _("Failed to start `ssh' process.\n"));
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Started `%s', waiting for `%s' to be up.\n",
511 "gnunet-arm", "gnunet-service-core");
513 d->phase = SP_START_ARMING;
515 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
518 case SP_START_ARMING:
519 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
521 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).rel_value == 0)
530 (NULL == d->hostname)
531 ? _("`gnunet-arm' does not seem to terminate.\n")
532 : _("`ssh' does not seem to terminate.\n"));
537 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 "Successfully started `%s'.\n", "gnunet-arm");
545 d->phase = SP_START_CORE;
546 d->server = GNUNET_CORE_connect (d->cfg,
552 NULL, GNUNET_NO, no_handlers);
560 case SP_SHUTDOWN_START:
561 /* confirm copying complete */
562 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
564 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).rel_value == 0)
566 if (NULL != d->dead_cb)
567 d->dead_cb (d->dead_cb_cls,
568 _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
571 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
572 GNUNET_TRANSPORT_disconnect(d->th);
575 GNUNET_CONFIGURATION_destroy (d->cfg);
576 GNUNET_free (d->cfgfile);
577 GNUNET_free_non_null(d->hello);
578 GNUNET_free_non_null (d->hostname);
579 GNUNET_free_non_null (d->username);
580 GNUNET_free_non_null (d->shortname);
586 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
590 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
592 if (NULL != d->dead_cb)
593 d->dead_cb (d->dead_cb_cls,
594 _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
597 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
598 GNUNET_TRANSPORT_disconnect(d->th);
601 GNUNET_CONFIGURATION_destroy (d->cfg);
602 GNUNET_free (d->cfgfile);
603 GNUNET_free_non_null(d->hello);
604 GNUNET_free_non_null (d->hostname);
605 GNUNET_free_non_null (d->username);
606 GNUNET_free_non_null (d->shortname);
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
615 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
616 GNUNET_TRANSPORT_disconnect(d->th);
619 /* state clean up and notifications */
620 if (d->churn == GNUNET_NO)
622 GNUNET_CONFIGURATION_destroy (d->cfg);
623 GNUNET_free (d->cfgfile);
624 GNUNET_free_non_null (d->hostname);
625 GNUNET_free_non_null (d->username);
628 GNUNET_free_non_null(d->hello);
630 GNUNET_free_non_null (d->shortname);
632 if (NULL != d->dead_cb)
633 d->dead_cb (d->dead_cb_cls, NULL);
635 if (d->churn == GNUNET_NO)
639 case SP_CONFIG_UPDATE:
640 /* confirm copying complete */
641 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
643 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
650 d->cfg, d, _("`scp' does not seem to terminate.\n"));
655 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
659 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
661 if (NULL != d->update_cb)
662 d->update_cb (d->update_cb_cls,
663 _("`scp' did not complete cleanly.\n"));
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "Successfully copied configuration file.\n");
670 if (NULL != d->update_cb)
671 d->update_cb (d->update_cb_cls, NULL);
672 d->phase = SP_START_DONE;
678 * Continues GNUnet daemon startup when user wanted to be notified
679 * once a hostkey was generated (for creating friends files, blacklists,
682 * @param daemon the daemon to finish starting
685 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon)
687 GNUNET_assert(daemon->phase == SP_HOSTKEY_CREATED);
688 daemon->phase = SP_TOPOLOGY_SETUP;
692 * Check whether the given daemon is running.
694 * @param daemon the daemon to check
696 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
697 * daemon is down, GNUNET_SYSERR on error.
700 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
703 return GNUNET_SYSERR;
705 if (daemon->running == GNUNET_YES)
712 * Start a peer that has previously been stopped using the daemon_stop
713 * call (and files weren't deleted and the allow restart flag)
715 * @param daemon the daemon to start (has been previously stopped)
716 * @param timeout how long to wait for restart
717 * @param cb the callback for notification when the peer is running
718 * @param cb_cls closure for the callback
721 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
722 struct GNUNET_TIME_Relative timeout,
723 GNUNET_TESTING_NotifyDaemonRunning cb,
726 if (daemon->running == GNUNET_YES)
728 cb(cb_cls, &daemon->id, daemon->cfg, daemon, "Daemon already running, can't restart!");
733 daemon->cb_cls = cb_cls;
734 daemon->phase = SP_TOPOLOGY_SETUP;
735 daemon->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
737 GNUNET_SCHEDULER_add_continuation (&start_fsm,
739 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
743 * Starts a GNUnet daemon. GNUnet must be installed on the target
744 * system and available in the PATH. The machine must furthermore be
745 * reachable via "ssh" (unless the hostname is "NULL") without the
746 * need to enter a password.
748 * @param cfg configuration to use
749 * @param timeout how long to wait starting up peers
750 * @param hostname name of the machine where to run GNUnet
751 * (use NULL for localhost).
752 * @param ssh_username ssh username to use when connecting to hostname
753 * @param sshport port to pass to ssh process when connecting to hostname
754 * @param hostkey_callback function to call once the hostkey has been
755 * generated for this peer, but it hasn't yet been started
756 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
757 * @param hostkey_cls closure for hostkey callback
758 * @param cb function to call once peer is up, or failed to start
759 * @param cb_cls closure for cb
760 * @return handle to the daemon (actual start will be completed asynchronously)
762 struct GNUNET_TESTING_Daemon *
763 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
764 struct GNUNET_TIME_Relative timeout,
765 const char *hostname,
766 const char *ssh_username,
768 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
770 GNUNET_TESTING_NotifyDaemonRunning cb,
773 struct GNUNET_TESTING_Daemon *ret;
777 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
778 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
781 GNUNET_asprintf(&ret->ssh_port_str, "%d", sshport);
784 ret->ssh_port_str = NULL;
785 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
788 "Setting up peer with configuration file `%s'.\n",
791 if (NULL == ret->cfgfile)
793 GNUNET_free_non_null (ret->ssh_port_str);
794 GNUNET_free_non_null (ret->hostname);
798 ret->hostkey_callback = hostkey_callback;
799 ret->hostkey_cls = hostkey_cls;
801 ret->cb_cls = cb_cls;
802 ret->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
803 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
804 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
806 "DEFAULTCONFIG", ret->cfgfile);
807 /* 1) write configuration to temporary file */
808 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
810 if (0 != UNLINK (ret->cfgfile))
811 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
812 "unlink", ret->cfgfile);
813 GNUNET_CONFIGURATION_destroy (ret->cfg);
814 GNUNET_free_non_null (ret->hostname);
815 GNUNET_free (ret->cfgfile);
819 if (ssh_username != NULL)
820 username = GNUNET_strdup(ssh_username);
821 if ((ssh_username == NULL) && (GNUNET_OK !=
822 GNUNET_CONFIGURATION_get_value_string (cfg,
824 "USERNAME", &username)))
826 if (NULL != getenv ("USER"))
827 username = GNUNET_strdup (getenv ("USER"));
831 ret->username = username;
833 /* 2) copy file to remote host */
834 if (NULL != hostname)
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Copying configuration file to host `%s'.\n", hostname);
840 ret->phase = SP_COPYING;
841 if (NULL != username)
842 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
844 GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
846 if (ret->ssh_port_str == NULL)
848 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
853 ret->cfgfile, arg, NULL);
857 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
858 "scp", "-P", ret->ssh_port_str,
862 ret->cfgfile, arg, NULL);
865 if (NULL == ret->proc)
867 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
869 ("Could not start `%s' process to copy configuration file.\n"),
871 if (0 != UNLINK (ret->cfgfile))
872 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
873 "unlink", ret->cfgfile);
874 GNUNET_CONFIGURATION_destroy (ret->cfg);
875 GNUNET_free_non_null (ret->hostname);
876 GNUNET_free_non_null (ret->username);
877 GNUNET_free (ret->cfgfile);
882 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 "No need to copy configuration file since we are running locally.\n");
890 ret->phase = SP_COPIED;
891 GNUNET_SCHEDULER_add_continuation (&start_fsm,
893 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
899 * Restart (stop and start) a GNUnet daemon.
901 * @param d the daemon that should be restarted
902 * @param cb function called once the daemon is (re)started
903 * @param cb_cls closure for cb
906 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
907 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls)
915 d->dead = GNUNET_YES;
922 if (d->phase == SP_CONFIG_UPDATE)
924 GNUNET_SCHEDULER_cancel (d->task);
925 d->phase = SP_START_DONE;
927 if (d->server != NULL)
929 GNUNET_CORE_disconnect (d->server);
935 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
936 GNUNET_TRANSPORT_disconnect(d->th);
939 /* state clean up and notifications */
940 GNUNET_free_non_null(d->hello);
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
947 d->phase = SP_START_ARMING;
949 /* Check if this is a local or remote process */
950 if (NULL != d->hostname)
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
954 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
957 if (d->username != NULL)
958 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
960 arg = GNUNET_strdup (d->hostname);
962 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
970 "-c", d->cfgfile, "-e", "-r", NULL);
971 /* Use -r to restart arm and all services */
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
981 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
986 "-c", d->cfgfile, "-e", "-r", NULL);
989 GNUNET_free_non_null(del_arg);
991 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
998 * Stops a GNUnet daemon.
1000 * @param d the daemon that should be stopped
1001 * @param timeout how long to wait for process for shutdown to complete
1002 * @param cb function called once the daemon was stopped
1003 * @param cb_cls closure for cb
1004 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1006 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1007 * GNUNET_NO to kill off and clean up for good
1010 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1011 struct GNUNET_TIME_Relative timeout,
1012 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1019 d->dead_cb_cls = cb_cls;
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
1027 d->dead = GNUNET_YES;
1031 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1033 /* Free what was left from churning! */
1034 GNUNET_assert(d->cfg != NULL);
1035 GNUNET_CONFIGURATION_destroy (d->cfg);
1036 if (delete_files == GNUNET_YES)
1038 if (0 != UNLINK(d->cfgfile))
1040 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "unlink");
1043 GNUNET_free (d->cfgfile);
1044 GNUNET_free_non_null (d->hostname);
1045 GNUNET_free_non_null (d->username);
1046 if (NULL != d->dead_cb)
1047 d->dead_cb (d->dead_cb_cls, NULL);
1053 if (delete_files == GNUNET_YES)
1055 GNUNET_asprintf(&del_arg, "-d");
1058 if (d->phase == SP_CONFIG_UPDATE)
1060 GNUNET_SCHEDULER_cancel (d->task);
1061 d->phase = SP_START_DONE;
1063 if (d->server != NULL)
1065 GNUNET_CORE_disconnect (d->server);
1068 /* shutdown ARM process (will terminate others) */
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1073 d->phase = SP_SHUTDOWN_START;
1074 d->running = GNUNET_NO;
1075 if (allow_restart == GNUNET_YES)
1076 d->churn = GNUNET_YES;
1079 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
1080 GNUNET_TRANSPORT_disconnect(d->th);
1083 /* Check if this is a local or remote process */
1084 if (NULL != d->hostname)
1087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1088 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
1091 if (d->username != NULL)
1092 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1094 arg = GNUNET_strdup (d->hostname);
1096 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1104 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1106 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n", arg, "gnunet-arm", d->cfgfile, del_arg);
1107 /* Use -e to end arm, and -d to remove temp files */
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1114 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1116 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1121 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1124 GNUNET_free_non_null(del_arg);
1125 d->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
1127 = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1132 * Changes the configuration of a GNUnet daemon.
1134 * @param d the daemon that should be modified
1135 * @param cfg the new configuration for the daemon
1136 * @param cb function called once the configuration was changed
1137 * @param cb_cls closure for cb
1140 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1141 struct GNUNET_CONFIGURATION_Handle *cfg,
1142 GNUNET_TESTING_NotifyCompletion cb,
1147 if (d->phase != SP_START_DONE)
1152 ("Peer not yet running, can not change configuration at this point."));
1156 /* 1) write configuration to temporary file */
1157 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1160 cb (cb_cls, _("Failed to write new configuration to disk."));
1164 /* 2) copy file to remote host (if necessary) */
1165 if (NULL == d->hostname)
1167 /* signal success */
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 "Copying updated configuration file to remote host `%s'.\n",
1177 d->phase = SP_CONFIG_UPDATE;
1178 if (NULL != d->username)
1179 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1181 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1182 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1186 d->cfgfile, arg, NULL);
1188 if (NULL == d->proc)
1190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1192 ("Could not start `%s' process to copy configuration file.\n"),
1195 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1196 d->phase = SP_START_DONE;
1200 d->update_cb_cls = cb_cls;
1202 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1208 * Data kept for each pair of peers that we try
1211 struct ConnectContext
1214 * Testing handle to the first daemon.
1216 struct GNUNET_TESTING_Daemon *d1;
1219 * Handle to core of first daemon (to check connect)
1221 struct GNUNET_CORE_Handle * d1core;
1224 * Testing handle to the second daemon.
1226 struct GNUNET_TESTING_Daemon *d2;
1229 * Handler for the request to core to connect to this peer.
1231 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1234 * Transport handle to the second daemon.
1236 struct GNUNET_TRANSPORT_Handle *d2th;
1239 * Function to call once we are done (or have timed out).
1241 GNUNET_TESTING_NotifyConnection cb;
1249 * When should this operation be complete (or we must trigger
1252 struct GNUNET_TIME_Absolute timeout;
1255 * The relative timeout from whence this connect attempt was
1256 * started. Allows for reconnect attempts.
1258 struct GNUNET_TIME_Relative relative_timeout;
1261 * Maximum number of connect attempts, will retry connection
1262 * this number of times on failures.
1264 unsigned int max_connect_attempts;
1267 * Hello timeout task
1269 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1272 * Connect timeout task
1274 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1277 * When should this operation be complete (or we must trigger
1280 struct GNUNET_TIME_Relative timeout_hello;
1283 * Was the connection attempt successful?
1288 * The distance between the two connected peers
1294 /** Forward declaration **/
1296 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1300 * Notify callback about success or failure of the attempt
1301 * to connect the two peers
1303 * @param cls our "struct ConnectContext" (freed)
1304 * @param tc reason tells us if we succeeded or failed
1307 notify_connect_result (void *cls,
1308 const struct GNUNET_SCHEDULER_TaskContext *tc)
1310 struct ConnectContext *ctx = cls;
1311 struct GNUNET_TIME_Relative remaining;
1313 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1314 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1316 GNUNET_SCHEDULER_cancel(ctx->hello_send_task);
1317 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1320 if (ctx->connect_request_handle != NULL)
1322 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1323 ctx->connect_request_handle = NULL;
1325 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1327 if (ctx->d2th != NULL)
1328 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1330 if (ctx->d1core != NULL)
1331 GNUNET_CORE_disconnect (ctx->d1core);
1333 if (ctx->d2core != NULL)
1334 GNUNET_CORE_disconnect (ctx->d2core);
1343 remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
1345 if (ctx->connected == GNUNET_YES)
1347 if (ctx->cb != NULL)
1349 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance, ctx->d1->cfg,
1350 ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1353 else if (remaining.rel_value > 0)
1355 if (ctx->d1core != NULL)
1357 GNUNET_CORE_disconnect(ctx->d1core);
1361 if (ctx->d2core != NULL)
1363 GNUNET_CORE_disconnect(ctx->d2core);
1368 if (ctx->d2th != NULL)
1370 GNUNET_TRANSPORT_disconnect(ctx->d2th);
1373 GNUNET_SCHEDULER_add_now(&reattempt_daemons_connect, ctx);
1378 if (ctx->cb != NULL)
1380 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1381 ctx->d2->cfg, ctx->d1, ctx->d2,
1382 _("Peers failed to connect"));
1386 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1388 GNUNET_CORE_disconnect (ctx->d1core);
1395 * Success, connection is up. Signal client our success.
1397 * @param cls our "struct ConnectContext"
1398 * @param peer identity of the peer that has connected
1399 * @param latency the round trip latency of the connection to this peer
1400 * @param distance distance the transport level distance to this peer
1404 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1407 struct ConnectContext *ctx = cls;
1409 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1411 ctx->connected = GNUNET_YES;
1412 ctx->distance = distance;
1413 GNUNET_SCHEDULER_cancel(ctx->timeout_task);
1414 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1422 * Success, connection is up. Signal client our success.
1424 * @param cls our "struct ConnectContext"
1425 * @param peer identity of the peer that has connected
1426 * @param latency the round trip latency of the connection to this peer
1427 * @param distance distance the transport level distance to this peer
1431 connect_notify_core2 (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1434 struct ConnectContext *ctx = cls;
1436 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1438 ctx->connected = GNUNET_YES;
1439 ctx->distance = distance;
1440 GNUNET_SCHEDULER_cancel(ctx->timeout_task);
1441 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1449 * Task called once a core connect request has been transmitted.
1451 * @param cls struct ConnectContext
1452 * @param tc context information (why was this task triggered now)
1454 void core_connect_request_cont (void *cls,
1456 GNUNET_SCHEDULER_TaskContext * tc)
1458 struct ConnectContext *ctx = cls;
1460 if (tc->reason == GNUNET_SCHEDULER_REASON_PREREQ_DONE)
1461 ctx->connect_request_handle = NULL;
1466 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1468 struct ConnectContext *ctx = cls;
1469 struct GNUNET_MessageHeader *hello;
1470 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1471 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1473 if ((ctx->d1->hello != NULL) && (NULL != GNUNET_HELLO_get_header(ctx->d1->hello)))
1475 hello = GNUNET_HELLO_get_header(ctx->d1->hello);
1476 GNUNET_assert(hello != NULL);
1477 GNUNET_TRANSPORT_offer_hello (ctx->d2th, hello);
1479 ctx->connect_request_handle = GNUNET_CORE_peer_request_connect (ctx->d2->cfg,
1480 GNUNET_TIME_relative_divide(ctx->relative_timeout,
1481 ctx->max_connect_attempts + 1),
1483 &core_connect_request_cont,
1485 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending connect request to core for peer %s\n", GNUNET_i2s(&ctx->d1->id));
1486 ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
1487 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1490 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->timeout_hello,
1495 * Establish a connection between two GNUnet daemons.
1497 * @param d1 handle for the first daemon
1498 * @param d2 handle for the second daemon
1499 * @param timeout how long is the connection attempt
1501 * @param max_connect_attempts how many times should we try to reconnect
1503 * @param cb function to call at the end
1504 * @param cb_cls closure for cb
1507 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1508 struct GNUNET_TESTING_Daemon *d2,
1509 struct GNUNET_TIME_Relative timeout,
1510 unsigned int max_connect_attempts,
1511 GNUNET_TESTING_NotifyConnection cb,
1514 struct ConnectContext *ctx;
1516 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1519 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1520 _("Peers are not fully running yet, can not connect!\n"));
1523 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1526 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1527 ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
1528 ctx->relative_timeout = timeout;
1530 ctx->cb_cls = cb_cls;
1531 ctx->max_connect_attempts = max_connect_attempts;
1532 ctx->connected = GNUNET_NO;
1534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1535 "Asked to connect peer %s to peer %s\n",
1536 d1->shortname, d2->shortname);
1539 ctx->d1core = GNUNET_CORE_connect (d1->cfg,
1543 &connect_notify, NULL, NULL,
1545 NULL, GNUNET_NO, no_handlers);
1546 if (ctx->d1core == NULL)
1550 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1551 _("Failed to connect to core service of first peer!\n"));
1556 ctx->d2core = GNUNET_CORE_connect (d2->cfg,
1562 NULL, GNUNET_NO, no_handlers);
1563 if (ctx->d2core == NULL)
1567 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1568 _("Failed to connect to core service of second peer!\n"));
1573 #if DEBUG_TESTING > 2
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1575 "Asked to connect peer %s to peer %s\n",
1576 d1->shortname, d2->shortname);
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1578 "Connecting to transport service of peer %s\n", d2->shortname);
1582 ctx->d2th = GNUNET_TRANSPORT_connect (d2->cfg,
1584 d2, NULL, NULL, NULL);
1585 if (ctx->d2th == NULL)
1587 GNUNET_CORE_disconnect(ctx->d1core);
1590 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1591 _("Failed to connect to transport service!\n"));
1595 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide(ctx->relative_timeout,
1596 ctx->max_connect_attempts),
1597 ¬ify_connect_result, ctx);
1599 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(&send_hello, ctx);
1603 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1606 struct ConnectContext *ctx = cls;
1607 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1611 #if DEBUG_TESTING_RECONNECT
1612 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "re-attempting connect of peer %s to peer %s\n",
1613 ctx->d1->shortname, ctx->d2->shortname);
1616 GNUNET_assert(ctx->d1core == NULL);
1618 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg,
1619 GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1622 &connect_notify, NULL, NULL,
1624 NULL, GNUNET_NO, no_handlers);
1625 if (ctx->d1core == NULL)
1627 if (NULL != ctx->cb)
1628 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1629 _("Failed to connect to core service of first peer!\n"));
1634 ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->cfg,
1636 ctx->d2, NULL, NULL, NULL);
1637 if (ctx->d2th == NULL)
1639 GNUNET_CORE_disconnect(ctx->d1core);
1641 if (NULL != ctx->cb)
1642 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1643 _("Failed to connect to transport service!\n"));
1647 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1648 ¬ify_connect_result, ctx);
1650 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(&send_hello, ctx);
1653 /* end of testing.c */