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 2, 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_YES
41 #define DEBUG_TESTING_RECONNECT GNUNET_YES
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;
71 if (daemon->server != NULL)
73 GNUNET_CORE_disconnect(daemon->server);
74 daemon->server = NULL;
77 GNUNET_assert (message != NULL);
78 if (daemon->th != NULL)
80 GNUNET_TRANSPORT_get_hello_cancel(daemon->th, &process_hello, daemon);
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84 "Received `%s' from transport service of `%4s'\n",
85 "HELLO", GNUNET_i2s (&daemon->id));
88 GNUNET_free_non_null(daemon->hello);
89 daemon->hello = GNUNET_malloc(ntohs(message->size));
90 memcpy(daemon->hello, message, ntohs(message->size));
92 if (daemon->th != NULL)
94 GNUNET_TRANSPORT_disconnect(daemon->th);
101 * Function called after GNUNET_CORE_connect has succeeded
102 * (or failed for good). Note that the private key of the
103 * peer is intentionally not exposed here; if you need it,
104 * your process should try to read the private key file
105 * directly (which should work if you are authorized...).
108 * @param server handle to the server, NULL if we failed
109 * @param my_identity ID of this peer, NULL if we failed
110 * @param publicKey public key of this peer, NULL if we failed
113 testing_init (void *cls,
114 struct GNUNET_CORE_Handle *server,
115 const struct GNUNET_PeerIdentity *my_identity,
116 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
118 struct GNUNET_TESTING_Daemon *d = cls;
119 GNUNET_TESTING_NotifyDaemonRunning cb;
121 GNUNET_assert (d->phase == SP_START_CORE);
122 d->phase = SP_START_DONE;
128 if (GNUNET_YES == d->dead)
129 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES);
131 cb (d->cb_cls, NULL, d->cfg, d,
132 _("Failed to connect to core service\n"));
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
139 d->id = *my_identity;
140 d->shortname = strdup (GNUNET_i2s (my_identity));
142 d->running = GNUNET_YES;
143 if (GNUNET_YES == d->dead)
144 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES);
146 cb (d->cb_cls, my_identity, d->cfg, d, NULL);
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
153 d->th = GNUNET_TRANSPORT_connect (d->sched,
154 d->cfg, d, NULL, NULL, NULL);
157 if (GNUNET_YES == d->dead)
158 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES);
159 else if (NULL != d->cb)
160 d->cb (d->cb_cls, &d->id, d->cfg, d,
161 _("Failed to connect to transport service!\n"));
165 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
170 * Finite-state machine for starting GNUnet.
172 * @param cls our "struct GNUNET_TESTING_Daemon"
176 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
178 struct GNUNET_TESTING_Daemon *d = cls;
179 GNUNET_TESTING_NotifyDaemonRunning cb;
180 enum GNUNET_OS_ProcessStatusType type;
184 static char hostkeybuf[105];
185 static const char temphostkey[104];
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189 "Peer FSM is in phase %u.\n", d->phase);
192 d->task = GNUNET_SCHEDULER_NO_TASK;
196 /* confirm copying complete */
197 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
199 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
206 d->cfg, d, _("`scp' does not seem to terminate (timeout copying config).\n"));
211 = GNUNET_SCHEDULER_add_delayed (d->sched,
212 GNUNET_CONSTANTS_EXEC_WAIT,
216 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
222 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "Successfully copied configuration file.\n");
229 d->phase = SP_COPIED;
232 /* Start create hostkey process */
233 d->pipe_stdout = GNUNET_DISK_pipe(GNUNET_NO);
234 if (d->pipe_stdout == NULL)
243 (NULL == d->hostname)
244 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
245 : _("Failed to create pipe for `ssh' process.\n"));
248 if (NULL == d->hostname)
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Starting `%s', with command `%s %s %s %s'.\n",
253 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
256 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
260 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
264 if (d->username != NULL)
265 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
267 dst = GNUNET_strdup (d->hostname);
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
272 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c", d->cfgfile,
275 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
279 "-c", d->cfgfile, "-sq", NULL);
280 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
286 _("Could not start `%s' process to create hostkey.\n"),
287 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
295 (NULL == d->hostname)
296 ? _("Failed to start `gnunet-peerinfo' process.\n")
297 : _("Failed to start `ssh' process.\n"));
298 GNUNET_DISK_pipe_close(d->pipe_stdout);
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303 "Started `%s', waiting for hostkey.\n",
306 d->phase = SP_HOSTKEY_CREATE;
308 = GNUNET_SCHEDULER_add_delayed (d->sched,
309 GNUNET_CONSTANTS_EXEC_WAIT,
312 case SP_HOSTKEY_CREATE:
314 bytes_read = GNUNET_DISK_file_read(GNUNET_DISK_pipe_handle(d->pipe_stdout, GNUNET_DISK_PIPE_END_READ), &hostkeybuf, sizeof(hostkeybuf));
315 if (bytes_read == 104) /* Success, we have read in the hostkey */
317 if (hostkeybuf[103] == '\n')
318 hostkeybuf[103] = '\0';
320 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Malformed output from gnunet-peerinfo!\n");
321 memcpy(&temphostkey, &hostkeybuf, bytes_read);
323 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (&temphostkey[0],
326 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to peer identity!\n");
330 GNUNET_DISK_pipe_close(d->pipe_stdout);
331 d->pipe_stdout = NULL;
335 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
337 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
346 (NULL == d->hostname)
347 ? _("`gnunet-peerinfo' does not seem to terminate.\n")
348 : _("`ssh' does not seem to terminate.\n"));
350 GNUNET_DISK_pipe_close(d->pipe_stdout);
355 = GNUNET_SCHEDULER_add_delayed (d->sched,
356 GNUNET_CONSTANTS_EXEC_WAIT,
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Successfully got hostkey!\n");
364 if (d->pipe_stdout != NULL)
373 _("`Failed to get hostkey!\n"));
374 GNUNET_DISK_pipe_close(d->pipe_stdout);
378 if (d->hostkey_callback != NULL)
380 d->hostkey_callback(d->hostkey_cls, &d->id, d, NULL);
381 d->phase = SP_HOSTKEY_CREATED;
385 d->phase = SP_TOPOLOGY_SETUP;
389 case SP_HOSTKEY_CREATED:
390 /* wait for topology finished */
391 if ((GNUNET_YES == d->dead) || (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0))
400 _("`Failed while waiting for topology setup!\n"));
405 = GNUNET_SCHEDULER_add_delayed (d->sched,
406 GNUNET_CONSTANTS_EXEC_WAIT,
409 case SP_TOPOLOGY_SETUP:
410 /* start GNUnet on remote host */
411 if (NULL == d->hostname)
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
416 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
420 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
430 if (d->username != NULL)
431 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
433 dst = GNUNET_strdup (d->hostname);
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
438 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
439 "-L", "DEBUG", "-s", "-q");
441 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
448 "-c", d->cfgfile, "-s", "-q", NULL);
453 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
454 _("Could not start `%s' process to start GNUnet.\n"),
455 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
463 (NULL == d->hostname)
464 ? _("Failed to start `gnunet-arm' process.\n")
465 : _("Failed to start `ssh' process.\n"));
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470 "Started `%s', waiting for `%s' to be up.\n",
471 "gnunet-arm", "gnunet-service-core");
473 d->phase = SP_START_ARMING;
475 = GNUNET_SCHEDULER_add_delayed (d->sched,
476 GNUNET_CONSTANTS_EXEC_WAIT,
479 case SP_START_ARMING:
480 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
482 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
491 (NULL == d->hostname)
492 ? _("`gnunet-arm' does not seem to terminate.\n")
493 : _("`ssh' does not seem to terminate.\n"));
498 = GNUNET_SCHEDULER_add_delayed (d->sched,
499 GNUNET_CONSTANTS_EXEC_WAIT,
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "Successfully started `%s'.\n", "gnunet-arm");
507 d->phase = SP_START_CORE;
508 d->server = GNUNET_CORE_connect (d->sched,
515 NULL, GNUNET_NO, no_handlers);
523 case SP_SHUTDOWN_START:
524 /* confirm copying complete */
525 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
527 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
529 d->dead_cb (d->dead_cb_cls,
530 _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
533 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
534 GNUNET_TRANSPORT_disconnect(d->th);
537 GNUNET_CONFIGURATION_destroy (d->cfg);
538 GNUNET_free (d->cfgfile);
539 GNUNET_free_non_null(d->hello);
540 GNUNET_free_non_null (d->hostname);
541 GNUNET_free_non_null (d->username);
542 GNUNET_free_non_null (d->shortname);
548 = GNUNET_SCHEDULER_add_delayed (d->sched,
549 GNUNET_CONSTANTS_EXEC_WAIT,
553 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
555 if (NULL != d->dead_cb)
556 d->dead_cb (d->dead_cb_cls,
557 _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
560 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
561 GNUNET_TRANSPORT_disconnect(d->th);
564 GNUNET_CONFIGURATION_destroy (d->cfg);
565 GNUNET_free (d->cfgfile);
566 GNUNET_free_non_null(d->hello);
567 GNUNET_free_non_null (d->hostname);
568 GNUNET_free_non_null (d->username);
569 GNUNET_free_non_null (d->shortname);
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
578 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
579 GNUNET_TRANSPORT_disconnect(d->th);
582 /* state clean up and notifications */
583 GNUNET_CONFIGURATION_destroy (d->cfg);
584 GNUNET_free (d->cfgfile);
585 GNUNET_free_non_null(d->hello);
586 GNUNET_free_non_null (d->hostname);
587 GNUNET_free_non_null (d->username);
588 GNUNET_free_non_null (d->shortname);
589 if (NULL != d->dead_cb)
590 d->dead_cb (d->dead_cb_cls, NULL);
593 case SP_CONFIG_UPDATE:
594 /* confirm copying complete */
595 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
597 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0) /* FIXME: config update should take timeout parameter! */
604 d->cfg, d, _("`scp' does not seem to terminate.\n"));
609 = GNUNET_SCHEDULER_add_delayed (d->sched,
610 GNUNET_CONSTANTS_EXEC_WAIT,
614 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
616 if (NULL != d->update_cb)
617 d->update_cb (d->update_cb_cls,
618 _("`scp' did not complete cleanly.\n"));
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
623 "Successfully copied configuration file.\n");
625 if (NULL != d->update_cb)
626 d->update_cb (d->update_cb_cls, NULL);
627 d->phase = SP_START_DONE;
633 * Continues GNUnet daemon startup when user wanted to be notified
634 * once a hostkey was generated (for creating friends files, blacklists,
637 * @param daemon the daemon to finish starting
640 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon)
642 GNUNET_assert(daemon->phase == SP_HOSTKEY_CREATED);
643 daemon->phase = SP_TOPOLOGY_SETUP;
647 * Starts a GNUnet daemon. GNUnet must be installed on the target
648 * system and available in the PATH. The machine must furthermore be
649 * reachable via "ssh" (unless the hostname is "NULL") without the
650 * need to enter a password.
652 * @param sched scheduler to use
653 * @param cfg configuration to use
654 * @param timeout how long to wait starting up peers
655 * @param hostname name of the machine where to run GNUnet
656 * (use NULL for localhost).
657 * @param hostkey_callback function to call once the hostkey has been
658 * generated for this peer, but it hasn't yet been started
659 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
660 * @param hostkey_cls closure for hostkey callback
661 * @param cb function to call with the result
662 * @param cb_cls closure for cb
663 * @return handle to the daemon (actual start will be completed asynchronously)
665 struct GNUNET_TESTING_Daemon *
666 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
667 const struct GNUNET_CONFIGURATION_Handle *cfg,
668 struct GNUNET_TIME_Relative timeout,
669 const char *hostname,
670 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
672 GNUNET_TESTING_NotifyDaemonRunning cb,
675 struct GNUNET_TESTING_Daemon *ret;
679 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
681 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
682 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "Setting up peer with configuration file `%s'.\n",
688 if (NULL == ret->cfgfile)
690 GNUNET_free_non_null (ret->hostname);
694 ret->hostkey_callback = hostkey_callback;
695 ret->hostkey_cls = hostkey_cls;
697 ret->cb_cls = cb_cls;
698 ret->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
699 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
700 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
702 "DEFAULTCONFIG", ret->cfgfile);
703 /* 1) write configuration to temporary file */
704 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
706 if (0 != UNLINK (ret->cfgfile))
707 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
708 "unlink", ret->cfgfile);
709 GNUNET_CONFIGURATION_destroy (ret->cfg);
710 GNUNET_free_non_null (ret->hostname);
711 GNUNET_free (ret->cfgfile);
716 GNUNET_CONFIGURATION_get_value_string (cfg,
718 "USERNAME", &username))
720 if (NULL != getenv ("USER"))
721 username = GNUNET_strdup (getenv ("USER"));
725 ret->username = username;
727 /* 2) copy file to remote host */
728 if (NULL != hostname)
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "Copying configuration file to host `%s'.\n", hostname);
734 ret->phase = SP_COPYING;
735 if (NULL != username)
736 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
738 GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
739 ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
740 "scp", ret->cfgfile, arg, NULL);
744 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
746 ("Could not start `%s' process to copy configuration file.\n"),
748 if (0 != UNLINK (ret->cfgfile))
749 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
750 "unlink", ret->cfgfile);
751 GNUNET_CONFIGURATION_destroy (ret->cfg);
752 GNUNET_free_non_null (ret->hostname);
753 GNUNET_free_non_null (ret->username);
754 GNUNET_free (ret->cfgfile);
759 = GNUNET_SCHEDULER_add_delayed (sched,
760 GNUNET_CONSTANTS_EXEC_WAIT,
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "No need to copy configuration file since we are running locally.\n");
768 ret->phase = SP_COPIED;
769 GNUNET_SCHEDULER_add_continuation (sched,
772 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
778 * Restart (stop and start) a GNUnet daemon.
780 * @param d the daemon that should be restarted
781 * @param cb function called once the daemon is (re)started
782 * @param cb_cls closure for cb
785 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
786 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls)
794 d->dead = GNUNET_YES;
801 if (d->phase == SP_CONFIG_UPDATE)
803 GNUNET_SCHEDULER_cancel (d->sched, d->task);
804 d->phase = SP_START_DONE;
806 if (d->server != NULL)
808 GNUNET_CORE_disconnect (d->server);
814 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
815 GNUNET_TRANSPORT_disconnect(d->th);
818 /* state clean up and notifications */
819 GNUNET_free_non_null(d->hello);
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
823 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
826 d->phase = SP_START_ARMING;
828 /* Check if this is a local or remote process */
829 if (NULL != d->hostname)
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
836 if (d->username != NULL)
837 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
839 arg = GNUNET_strdup (d->hostname);
841 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
846 "-c", d->cfgfile, "-e", "-r", NULL);
847 /* Use -r to restart arm and all services */
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
857 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
862 "-c", d->cfgfile, "-e", "-r", NULL);
865 GNUNET_free_non_null(del_arg);
867 = GNUNET_SCHEDULER_add_delayed (d->sched,
868 GNUNET_CONSTANTS_EXEC_WAIT,
875 * Stops a GNUnet daemon.
877 * @param d the daemon that should be stopped
878 * @param timeout how long to wait for process for shutdown to complete
879 * @param cb function called once the daemon was stopped
880 * @param cb_cls closure for cb
881 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
882 * to leave them (i.e. for restarting at a later time,
883 * or logfile inspection once finished)
886 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
887 struct GNUNET_TIME_Relative timeout,
888 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
894 d->dead_cb_cls = cb_cls;
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
902 d->dead = GNUNET_YES;
907 if (delete_files == GNUNET_YES)
909 GNUNET_asprintf(&del_arg, "-d");
912 if (d->phase == SP_CONFIG_UPDATE)
914 GNUNET_SCHEDULER_cancel (d->sched, d->task);
915 d->phase = SP_START_DONE;
917 if (d->server != NULL)
919 GNUNET_CORE_disconnect (d->server);
922 /* shutdown ARM process (will terminate others) */
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
925 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
928 d->phase = SP_SHUTDOWN_START;
929 /* Check if this is a local or remote process */
930 if (NULL != d->hostname)
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
937 if (d->username != NULL)
938 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
940 arg = GNUNET_strdup (d->hostname);
942 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
947 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
948 /* Use -e to end arm, and -d to remove temp files */
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
956 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
958 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
963 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
966 GNUNET_free_non_null(del_arg);
967 d->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
969 = GNUNET_SCHEDULER_add_delayed (d->sched,
970 GNUNET_CONSTANTS_EXEC_WAIT,
976 * Changes the configuration of a GNUnet daemon.
978 * @param d the daemon that should be modified
979 * @param cfg the new configuration for the daemon
980 * @param cb function called once the configuration was changed
981 * @param cb_cls closure for cb
984 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
985 struct GNUNET_CONFIGURATION_Handle *cfg,
986 GNUNET_TESTING_NotifyCompletion cb,
991 if (d->phase != SP_START_DONE)
996 ("Peer not yet running, can not change configuration at this point."));
1000 /* 1) write configuration to temporary file */
1001 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1004 cb (cb_cls, _("Failed to write new configuration to disk."));
1008 /* 2) copy file to remote host (if necessary) */
1009 if (NULL == d->hostname)
1011 /* signal success */
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "Copying updated configuration file to remote host `%s'.\n",
1021 d->phase = SP_CONFIG_UPDATE;
1022 if (NULL != d->username)
1023 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1025 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1026 d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", d->cfgfile, arg, NULL);
1030 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1032 ("Could not start `%s' process to copy configuration file.\n"),
1035 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1036 d->phase = SP_START_DONE;
1040 d->update_cb_cls = cb_cls;
1042 = GNUNET_SCHEDULER_add_delayed (d->sched,
1043 GNUNET_CONSTANTS_EXEC_WAIT,
1049 * Data kept for each pair of peers that we try
1052 struct ConnectContext
1055 * Testing handle to the first daemon.
1057 struct GNUNET_TESTING_Daemon *d1;
1060 * Handle to core of first daemon (to check connect)
1062 struct GNUNET_CORE_Handle * d1core;
1065 * Testing handle to the second daemon.
1067 struct GNUNET_TESTING_Daemon *d2;
1070 * Transport handle to the second daemon.
1072 struct GNUNET_TRANSPORT_Handle *d2th;
1075 * Function to call once we are done (or have timed out).
1077 GNUNET_TESTING_NotifyConnection cb;
1085 * When should this operation be complete (or we must trigger
1088 struct GNUNET_TIME_Absolute timeout;
1091 * The relative timeout from whence this connect attempt was
1092 * started. Allows for reconnect attempts.
1094 struct GNUNET_TIME_Relative relative_timeout;
1097 * Maximum number of connect attempts, will retry connection
1098 * this number of times on failures.
1100 unsigned int max_connect_attempts;
1103 * Hello timeout task
1105 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1108 * Connect timeout task
1110 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1113 * When should this operation be complete (or we must trigger
1116 struct GNUNET_TIME_Relative timeout_hello;
1120 * Was the connection attempt successful?
1126 /** Forward declaration **/
1128 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1132 * Notify callback about success or failure of the attempt
1133 * to connect the two peers
1135 * @param cls our "struct ConnectContext" (freed)
1136 * @param tc reason tells us if we succeeded or failed
1139 notify_connect_result (void *cls,
1140 const struct GNUNET_SCHEDULER_TaskContext *tc)
1142 struct ConnectContext *ctx = cls;
1143 struct GNUNET_TIME_Relative remaining;
1145 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1147 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
1148 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1151 if ((ctx->timeout_task != GNUNET_SCHEDULER_NO_TASK) && (tc->reason != GNUNET_SCHEDULER_REASON_TIMEOUT))
1153 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1154 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1157 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1159 if (ctx->d2th != NULL)
1160 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1162 if (ctx->d1core != NULL)
1163 GNUNET_CORE_disconnect (ctx->d1core);
1170 remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
1172 if (ctx->connected == GNUNET_YES)
1174 if (ctx->cb != NULL)
1176 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
1177 ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1180 else if (remaining.value > 0)
1182 if (ctx->d1core != NULL)
1184 GNUNET_CORE_disconnect(ctx->d1core);
1188 if (ctx->d2th != NULL)
1190 GNUNET_TRANSPORT_disconnect(ctx->d2th);
1193 GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
1198 if (ctx->cb != NULL)
1200 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
1201 ctx->d2->cfg, ctx->d1, ctx->d2,
1202 _("Peers failed to connect"));
1206 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1208 GNUNET_CORE_disconnect (ctx->d1core);
1215 * Success, connection is up. Signal client our success.
1217 * @param cls our "struct ConnectContext"
1218 * @param peer identity of the peer that has connected
1219 * @param latency the round trip latency of the connection to this peer
1220 * @param distance distance the transport level distance to this peer
1224 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1227 struct ConnectContext *ctx = cls;
1229 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1231 ctx->connected = GNUNET_YES;
1232 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1233 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1234 GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1235 ¬ify_connect_result,
1242 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1244 struct ConnectContext *ctx = cls;
1246 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1249 if (ctx->d1->hello != NULL)
1251 GNUNET_TRANSPORT_offer_hello (ctx->d2th, GNUNET_HELLO_get_header(ctx->d1->hello));
1252 ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
1253 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1256 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
1262 * Establish a connection between two GNUnet daemons.
1264 * @param d1 handle for the first daemon
1265 * @param d2 handle for the second daemon
1266 * @param timeout how long is the connection attempt
1268 * @param max_connect_attempts how many times should we try to reconnect
1270 * @param cb function to call at the end
1271 * @param cb_cls closure for cb
1274 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1275 struct GNUNET_TESTING_Daemon *d2,
1276 struct GNUNET_TIME_Relative timeout,
1277 unsigned int max_connect_attempts,
1278 GNUNET_TESTING_NotifyConnection cb,
1281 struct ConnectContext *ctx;
1283 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1286 cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1287 _("Peers are not fully running yet, can not connect!\n"));
1290 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1293 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1294 ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
1295 ctx->relative_timeout = timeout;
1297 ctx->cb_cls = cb_cls;
1298 ctx->max_connect_attempts = max_connect_attempts;
1299 ctx->connected = GNUNET_NO;
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 "Asked to connect peer %s to peer %s\n",
1303 d1->shortname, d2->shortname);
1306 ctx->d1core = GNUNET_CORE_connect (d1->sched,
1311 &connect_notify, NULL,
1313 NULL, GNUNET_NO, no_handlers);
1314 if (ctx->d1core == NULL)
1318 cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1319 _("Failed to connect to core service of first peer!\n"));
1323 #if DEBUG_TESTING > 2
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "Asked to connect peer %s to peer %s\n",
1326 d1->shortname, d2->shortname);
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1328 "Connecting to transport service of peer %s\n", d2->shortname);
1332 ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
1333 d2->cfg, d2, NULL, NULL, NULL);
1334 if (ctx->d2th == NULL)
1336 GNUNET_CORE_disconnect(ctx->d1core);
1339 cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1340 _("Failed to connect to transport service!\n"));
1344 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
1345 GNUNET_TIME_relative_divide(ctx->relative_timeout, max_connect_attempts), /* Allow up to 8 reconnect attempts */
1346 ¬ify_connect_result, ctx);
1348 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1352 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1355 struct ConnectContext *ctx = cls;
1356 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1360 #if DEBUG_TESTING_RECONNECT
1361 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "re-attempting connect of peer %s to peer %s\n",
1362 ctx->d1->shortname, ctx->d2->shortname);
1365 GNUNET_assert(ctx->d1core == NULL);
1367 ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1369 GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1372 &connect_notify, NULL,
1374 NULL, GNUNET_NO, no_handlers);
1375 if (ctx->d1core == NULL)
1377 if (NULL != ctx->cb)
1378 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1379 _("Failed to connect to core service of first peer!\n"));
1384 ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1385 ctx->d2->cfg, ctx->d2, NULL, NULL, NULL);
1386 if (ctx->d2th == NULL)
1388 GNUNET_CORE_disconnect(ctx->d1core);
1390 if (NULL != ctx->cb)
1391 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1392 _("Failed to connect to transport service!\n"));
1396 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1397 GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1398 ¬ify_connect_result, ctx);
1400 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1403 /* end of testing.c */