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, GNUNET_NO, GNUNET_YES);
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;
700 * Check whether the given daemon is running.
702 * @param daemon the daemon to check
704 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
705 * daemon is down, GNUNET_SYSERR on error.
708 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
711 return GNUNET_SYSERR;
713 if (daemon->running == GNUNET_YES)
720 * Start a peer that has previously been stopped using the daemon_stop
721 * call (and files weren't deleted and the allow restart flag)
723 * @param daemon the daemon to start (has been previously stopped)
724 * @param timeout how long to wait for restart
725 * @param cb the callback for notification when the peer is running
726 * @param cb_cls closure for the callback
729 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
730 struct GNUNET_TIME_Relative timeout,
731 GNUNET_TESTING_NotifyDaemonRunning cb,
734 if (daemon->running == GNUNET_YES)
736 cb(cb_cls, &daemon->id, daemon->cfg, daemon, "Daemon already running, can't restart!");
741 daemon->cb_cls = cb_cls;
742 daemon->phase = SP_TOPOLOGY_SETUP;
743 daemon->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
745 GNUNET_SCHEDULER_add_continuation (daemon->sched,
748 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
752 * Starts a GNUnet daemon. GNUnet must be installed on the target
753 * system and available in the PATH. The machine must furthermore be
754 * reachable via "ssh" (unless the hostname is "NULL") without the
755 * need to enter a password.
757 * @param sched scheduler to use
758 * @param cfg configuration to use
759 * @param timeout how long to wait starting up peers
760 * @param hostname name of the machine where to run GNUnet
761 * (use NULL for localhost).
762 * @param ssh_username ssh username to use when connecting to hostname
763 * @param sshport port to pass to ssh process when connecting to hostname
764 * @param hostkey_callback function to call once the hostkey has been
765 * generated for this peer, but it hasn't yet been started
766 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
767 * @param hostkey_cls closure for hostkey callback
768 * @param cb function to call once peer is up, or failed to start
769 * @param cb_cls closure for cb
770 * @return handle to the daemon (actual start will be completed asynchronously)
772 struct GNUNET_TESTING_Daemon *
773 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
774 const struct GNUNET_CONFIGURATION_Handle *cfg,
775 struct GNUNET_TIME_Relative timeout,
776 const char *hostname,
777 const char *ssh_username,
779 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
781 GNUNET_TESTING_NotifyDaemonRunning cb,
784 struct GNUNET_TESTING_Daemon *ret;
788 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
790 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
793 GNUNET_asprintf(&ret->ssh_port_str, "%d", sshport);
796 ret->ssh_port_str = NULL;
797 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Setting up peer with configuration file `%s'.\n",
803 if (NULL == ret->cfgfile)
805 GNUNET_free_non_null (ret->hostname);
809 ret->hostkey_callback = hostkey_callback;
810 ret->hostkey_cls = hostkey_cls;
812 ret->cb_cls = cb_cls;
813 ret->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
814 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
815 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
817 "DEFAULTCONFIG", ret->cfgfile);
818 /* 1) write configuration to temporary file */
819 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
821 if (0 != UNLINK (ret->cfgfile))
822 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
823 "unlink", ret->cfgfile);
824 GNUNET_CONFIGURATION_destroy (ret->cfg);
825 GNUNET_free_non_null (ret->hostname);
826 GNUNET_free (ret->cfgfile);
830 if (ssh_username != NULL)
831 username = GNUNET_strdup(ssh_username);
832 if ((ssh_username == NULL) && (GNUNET_OK !=
833 GNUNET_CONFIGURATION_get_value_string (cfg,
835 "USERNAME", &username)))
837 if (NULL != getenv ("USER"))
838 username = GNUNET_strdup (getenv ("USER"));
842 ret->username = username;
844 /* 2) copy file to remote host */
845 if (NULL != hostname)
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Copying configuration file to host `%s'.\n", hostname);
851 ret->phase = SP_COPYING;
852 if (NULL != username)
853 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
855 GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
857 if (ret->ssh_port_str == NULL)
859 ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
864 ret->cfgfile, arg, NULL);
868 ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
869 "scp", "-P", ret->ssh_port_str,
873 ret->cfgfile, arg, NULL);
878 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
880 ("Could not start `%s' process to copy configuration file.\n"),
882 if (0 != UNLINK (ret->cfgfile))
883 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
884 "unlink", ret->cfgfile);
885 GNUNET_CONFIGURATION_destroy (ret->cfg);
886 GNUNET_free_non_null (ret->hostname);
887 GNUNET_free_non_null (ret->username);
888 GNUNET_free (ret->cfgfile);
893 = GNUNET_SCHEDULER_add_delayed (sched,
894 GNUNET_CONSTANTS_EXEC_WAIT,
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "No need to copy configuration file since we are running locally.\n");
902 ret->phase = SP_COPIED;
903 GNUNET_SCHEDULER_add_continuation (sched,
906 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
912 * Restart (stop and start) a GNUnet daemon.
914 * @param d the daemon that should be restarted
915 * @param cb function called once the daemon is (re)started
916 * @param cb_cls closure for cb
919 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
920 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls)
928 d->dead = GNUNET_YES;
935 if (d->phase == SP_CONFIG_UPDATE)
937 GNUNET_SCHEDULER_cancel (d->sched, d->task);
938 d->phase = SP_START_DONE;
940 if (d->server != NULL)
942 GNUNET_CORE_disconnect (d->server);
948 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
949 GNUNET_TRANSPORT_disconnect(d->th);
952 /* state clean up and notifications */
953 GNUNET_free_non_null(d->hello);
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
960 d->phase = SP_START_ARMING;
962 /* Check if this is a local or remote process */
963 if (NULL != d->hostname)
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
970 if (d->username != NULL)
971 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
973 arg = GNUNET_strdup (d->hostname);
975 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
983 "-c", d->cfgfile, "-e", "-r", NULL);
984 /* Use -r to restart arm and all services */
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
992 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
994 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
999 "-c", d->cfgfile, "-e", "-r", NULL);
1002 GNUNET_free_non_null(del_arg);
1004 = GNUNET_SCHEDULER_add_delayed (d->sched,
1005 GNUNET_CONSTANTS_EXEC_WAIT,
1012 * Stops a GNUnet daemon.
1014 * @param d the daemon that should be stopped
1015 * @param timeout how long to wait for process for shutdown to complete
1016 * @param cb function called once the daemon was stopped
1017 * @param cb_cls closure for cb
1018 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1020 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1021 * GNUNET_NO to kill off and clean up for good
1024 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1025 struct GNUNET_TIME_Relative timeout,
1026 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1033 d->dead_cb_cls = cb_cls;
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1039 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
1041 d->dead = GNUNET_YES;
1045 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1047 /* Free what was left from churning! */
1048 GNUNET_assert(d->cfg != NULL);
1049 GNUNET_CONFIGURATION_destroy (d->cfg);
1050 if (delete_files == GNUNET_YES)
1052 if (0 != UNLINK(d->cfgfile))
1054 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "unlink");
1057 GNUNET_free (d->cfgfile);
1058 GNUNET_free_non_null (d->hostname);
1059 GNUNET_free_non_null (d->username);
1060 if (NULL != d->dead_cb)
1061 d->dead_cb (d->dead_cb_cls, NULL);
1067 if (delete_files == GNUNET_YES)
1069 GNUNET_asprintf(&del_arg, "-d");
1072 if (d->phase == SP_CONFIG_UPDATE)
1074 GNUNET_SCHEDULER_cancel (d->sched, d->task);
1075 d->phase = SP_START_DONE;
1077 if (d->server != NULL)
1079 GNUNET_CORE_disconnect (d->server);
1082 /* shutdown ARM process (will terminate others) */
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1087 d->phase = SP_SHUTDOWN_START;
1088 d->running = GNUNET_NO;
1089 if (allow_restart == GNUNET_YES)
1090 d->churn = GNUNET_YES;
1093 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
1094 GNUNET_TRANSPORT_disconnect(d->th);
1097 /* Check if this is a local or remote process */
1098 if (NULL != d->hostname)
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
1105 if (d->username != NULL)
1106 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1108 arg = GNUNET_strdup (d->hostname);
1110 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1118 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1120 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n", arg, "gnunet-arm", d->cfgfile, del_arg);
1121 /* Use -e to end arm, and -d to remove temp files */
1127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1128 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1130 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1135 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1138 GNUNET_free_non_null(del_arg);
1139 d->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
1141 = GNUNET_SCHEDULER_add_now (d->sched,
1147 * Changes the configuration of a GNUnet daemon.
1149 * @param d the daemon that should be modified
1150 * @param cfg the new configuration for the daemon
1151 * @param cb function called once the configuration was changed
1152 * @param cb_cls closure for cb
1155 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1156 struct GNUNET_CONFIGURATION_Handle *cfg,
1157 GNUNET_TESTING_NotifyCompletion cb,
1162 if (d->phase != SP_START_DONE)
1167 ("Peer not yet running, can not change configuration at this point."));
1171 /* 1) write configuration to temporary file */
1172 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1175 cb (cb_cls, _("Failed to write new configuration to disk."));
1179 /* 2) copy file to remote host (if necessary) */
1180 if (NULL == d->hostname)
1182 /* signal success */
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "Copying updated configuration file to remote host `%s'.\n",
1192 d->phase = SP_CONFIG_UPDATE;
1193 if (NULL != d->username)
1194 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1196 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1197 d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1201 d->cfgfile, arg, NULL);
1205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1207 ("Could not start `%s' process to copy configuration file.\n"),
1210 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1211 d->phase = SP_START_DONE;
1215 d->update_cb_cls = cb_cls;
1217 = GNUNET_SCHEDULER_add_delayed (d->sched,
1218 GNUNET_CONSTANTS_EXEC_WAIT,
1224 * Data kept for each pair of peers that we try
1227 struct ConnectContext
1230 * Testing handle to the first daemon.
1232 struct GNUNET_TESTING_Daemon *d1;
1235 * Handle to core of first daemon (to check connect)
1237 struct GNUNET_CORE_Handle * d1core;
1240 * Testing handle to the second daemon.
1242 struct GNUNET_TESTING_Daemon *d2;
1245 * Handler for the request to core to connect to this peer.
1247 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1250 * Transport handle to the second daemon.
1252 struct GNUNET_TRANSPORT_Handle *d2th;
1255 * Function to call once we are done (or have timed out).
1257 GNUNET_TESTING_NotifyConnection cb;
1265 * When should this operation be complete (or we must trigger
1268 struct GNUNET_TIME_Absolute timeout;
1271 * The relative timeout from whence this connect attempt was
1272 * started. Allows for reconnect attempts.
1274 struct GNUNET_TIME_Relative relative_timeout;
1277 * Maximum number of connect attempts, will retry connection
1278 * this number of times on failures.
1280 unsigned int max_connect_attempts;
1283 * Hello timeout task
1285 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1288 * Connect timeout task
1290 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1293 * When should this operation be complete (or we must trigger
1296 struct GNUNET_TIME_Relative timeout_hello;
1299 * Was the connection attempt successful?
1304 * The distance between the two connected peers
1310 /** Forward declaration **/
1312 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1316 * Notify callback about success or failure of the attempt
1317 * to connect the two peers
1319 * @param cls our "struct ConnectContext" (freed)
1320 * @param tc reason tells us if we succeeded or failed
1323 notify_connect_result (void *cls,
1324 const struct GNUNET_SCHEDULER_TaskContext *tc)
1326 struct ConnectContext *ctx = cls;
1327 struct GNUNET_TIME_Relative remaining;
1329 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1330 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1332 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
1333 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1336 if (ctx->connect_request_handle != NULL)
1338 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1339 ctx->connect_request_handle = NULL;
1341 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1343 if (ctx->d2th != NULL)
1344 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1346 if (ctx->d1core != NULL)
1347 GNUNET_CORE_disconnect (ctx->d1core);
1349 if (ctx->d2core != NULL)
1350 GNUNET_CORE_disconnect (ctx->d2core);
1359 remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
1361 if (ctx->connected == GNUNET_YES)
1363 if (ctx->cb != NULL)
1365 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance, ctx->d1->cfg,
1366 ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1369 else if (remaining.value > 0)
1371 if (ctx->d1core != NULL)
1373 GNUNET_CORE_disconnect(ctx->d1core);
1377 if (ctx->d2core != NULL)
1379 GNUNET_CORE_disconnect(ctx->d2core);
1384 if (ctx->d2th != NULL)
1386 GNUNET_TRANSPORT_disconnect(ctx->d2th);
1389 GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
1394 if (ctx->cb != NULL)
1396 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1397 ctx->d2->cfg, ctx->d1, ctx->d2,
1398 _("Peers failed to connect"));
1402 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1404 GNUNET_CORE_disconnect (ctx->d1core);
1411 * Success, connection is up. Signal client our success.
1413 * @param cls our "struct ConnectContext"
1414 * @param peer identity of the peer that has connected
1415 * @param latency the round trip latency of the connection to this peer
1416 * @param distance distance the transport level distance to this peer
1420 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1423 struct ConnectContext *ctx = cls;
1425 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1427 ctx->connected = GNUNET_YES;
1428 ctx->distance = distance;
1429 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1430 ctx->timeout_task = GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1431 ¬ify_connect_result,
1439 * Success, connection is up. Signal client our success.
1441 * @param cls our "struct ConnectContext"
1442 * @param peer identity of the peer that has connected
1443 * @param latency the round trip latency of the connection to this peer
1444 * @param distance distance the transport level distance to this peer
1448 connect_notify_core2 (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1451 struct ConnectContext *ctx = cls;
1453 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1455 ctx->connected = GNUNET_YES;
1456 ctx->distance = distance;
1457 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1458 ctx->timeout_task = GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1459 ¬ify_connect_result,
1467 * Task called once a core connect request has been transmitted.
1469 * @param cls struct ConnectContext
1470 * @param tc context information (why was this task triggered now)
1472 void core_connect_request_cont (void *cls,
1474 GNUNET_SCHEDULER_TaskContext * tc)
1476 struct ConnectContext *ctx = cls;
1478 if (tc->reason == GNUNET_SCHEDULER_REASON_PREREQ_DONE)
1479 ctx->connect_request_handle = NULL;
1484 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1486 struct ConnectContext *ctx = cls;
1487 struct GNUNET_MessageHeader *hello;
1488 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1489 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1491 if ((ctx->d1->hello != NULL) && (NULL != GNUNET_HELLO_get_header(ctx->d1->hello)))
1493 hello = GNUNET_HELLO_get_header(ctx->d1->hello);
1494 GNUNET_assert(hello != NULL);
1495 GNUNET_TRANSPORT_offer_hello (ctx->d2th, hello);
1497 ctx->connect_request_handle = GNUNET_CORE_peer_request_connect (ctx->d1->sched,
1499 GNUNET_TIME_relative_divide(ctx->relative_timeout,
1500 ctx->max_connect_attempts + 1),
1502 &core_connect_request_cont,
1504 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending connect request to core for peer %s\n", GNUNET_i2s(&ctx->d1->id));
1505 ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
1506 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1509 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
1515 * Establish a connection between two GNUnet daemons.
1517 * @param d1 handle for the first daemon
1518 * @param d2 handle for the second daemon
1519 * @param timeout how long is the connection attempt
1521 * @param max_connect_attempts how many times should we try to reconnect
1523 * @param cb function to call at the end
1524 * @param cb_cls closure for cb
1527 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1528 struct GNUNET_TESTING_Daemon *d2,
1529 struct GNUNET_TIME_Relative timeout,
1530 unsigned int max_connect_attempts,
1531 GNUNET_TESTING_NotifyConnection cb,
1534 struct ConnectContext *ctx;
1536 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1539 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1540 _("Peers are not fully running yet, can not connect!\n"));
1543 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1546 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1547 ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
1548 ctx->relative_timeout = timeout;
1550 ctx->cb_cls = cb_cls;
1551 ctx->max_connect_attempts = max_connect_attempts;
1552 ctx->connected = GNUNET_NO;
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1555 "Asked to connect peer %s to peer %s\n",
1556 d1->shortname, d2->shortname);
1559 ctx->d1core = GNUNET_CORE_connect (d1->sched,
1564 &connect_notify, NULL, NULL,
1566 NULL, GNUNET_NO, no_handlers);
1567 if (ctx->d1core == NULL)
1571 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1572 _("Failed to connect to core service of first peer!\n"));
1577 ctx->d2core = GNUNET_CORE_connect (d2->sched,
1584 NULL, GNUNET_NO, no_handlers);
1585 if (ctx->d2core == NULL)
1589 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1590 _("Failed to connect to core service of second peer!\n"));
1595 #if DEBUG_TESTING > 2
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1597 "Asked to connect peer %s to peer %s\n",
1598 d1->shortname, d2->shortname);
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1600 "Connecting to transport service of peer %s\n", d2->shortname);
1604 ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
1607 d2, NULL, NULL, NULL);
1608 if (ctx->d2th == NULL)
1610 GNUNET_CORE_disconnect(ctx->d1core);
1613 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1614 _("Failed to connect to transport service!\n"));
1618 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
1619 GNUNET_TIME_relative_divide(ctx->relative_timeout,
1620 ctx->max_connect_attempts),
1621 ¬ify_connect_result, ctx);
1623 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1627 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1630 struct ConnectContext *ctx = cls;
1631 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1635 #if DEBUG_TESTING_RECONNECT
1636 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "re-attempting connect of peer %s to peer %s\n",
1637 ctx->d1->shortname, ctx->d2->shortname);
1640 GNUNET_assert(ctx->d1core == NULL);
1642 ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1644 GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1647 &connect_notify, NULL, NULL,
1649 NULL, GNUNET_NO, no_handlers);
1650 if (ctx->d1core == NULL)
1652 if (NULL != ctx->cb)
1653 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1654 _("Failed to connect to core service of first peer!\n"));
1659 ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1662 ctx->d2, NULL, NULL, NULL);
1663 if (ctx->d2th == NULL)
1665 GNUNET_CORE_disconnect(ctx->d1core);
1667 if (NULL != ctx->cb)
1668 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1669 _("Failed to connect to transport service!\n"));
1673 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1674 GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1675 ¬ify_connect_result, ctx);
1677 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1680 /* end of testing.c */