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, 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, 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, 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))
200 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
207 d->cfg, d, _("`scp' does not seem to terminate.\n"));
212 = GNUNET_SCHEDULER_add_delayed (d->sched,
213 GNUNET_CONSTANTS_EXEC_WAIT,
217 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
223 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Successfully copied configuration file.\n");
230 d->phase = SP_COPIED;
233 /* Start create hostkey process */
234 d->pipe_stdout = GNUNET_DISK_pipe(GNUNET_NO);
235 if (d->pipe_stdout == NULL)
244 (NULL == d->hostname)
245 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
246 : _("Failed to create pipe for `ssh' process.\n"));
249 if (NULL == d->hostname)
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "Starting `%s', with command `%s %s %s %s'.\n",
254 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
257 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
261 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
265 if (d->username != NULL)
266 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
268 dst = GNUNET_strdup (d->hostname);
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
273 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c", d->cfgfile,
276 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
280 "-c", d->cfgfile, "-sq", NULL);
281 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
287 _("Could not start `%s' process to create hostkey.\n"),
288 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
296 (NULL == d->hostname)
297 ? _("Failed to start `gnunet-peerinfo' process.\n")
298 : _("Failed to start `ssh' process.\n"));
299 GNUNET_DISK_pipe_close(d->pipe_stdout);
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "Started `%s', waiting for hostkey.\n",
307 d->phase = SP_HOSTKEY_CREATE;
310 = GNUNET_SCHEDULER_add_delayed (d->sched,
311 GNUNET_CONSTANTS_EXEC_WAIT,
314 case SP_HOSTKEY_CREATE:
316 bytes_read = GNUNET_DISK_file_read(GNUNET_DISK_pipe_handle(d->pipe_stdout, GNUNET_DISK_PIPE_END_READ), &hostkeybuf, sizeof(hostkeybuf));
317 if (bytes_read == 104) /* Success, we have read in the hostkey */
319 if (hostkeybuf[103] == '\n')
320 hostkeybuf[103] = '\0';
322 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Malformed output from gnunet-peerinfo!\n");
323 memcpy(&temphostkey, &hostkeybuf, bytes_read);
325 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (&temphostkey[0],
328 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to peer identity!\n");
332 GNUNET_DISK_pipe_close(d->pipe_stdout);
333 d->pipe_stdout = NULL;
337 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
340 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
349 (NULL == d->hostname)
350 ? _("`gnunet-peerinfo' does not seem to terminate.\n")
351 : _("`ssh' does not seem to terminate.\n"));
353 GNUNET_DISK_pipe_close(d->pipe_stdout);
358 = GNUNET_SCHEDULER_add_delayed (d->sched,
359 GNUNET_CONSTANTS_EXEC_WAIT,
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "Successfully got hostkey!\n");
367 if (d->pipe_stdout != NULL)
376 _("`Failed to get hostkey!\n"));
377 GNUNET_DISK_pipe_close(d->pipe_stdout);
381 if (d->hostkey_callback != NULL)
383 d->hostkey_callback(d->hostkey_cls, &d->id, d, NULL);
384 d->phase = SP_HOSTKEY_CREATED;
388 d->phase = SP_TOPOLOGY_SETUP;
392 case SP_HOSTKEY_CREATED:
393 /* wait for topology finished */
395 if ((GNUNET_YES == d->dead) || (d->wait_runs > MAX_EXEC_WAIT_RUNS))
404 _("`Failed while waiting for topology setup!\n"));
409 = GNUNET_SCHEDULER_add_delayed (d->sched,
410 GNUNET_CONSTANTS_EXEC_WAIT,
413 case SP_TOPOLOGY_SETUP:
414 /* start GNUnet on remote host */
415 if (NULL == d->hostname)
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
420 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
424 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
434 if (d->username != NULL)
435 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
437 dst = GNUNET_strdup (d->hostname);
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
442 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
443 "-L", "DEBUG", "-s", "-q");
445 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
452 "-c", d->cfgfile, "-s", "-q", NULL);
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
458 _("Could not start `%s' process to start GNUnet.\n"),
459 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
467 (NULL == d->hostname)
468 ? _("Failed to start `gnunet-arm' process.\n")
469 : _("Failed to start `ssh' process.\n"));
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Started `%s', waiting for `%s' to be up.\n",
475 "gnunet-arm", "gnunet-service-core");
477 d->phase = SP_START_ARMING;
480 = GNUNET_SCHEDULER_add_delayed (d->sched,
481 GNUNET_CONSTANTS_EXEC_WAIT,
484 case SP_START_ARMING:
485 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
488 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
497 (NULL == d->hostname)
498 ? _("`gnunet-arm' does not seem to terminate.\n")
499 : _("`ssh' does not seem to terminate.\n"));
504 = GNUNET_SCHEDULER_add_delayed (d->sched,
505 GNUNET_CONSTANTS_EXEC_WAIT,
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "Successfully started `%s'.\n", "gnunet-arm");
513 d->phase = SP_START_CORE;
514 d->server = GNUNET_CORE_connect (d->sched,
521 NULL, GNUNET_NO, no_handlers);
529 case SP_SHUTDOWN_START:
530 /* confirm copying complete */
531 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
534 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
536 d->dead_cb (d->dead_cb_cls,
537 _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
540 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
541 GNUNET_TRANSPORT_disconnect(d->th);
544 GNUNET_CONFIGURATION_destroy (d->cfg);
545 GNUNET_free (d->cfgfile);
546 GNUNET_free_non_null(d->hello);
547 GNUNET_free_non_null (d->hostname);
548 GNUNET_free_non_null (d->username);
549 GNUNET_free_non_null (d->shortname);
555 = GNUNET_SCHEDULER_add_delayed (d->sched,
556 GNUNET_CONSTANTS_EXEC_WAIT,
560 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
562 if (NULL != d->dead_cb)
563 d->dead_cb (d->dead_cb_cls,
564 _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
567 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
568 GNUNET_TRANSPORT_disconnect(d->th);
571 GNUNET_CONFIGURATION_destroy (d->cfg);
572 GNUNET_free (d->cfgfile);
573 GNUNET_free_non_null(d->hello);
574 GNUNET_free_non_null (d->hostname);
575 GNUNET_free_non_null (d->username);
576 GNUNET_free_non_null (d->shortname);
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
585 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
586 GNUNET_TRANSPORT_disconnect(d->th);
589 /* state clean up and notifications */
590 GNUNET_CONFIGURATION_destroy (d->cfg);
591 GNUNET_free (d->cfgfile);
592 GNUNET_free_non_null(d->hello);
593 GNUNET_free_non_null (d->hostname);
594 GNUNET_free_non_null (d->username);
595 GNUNET_free_non_null (d->shortname);
596 if (NULL != d->dead_cb)
597 d->dead_cb (d->dead_cb_cls, NULL);
600 case SP_CONFIG_UPDATE:
601 /* confirm copying complete */
602 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
605 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
612 d->cfg, d, _("`scp' does not seem to terminate.\n"));
617 = GNUNET_SCHEDULER_add_delayed (d->sched,
618 GNUNET_CONSTANTS_EXEC_WAIT,
622 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
624 if (NULL != d->update_cb)
625 d->update_cb (d->update_cb_cls,
626 _("`scp' did not complete cleanly.\n"));
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631 "Successfully copied configuration file.\n");
633 if (NULL != d->update_cb)
634 d->update_cb (d->update_cb_cls, NULL);
635 d->phase = SP_START_DONE;
641 * Continues GNUnet daemon startup when user wanted to be notified
642 * once a hostkey was generated (for creating friends files, blacklists,
645 * @param daemon the daemon to finish starting
648 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon)
650 GNUNET_assert(daemon->phase == SP_HOSTKEY_CREATED);
651 daemon->phase = SP_TOPOLOGY_SETUP;
655 * Starts a GNUnet daemon. GNUnet must be installed on the target
656 * system and available in the PATH. The machine must furthermore be
657 * reachable via "ssh" (unless the hostname is "NULL") without the
658 * need to enter a password.
660 * @param sched scheduler to use
661 * @param cfg configuration to use
662 * @param hostname name of the machine where to run GNUnet
663 * (use NULL for localhost).
664 * @param hostkey_callback function to call once the hostkey has been
665 * generated for this peer, but it hasn't yet been started
666 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
667 * @param hostkey_cls closure for hostkey callback
668 * @param cb function to call with the result
669 * @param cb_cls closure for cb
670 * @return handle to the daemon (actual start will be completed asynchronously)
672 struct GNUNET_TESTING_Daemon *
673 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
674 const struct GNUNET_CONFIGURATION_Handle *cfg,
675 const char *hostname,
676 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
678 GNUNET_TESTING_NotifyDaemonRunning cb,
681 struct GNUNET_TESTING_Daemon *ret;
685 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
687 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
688 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
691 "Setting up peer with configuration file `%s'.\n",
694 if (NULL == ret->cfgfile)
696 GNUNET_free_non_null (ret->hostname);
700 ret->hostkey_callback = hostkey_callback;
701 ret->hostkey_cls = hostkey_cls;
703 ret->cb_cls = cb_cls;
704 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
705 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
707 "DEFAULTCONFIG", ret->cfgfile);
708 /* 1) write configuration to temporary file */
709 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
711 if (0 != UNLINK (ret->cfgfile))
712 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
713 "unlink", ret->cfgfile);
714 GNUNET_CONFIGURATION_destroy (ret->cfg);
715 GNUNET_free_non_null (ret->hostname);
716 GNUNET_free (ret->cfgfile);
721 GNUNET_CONFIGURATION_get_value_string (cfg,
723 "USERNAME", &username))
725 if (NULL != getenv ("USER"))
726 username = GNUNET_strdup (getenv ("USER"));
730 ret->username = username;
732 /* 2) copy file to remote host */
733 if (NULL != hostname)
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 "Copying configuration file to host `%s'.\n", hostname);
739 ret->phase = SP_COPYING;
740 if (NULL != username)
741 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
743 GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
744 ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
745 "scp", ret->cfgfile, arg, NULL);
749 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
751 ("Could not start `%s' process to copy configuration file.\n"),
753 if (0 != UNLINK (ret->cfgfile))
754 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
755 "unlink", ret->cfgfile);
756 GNUNET_CONFIGURATION_destroy (ret->cfg);
757 GNUNET_free_non_null (ret->hostname);
758 GNUNET_free_non_null (ret->username);
759 GNUNET_free (ret->cfgfile);
764 = GNUNET_SCHEDULER_add_delayed (sched,
765 GNUNET_CONSTANTS_EXEC_WAIT,
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "No need to copy configuration file since we are running locally.\n");
773 ret->phase = SP_COPIED;
774 GNUNET_SCHEDULER_add_continuation (sched,
777 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
783 * Restart (stop and start) a GNUnet daemon.
785 * @param d the daemon that should be restarted
786 * @param cb function called once the daemon is (re)started
787 * @param cb_cls closure for cb
790 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
791 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls)
799 d->dead = GNUNET_YES;
806 if (d->phase == SP_CONFIG_UPDATE)
808 GNUNET_SCHEDULER_cancel (d->sched, d->task);
809 d->phase = SP_START_DONE;
811 if (d->server != NULL)
813 GNUNET_CORE_disconnect (d->server);
819 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
820 GNUNET_TRANSPORT_disconnect(d->th);
823 /* state clean up and notifications */
824 GNUNET_free_non_null(d->hello);
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
828 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
831 d->phase = SP_START_ARMING;
833 /* Check if this is a local or remote process */
834 if (NULL != d->hostname)
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
841 if (d->username != NULL)
842 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
844 arg = GNUNET_strdup (d->hostname);
846 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
851 "-c", d->cfgfile, "-e", "-r", NULL);
852 /* Use -r to restart arm and all services */
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
862 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
867 "-c", d->cfgfile, "-e", "-r", NULL);
870 GNUNET_free_non_null(del_arg);
873 = GNUNET_SCHEDULER_add_delayed (d->sched,
874 GNUNET_CONSTANTS_EXEC_WAIT,
881 * Stops a GNUnet daemon.
883 * @param d the daemon that should be stopped
884 * @param cb function called once the daemon was stopped
885 * @param cb_cls closure for cb
886 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
887 * to leave them (i.e. for restarting at a later time,
888 * or logfile inspection once finished)
891 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
892 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
898 d->dead_cb_cls = cb_cls;
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
906 d->dead = GNUNET_YES;
911 if (delete_files == GNUNET_YES)
913 GNUNET_asprintf(&del_arg, "-d");
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);
926 /* shutdown ARM process (will terminate others) */
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
932 d->phase = SP_SHUTDOWN_START;
934 /* Check if this is a local or remote process */
935 if (NULL != d->hostname)
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
939 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
942 if (d->username != NULL)
943 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
945 arg = GNUNET_strdup (d->hostname);
947 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
952 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
953 /* Use -e to end arm, and -d to remove temp files */
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
961 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
963 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
968 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
971 GNUNET_free_non_null(del_arg);
974 = GNUNET_SCHEDULER_add_delayed (d->sched,
975 GNUNET_CONSTANTS_EXEC_WAIT,
981 * Changes the configuration of a GNUnet daemon.
983 * @param d the daemon that should be modified
984 * @param cfg the new configuration for the daemon
985 * @param cb function called once the configuration was changed
986 * @param cb_cls closure for cb
989 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
990 struct GNUNET_CONFIGURATION_Handle *cfg,
991 GNUNET_TESTING_NotifyCompletion cb,
996 if (d->phase != SP_START_DONE)
1001 ("Peer not yet running, can not change configuration at this point."));
1005 /* 1) write configuration to temporary file */
1006 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1009 cb (cb_cls, _("Failed to write new configuration to disk."));
1013 /* 2) copy file to remote host (if necessary) */
1014 if (NULL == d->hostname)
1016 /* signal success */
1022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1023 "Copying updated configuration file to remote host `%s'.\n",
1026 d->phase = SP_CONFIG_UPDATE;
1027 if (NULL != d->username)
1028 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1030 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1031 d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", d->cfgfile, arg, NULL);
1035 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1037 ("Could not start `%s' process to copy configuration file.\n"),
1040 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1041 d->phase = SP_START_DONE;
1045 d->update_cb_cls = cb_cls;
1047 = GNUNET_SCHEDULER_add_delayed (d->sched,
1048 GNUNET_CONSTANTS_EXEC_WAIT,
1054 * Data kept for each pair of peers that we try
1057 struct ConnectContext
1060 * Testing handle to the first daemon.
1062 struct GNUNET_TESTING_Daemon *d1;
1065 * Handle to core of first daemon (to check connect)
1067 struct GNUNET_CORE_Handle * d1core;
1070 * Testing handle to the second daemon.
1072 struct GNUNET_TESTING_Daemon *d2;
1075 * Transport handle to the second daemon.
1077 struct GNUNET_TRANSPORT_Handle *d2th;
1080 * Function to call once we are done (or have timed out).
1082 GNUNET_TESTING_NotifyConnection cb;
1090 * When should this operation be complete (or we must trigger
1093 struct GNUNET_TIME_Absolute timeout;
1096 * The relative timeout from whence this connect attempt was
1097 * started. Allows for reconnect attempts.
1099 struct GNUNET_TIME_Relative relative_timeout;
1102 * Maximum number of connect attempts, will retry connection
1103 * this number of times on failures.
1105 unsigned int max_connect_attempts;
1108 * Hello timeout task
1110 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1113 * Connect timeout task
1115 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1118 * When should this operation be complete (or we must trigger
1121 struct GNUNET_TIME_Relative timeout_hello;
1125 * Was the connection attempt successful?
1131 /** Forward declaration **/
1133 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1137 * Notify callback about success or failure of the attempt
1138 * to connect the two peers
1140 * @param cls our "struct ConnectContext" (freed)
1141 * @param tc reason tells us if we succeeded or failed
1144 notify_connect_result (void *cls,
1145 const struct GNUNET_SCHEDULER_TaskContext *tc)
1147 struct ConnectContext *ctx = cls;
1148 struct GNUNET_TIME_Relative remaining;
1150 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1152 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
1153 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1156 if ((ctx->timeout_task != GNUNET_SCHEDULER_NO_TASK) && (tc->reason != GNUNET_SCHEDULER_REASON_TIMEOUT))
1158 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1159 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1162 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1164 if (ctx->d2th != NULL)
1165 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1167 if (ctx->d1core != NULL)
1168 GNUNET_CORE_disconnect (ctx->d1core);
1175 remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
1177 if (ctx->connected == GNUNET_YES)
1179 if (ctx->cb != NULL)
1181 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
1182 ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1185 else if (remaining.value > 0)
1187 if (ctx->d1core != NULL)
1189 GNUNET_CORE_disconnect(ctx->d1core);
1193 if (ctx->d2th != NULL)
1195 GNUNET_TRANSPORT_disconnect(ctx->d2th);
1198 GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
1203 if (ctx->cb != NULL)
1205 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
1206 ctx->d2->cfg, ctx->d1, ctx->d2,
1207 _("Peers failed to connect"));
1211 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1213 GNUNET_CORE_disconnect (ctx->d1core);
1220 * Success, connection is up. Signal client our success.
1222 * @param cls our "struct ConnectContext"
1223 * @param peer identity of the peer that has connected
1224 * @param latency the round trip latency of the connection to this peer
1225 * @param distance distance the transport level distance to this peer
1229 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1232 struct ConnectContext *ctx = cls;
1234 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1236 ctx->connected = GNUNET_YES;
1237 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1238 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1239 GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1240 ¬ify_connect_result,
1247 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1249 struct ConnectContext *ctx = cls;
1251 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1254 if (ctx->d1->hello != NULL)
1256 GNUNET_TRANSPORT_offer_hello (ctx->d2th, GNUNET_HELLO_get_header(ctx->d1->hello));
1257 ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
1258 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1261 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
1267 * Establish a connection between two GNUnet daemons.
1269 * @param d1 handle for the first daemon
1270 * @param d2 handle for the second daemon
1271 * @param timeout how long is the connection attempt
1273 * @param max_connect_attempts how many times should we try to reconnect
1275 * @param cb function to call at the end
1276 * @param cb_cls closure for cb
1279 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1280 struct GNUNET_TESTING_Daemon *d2,
1281 struct GNUNET_TIME_Relative timeout,
1282 unsigned int max_connect_attempts,
1283 GNUNET_TESTING_NotifyConnection cb,
1286 struct ConnectContext *ctx;
1288 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1291 cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1292 _("Peers are not fully running yet, can not connect!\n"));
1295 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1298 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1299 ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
1300 ctx->relative_timeout = timeout;
1302 ctx->cb_cls = cb_cls;
1303 ctx->max_connect_attempts = max_connect_attempts;
1304 ctx->connected = GNUNET_NO;
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307 "Asked to connect peer %s to peer %s\n",
1308 d1->shortname, d2->shortname);
1311 ctx->d1core = GNUNET_CORE_connect (d1->sched,
1316 &connect_notify, NULL,
1318 NULL, GNUNET_NO, no_handlers);
1319 if (ctx->d1core == NULL)
1323 cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1324 _("Failed to connect to core service of first peer!\n"));
1328 #if DEBUG_TESTING > 2
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "Asked to connect peer %s to peer %s\n",
1331 d1->shortname, d2->shortname);
1332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1333 "Connecting to transport service of peer %s\n", d2->shortname);
1337 ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
1338 d2->cfg, d2, NULL, NULL, NULL);
1339 if (ctx->d2th == NULL)
1341 GNUNET_CORE_disconnect(ctx->d1core);
1344 cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1345 _("Failed to connect to transport service!\n"));
1349 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
1350 GNUNET_TIME_relative_divide(ctx->relative_timeout, max_connect_attempts), /* Allow up to 8 reconnect attempts */
1351 ¬ify_connect_result, ctx);
1353 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1357 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1360 struct ConnectContext *ctx = cls;
1361 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1365 #if DEBUG_TESTING_RECONNECT
1366 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "re-attempting connect of peer %s to peer %s\n",
1367 ctx->d1->shortname, ctx->d2->shortname);
1370 GNUNET_assert(ctx->d1core == NULL);
1372 ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1374 GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1377 &connect_notify, NULL,
1379 NULL, GNUNET_NO, no_handlers);
1380 if (ctx->d1core == NULL)
1382 if (NULL != ctx->cb)
1383 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1384 _("Failed to connect to core service of first peer!\n"));
1389 ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1390 ctx->d2->cfg, ctx->d2, NULL, NULL, NULL);
1391 if (ctx->d2th == NULL)
1393 GNUNET_CORE_disconnect(ctx->d1core);
1395 if (NULL != ctx->cb)
1396 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1397 _("Failed to connect to transport service!\n"));
1401 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1402 GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1403 ¬ify_connect_result, ctx);
1405 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1408 /* end of testing.c */