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
32 * - modify configuration to allow 2087-connections from
33 * controlling host (otherwise shutdown won't work)
37 #include "gnunet_arm_service.h"
38 #include "gnunet_core_service.h"
39 #include "gnunet_constants.h"
40 #include "gnunet_testing_lib.h"
41 #include "gnunet_transport_service.h"
43 #define DEBUG_TESTING GNUNET_YES
46 * How long do we wait after starting gnunet-service-arm
47 * for the core service to be alive?
49 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
52 * How many times are we willing to try to
53 * wait for "scp" or "gnunet-service-arm" to
54 * complete (waitpid) before giving up?
56 #define MAX_EXEC_WAIT_RUNS 50
59 * Phases of starting GNUnet on a system.
74 * Handle for a GNUnet daemon (technically a set of
75 * daemons; the handle is really for the master ARM
76 * daemon) started by the testing library.
78 struct GNUNET_TESTING_Daemon
83 struct GNUNET_SCHEDULER_Handle *sched;
88 struct GNUNET_CONFIGURATION_Handle *cfg;
91 * Host to run GNUnet on.
96 * Username we are using.
101 * Name of the configuration file
106 * Function to call when the peer is running.
108 GNUNET_TESTING_NotifyDaemonRunning cb;
116 * Arguments from "daemon_stop" call.
118 GNUNET_TESTING_NotifyCompletion dead_cb;
121 * Closure for 'dead_cb'.
126 * Arguments from "daemon_stop" call.
128 GNUNET_TESTING_NotifyCompletion update_cb;
131 * Closure for 'update_cb'.
136 * Identity of this peer (once started).
138 struct GNUNET_PeerIdentity id;
141 * Flag to indicate that we've already been asked
142 * to terminate (but could not because some action
143 * was still pending).
148 * PID of the process that we started last.
153 * How many iterations have we been waiting for
154 * the started process to complete?
156 unsigned int wait_runs;
159 * In which phase are we during the start of
162 enum StartPhase phase;
165 * ID of the current task.
167 GNUNET_SCHEDULER_TaskIdentifier task;
170 * Handle to the server.
172 struct GNUNET_CORE_Handle * server;
177 * Function called after GNUNET_CORE_connect has succeeded
178 * (or failed for good). Note that the private key of the
179 * peer is intentionally not exposed here; if you need it,
180 * your process should try to read the private key file
181 * directly (which should work if you are authorized...).
184 * @param server handle to the server, NULL if we failed
185 * @param my_identity ID of this peer, NULL if we failed
186 * @param publicKey public key of this peer, NULL if we failed
189 testing_init (void *cls,
190 struct GNUNET_CORE_Handle * server,
191 const struct GNUNET_PeerIdentity *
194 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
197 struct GNUNET_TESTING_Daemon *d = cls;
198 GNUNET_TESTING_NotifyDaemonRunning cb;
200 d->phase = SP_START_DONE;
205 if (GNUNET_YES == d->dead)
206 GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
208 cb (d->cb_cls, NULL, d->cfg, d,
209 _("Failed to connect to core service\n"));
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214 "Successfully started peer `%4s'.\n",
215 GNUNET_i2s(my_identity));
217 d->id = *my_identity;
219 if (GNUNET_YES == d->dead)
220 GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
222 cb (d->cb_cls, my_identity, d->cfg, d, NULL);
227 * Finite-state machine for starting GNUnet.
229 * @param cls our "struct GNUNET_TESTING_Daemon"
233 start_fsm (void *cls,
234 const struct GNUNET_SCHEDULER_TaskContext *tc)
236 static struct GNUNET_CORE_MessageHandler no_handlers[] =
238 struct GNUNET_TESTING_Daemon * d = cls;
239 GNUNET_TESTING_NotifyDaemonRunning cb;
240 enum GNUNET_OS_ProcessStatusType type;
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246 "Peer FSM is in phase %u.\n",
249 d->task = GNUNET_SCHEDULER_NO_TASK;
253 /* confirm copying complete */
255 GNUNET_OS_process_status (d->pid,
260 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
269 _("`scp' does not seem to terminate.\n"));
274 = GNUNET_SCHEDULER_add_delayed (d->sched,
276 GNUNET_SCHEDULER_PRIORITY_KEEP,
277 GNUNET_SCHEDULER_NO_TASK,
278 GNUNET_CONSTANTS_EXEC_WAIT,
283 if ( (type != GNUNET_OS_PROCESS_EXITED) ||
293 _("`scp' did not complete cleanly.\n"));
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "Successfully copied configuration file.\n");
300 d->phase = SP_COPIED;
303 /* start GNUnet on remote host */
304 if (NULL == d->hostname)
306 d->pid = GNUNET_OS_start_process ("gnunet-service-arm",
307 "gnunet-service-arm",
318 if (d->username != NULL)
319 GNUNET_asprintf (&dst,
324 dst = GNUNET_strdup (d->hostname);
325 d->pid = GNUNET_OS_start_process ("ssh",
328 "gnunet-service-arm",
337 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
338 _("Could not start `%s' process to start GNUnet.\n"),
339 (NULL == d->hostname) ? "gnunet-service-arm" : "ssh");
347 (NULL == d->hostname)
348 ? _("Failed to start `gnunet-service-arm' process.\n")
349 : _("Failed to start `ssh' process.\n"));
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Started `%s', waiting for `%s' to be up.\n",
354 "gnunet-service-arm",
355 "gnunet-service-core");
357 d->phase = SP_START_ARMING;
360 = GNUNET_SCHEDULER_add_delayed (d->sched,
362 GNUNET_SCHEDULER_PRIORITY_KEEP,
363 GNUNET_SCHEDULER_NO_TASK,
364 GNUNET_CONSTANTS_EXEC_WAIT,
368 case SP_START_ARMING:
370 GNUNET_OS_process_status (d->pid,
375 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
384 (NULL == d->hostname)
385 ? _("`gnunet-service-arm' does not seem to terminate.\n")
386 : _("`ssh' does not seem to terminate.\n"));
391 = GNUNET_SCHEDULER_add_delayed (d->sched,
393 GNUNET_SCHEDULER_PRIORITY_KEEP,
394 GNUNET_SCHEDULER_NO_TASK,
395 GNUNET_CONSTANTS_EXEC_WAIT,
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "Successfully started `%s'.\n",
403 "gnunet-service-arm");
405 d->phase = SP_START_CORE;
406 GNUNET_CORE_connect (d->sched,
423 /* confirm copying complete */
425 GNUNET_OS_process_status (d->pid,
430 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
432 d->dead_cb (d->dead_cb_cls,
433 _("`ssh' does not seem to terminate.\n"));
434 GNUNET_free (d->cfgfile);
435 GNUNET_free_non_null (d->hostname);
436 GNUNET_free_non_null (d->username);
442 = GNUNET_SCHEDULER_add_delayed (d->sched,
444 GNUNET_SCHEDULER_PRIORITY_KEEP,
445 GNUNET_SCHEDULER_NO_TASK,
446 GNUNET_CONSTANTS_EXEC_WAIT,
451 if ( (type != GNUNET_OS_PROCESS_EXITED) ||
454 if (NULL != d->dead_cb)
455 d->dead_cb (d->dead_cb_cls,
456 _("`ssh' did not complete cleanly.\n"));
457 GNUNET_free (d->cfgfile);
458 GNUNET_free_non_null (d->hostname);
459 GNUNET_free_non_null (d->username);
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465 "Peer shutdown complete.\n");
467 GNUNET_free (d->cfgfile);
468 GNUNET_free_non_null (d->hostname);
469 GNUNET_free_non_null (d->username);
470 if (NULL != d->dead_cb)
471 d->dead_cb (d->dead_cb_cls, NULL);
474 case SP_CONFIG_UPDATE:
475 /* confirm copying complete */
477 GNUNET_OS_process_status (d->pid,
482 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
491 _("`scp' does not seem to terminate.\n"));
496 = GNUNET_SCHEDULER_add_delayed (d->sched,
498 GNUNET_SCHEDULER_PRIORITY_KEEP,
499 GNUNET_SCHEDULER_NO_TASK,
500 GNUNET_CONSTANTS_EXEC_WAIT,
505 if ( (type != GNUNET_OS_PROCESS_EXITED) ||
508 if (NULL != d->update_cb)
509 d->update_cb (d->update_cb_cls,
510 _("`scp' did not complete cleanly.\n"));
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Successfully copied configuration file.\n");
517 if (NULL != d->update_cb)
518 d->update_cb (d->update_cb_cls, NULL);
519 d->phase = SP_START_DONE;
526 * Starts a GNUnet daemon. GNUnet must be installed on the target
527 * system and available in the PATH. The machine must furthermore be
528 * reachable via "ssh" (unless the hostname is "NULL") without the
529 * need to enter a password.
531 * @param sched scheduler to use
532 * @param cfg configuration to use
533 * @param hostname name of the machine where to run GNUnet
534 * (use NULL for localhost).
535 * @param cb function to call with the result
536 * @param cb_cls closure for cb
537 * @return handle to the daemon (actual start will be completed asynchronously)
539 struct GNUNET_TESTING_Daemon *
540 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
541 const struct GNUNET_CONFIGURATION_Handle *cfg,
542 const char *hostname,
543 GNUNET_TESTING_NotifyDaemonRunning cb,
546 struct GNUNET_TESTING_Daemon * ret;
550 ret = GNUNET_malloc (sizeof(struct GNUNET_TESTING_Daemon));
552 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
553 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556 "Setting up peer with configuration file `%s'.\n",
559 if (NULL == ret->cfgfile)
561 GNUNET_free_non_null (ret->hostname);
566 ret->cb_cls = cb_cls;
567 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
568 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
572 /* 1) write configuration to temporary file */
574 GNUNET_CONFIGURATION_write (ret->cfg,
577 if (0 != UNLINK (ret->cfgfile))
578 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
581 GNUNET_CONFIGURATION_destroy (ret->cfg);
582 GNUNET_free_non_null (ret->hostname);
583 GNUNET_free (ret->cfgfile);
588 GNUNET_CONFIGURATION_get_value_string (cfg,
593 if (NULL != getenv ("USER"))
594 username = GNUNET_strdup (getenv("USER"));
598 ret->username = username;
600 /* 2) copy file to remote host */
601 if (NULL != hostname)
603 ret->phase = SP_COPYING;
604 if (NULL != username)
605 GNUNET_asprintf (&arg,
611 GNUNET_asprintf (&arg,
615 ret->pid = GNUNET_OS_start_process ("scp",
623 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
624 _("Could not start `%s' process to copy configuration file.\n"),
626 if (0 != UNLINK (ret->cfgfile))
627 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
630 GNUNET_CONFIGURATION_destroy (ret->cfg);
631 GNUNET_free_non_null (ret->hostname);
632 GNUNET_free_non_null (ret->username);
633 GNUNET_free (ret->cfgfile);
638 = GNUNET_SCHEDULER_add_delayed (sched,
640 GNUNET_SCHEDULER_PRIORITY_KEEP,
641 GNUNET_SCHEDULER_NO_TASK,
642 GNUNET_CONSTANTS_EXEC_WAIT,
648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
649 "No need to copy configuration file since we are running locally.\n");
651 ret->phase = SP_COPIED;
652 GNUNET_SCHEDULER_add_continuation (sched,
656 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
662 * Stops a GNUnet daemon.
664 * @param d the daemon that should be stopped
665 * @param cb function called once the daemon was stopped
666 * @param cb_cls closure for cb
668 void GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
669 GNUNET_TESTING_NotifyCompletion cb,
672 struct GNUNET_CLIENT_Connection *cc;
677 d->dead = GNUNET_YES;
679 d->dead_cb_cls = cb_cls;
682 if (d->phase == SP_CONFIG_UPDATE)
684 GNUNET_SCHEDULER_cancel (d->sched,
686 d->phase = SP_START_DONE;
688 if (d->server != NULL)
690 GNUNET_CORE_disconnect (d->server);
693 /* shutdown ARM process (will also terminate others) */
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 _("Terminating peer `%4s'\n"),
697 cc = GNUNET_CLIENT_connect (d->sched,
700 GNUNET_CLIENT_service_shutdown (cc);
701 GNUNET_CLIENT_disconnect (cc);
703 /* state clean up and notifications */
704 if (0 != UNLINK (d->cfgfile))
705 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
708 if (d->hostname != NULL)
710 if (NULL != d->username)
711 GNUNET_asprintf (&dst,
716 dst = GNUNET_strdup (d->hostname);
717 d->pid = GNUNET_OS_start_process ("ssh",
726 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
727 _("Could not start `%s' process to delete configuration file.\n"),
729 GNUNET_free (d->cfgfile);
730 GNUNET_free_non_null (d->hostname);
731 GNUNET_free_non_null (d->username);
733 cb (cb_cls, _("Error cleaning up configuration file.\n"));
736 d->phase = SP_CLEANUP;
738 d->dead_cb_cls = cb_cls;
740 = GNUNET_SCHEDULER_add_delayed (d->sched,
742 GNUNET_SCHEDULER_PRIORITY_KEEP,
743 GNUNET_SCHEDULER_NO_TASK,
744 GNUNET_CONSTANTS_EXEC_WAIT,
749 GNUNET_CONFIGURATION_destroy (d->cfg);
750 GNUNET_free (d->cfgfile);
751 GNUNET_free_non_null (d->hostname);
752 GNUNET_free_non_null (d->username);
760 * Changes the configuration of a GNUnet daemon.
762 * @param d the daemon that should be modified
763 * @param cfg the new configuration for the daemon
764 * @param cb function called once the configuration was changed
765 * @param cb_cls closure for cb
767 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
768 struct GNUNET_CONFIGURATION_Handle *cfg,
769 GNUNET_TESTING_NotifyCompletion cb,
774 if (d->phase != SP_START_DONE)
778 _("Peer not yet running, can not change configuration at this point."));
782 /* 1) write configuration to temporary file */
784 GNUNET_CONFIGURATION_write (cfg,
789 _("Failed to write new configuration to disk."));
793 /* 2) copy file to remote host (if necessary) */
794 if (NULL == d->hostname)
801 d->phase = SP_CONFIG_UPDATE;
802 if (NULL != d->username)
803 GNUNET_asprintf (&arg,
809 GNUNET_asprintf (&arg,
813 d->pid = GNUNET_OS_start_process ("scp",
821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
822 _("Could not start `%s' process to copy configuration file.\n"),
826 _("Failed to copy new configuration to remote machine."));
827 d->phase = SP_START_DONE;
831 d->update_cb_cls = cb_cls;
833 = GNUNET_SCHEDULER_add_delayed (d->sched,
835 GNUNET_SCHEDULER_PRIORITY_KEEP,
836 GNUNET_SCHEDULER_NO_TASK,
837 GNUNET_CONSTANTS_EXEC_WAIT,
843 struct ConnectContext
845 struct GNUNET_TESTING_Daemon *d1;
846 struct GNUNET_TESTING_Daemon *d2;
847 struct GNUNET_TRANSPORT_Handle *d1th;
848 struct GNUNET_TRANSPORT_Handle *d2th;
849 struct GNUNET_TIME_Absolute timeout;
850 GNUNET_TESTING_NotifyCompletion cb;
856 * Success, connection is up. Signal client our success.
859 transmit_ready (void *cls, size_t size, void *buf)
861 struct ConnectContext *ctx = cls;
866 ctx->cb (ctx->cb_cls, _("Peers failed to connect"));
868 ctx->cb (ctx->cb_cls, NULL);
876 * Receive the HELLO from one peer, give it to the other
877 * and ask them to connect.
879 * @param cls "struct ConnectContext"
880 * @param latency how fast is the connection
881 * @param peer ID of peer giving us the HELLO
882 * @param message HELLO message of peer
885 process_hello (void *cls,
886 struct GNUNET_TIME_Relative latency,
887 const struct GNUNET_PeerIdentity *peer,
888 const struct GNUNET_MessageHeader *message)
890 struct ConnectContext *ctx = cls;
896 ctx->cb (ctx->cb_cls,
897 _("Failed to receive `HELLO' from peer\n"));
898 GNUNET_TRANSPORT_disconnect (ctx->d1th);
899 GNUNET_TRANSPORT_disconnect (ctx->d2th);
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "Received `%s' from transport service of `%4s'\n",
905 "HELLO", GNUNET_i2s (peer));
906 GNUNET_assert (message != NULL);
907 GNUNET_TRANSPORT_offer_hello (ctx->d2th, message);
908 GNUNET_CORE_notify_transmit_ready (ctx->d2->server,
910 GNUNET_TIME_absolute_get_remaining (ctx->timeout),
912 sizeof (struct GNUNET_MessageHeader),
913 &transmit_ready, ctx);
918 * Establish a connection between two GNUnet daemons.
920 * @param d1 handle for the first daemon
921 * @param d2 handle for the second daemon
922 * @param timeout how long is the connection attempt
924 * @param cb function to call at the end
925 * @param cb_cls closure for cb
927 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
928 struct GNUNET_TESTING_Daemon *d2,
929 struct GNUNET_TIME_Relative timeout,
930 GNUNET_TESTING_NotifyCompletion cb,
933 struct ConnectContext *ctx;
935 if ( (d1->server == NULL) ||
936 (d2->server == NULL) )
939 cb (cb_cls, _("Peers are not fully running yet, can not connect!\n"));
942 ctx = GNUNET_malloc (sizeof(struct ConnectContext));
945 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
947 ctx->cb_cls = cb_cls;
948 ctx->d1th = GNUNET_TRANSPORT_connect (d1->sched, d1->cfg, d1, NULL, NULL, NULL);
949 if (ctx->d1th == NULL)
953 cb (cb_cls, _("Failed to connect to transport service!\n"));
956 ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched, d2->cfg, d2, NULL, NULL, NULL);
957 if (ctx->d2th == NULL)
959 GNUNET_TRANSPORT_disconnect (ctx->d1th);
962 cb (cb_cls, _("Failed to connect to transport service!\n"));
965 GNUNET_TRANSPORT_get_hello (ctx->d1th,
972 /* end of testing.c */