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 if (d->ssh_port_str == NULL)
288 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
295 "-c", d->cfgfile, "-sq", NULL);
299 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
300 "ssh", "-p", d->ssh_port_str,
306 "-c", d->cfgfile, "-sq", NULL);
308 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
314 _("Could not start `%s' process to create hostkey.\n"),
315 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
323 (NULL == d->hostname)
324 ? _("Failed to start `gnunet-peerinfo' process.\n")
325 : _("Failed to start `ssh' process.\n"));
326 GNUNET_DISK_pipe_close(d->pipe_stdout);
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "Started `%s', waiting for hostkey.\n",
334 d->phase = SP_HOSTKEY_CREATE;
336 = GNUNET_SCHEDULER_add_read_file (d->sched,
337 GNUNET_TIME_absolute_get_remaining(d->max_timeout),
338 GNUNET_DISK_pipe_handle(d->pipe_stdout,
339 GNUNET_DISK_PIPE_END_READ),
343 case SP_HOSTKEY_CREATE:
344 bytes_read = GNUNET_DISK_file_read(GNUNET_DISK_pipe_handle(d->pipe_stdout,
345 GNUNET_DISK_PIPE_END_READ),
346 &d->hostkeybuf[d->hostkeybufpos],
347 sizeof(d->hostkeybuf) - d->hostkeybufpos);
349 d->hostkeybufpos += bytes_read;
351 if ( (d->hostkeybufpos < 104) &&
356 = GNUNET_SCHEDULER_add_read_file (d->sched,
357 GNUNET_TIME_absolute_get_remaining(d->max_timeout),
358 GNUNET_DISK_pipe_handle(d->pipe_stdout,
359 GNUNET_DISK_PIPE_END_READ),
364 d->hostkeybuf[103] = '\0';
365 if ( (bytes_read < 0) ||
366 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
367 &d->id.hashPubKey)) )
371 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
372 _("Error reading from gnunet-peerinfo: %s\n"),
375 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
376 _("Malformed output from gnunet-peerinfo!\n"));
379 GNUNET_DISK_pipe_close(d->pipe_stdout);
380 d->pipe_stdout = NULL;
381 (void) PLIBC_KILL (d->pid, SIGKILL);
382 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
389 _("`Failed to get hostkey!\n"));
392 GNUNET_DISK_pipe_close(d->pipe_stdout);
393 d->pipe_stdout = NULL;
394 (void) PLIBC_KILL (d->pid, SIGKILL);
395 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Successfully got hostkey!\n");
401 if (d->hostkey_callback != NULL)
403 d->hostkey_callback(d->hostkey_cls, &d->id, d, NULL);
404 d->phase = SP_HOSTKEY_CREATED;
408 d->phase = SP_TOPOLOGY_SETUP;
411 case SP_HOSTKEY_CREATED:
412 /* wait for topology finished */
413 if ((GNUNET_YES == d->dead) || (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0))
422 _("`Failed while waiting for topology setup!\n"));
427 = GNUNET_SCHEDULER_add_delayed (d->sched,
428 GNUNET_CONSTANTS_EXEC_WAIT,
431 case SP_TOPOLOGY_SETUP:
432 /* start GNUnet on remote host */
433 if (NULL == d->hostname)
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
438 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
442 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
452 if (d->username != NULL)
453 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
455 dst = GNUNET_strdup (d->hostname);
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
460 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
461 "-L", "DEBUG", "-s", "-q");
463 if (d->ssh_port_str == NULL)
465 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
475 "-c", d->cfgfile, "-s", "-q", NULL);
480 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
481 "ssh", "-p", d->ssh_port_str,
490 "-c", d->cfgfile, "-s", "-q", NULL);
496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
497 _("Could not start `%s' process to start GNUnet.\n"),
498 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
506 (NULL == d->hostname)
507 ? _("Failed to start `gnunet-arm' process.\n")
508 : _("Failed to start `ssh' process.\n"));
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 "Started `%s', waiting for `%s' to be up.\n",
514 "gnunet-arm", "gnunet-service-core");
516 d->phase = SP_START_ARMING;
518 = GNUNET_SCHEDULER_add_delayed (d->sched,
519 GNUNET_CONSTANTS_EXEC_WAIT,
522 case SP_START_ARMING:
523 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
525 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
534 (NULL == d->hostname)
535 ? _("`gnunet-arm' does not seem to terminate.\n")
536 : _("`ssh' does not seem to terminate.\n"));
541 = GNUNET_SCHEDULER_add_delayed (d->sched,
542 GNUNET_CONSTANTS_EXEC_WAIT,
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548 "Successfully started `%s'.\n", "gnunet-arm");
550 d->phase = SP_START_CORE;
551 d->server = GNUNET_CORE_connect (d->sched,
558 NULL, GNUNET_NO, no_handlers);
566 case SP_SHUTDOWN_START:
567 /* confirm copying complete */
568 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
570 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
572 if (NULL != d->dead_cb)
573 d->dead_cb (d->dead_cb_cls,
574 _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
577 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
578 GNUNET_TRANSPORT_disconnect(d->th);
581 GNUNET_CONFIGURATION_destroy (d->cfg);
582 GNUNET_free (d->cfgfile);
583 GNUNET_free_non_null(d->hello);
584 GNUNET_free_non_null (d->hostname);
585 GNUNET_free_non_null (d->username);
586 GNUNET_free_non_null (d->shortname);
592 = GNUNET_SCHEDULER_add_delayed (d->sched,
593 GNUNET_CONSTANTS_EXEC_WAIT,
597 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
599 if (NULL != d->dead_cb)
600 d->dead_cb (d->dead_cb_cls,
601 _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
604 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
605 GNUNET_TRANSPORT_disconnect(d->th);
608 GNUNET_CONFIGURATION_destroy (d->cfg);
609 GNUNET_free (d->cfgfile);
610 GNUNET_free_non_null(d->hello);
611 GNUNET_free_non_null (d->hostname);
612 GNUNET_free_non_null (d->username);
613 GNUNET_free_non_null (d->shortname);
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
622 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
623 GNUNET_TRANSPORT_disconnect(d->th);
626 /* state clean up and notifications */
627 if (d->churn == GNUNET_NO)
629 GNUNET_CONFIGURATION_destroy (d->cfg);
630 GNUNET_free (d->cfgfile);
631 GNUNET_free_non_null (d->hostname);
632 GNUNET_free_non_null (d->username);
635 GNUNET_free_non_null(d->hello);
637 GNUNET_free_non_null (d->shortname);
639 if (NULL != d->dead_cb)
640 d->dead_cb (d->dead_cb_cls, NULL);
642 if (d->churn == GNUNET_NO)
646 case SP_CONFIG_UPDATE:
647 /* confirm copying complete */
648 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
650 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0) /* FIXME: config update should take timeout parameter! */
657 d->cfg, d, _("`scp' does not seem to terminate.\n"));
662 = GNUNET_SCHEDULER_add_delayed (d->sched,
663 GNUNET_CONSTANTS_EXEC_WAIT,
667 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
669 if (NULL != d->update_cb)
670 d->update_cb (d->update_cb_cls,
671 _("`scp' did not complete cleanly.\n"));
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Successfully copied configuration file.\n");
678 if (NULL != d->update_cb)
679 d->update_cb (d->update_cb_cls, NULL);
680 d->phase = SP_START_DONE;
686 * Continues GNUnet daemon startup when user wanted to be notified
687 * once a hostkey was generated (for creating friends files, blacklists,
690 * @param daemon the daemon to finish starting
693 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon)
695 GNUNET_assert(daemon->phase == SP_HOSTKEY_CREATED);
696 daemon->phase = SP_TOPOLOGY_SETUP;
701 * Start a peer that has previously been stopped using the daemon_stop
702 * call (and files weren't deleted and the allow restart flag)
704 * @param daemon the daemon to start (has been previously stopped)
705 * @param timeout how long to wait for restart
706 * @param cb the callback for notification when the peer is running
707 * @param cb_cls closure for the callback
710 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
711 struct GNUNET_TIME_Relative timeout,
712 GNUNET_TESTING_NotifyDaemonRunning cb,
715 if (daemon->running == GNUNET_YES)
717 cb(cb_cls, &daemon->id, daemon->cfg, daemon, "Daemon already running, can't restart!");
722 daemon->cb_cls = cb_cls;
723 daemon->phase = SP_TOPOLOGY_SETUP;
724 daemon->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
726 GNUNET_SCHEDULER_add_continuation (daemon->sched,
729 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
733 * Starts a GNUnet daemon. GNUnet must be installed on the target
734 * system and available in the PATH. The machine must furthermore be
735 * reachable via "ssh" (unless the hostname is "NULL") without the
736 * need to enter a password.
738 * @param sched scheduler to use
739 * @param cfg configuration to use
740 * @param timeout how long to wait starting up peers
741 * @param hostname name of the machine where to run GNUnet
742 * (use NULL for localhost).
743 * @param ssh_username ssh username to use when connecting to hostname
744 * @param sshport port to pass to ssh process when connecting to hostname
745 * @param hostkey_callback function to call once the hostkey has been
746 * generated for this peer, but it hasn't yet been started
747 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
748 * @param hostkey_cls closure for hostkey callback
749 * @param cb function to call once peer is up, or failed to start
750 * @param cb_cls closure for cb
751 * @return handle to the daemon (actual start will be completed asynchronously)
753 struct GNUNET_TESTING_Daemon *
754 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
755 const struct GNUNET_CONFIGURATION_Handle *cfg,
756 struct GNUNET_TIME_Relative timeout,
757 const char *hostname,
758 const char *ssh_username,
760 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
762 GNUNET_TESTING_NotifyDaemonRunning cb,
765 struct GNUNET_TESTING_Daemon *ret;
769 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
771 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
774 GNUNET_asprintf(&ret->ssh_port_str, "%d", sshport);
777 ret->ssh_port_str = NULL;
778 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781 "Setting up peer with configuration file `%s'.\n",
784 if (NULL == ret->cfgfile)
786 GNUNET_free_non_null (ret->hostname);
790 ret->hostkey_callback = hostkey_callback;
791 ret->hostkey_cls = hostkey_cls;
793 ret->cb_cls = cb_cls;
794 ret->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
795 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
796 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
798 "DEFAULTCONFIG", ret->cfgfile);
799 /* 1) write configuration to temporary file */
800 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
802 if (0 != UNLINK (ret->cfgfile))
803 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
804 "unlink", ret->cfgfile);
805 GNUNET_CONFIGURATION_destroy (ret->cfg);
806 GNUNET_free_non_null (ret->hostname);
807 GNUNET_free (ret->cfgfile);
811 if (ssh_username != NULL)
812 username = GNUNET_strdup(ssh_username);
813 if ((ssh_username == NULL) && (GNUNET_OK !=
814 GNUNET_CONFIGURATION_get_value_string (cfg,
816 "USERNAME", &username)))
818 if (NULL != getenv ("USER"))
819 username = GNUNET_strdup (getenv ("USER"));
823 ret->username = username;
825 /* 2) copy file to remote host */
826 if (NULL != hostname)
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
830 "Copying configuration file to host `%s'.\n", hostname);
832 ret->phase = SP_COPYING;
833 if (NULL != username)
834 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
836 GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
838 if (ret->ssh_port_str == NULL)
840 ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
845 ret->cfgfile, arg, NULL);
849 ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
850 "scp", "-P", ret->ssh_port_str,
854 ret->cfgfile, arg, NULL);
859 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
861 ("Could not start `%s' process to copy configuration file.\n"),
863 if (0 != UNLINK (ret->cfgfile))
864 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
865 "unlink", ret->cfgfile);
866 GNUNET_CONFIGURATION_destroy (ret->cfg);
867 GNUNET_free_non_null (ret->hostname);
868 GNUNET_free_non_null (ret->username);
869 GNUNET_free (ret->cfgfile);
874 = GNUNET_SCHEDULER_add_delayed (sched,
875 GNUNET_CONSTANTS_EXEC_WAIT,
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "No need to copy configuration file since we are running locally.\n");
883 ret->phase = SP_COPIED;
884 GNUNET_SCHEDULER_add_continuation (sched,
887 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
893 * Restart (stop and start) a GNUnet daemon.
895 * @param d the daemon that should be restarted
896 * @param cb function called once the daemon is (re)started
897 * @param cb_cls closure for cb
900 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
901 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls)
909 d->dead = GNUNET_YES;
916 if (d->phase == SP_CONFIG_UPDATE)
918 GNUNET_SCHEDULER_cancel (d->sched, d->task);
919 d->phase = SP_START_DONE;
921 if (d->server != NULL)
923 GNUNET_CORE_disconnect (d->server);
929 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
930 GNUNET_TRANSPORT_disconnect(d->th);
933 /* state clean up and notifications */
934 GNUNET_free_non_null(d->hello);
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
941 d->phase = SP_START_ARMING;
943 /* Check if this is a local or remote process */
944 if (NULL != d->hostname)
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
951 if (d->username != NULL)
952 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
954 arg = GNUNET_strdup (d->hostname);
956 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
964 "-c", d->cfgfile, "-e", "-r", NULL);
965 /* Use -r to restart arm and all services */
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
975 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
980 "-c", d->cfgfile, "-e", "-r", NULL);
983 GNUNET_free_non_null(del_arg);
985 = GNUNET_SCHEDULER_add_delayed (d->sched,
986 GNUNET_CONSTANTS_EXEC_WAIT,
993 * Stops a GNUnet daemon.
995 * @param d the daemon that should be stopped
996 * @param timeout how long to wait for process for shutdown to complete
997 * @param cb function called once the daemon was stopped
998 * @param cb_cls closure for cb
999 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1001 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1002 * GNUNET_NO to kill off and clean up for good
1005 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1006 struct GNUNET_TIME_Relative timeout,
1007 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1014 d->dead_cb_cls = cb_cls;
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1020 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
1022 d->dead = GNUNET_YES;
1026 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1028 /* Free what was left from churning! */
1029 GNUNET_assert(d->cfg != NULL);
1030 GNUNET_CONFIGURATION_destroy (d->cfg);
1031 if (delete_files == GNUNET_YES)
1033 if (0 != UNLINK(d->cfgfile))
1035 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "unlink");
1038 GNUNET_free (d->cfgfile);
1039 GNUNET_free_non_null (d->hostname);
1040 GNUNET_free_non_null (d->username);
1041 if (NULL != d->dead_cb)
1042 d->dead_cb (d->dead_cb_cls, NULL);
1048 if (delete_files == GNUNET_YES)
1050 GNUNET_asprintf(&del_arg, "-d");
1053 if (d->phase == SP_CONFIG_UPDATE)
1055 GNUNET_SCHEDULER_cancel (d->sched, d->task);
1056 d->phase = SP_START_DONE;
1058 if (d->server != NULL)
1060 GNUNET_CORE_disconnect (d->server);
1063 /* shutdown ARM process (will terminate others) */
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1068 d->phase = SP_SHUTDOWN_START;
1069 d->running = GNUNET_NO;
1070 if (allow_restart == GNUNET_YES)
1071 d->churn = GNUNET_YES;
1074 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
1075 GNUNET_TRANSPORT_disconnect(d->th);
1078 /* Check if this is a local or remote process */
1079 if (NULL != d->hostname)
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
1086 if (d->username != NULL)
1087 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1089 arg = GNUNET_strdup (d->hostname);
1091 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1099 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1101 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n", arg, "gnunet-arm", d->cfgfile, del_arg);
1102 /* Use -e to end arm, and -d to remove temp files */
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1109 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1111 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1116 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1119 GNUNET_free_non_null(del_arg);
1120 d->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
1122 = GNUNET_SCHEDULER_add_now (d->sched,
1128 * Changes the configuration of a GNUnet daemon.
1130 * @param d the daemon that should be modified
1131 * @param cfg the new configuration for the daemon
1132 * @param cb function called once the configuration was changed
1133 * @param cb_cls closure for cb
1136 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1137 struct GNUNET_CONFIGURATION_Handle *cfg,
1138 GNUNET_TESTING_NotifyCompletion cb,
1143 if (d->phase != SP_START_DONE)
1148 ("Peer not yet running, can not change configuration at this point."));
1152 /* 1) write configuration to temporary file */
1153 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1156 cb (cb_cls, _("Failed to write new configuration to disk."));
1160 /* 2) copy file to remote host (if necessary) */
1161 if (NULL == d->hostname)
1163 /* signal success */
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Copying updated configuration file to remote host `%s'.\n",
1173 d->phase = SP_CONFIG_UPDATE;
1174 if (NULL != d->username)
1175 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1177 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1178 d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1182 d->cfgfile, arg, NULL);
1186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1188 ("Could not start `%s' process to copy configuration file.\n"),
1191 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1192 d->phase = SP_START_DONE;
1196 d->update_cb_cls = cb_cls;
1198 = GNUNET_SCHEDULER_add_delayed (d->sched,
1199 GNUNET_CONSTANTS_EXEC_WAIT,
1205 * Data kept for each pair of peers that we try
1208 struct ConnectContext
1211 * Testing handle to the first daemon.
1213 struct GNUNET_TESTING_Daemon *d1;
1216 * Handle to core of first daemon (to check connect)
1218 struct GNUNET_CORE_Handle * d1core;
1221 * Testing handle to the second daemon.
1223 struct GNUNET_TESTING_Daemon *d2;
1226 * Handler for the request to core to connect to this peer.
1228 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1231 * Transport handle to the second daemon.
1233 struct GNUNET_TRANSPORT_Handle *d2th;
1236 * Function to call once we are done (or have timed out).
1238 GNUNET_TESTING_NotifyConnection cb;
1246 * When should this operation be complete (or we must trigger
1249 struct GNUNET_TIME_Absolute timeout;
1252 * The relative timeout from whence this connect attempt was
1253 * started. Allows for reconnect attempts.
1255 struct GNUNET_TIME_Relative relative_timeout;
1258 * Maximum number of connect attempts, will retry connection
1259 * this number of times on failures.
1261 unsigned int max_connect_attempts;
1264 * Hello timeout task
1266 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1269 * Connect timeout task
1271 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1274 * When should this operation be complete (or we must trigger
1277 struct GNUNET_TIME_Relative timeout_hello;
1280 * Was the connection attempt successful?
1285 * The distance between the two connected peers
1291 /** Forward declaration **/
1293 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1297 * Notify callback about success or failure of the attempt
1298 * to connect the two peers
1300 * @param cls our "struct ConnectContext" (freed)
1301 * @param tc reason tells us if we succeeded or failed
1304 notify_connect_result (void *cls,
1305 const struct GNUNET_SCHEDULER_TaskContext *tc)
1307 struct ConnectContext *ctx = cls;
1308 struct GNUNET_TIME_Relative remaining;
1310 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1311 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1313 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
1314 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1317 if (ctx->connect_request_handle != NULL)
1319 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1320 ctx->connect_request_handle = NULL;
1322 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1324 if (ctx->d2th != NULL)
1325 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1327 if (ctx->d1core != NULL)
1328 GNUNET_CORE_disconnect (ctx->d1core);
1330 if (ctx->d2core != NULL)
1331 GNUNET_CORE_disconnect (ctx->d2core);
1340 remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
1342 if (ctx->connected == GNUNET_YES)
1344 if (ctx->cb != NULL)
1346 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance, ctx->d1->cfg,
1347 ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1350 else if (remaining.value > 0)
1352 if (ctx->d1core != NULL)
1354 GNUNET_CORE_disconnect(ctx->d1core);
1358 if (ctx->d2core != NULL)
1360 GNUNET_CORE_disconnect(ctx->d2core);
1365 if (ctx->d2th != NULL)
1367 GNUNET_TRANSPORT_disconnect(ctx->d2th);
1370 GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
1375 if (ctx->cb != NULL)
1377 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1378 ctx->d2->cfg, ctx->d1, ctx->d2,
1379 _("Peers failed to connect"));
1383 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1385 GNUNET_CORE_disconnect (ctx->d1core);
1392 * Success, connection is up. Signal client our success.
1394 * @param cls our "struct ConnectContext"
1395 * @param peer identity of the peer that has connected
1396 * @param latency the round trip latency of the connection to this peer
1397 * @param distance distance the transport level distance to this peer
1401 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1404 struct ConnectContext *ctx = cls;
1406 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1408 ctx->connected = GNUNET_YES;
1409 ctx->distance = distance;
1410 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1411 ctx->timeout_task = GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1412 ¬ify_connect_result,
1420 * Success, connection is up. Signal client our success.
1422 * @param cls our "struct ConnectContext"
1423 * @param peer identity of the peer that has connected
1424 * @param latency the round trip latency of the connection to this peer
1425 * @param distance distance the transport level distance to this peer
1429 connect_notify_core2 (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1432 struct ConnectContext *ctx = cls;
1434 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1436 ctx->connected = GNUNET_YES;
1437 ctx->distance = distance;
1438 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1439 ctx->timeout_task = GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1440 ¬ify_connect_result,
1448 * Task called once a core connect request has been transmitted.
1450 * @param cls struct ConnectContext
1451 * @param tc context information (why was this task triggered now)
1453 void core_connect_request_cont (void *cls,
1455 GNUNET_SCHEDULER_TaskContext * tc)
1457 struct ConnectContext *ctx = cls;
1459 if (tc->reason == GNUNET_SCHEDULER_REASON_PREREQ_DONE)
1460 ctx->connect_request_handle = NULL;
1465 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1467 struct ConnectContext *ctx = cls;
1468 struct GNUNET_MessageHeader *hello;
1469 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1470 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1472 if ((ctx->d1->hello != NULL) && (NULL != GNUNET_HELLO_get_header(ctx->d1->hello)))
1474 hello = GNUNET_HELLO_get_header(ctx->d1->hello);
1475 GNUNET_assert(hello != NULL);
1476 GNUNET_TRANSPORT_offer_hello (ctx->d2th, hello);
1478 ctx->connect_request_handle = GNUNET_CORE_peer_request_connect (ctx->d1->sched,
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->d1->sched,
1496 * Establish a connection between two GNUnet daemons.
1498 * @param d1 handle for the first daemon
1499 * @param d2 handle for the second daemon
1500 * @param timeout how long is the connection attempt
1502 * @param max_connect_attempts how many times should we try to reconnect
1504 * @param cb function to call at the end
1505 * @param cb_cls closure for cb
1508 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1509 struct GNUNET_TESTING_Daemon *d2,
1510 struct GNUNET_TIME_Relative timeout,
1511 unsigned int max_connect_attempts,
1512 GNUNET_TESTING_NotifyConnection cb,
1515 struct ConnectContext *ctx;
1517 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1520 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1521 _("Peers are not fully running yet, can not connect!\n"));
1524 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1527 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1528 ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
1529 ctx->relative_timeout = timeout;
1531 ctx->cb_cls = cb_cls;
1532 ctx->max_connect_attempts = max_connect_attempts;
1533 ctx->connected = GNUNET_NO;
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1536 "Asked to connect peer %s to peer %s\n",
1537 d1->shortname, d2->shortname);
1540 ctx->d1core = GNUNET_CORE_connect (d1->sched,
1545 &connect_notify, NULL, NULL,
1547 NULL, GNUNET_NO, no_handlers);
1548 if (ctx->d1core == NULL)
1552 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1553 _("Failed to connect to core service of first peer!\n"));
1558 ctx->d2core = GNUNET_CORE_connect (d2->sched,
1565 NULL, GNUNET_NO, no_handlers);
1566 if (ctx->d2core == NULL)
1570 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1571 _("Failed to connect to core service of second peer!\n"));
1576 #if DEBUG_TESTING > 2
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1578 "Asked to connect peer %s to peer %s\n",
1579 d1->shortname, d2->shortname);
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1581 "Connecting to transport service of peer %s\n", d2->shortname);
1585 ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
1588 d2, NULL, NULL, NULL);
1589 if (ctx->d2th == NULL)
1591 GNUNET_CORE_disconnect(ctx->d1core);
1594 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1595 _("Failed to connect to transport service!\n"));
1599 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
1600 GNUNET_TIME_relative_divide(ctx->relative_timeout,
1601 ctx->max_connect_attempts),
1602 ¬ify_connect_result, ctx);
1604 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1608 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1611 struct ConnectContext *ctx = cls;
1612 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1616 #if DEBUG_TESTING_RECONNECT
1617 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "re-attempting connect of peer %s to peer %s\n",
1618 ctx->d1->shortname, ctx->d2->shortname);
1621 GNUNET_assert(ctx->d1core == NULL);
1623 ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1625 GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1628 &connect_notify, NULL, NULL,
1630 NULL, GNUNET_NO, no_handlers);
1631 if (ctx->d1core == NULL)
1633 if (NULL != ctx->cb)
1634 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1635 _("Failed to connect to core service of first peer!\n"));
1640 ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1643 ctx->d2, NULL, NULL, NULL);
1644 if (ctx->d2th == NULL)
1646 GNUNET_CORE_disconnect(ctx->d1core);
1648 if (NULL != ctx->cb)
1649 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1650 _("Failed to connect to transport service!\n"));
1654 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1655 GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1656 ¬ify_connect_result, ctx);
1658 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1661 /* end of testing.c */