2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing.c
23 * @brief convenience API for writing testcases for GNUnet
24 * Many testcases need to start and stop gnunetd,
25 * and this library is supposed to make that easier
26 * for TESTCASES. Normal programs should always
27 * use functions from gnunet_{util,arm}_lib.h. This API is
28 * ONLY for writing testcases!
29 * @author Christian Grothoff
33 #include "gnunet_arm_service.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_testing_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_hello_lib.h"
40 #define DEBUG_TESTING GNUNET_NO
41 #define DEBUG_TESTING_RECONNECT GNUNET_NO
44 * How long do we wait after starting gnunet-service-arm
45 * for the core service to be alive?
47 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
50 * How many times are we willing to try to wait for "scp" or
51 * "gnunet-service-arm" to complete (waitpid) before giving up?
53 #define MAX_EXEC_WAIT_RUNS 250
55 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
58 * Receive the HELLO from one peer, give it to the other
59 * and ask them to connect.
61 * @param cls "struct ConnectContext"
62 * @param message HELLO message of peer
65 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
67 struct GNUNET_TESTING_Daemon *daemon = cls;
72 if (daemon->server != NULL)
74 GNUNET_CORE_disconnect(daemon->server);
75 daemon->server = NULL;
78 GNUNET_assert (message != NULL);
79 msize = ntohs(message->size);
84 if (daemon->th != NULL)
86 GNUNET_TRANSPORT_get_hello_cancel(daemon->th, &process_hello, daemon);
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90 "Received `%s' from transport service of `%4s'\n",
91 "HELLO", GNUNET_i2s (&daemon->id));
97 GNUNET_free_non_null(daemon->hello);
98 daemon->hello = GNUNET_malloc(msize);
99 memcpy(daemon->hello, message, msize);
101 if (daemon->th != NULL)
103 GNUNET_TRANSPORT_disconnect(daemon->th);
111 * Function called after GNUNET_CORE_connect has succeeded
112 * (or failed for good). Note that the private key of the
113 * peer is intentionally not exposed here; if you need it,
114 * your process should try to read the private key file
115 * directly (which should work if you are authorized...).
118 * @param server handle to the server, NULL if we failed
119 * @param my_identity ID of this peer, NULL if we failed
120 * @param publicKey public key of this peer, NULL if we failed
123 testing_init (void *cls,
124 struct GNUNET_CORE_Handle *server,
125 const struct GNUNET_PeerIdentity *my_identity,
126 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
128 struct GNUNET_TESTING_Daemon *d = cls;
129 GNUNET_TESTING_NotifyDaemonRunning cb;
131 GNUNET_assert (d->phase == SP_START_CORE);
132 d->phase = SP_START_DONE;
138 if (GNUNET_YES == d->dead)
139 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
141 cb (d->cb_cls, NULL, d->cfg, d,
142 _("Failed to connect to core service\n"));
146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
149 d->id = *my_identity;
150 d->shortname = strdup (GNUNET_i2s (my_identity));
152 d->running = GNUNET_YES;
153 if (GNUNET_YES == d->dead)
154 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
156 cb (d->cb_cls, my_identity, d->cfg, d, NULL);
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
163 d->th = GNUNET_TRANSPORT_connect (d->sched,
166 d, NULL, NULL, NULL);
169 if (GNUNET_YES == d->dead)
170 GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
171 else if (NULL != d->cb)
172 d->cb (d->cb_cls, &d->id, d->cfg, d,
173 _("Failed to connect to transport service!\n"));
177 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
182 * Finite-state machine for starting GNUnet.
184 * @param cls our "struct GNUNET_TESTING_Daemon"
188 start_fsm (void *cls,
189 const struct GNUNET_SCHEDULER_TaskContext *tc)
191 struct GNUNET_TESTING_Daemon *d = cls;
192 GNUNET_TESTING_NotifyDaemonRunning cb;
193 enum GNUNET_OS_ProcessStatusType type;
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Peer FSM is in phase %u.\n", d->phase);
203 d->task = GNUNET_SCHEDULER_NO_TASK;
207 /* confirm copying complete */
208 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
210 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
217 d->cfg, d, _("`scp' does not seem to terminate (timeout copying config).\n"));
222 = GNUNET_SCHEDULER_add_delayed (d->sched,
223 GNUNET_CONSTANTS_EXEC_WAIT,
227 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
233 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Successfully copied configuration file.\n");
240 d->phase = SP_COPIED;
243 /* Start create hostkey process */
244 d->pipe_stdout = GNUNET_DISK_pipe(GNUNET_NO);
245 if (d->pipe_stdout == NULL)
254 (NULL == d->hostname)
255 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
256 : _("Failed to create pipe for `ssh' process.\n"));
259 if (NULL == d->hostname)
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263 "Starting `%s', with command `%s %s %s %s'.\n",
264 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
267 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
271 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
275 if (d->username != NULL)
276 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
278 dst = GNUNET_strdup (d->hostname);
281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
282 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
283 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c", d->cfgfile,
286 d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
290 "-c", d->cfgfile, "-sq", NULL);
291 GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
297 _("Could not start `%s' process to create hostkey.\n"),
298 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
306 (NULL == d->hostname)
307 ? _("Failed to start `gnunet-peerinfo' process.\n")
308 : _("Failed to start `ssh' process.\n"));
309 GNUNET_DISK_pipe_close(d->pipe_stdout);
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Started `%s', waiting for hostkey.\n",
317 d->phase = SP_HOSTKEY_CREATE;
319 = GNUNET_SCHEDULER_add_read_file (d->sched,
320 GNUNET_TIME_absolute_get_remaining(d->max_timeout),
321 GNUNET_DISK_pipe_handle(d->pipe_stdout,
322 GNUNET_DISK_PIPE_END_READ),
326 case SP_HOSTKEY_CREATE:
327 bytes_read = GNUNET_DISK_file_read(GNUNET_DISK_pipe_handle(d->pipe_stdout,
328 GNUNET_DISK_PIPE_END_READ),
329 &d->hostkeybuf[d->hostkeybufpos],
330 sizeof(d->hostkeybuf) - d->hostkeybufpos);
332 d->hostkeybufpos += bytes_read;
334 if ( (d->hostkeybufpos < 104) &&
339 = GNUNET_SCHEDULER_add_read_file (d->sched,
340 GNUNET_TIME_absolute_get_remaining(d->max_timeout),
341 GNUNET_DISK_pipe_handle(d->pipe_stdout,
342 GNUNET_DISK_PIPE_END_READ),
347 d->hostkeybuf[103] = '\0';
348 if ( (bytes_read < 0) ||
349 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
350 &d->id.hashPubKey)) )
354 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
355 _("Error reading from gnunet-peerinfo: %s\n"),
358 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
359 _("Malformed output from gnunet-peerinfo!\n"));
362 GNUNET_DISK_pipe_close(d->pipe_stdout);
363 d->pipe_stdout = NULL;
364 (void) PLIBC_KILL (d->pid, SIGKILL);
365 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
372 _("`Failed to get hostkey!\n"));
375 GNUNET_DISK_pipe_close(d->pipe_stdout);
376 d->pipe_stdout = NULL;
377 (void) PLIBC_KILL (d->pid, SIGKILL);
378 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->pid));
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Successfully got hostkey!\n");
384 if (d->hostkey_callback != NULL)
386 d->hostkey_callback(d->hostkey_cls, &d->id, d, NULL);
387 d->phase = SP_HOSTKEY_CREATED;
391 d->phase = SP_TOPOLOGY_SETUP;
394 case SP_HOSTKEY_CREATED:
395 /* wait for topology finished */
396 if ((GNUNET_YES == d->dead) || (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0))
405 _("`Failed while waiting for topology setup!\n"));
410 = GNUNET_SCHEDULER_add_delayed (d->sched,
411 GNUNET_CONSTANTS_EXEC_WAIT,
414 case SP_TOPOLOGY_SETUP:
415 /* start GNUnet on remote host */
416 if (NULL == d->hostname)
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
421 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
425 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
435 if (d->username != NULL)
436 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
438 dst = GNUNET_strdup (d->hostname);
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
443 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
444 "-L", "DEBUG", "-s", "-q");
446 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
453 "-c", d->cfgfile, "-s", "-q", NULL);
458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
459 _("Could not start `%s' process to start GNUnet.\n"),
460 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
468 (NULL == d->hostname)
469 ? _("Failed to start `gnunet-arm' process.\n")
470 : _("Failed to start `ssh' process.\n"));
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 "Started `%s', waiting for `%s' to be up.\n",
476 "gnunet-arm", "gnunet-service-core");
478 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))
487 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
496 (NULL == d->hostname)
497 ? _("`gnunet-arm' does not seem to terminate.\n")
498 : _("`ssh' does not seem to terminate.\n"));
503 = GNUNET_SCHEDULER_add_delayed (d->sched,
504 GNUNET_CONSTANTS_EXEC_WAIT,
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Successfully started `%s'.\n", "gnunet-arm");
512 d->phase = SP_START_CORE;
513 d->server = GNUNET_CORE_connect (d->sched,
520 NULL, GNUNET_NO, no_handlers);
528 case SP_SHUTDOWN_START:
529 /* confirm copying complete */
530 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
532 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
534 if (NULL != d->dead_cb)
535 d->dead_cb (d->dead_cb_cls,
536 _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
539 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
540 GNUNET_TRANSPORT_disconnect(d->th);
543 GNUNET_CONFIGURATION_destroy (d->cfg);
544 GNUNET_free (d->cfgfile);
545 GNUNET_free_non_null(d->hello);
546 GNUNET_free_non_null (d->hostname);
547 GNUNET_free_non_null (d->username);
548 GNUNET_free_non_null (d->shortname);
554 = GNUNET_SCHEDULER_add_delayed (d->sched,
555 GNUNET_CONSTANTS_EXEC_WAIT,
559 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
561 if (NULL != d->dead_cb)
562 d->dead_cb (d->dead_cb_cls,
563 _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
566 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
567 GNUNET_TRANSPORT_disconnect(d->th);
570 GNUNET_CONFIGURATION_destroy (d->cfg);
571 GNUNET_free (d->cfgfile);
572 GNUNET_free_non_null(d->hello);
573 GNUNET_free_non_null (d->hostname);
574 GNUNET_free_non_null (d->username);
575 GNUNET_free_non_null (d->shortname);
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
584 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
585 GNUNET_TRANSPORT_disconnect(d->th);
588 /* state clean up and notifications */
589 if (d->churn == GNUNET_NO)
591 GNUNET_CONFIGURATION_destroy (d->cfg);
592 GNUNET_free (d->cfgfile);
593 GNUNET_free_non_null (d->hostname);
594 GNUNET_free_non_null (d->username);
597 GNUNET_free_non_null(d->hello);
599 GNUNET_free_non_null (d->shortname);
601 if (NULL != d->dead_cb)
602 d->dead_cb (d->dead_cb_cls, NULL);
604 if (d->churn == GNUNET_NO)
608 case SP_CONFIG_UPDATE:
609 /* confirm copying complete */
610 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
612 if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0) /* FIXME: config update should take timeout parameter! */
619 d->cfg, d, _("`scp' does not seem to terminate.\n"));
624 = GNUNET_SCHEDULER_add_delayed (d->sched,
625 GNUNET_CONSTANTS_EXEC_WAIT,
629 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
631 if (NULL != d->update_cb)
632 d->update_cb (d->update_cb_cls,
633 _("`scp' did not complete cleanly.\n"));
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "Successfully copied configuration file.\n");
640 if (NULL != d->update_cb)
641 d->update_cb (d->update_cb_cls, NULL);
642 d->phase = SP_START_DONE;
648 * Continues GNUnet daemon startup when user wanted to be notified
649 * once a hostkey was generated (for creating friends files, blacklists,
652 * @param daemon the daemon to finish starting
655 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon)
657 GNUNET_assert(daemon->phase == SP_HOSTKEY_CREATED);
658 daemon->phase = SP_TOPOLOGY_SETUP;
663 * Start a peer that has previously been stopped using the daemon_stop
664 * call (and files weren't deleted and the allow restart flag)
666 * @param daemon the daemon to start (has been previously stopped)
667 * @param timeout how long to wait for restart
668 * @param cb the callback for notification when the peer is running
669 * @param cb_cls closure for the callback
672 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
673 struct GNUNET_TIME_Relative timeout,
674 GNUNET_TESTING_NotifyDaemonRunning cb,
677 if (daemon->running == GNUNET_YES)
679 cb(cb_cls, &daemon->id, daemon->cfg, daemon, "Daemon already running, can't restart!");
684 daemon->cb_cls = cb_cls;
685 daemon->phase = SP_TOPOLOGY_SETUP;
686 daemon->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
688 GNUNET_SCHEDULER_add_continuation (daemon->sched,
691 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
695 * Starts a GNUnet daemon. GNUnet must be installed on the target
696 * system and available in the PATH. The machine must furthermore be
697 * reachable via "ssh" (unless the hostname is "NULL") without the
698 * need to enter a password.
700 * @param sched scheduler to use
701 * @param cfg configuration to use
702 * @param timeout how long to wait starting up peers
703 * @param hostname name of the machine where to run GNUnet
704 * (use NULL for localhost).
705 * @param hostkey_callback function to call once the hostkey has been
706 * generated for this peer, but it hasn't yet been started
707 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
708 * @param hostkey_cls closure for hostkey callback
709 * @param cb function to call once peer is up, or failed to start
710 * @param cb_cls closure for cb
711 * @return handle to the daemon (actual start will be completed asynchronously)
713 struct GNUNET_TESTING_Daemon *
714 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
715 const struct GNUNET_CONFIGURATION_Handle *cfg,
716 struct GNUNET_TIME_Relative timeout,
717 const char *hostname,
718 GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
720 GNUNET_TESTING_NotifyDaemonRunning cb,
723 struct GNUNET_TESTING_Daemon *ret;
727 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
729 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
730 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "Setting up peer with configuration file `%s'.\n",
736 if (NULL == ret->cfgfile)
738 GNUNET_free_non_null (ret->hostname);
742 ret->hostkey_callback = hostkey_callback;
743 ret->hostkey_cls = hostkey_cls;
745 ret->cb_cls = cb_cls;
746 ret->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
747 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
748 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
750 "DEFAULTCONFIG", ret->cfgfile);
751 /* 1) write configuration to temporary file */
752 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
754 if (0 != UNLINK (ret->cfgfile))
755 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
756 "unlink", ret->cfgfile);
757 GNUNET_CONFIGURATION_destroy (ret->cfg);
758 GNUNET_free_non_null (ret->hostname);
759 GNUNET_free (ret->cfgfile);
764 GNUNET_CONFIGURATION_get_value_string (cfg,
766 "USERNAME", &username))
768 if (NULL != getenv ("USER"))
769 username = GNUNET_strdup (getenv ("USER"));
773 ret->username = username;
775 /* 2) copy file to remote host */
776 if (NULL != hostname)
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "Copying configuration file to host `%s'.\n", hostname);
782 ret->phase = SP_COPYING;
783 if (NULL != username)
784 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
786 GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
787 ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
788 "scp", ret->cfgfile, arg, NULL);
792 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
794 ("Could not start `%s' process to copy configuration file.\n"),
796 if (0 != UNLINK (ret->cfgfile))
797 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
798 "unlink", ret->cfgfile);
799 GNUNET_CONFIGURATION_destroy (ret->cfg);
800 GNUNET_free_non_null (ret->hostname);
801 GNUNET_free_non_null (ret->username);
802 GNUNET_free (ret->cfgfile);
807 = GNUNET_SCHEDULER_add_delayed (sched,
808 GNUNET_CONSTANTS_EXEC_WAIT,
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "No need to copy configuration file since we are running locally.\n");
816 ret->phase = SP_COPIED;
817 GNUNET_SCHEDULER_add_continuation (sched,
820 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
826 * Restart (stop and start) a GNUnet daemon.
828 * @param d the daemon that should be restarted
829 * @param cb function called once the daemon is (re)started
830 * @param cb_cls closure for cb
833 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
834 GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls)
842 d->dead = GNUNET_YES;
849 if (d->phase == SP_CONFIG_UPDATE)
851 GNUNET_SCHEDULER_cancel (d->sched, d->task);
852 d->phase = SP_START_DONE;
854 if (d->server != NULL)
856 GNUNET_CORE_disconnect (d->server);
862 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
863 GNUNET_TRANSPORT_disconnect(d->th);
866 /* state clean up and notifications */
867 GNUNET_free_non_null(d->hello);
870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
871 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
874 d->phase = SP_START_ARMING;
876 /* Check if this is a local or remote process */
877 if (NULL != d->hostname)
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
884 if (d->username != NULL)
885 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
887 arg = GNUNET_strdup (d->hostname);
889 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
894 "-c", d->cfgfile, "-e", "-r", NULL);
895 /* Use -r to restart arm and all services */
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
903 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
905 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
910 "-c", d->cfgfile, "-e", "-r", NULL);
913 GNUNET_free_non_null(del_arg);
915 = GNUNET_SCHEDULER_add_delayed (d->sched,
916 GNUNET_CONSTANTS_EXEC_WAIT,
923 * Stops a GNUnet daemon.
925 * @param d the daemon that should be stopped
926 * @param timeout how long to wait for process for shutdown to complete
927 * @param cb function called once the daemon was stopped
928 * @param cb_cls closure for cb
929 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
931 * @param allow_restart GNUNET_YES to restart peer later (using this API)
932 * GNUNET_NO to kill off and clean up for good
935 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
936 struct GNUNET_TIME_Relative timeout,
937 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
944 d->dead_cb_cls = cb_cls;
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
952 d->dead = GNUNET_YES;
956 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
958 /* Free what was left from churning! */
959 GNUNET_assert(d->cfg != NULL);
960 GNUNET_CONFIGURATION_destroy (d->cfg);
961 if (delete_files == GNUNET_YES)
963 if (0 != UNLINK(d->cfgfile))
965 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "unlink");
968 GNUNET_free (d->cfgfile);
969 GNUNET_free_non_null (d->hostname);
970 GNUNET_free_non_null (d->username);
971 if (NULL != d->dead_cb)
972 d->dead_cb (d->dead_cb_cls, NULL);
977 if (delete_files == GNUNET_YES)
979 GNUNET_asprintf(&del_arg, "-d");
982 if (d->phase == SP_CONFIG_UPDATE)
984 GNUNET_SCHEDULER_cancel (d->sched, d->task);
985 d->phase = SP_START_DONE;
987 if (d->server != NULL)
989 GNUNET_CORE_disconnect (d->server);
992 /* shutdown ARM process (will terminate others) */
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
997 d->phase = SP_SHUTDOWN_START;
998 d->running = GNUNET_NO;
999 if (allow_restart == GNUNET_YES)
1000 d->churn = GNUNET_YES;
1003 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
1004 GNUNET_TRANSPORT_disconnect(d->th);
1007 /* Check if this is a local or remote process */
1008 if (NULL != d->hostname)
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
1015 if (d->username != NULL)
1016 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1018 arg = GNUNET_strdup (d->hostname);
1020 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1025 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1026 /* Use -e to end arm, and -d to remove temp files */
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1035 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1040 "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1043 GNUNET_free_non_null(del_arg);
1044 d->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
1046 = GNUNET_SCHEDULER_add_now (d->sched,
1052 * Changes the configuration of a GNUnet daemon.
1054 * @param d the daemon that should be modified
1055 * @param cfg the new configuration for the daemon
1056 * @param cb function called once the configuration was changed
1057 * @param cb_cls closure for cb
1060 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1061 struct GNUNET_CONFIGURATION_Handle *cfg,
1062 GNUNET_TESTING_NotifyCompletion cb,
1067 if (d->phase != SP_START_DONE)
1072 ("Peer not yet running, can not change configuration at this point."));
1076 /* 1) write configuration to temporary file */
1077 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1080 cb (cb_cls, _("Failed to write new configuration to disk."));
1084 /* 2) copy file to remote host (if necessary) */
1085 if (NULL == d->hostname)
1087 /* signal success */
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 "Copying updated configuration file to remote host `%s'.\n",
1097 d->phase = SP_CONFIG_UPDATE;
1098 if (NULL != d->username)
1099 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1101 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1102 d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", d->cfgfile, arg, NULL);
1106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1108 ("Could not start `%s' process to copy configuration file.\n"),
1111 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1112 d->phase = SP_START_DONE;
1116 d->update_cb_cls = cb_cls;
1118 = GNUNET_SCHEDULER_add_delayed (d->sched,
1119 GNUNET_CONSTANTS_EXEC_WAIT,
1125 * Data kept for each pair of peers that we try
1128 struct ConnectContext
1131 * Testing handle to the first daemon.
1133 struct GNUNET_TESTING_Daemon *d1;
1136 * Handle to core of first daemon (to check connect)
1138 struct GNUNET_CORE_Handle * d1core;
1141 * Testing handle to the second daemon.
1143 struct GNUNET_TESTING_Daemon *d2;
1146 * Transport handle to the second daemon.
1148 struct GNUNET_TRANSPORT_Handle *d2th;
1151 * Function to call once we are done (or have timed out).
1153 GNUNET_TESTING_NotifyConnection cb;
1161 * When should this operation be complete (or we must trigger
1164 struct GNUNET_TIME_Absolute timeout;
1167 * The relative timeout from whence this connect attempt was
1168 * started. Allows for reconnect attempts.
1170 struct GNUNET_TIME_Relative relative_timeout;
1173 * Maximum number of connect attempts, will retry connection
1174 * this number of times on failures.
1176 unsigned int max_connect_attempts;
1179 * Hello timeout task
1181 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1184 * Connect timeout task
1186 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1189 * When should this operation be complete (or we must trigger
1192 struct GNUNET_TIME_Relative timeout_hello;
1195 * Was the connection attempt successful?
1200 * The distance between the two connected peers
1206 /** Forward declaration **/
1208 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1212 * Notify callback about success or failure of the attempt
1213 * to connect the two peers
1215 * @param cls our "struct ConnectContext" (freed)
1216 * @param tc reason tells us if we succeeded or failed
1219 notify_connect_result (void *cls,
1220 const struct GNUNET_SCHEDULER_TaskContext *tc)
1222 struct ConnectContext *ctx = cls;
1223 struct GNUNET_TIME_Relative remaining;
1225 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1226 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1228 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
1229 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1231 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1233 if (ctx->d2th != NULL)
1234 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1236 if (ctx->d1core != NULL)
1237 GNUNET_CORE_disconnect (ctx->d1core);
1244 remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
1246 if (ctx->connected == GNUNET_YES)
1248 if (ctx->cb != NULL)
1250 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance, ctx->d1->cfg,
1251 ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1254 else if (remaining.value > 0)
1256 if (ctx->d1core != NULL)
1258 GNUNET_CORE_disconnect(ctx->d1core);
1262 if (ctx->d2th != NULL)
1264 GNUNET_TRANSPORT_disconnect(ctx->d2th);
1267 GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
1272 if (ctx->cb != NULL)
1274 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1275 ctx->d2->cfg, ctx->d1, ctx->d2,
1276 _("Peers failed to connect"));
1280 GNUNET_TRANSPORT_disconnect (ctx->d2th);
1282 GNUNET_CORE_disconnect (ctx->d1core);
1289 * Success, connection is up. Signal client our success.
1291 * @param cls our "struct ConnectContext"
1292 * @param peer identity of the peer that has connected
1293 * @param latency the round trip latency of the connection to this peer
1294 * @param distance distance the transport level distance to this peer
1298 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1301 struct ConnectContext *ctx = cls;
1303 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1305 ctx->connected = GNUNET_YES;
1306 ctx->distance = distance;
1307 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1308 ctx->timeout_task = GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1309 ¬ify_connect_result,
1316 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1318 struct ConnectContext *ctx = cls;
1320 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1321 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1323 if (ctx->d1->hello != NULL)
1325 GNUNET_TRANSPORT_offer_hello (ctx->d2th, GNUNET_HELLO_get_header(ctx->d1->hello));
1326 ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
1327 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1330 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
1336 * Establish a connection between two GNUnet daemons.
1338 * @param d1 handle for the first daemon
1339 * @param d2 handle for the second daemon
1340 * @param timeout how long is the connection attempt
1342 * @param max_connect_attempts how many times should we try to reconnect
1344 * @param cb function to call at the end
1345 * @param cb_cls closure for cb
1348 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1349 struct GNUNET_TESTING_Daemon *d2,
1350 struct GNUNET_TIME_Relative timeout,
1351 unsigned int max_connect_attempts,
1352 GNUNET_TESTING_NotifyConnection cb,
1355 struct ConnectContext *ctx;
1357 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1360 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1361 _("Peers are not fully running yet, can not connect!\n"));
1364 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1367 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1368 ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
1369 ctx->relative_timeout = timeout;
1371 ctx->cb_cls = cb_cls;
1372 ctx->max_connect_attempts = max_connect_attempts;
1373 ctx->connected = GNUNET_NO;
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 "Asked to connect peer %s to peer %s\n",
1377 d1->shortname, d2->shortname);
1380 ctx->d1core = GNUNET_CORE_connect (d1->sched,
1385 &connect_notify, NULL, NULL,
1387 NULL, GNUNET_NO, no_handlers);
1388 if (ctx->d1core == NULL)
1392 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1393 _("Failed to connect to core service of first peer!\n"));
1397 #if DEBUG_TESTING > 2
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1399 "Asked to connect peer %s to peer %s\n",
1400 d1->shortname, d2->shortname);
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "Connecting to transport service of peer %s\n", d2->shortname);
1406 ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
1409 d2, NULL, NULL, NULL);
1410 if (ctx->d2th == NULL)
1412 GNUNET_CORE_disconnect(ctx->d1core);
1415 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1416 _("Failed to connect to transport service!\n"));
1420 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
1421 GNUNET_TIME_relative_divide(ctx->relative_timeout,
1422 max_connect_attempts),
1423 ¬ify_connect_result, ctx);
1425 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1429 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1432 struct ConnectContext *ctx = cls;
1433 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1437 #if DEBUG_TESTING_RECONNECT
1438 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "re-attempting connect of peer %s to peer %s\n",
1439 ctx->d1->shortname, ctx->d2->shortname);
1442 GNUNET_assert(ctx->d1core == NULL);
1444 ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1446 GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1449 &connect_notify, NULL, NULL,
1451 NULL, GNUNET_NO, no_handlers);
1452 if (ctx->d1core == NULL)
1454 if (NULL != ctx->cb)
1455 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1456 _("Failed to connect to core service of first peer!\n"));
1461 ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1464 ctx->d2, NULL, NULL, NULL);
1465 if (ctx->d2th == NULL)
1467 GNUNET_CORE_disconnect(ctx->d1core);
1469 if (NULL != ctx->cb)
1470 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1471 _("Failed to connect to transport service!\n"));
1475 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1476 GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1477 ¬ify_connect_result, ctx);
1479 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1482 /* end of testing.c */