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,
164 d->cfg, d, NULL, NULL, NULL);
167 if (GNUNET_YES == d->dead)
168 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
169 else if (NULL != d->cb)
170 d->cb (d->cb_cls, &d->id, d->cfg, d,
171 _("Failed to connect to transport service!\n"));
175 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
180 * Finite-state machine for starting GNUnet.
182 * @param cls our "struct GNUNET_TESTING_Daemon"
186 start_fsm (void *cls,
187 const struct GNUNET_SCHEDULER_TaskContext *tc)
189 struct GNUNET_TESTING_Daemon *d = cls;
190 GNUNET_TESTING_NotifyDaemonRunning cb;
191 enum GNUNET_OS_ProcessStatusType type;
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Peer FSM is in phase %u.\n", d->phase);
201 d->task = GNUNET_SCHEDULER_NO_TASK;
205 /* confirm copying complete */
206 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
208 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
215 d->cfg, d, _("`scp' does not seem to terminate (timeout copying config).\n"));
220 = GNUNET_SCHEDULER_add_delayed (d->sched,
221 GNUNET_CONSTANTS_EXEC_WAIT,
225 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
231 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Successfully copied configuration file.\n");
238 d->phase = SP_COPIED;
241 /* Start create hostkey process */
242 d->pipe_stdout = GNUNET_DISK_pipe(GNUNET_NO);
243 if (d->pipe_stdout == NULL)
252 (NULL == d->hostname)
253 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
254 : _("Failed to create pipe for `ssh' process.\n"));
257 if (NULL == d->hostname)
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Starting `%s', with command `%s %s %s %s'.\n",
262 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
265 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
269 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
273 if (d->username != NULL)
274 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
276 dst = GNUNET_strdup (d->hostname);
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
281 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c", d->cfgfile,
284 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
288 "-c", d->cfgfile, "-sq", NULL);
289 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
295 _("Could not start `%s' process to create hostkey.\n"),
296 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
304 (NULL == d->hostname)
305 ? _("Failed to start `gnunet-peerinfo' process.\n")
306 : _("Failed to start `ssh' process.\n"));
307 GNUNET_DISK_pipe_close(d->pipe_stdout);
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "Started `%s', waiting for hostkey.\n",
315 d->phase = SP_HOSTKEY_CREATE;
317 = GNUNET_SCHEDULER_add_read_file (d->sched,
318 GNUNET_TIME_absolute_get_remaining(d->max_timeout),
319 GNUNET_DISK_pipe_handle(d->pipe_stdout,
320 GNUNET_DISK_PIPE_END_READ),
324 case SP_HOSTKEY_CREATE:
325 bytes_read = GNUNET_DISK_file_read(GNUNET_DISK_pipe_handle(d->pipe_stdout,
326 GNUNET_DISK_PIPE_END_READ),
327 &d->hostkeybuf[d->hostkeybufpos],
328 sizeof(d->hostkeybuf) - d->hostkeybufpos);
330 d->hostkeybufpos += bytes_read;
332 if ( (d->hostkeybufpos < 104) &&
337 = GNUNET_SCHEDULER_add_read_file (d->sched,
338 GNUNET_TIME_absolute_get_remaining(d->max_timeout),
339 GNUNET_DISK_pipe_handle(d->pipe_stdout,
340 GNUNET_DISK_PIPE_END_READ),
345 d->hostkeybuf[103] = '\0';
346 if ( (bytes_read < 0) ||
347 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
348 &d->id.hashPubKey)) )
352 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
353 _("Error reading from gnunet-peerinfo: %s\n"),
356 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
357 _("Malformed output from gnunet-peerinfo!\n"));
360 GNUNET_DISK_pipe_close(d->pipe_stdout);
361 d->pipe_stdout = NULL;
362 (void) PLIBC_KILL (d->pid, SIGKILL);
363 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
370 _("`Failed to get hostkey!\n"));
373 GNUNET_DISK_pipe_close(d->pipe_stdout);
374 d->pipe_stdout = NULL;
375 (void) PLIBC_KILL (d->pid, SIGKILL);
376 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Successfully got hostkey!\n");
382 if (d->hostkey_callback != NULL)
384 d->hostkey_callback(d->hostkey_cls, &d->id, d, NULL);
385 d->phase = SP_HOSTKEY_CREATED;
389 d->phase = SP_TOPOLOGY_SETUP;
392 case SP_HOSTKEY_CREATED:
393 /* wait for topology finished */
394 if ((GNUNET_YES == d->dead) || (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0))
403 _("`Failed while waiting for topology setup!\n"));
408 = GNUNET_SCHEDULER_add_delayed (d->sched,
409 GNUNET_CONSTANTS_EXEC_WAIT,
412 case SP_TOPOLOGY_SETUP:
413 /* start GNUnet on remote host */
414 if (NULL == d->hostname)
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
419 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
423 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
433 if (d->username != NULL)
434 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
436 dst = GNUNET_strdup (d->hostname);
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
441 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
442 "-L", "DEBUG", "-s", "-q");
444 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
451 "-c", d->cfgfile, "-s", "-q", NULL);
456 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
457 _("Could not start `%s' process to start GNUnet.\n"),
458 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
466 (NULL == d->hostname)
467 ? _("Failed to start `gnunet-arm' process.\n")
468 : _("Failed to start `ssh' process.\n"));
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Started `%s', waiting for `%s' to be up.\n",
474 "gnunet-arm", "gnunet-service-core");
476 d->phase = SP_START_ARMING;
478 = GNUNET_SCHEDULER_add_delayed (d->sched,
479 GNUNET_CONSTANTS_EXEC_WAIT,
482 case SP_START_ARMING:
483 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
485 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
494 (NULL == d->hostname)
495 ? _("`gnunet-arm' does not seem to terminate.\n")
496 : _("`ssh' does not seem to terminate.\n"));
501 = GNUNET_SCHEDULER_add_delayed (d->sched,
502 GNUNET_CONSTANTS_EXEC_WAIT,
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "Successfully started `%s'.\n", "gnunet-arm");
510 d->phase = SP_START_CORE;
511 d->server = GNUNET_CORE_connect (d->sched,
518 NULL, GNUNET_NO, no_handlers);
526 case SP_SHUTDOWN_START:
527 /* confirm copying complete */
528 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
530 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
532 if (NULL != d->dead_cb)
533 d->dead_cb (d->dead_cb_cls,
534 _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
537 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
538 GNUNET_TRANSPORT_disconnect(d->th);
541 GNUNET_CONFIGURATION_destroy (d->cfg);
542 GNUNET_free (d->cfgfile);
543 GNUNET_free_non_null(d->hello);
544 GNUNET_free_non_null (d->hostname);
545 GNUNET_free_non_null (d->username);
546 GNUNET_free_non_null (d->shortname);
552 = GNUNET_SCHEDULER_add_delayed (d->sched,
553 GNUNET_CONSTANTS_EXEC_WAIT,
557 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
559 if (NULL != d->dead_cb)
560 d->dead_cb (d->dead_cb_cls,
561 _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
564 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
565 GNUNET_TRANSPORT_disconnect(d->th);
568 GNUNET_CONFIGURATION_destroy (d->cfg);
569 GNUNET_free (d->cfgfile);
570 GNUNET_free_non_null(d->hello);
571 GNUNET_free_non_null (d->hostname);
572 GNUNET_free_non_null (d->username);
573 GNUNET_free_non_null (d->shortname);
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
582 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
583 GNUNET_TRANSPORT_disconnect(d->th);
586 /* state clean up and notifications */
587 if (d->churn == GNUNET_NO)
589 GNUNET_CONFIGURATION_destroy (d->cfg);
590 GNUNET_free (d->cfgfile);
591 GNUNET_free_non_null (d->hostname);
592 GNUNET_free_non_null (d->username);
595 GNUNET_free_non_null(d->hello);
597 GNUNET_free_non_null (d->shortname);
599 if (NULL != d->dead_cb)
600 d->dead_cb (d->dead_cb_cls, NULL);
602 if (d->churn == GNUNET_NO)
606 case SP_CONFIG_UPDATE:
607 /* confirm copying complete */
608 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
610 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0) /* FIXME: config update should take timeout parameter! */
617 d->cfg, d, _("`scp' does not seem to terminate.\n"));
622 = GNUNET_SCHEDULER_add_delayed (d->sched,
623 GNUNET_CONSTANTS_EXEC_WAIT,
627 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
629 if (NULL != d->update_cb)
630 d->update_cb (d->update_cb_cls,
631 _("`scp' did not complete cleanly.\n"));
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Successfully copied configuration file.\n");
638 if (NULL != d->update_cb)
639 d->update_cb (d->update_cb_cls, NULL);
640 d->phase = SP_START_DONE;
646 * Continues GNUnet daemon startup when user wanted to be notified
647 * once a hostkey was generated (for creating friends files, blacklists,
650 * @param daemon the daemon to finish starting
653 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon)
655 GNUNET_assert(daemon->phase == SP_HOSTKEY_CREATED);
656 daemon->phase = SP_TOPOLOGY_SETUP;
661 * Start a peer that has previously been stopped using the daemon_stop
662 * call (and files weren't deleted and the allow restart flag)
664 * @param daemon the daemon to start (has been previously stopped)
665 * @param timeout how long to wait for restart
666 * @param cb the callback for notification when the peer is running
667 * @param cb_cls closure for the callback
670 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
671 struct GNUNET_TIME_Relative timeout,
672 GNUNET_TESTING_NotifyDaemonRunning cb,
675 if (daemon->running == GNUNET_YES)
677 cb(cb_cls, &daemon->id, daemon->cfg, daemon, "Daemon already running, can't restart!");
682 daemon->cb_cls = cb_cls;
683 daemon->phase = SP_TOPOLOGY_SETUP;
684 daemon->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
686 GNUNET_SCHEDULER_add_continuation (daemon->sched,
689 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
693 * Starts a GNUnet daemon. GNUnet must be installed on the target
694 * system and available in the PATH. The machine must furthermore be
695 * reachable via "ssh" (unless the hostname is "NULL") without the
696 * need to enter a password.
698 * @param sched scheduler to use
699 * @param cfg configuration to use
700 * @param timeout how long to wait starting up peers
701 * @param hostname name of the machine where to run GNUnet
702 * (use NULL for localhost).
703 * @param hostkey_callback function to call once the hostkey has been
704 * generated for this peer, but it hasn't yet been started
705 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
706 * @param hostkey_cls closure for hostkey callback
707 * @param cb function to call once peer is up, or failed to start
708 * @param cb_cls closure for cb
709 * @return handle to the daemon (actual start will be completed asynchronously)
711 struct GNUNET_TESTING_Daemon *
712 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
713 const struct GNUNET_CONFIGURATION_Handle *cfg,
714 struct GNUNET_TIME_Relative timeout,
715 const char *hostname,
716 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
718 GNUNET_TESTING_NotifyDaemonRunning cb,
721 struct GNUNET_TESTING_Daemon *ret;
725 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
727 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
728 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
731 "Setting up peer with configuration file `%s'.\n",
734 if (NULL == ret->cfgfile)
736 GNUNET_free_non_null (ret->hostname);
740 ret->hostkey_callback = hostkey_callback;
741 ret->hostkey_cls = hostkey_cls;
743 ret->cb_cls = cb_cls;
744 ret->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
745 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
746 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
748 "DEFAULTCONFIG", ret->cfgfile);
749 /* 1) write configuration to temporary file */
750 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
752 if (0 != UNLINK (ret->cfgfile))
753 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
754 "unlink", ret->cfgfile);
755 GNUNET_CONFIGURATION_destroy (ret->cfg);
756 GNUNET_free_non_null (ret->hostname);
757 GNUNET_free (ret->cfgfile);
762 GNUNET_CONFIGURATION_get_value_string (cfg,
764 "USERNAME", &username))
766 if (NULL != getenv ("USER"))
767 username = GNUNET_strdup (getenv ("USER"));
771 ret->username = username;
773 /* 2) copy file to remote host */
774 if (NULL != hostname)
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
778 "Copying configuration file to host `%s'.\n", hostname);
780 ret->phase = SP_COPYING;
781 if (NULL != username)
782 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
784 GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
785 ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
786 "scp", ret->cfgfile, arg, NULL);
790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
792 ("Could not start `%s' process to copy configuration file.\n"),
794 if (0 != UNLINK (ret->cfgfile))
795 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
796 "unlink", ret->cfgfile);
797 GNUNET_CONFIGURATION_destroy (ret->cfg);
798 GNUNET_free_non_null (ret->hostname);
799 GNUNET_free_non_null (ret->username);
800 GNUNET_free (ret->cfgfile);
805 = GNUNET_SCHEDULER_add_delayed (sched,
806 GNUNET_CONSTANTS_EXEC_WAIT,
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "No need to copy configuration file since we are running locally.\n");
814 ret->phase = SP_COPIED;
815 GNUNET_SCHEDULER_add_continuation (sched,
818 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
824 * Restart (stop and start) a GNUnet daemon.
826 * @param d the daemon that should be restarted
827 * @param cb function called once the daemon is (re)started
828 * @param cb_cls closure for cb
831 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
832 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls)
840 d->dead = GNUNET_YES;
847 if (d->phase == SP_CONFIG_UPDATE)
849 GNUNET_SCHEDULER_cancel (d->sched, d->task);
850 d->phase = SP_START_DONE;
852 if (d->server != NULL)
854 GNUNET_CORE_disconnect (d->server);
860 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
861 GNUNET_TRANSPORT_disconnect(d->th);
864 /* state clean up and notifications */
865 GNUNET_free_non_null(d->hello);
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
869 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
872 d->phase = SP_START_ARMING;
874 /* Check if this is a local or remote process */
875 if (NULL != d->hostname)
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
882 if (d->username != NULL)
883 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
885 arg = GNUNET_strdup (d->hostname);
887 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
892 "-c", d->cfgfile, "-e", "-r", NULL);
893 /* Use -r to restart arm and all services */
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
903 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
908 "-c", d->cfgfile, "-e", "-r", NULL);
911 GNUNET_free_non_null(del_arg);
913 = GNUNET_SCHEDULER_add_delayed (d->sched,
914 GNUNET_CONSTANTS_EXEC_WAIT,
921 * Stops a GNUnet daemon.
923 * @param d the daemon that should be stopped
924 * @param timeout how long to wait for process for shutdown to complete
925 * @param cb function called once the daemon was stopped
926 * @param cb_cls closure for cb
927 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
929 * @param allow_restart GNUNET_YES to restart peer later (using this API)
930 * GNUNET_NO to kill off and clean up for good
933 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
934 struct GNUNET_TIME_Relative timeout,
935 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
942 d->dead_cb_cls = cb_cls;
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
950 d->dead = GNUNET_YES;
954 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
956 /* Free what was left from churning! */
957 GNUNET_assert(d->cfg != NULL);
958 GNUNET_CONFIGURATION_destroy (d->cfg);
959 if (delete_files == GNUNET_YES)
961 if (0 != UNLINK(d->cfgfile))
963 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "unlink");
966 GNUNET_free (d->cfgfile);
967 GNUNET_free_non_null (d->hostname);
968 GNUNET_free_non_null (d->username);
969 if (NULL != d->dead_cb)
970 d->dead_cb (d->dead_cb_cls, NULL);
975 if (delete_files == GNUNET_YES)
977 GNUNET_asprintf(&del_arg, "-d");
980 if (d->phase == SP_CONFIG_UPDATE)
982 GNUNET_SCHEDULER_cancel (d->sched, d->task);
983 d->phase = SP_START_DONE;
985 if (d->server != NULL)
987 GNUNET_CORE_disconnect (d->server);
990 /* shutdown ARM process (will terminate others) */
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
995 d->phase = SP_SHUTDOWN_START;
996 d->running = GNUNET_NO;
997 if (allow_restart == GNUNET_YES)
998 d->churn = GNUNET_YES;
1001 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
1002 GNUNET_TRANSPORT_disconnect(d->th);
1005 /* Check if this is a local or remote process */
1006 if (NULL != d->hostname)
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
1013 if (d->username != NULL)
1014 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1016 arg = GNUNET_strdup (d->hostname);
1018 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1023 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1024 /* Use -e to end arm, and -d to remove temp files */
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1033 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1038 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1041 GNUNET_free_non_null(del_arg);
1042 d->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
1044 = GNUNET_SCHEDULER_add_now (d->sched,
1050 * Changes the configuration of a GNUnet daemon.
1052 * @param d the daemon that should be modified
1053 * @param cfg the new configuration for the daemon
1054 * @param cb function called once the configuration was changed
1055 * @param cb_cls closure for cb
1058 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1059 struct GNUNET_CONFIGURATION_Handle *cfg,
1060 GNUNET_TESTING_NotifyCompletion cb,
1065 if (d->phase != SP_START_DONE)
1070 ("Peer not yet running, can not change configuration at this point."));
1074 /* 1) write configuration to temporary file */
1075 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1078 cb (cb_cls, _("Failed to write new configuration to disk."));
1082 /* 2) copy file to remote host (if necessary) */
1083 if (NULL == d->hostname)
1085 /* signal success */
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1092 "Copying updated configuration file to remote host `%s'.\n",
1095 d->phase = SP_CONFIG_UPDATE;
1096 if (NULL != d->username)
1097 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1099 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1100 d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", d->cfgfile, arg, NULL);
1104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1106 ("Could not start `%s' process to copy configuration file.\n"),
1109 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1110 d->phase = SP_START_DONE;
1114 d->update_cb_cls = cb_cls;
1116 = GNUNET_SCHEDULER_add_delayed (d->sched,
1117 GNUNET_CONSTANTS_EXEC_WAIT,
1123 * Data kept for each pair of peers that we try
1126 struct ConnectContext
1129 * Testing handle to the first daemon.
1131 struct GNUNET_TESTING_Daemon *d1;
1134 * Handle to core of first daemon (to check connect)
1136 struct GNUNET_CORE_Handle * d1core;
1139 * Testing handle to the second daemon.
1141 struct GNUNET_TESTING_Daemon *d2;
1144 * Transport handle to the second daemon.
1146 struct GNUNET_TRANSPORT_Handle *d2th;
1149 * Function to call once we are done (or have timed out).
1151 GNUNET_TESTING_NotifyConnection cb;
1159 * When should this operation be complete (or we must trigger
1162 struct GNUNET_TIME_Absolute timeout;
1165 * The relative timeout from whence this connect attempt was
1166 * started. Allows for reconnect attempts.
1168 struct GNUNET_TIME_Relative relative_timeout;
1171 * Maximum number of connect attempts, will retry connection
1172 * this number of times on failures.
1174 unsigned int max_connect_attempts;
1177 * Hello timeout task
1179 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1182 * Connect timeout task
1184 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1187 * When should this operation be complete (or we must trigger
1190 struct GNUNET_TIME_Relative timeout_hello;
1193 * Was the connection attempt successful?
1198 * The distance between the two connected peers
1204 /** Forward declaration **/
1206 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1210 * Notify callback about success or failure of the attempt
1211 * to connect the two peers
1213 * @param cls our "struct ConnectContext" (freed)
1214 * @param tc reason tells us if we succeeded or failed
1217 notify_connect_result (void *cls,
1218 const struct GNUNET_SCHEDULER_TaskContext *tc)
1220 struct ConnectContext *ctx = cls;
1221 struct GNUNET_TIME_Relative remaining;
1223 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1224 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1226 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
1227 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1229 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1231 if (ctx->d2th != NULL)
1232 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1234 if (ctx->d1core != NULL)
1235 GNUNET_CORE_disconnect (ctx->d1core);
1242 remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
1244 if (ctx->connected == GNUNET_YES)
1246 if (ctx->cb != NULL)
1248 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance, ctx->d1->cfg,
1249 ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1252 else if (remaining.value > 0)
1254 if (ctx->d1core != NULL)
1256 GNUNET_CORE_disconnect(ctx->d1core);
1260 if (ctx->d2th != NULL)
1262 GNUNET_TRANSPORT_disconnect(ctx->d2th);
1265 GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
1270 if (ctx->cb != NULL)
1272 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1273 ctx->d2->cfg, ctx->d1, ctx->d2,
1274 _("Peers failed to connect"));
1278 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1280 GNUNET_CORE_disconnect (ctx->d1core);
1287 * Success, connection is up. Signal client our success.
1289 * @param cls our "struct ConnectContext"
1290 * @param peer identity of the peer that has connected
1291 * @param latency the round trip latency of the connection to this peer
1292 * @param distance distance the transport level distance to this peer
1296 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1299 struct ConnectContext *ctx = cls;
1301 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1303 ctx->connected = GNUNET_YES;
1304 ctx->distance = distance;
1305 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1306 ctx->timeout_task = GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1307 ¬ify_connect_result,
1314 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1316 struct ConnectContext *ctx = cls;
1318 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1319 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1321 if (ctx->d1->hello != NULL)
1323 GNUNET_TRANSPORT_offer_hello (ctx->d2th, GNUNET_HELLO_get_header(ctx->d1->hello));
1324 ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
1325 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1328 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
1334 * Establish a connection between two GNUnet daemons.
1336 * @param d1 handle for the first daemon
1337 * @param d2 handle for the second daemon
1338 * @param timeout how long is the connection attempt
1340 * @param max_connect_attempts how many times should we try to reconnect
1342 * @param cb function to call at the end
1343 * @param cb_cls closure for cb
1346 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1347 struct GNUNET_TESTING_Daemon *d2,
1348 struct GNUNET_TIME_Relative timeout,
1349 unsigned int max_connect_attempts,
1350 GNUNET_TESTING_NotifyConnection cb,
1353 struct ConnectContext *ctx;
1355 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1358 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1359 _("Peers are not fully running yet, can not connect!\n"));
1362 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1365 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1366 ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
1367 ctx->relative_timeout = timeout;
1369 ctx->cb_cls = cb_cls;
1370 ctx->max_connect_attempts = max_connect_attempts;
1371 ctx->connected = GNUNET_NO;
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374 "Asked to connect peer %s to peer %s\n",
1375 d1->shortname, d2->shortname);
1378 ctx->d1core = GNUNET_CORE_connect (d1->sched,
1383 &connect_notify, NULL,
1385 NULL, GNUNET_NO, no_handlers);
1386 if (ctx->d1core == NULL)
1390 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1391 _("Failed to connect to core service of first peer!\n"));
1395 #if DEBUG_TESTING > 2
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "Asked to connect peer %s to peer %s\n",
1398 d1->shortname, d2->shortname);
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400 "Connecting to transport service of peer %s\n", d2->shortname);
1404 ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
1405 d2->cfg, d2, NULL, NULL, NULL);
1406 if (ctx->d2th == NULL)
1408 GNUNET_CORE_disconnect(ctx->d1core);
1411 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1412 _("Failed to connect to transport service!\n"));
1416 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
1417 GNUNET_TIME_relative_divide(ctx->relative_timeout,
1418 max_connect_attempts),
1419 ¬ify_connect_result, ctx);
1421 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1425 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1428 struct ConnectContext *ctx = cls;
1429 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1433 #if DEBUG_TESTING_RECONNECT
1434 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "re-attempting connect of peer %s to peer %s\n",
1435 ctx->d1->shortname, ctx->d2->shortname);
1438 GNUNET_assert(ctx->d1core == NULL);
1440 ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1442 GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1445 &connect_notify, NULL,
1447 NULL, GNUNET_NO, no_handlers);
1448 if (ctx->d1core == NULL)
1450 if (NULL != ctx->cb)
1451 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1452 _("Failed to connect to core service of first peer!\n"));
1457 ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1458 ctx->d2->cfg, ctx->d2, NULL, NULL, NULL);
1459 if (ctx->d2th == NULL)
1461 GNUNET_CORE_disconnect(ctx->d1core);
1463 if (NULL != ctx->cb)
1464 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1465 _("Failed to connect to transport service!\n"));
1469 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1470 GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1471 ¬ify_connect_result, ctx);
1473 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1476 /* end of testing.c */