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_NO
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, 60)
52 * How many times are we willing to try to wait for "scp" or
53 * "gnunet-service-arm" to complete (waitpid) before giving up?
55 #define MAX_EXEC_WAIT_RUNS 50
58 * Phases of starting GNUnet on a system.
63 * Copy the configuration file to the target system.
68 * Configuration file has been copied, start ARM on target system.
73 * ARM has been started, check that it has properly daemonized and
74 * then try to connect to the CORE service (which should be
75 * auto-started by ARM).
80 * We're waiting for CORE to start.
85 * Core has notified us that we've established a connection to the service.
86 * The main FSM halts here and waits to be moved to UPDATE or CLEANUP.
91 * We've been asked to terminate the instance and are now waiting for
92 * the remote command to delete the configuration file to complete.
97 * We've received a configuration update and are currently waiting for
98 * the copy process for the update to complete. Once it is, we will
99 * return to "SP_START_DONE" (and rely on ARM to restart all affected
107 * Handle for a GNUnet daemon (technically a set of
108 * daemons; the handle is really for the master ARM
109 * daemon) started by the testing library.
111 struct GNUNET_TESTING_Daemon
116 struct GNUNET_SCHEDULER_Handle *sched;
121 struct GNUNET_CONFIGURATION_Handle *cfg;
124 * Host to run GNUnet on.
129 * Username we are using.
134 * Name of the configuration file
139 * Function to call when the peer is running.
141 GNUNET_TESTING_NotifyDaemonRunning cb;
149 * Arguments from "daemon_stop" call.
151 GNUNET_TESTING_NotifyCompletion dead_cb;
154 * Closure for 'dead_cb'.
159 * Arguments from "daemon_stop" call.
161 GNUNET_TESTING_NotifyCompletion update_cb;
164 * Closure for 'update_cb'.
169 * Identity of this peer (once started).
171 struct GNUNET_PeerIdentity id;
174 * Flag to indicate that we've already been asked
175 * to terminate (but could not because some action
176 * was still pending).
181 * PID of the process that we started last.
186 * How many iterations have we been waiting for
187 * the started process to complete?
189 unsigned int wait_runs;
192 * In which phase are we during the start of
195 enum StartPhase phase;
198 * ID of the current task.
200 GNUNET_SCHEDULER_TaskIdentifier task;
203 * Handle to the server.
205 struct GNUNET_CORE_Handle * server;
210 * Function called after GNUNET_CORE_connect has succeeded
211 * (or failed for good). Note that the private key of the
212 * peer is intentionally not exposed here; if you need it,
213 * your process should try to read the private key file
214 * directly (which should work if you are authorized...).
217 * @param server handle to the server, NULL if we failed
218 * @param my_identity ID of this peer, NULL if we failed
219 * @param publicKey public key of this peer, NULL if we failed
222 testing_init (void *cls,
223 struct GNUNET_CORE_Handle * server,
224 const struct GNUNET_PeerIdentity *
227 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
230 struct GNUNET_TESTING_Daemon *d = cls;
231 GNUNET_TESTING_NotifyDaemonRunning cb;
233 GNUNET_assert (d->phase == SP_START_CORE);
234 d->phase = SP_START_DONE;
240 if (GNUNET_YES == d->dead)
241 GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
243 cb (d->cb_cls, NULL, d->cfg, d,
244 _("Failed to connect to core service\n"));
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 "Successfully started peer `%4s'.\n",
250 GNUNET_i2s(my_identity));
252 d->id = *my_identity;
254 if (GNUNET_YES == d->dead)
255 GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
257 cb (d->cb_cls, my_identity, d->cfg, d, NULL);
262 * Finite-state machine for starting GNUnet.
264 * @param cls our "struct GNUNET_TESTING_Daemon"
268 start_fsm (void *cls,
269 const struct GNUNET_SCHEDULER_TaskContext *tc)
271 static struct GNUNET_CORE_MessageHandler no_handlers[] =
273 struct GNUNET_TESTING_Daemon * d = cls;
274 GNUNET_TESTING_NotifyDaemonRunning cb;
275 enum GNUNET_OS_ProcessStatusType type;
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Peer FSM is in phase %u.\n",
284 d->task = GNUNET_SCHEDULER_NO_TASK;
288 /* confirm copying complete */
290 GNUNET_OS_process_status (d->pid,
295 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
304 _("`scp' does not seem to terminate.\n"));
309 = GNUNET_SCHEDULER_add_delayed (d->sched,
310 GNUNET_CONSTANTS_EXEC_WAIT,
315 if ( (type != GNUNET_OS_PROCESS_EXITED) ||
325 _("`scp' did not complete cleanly.\n"));
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330 "Successfully copied configuration file.\n");
332 d->phase = SP_COPIED;
335 /* start GNUnet on remote host */
336 if (NULL == d->hostname)
338 d->pid = GNUNET_OS_start_process ("gnunet-service-arm",
339 "gnunet-service-arm",
350 if (d->username != NULL)
351 GNUNET_asprintf (&dst,
356 dst = GNUNET_strdup (d->hostname);
357 d->pid = GNUNET_OS_start_process ("ssh",
360 "gnunet-service-arm",
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370 _("Could not start `%s' process to start GNUnet.\n"),
371 (NULL == d->hostname) ? "gnunet-service-arm" : "ssh");
379 (NULL == d->hostname)
380 ? _("Failed to start `gnunet-service-arm' process.\n")
381 : _("Failed to start `ssh' process.\n"));
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Started `%s', waiting for `%s' to be up.\n",
386 "gnunet-service-arm",
387 "gnunet-service-core");
389 d->phase = SP_START_ARMING;
392 = GNUNET_SCHEDULER_add_delayed (d->sched,
393 GNUNET_CONSTANTS_EXEC_WAIT,
397 case SP_START_ARMING:
399 GNUNET_OS_process_status (d->pid,
404 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
413 (NULL == d->hostname)
414 ? _("`gnunet-service-arm' does not seem to terminate.\n")
415 : _("`ssh' does not seem to terminate.\n"));
420 = GNUNET_SCHEDULER_add_delayed (d->sched,
421 GNUNET_CONSTANTS_EXEC_WAIT,
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "Successfully started `%s'.\n",
429 "gnunet-service-arm");
431 d->phase = SP_START_CORE;
432 d->server = GNUNET_CORE_connect (d->sched,
449 /* confirm copying complete */
451 GNUNET_OS_process_status (d->pid,
456 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
458 d->dead_cb (d->dead_cb_cls,
459 _("`ssh' does not seem to terminate.\n"));
460 GNUNET_free (d->cfgfile);
461 GNUNET_free_non_null (d->hostname);
462 GNUNET_free_non_null (d->username);
468 = GNUNET_SCHEDULER_add_delayed (d->sched,
469 GNUNET_CONSTANTS_EXEC_WAIT,
474 if ( (type != GNUNET_OS_PROCESS_EXITED) ||
477 if (NULL != d->dead_cb)
478 d->dead_cb (d->dead_cb_cls,
479 _("`ssh' did not complete cleanly.\n"));
480 GNUNET_free (d->cfgfile);
481 GNUNET_free_non_null (d->hostname);
482 GNUNET_free_non_null (d->username);
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488 "Peer shutdown complete.\n");
490 GNUNET_free (d->cfgfile);
491 GNUNET_free_non_null (d->hostname);
492 GNUNET_free_non_null (d->username);
493 if (NULL != d->dead_cb)
494 d->dead_cb (d->dead_cb_cls, NULL);
497 case SP_CONFIG_UPDATE:
498 /* confirm copying complete */
500 GNUNET_OS_process_status (d->pid,
505 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
514 _("`scp' does not seem to terminate.\n"));
519 = GNUNET_SCHEDULER_add_delayed (d->sched,
520 GNUNET_CONSTANTS_EXEC_WAIT,
525 if ( (type != GNUNET_OS_PROCESS_EXITED) ||
528 if (NULL != d->update_cb)
529 d->update_cb (d->update_cb_cls,
530 _("`scp' did not complete cleanly.\n"));
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535 "Successfully copied configuration file.\n");
537 if (NULL != d->update_cb)
538 d->update_cb (d->update_cb_cls, NULL);
539 d->phase = SP_START_DONE;
546 * Starts a GNUnet daemon. GNUnet must be installed on the target
547 * system and available in the PATH. The machine must furthermore be
548 * reachable via "ssh" (unless the hostname is "NULL") without the
549 * need to enter a password.
551 * @param sched scheduler to use
552 * @param cfg configuration to use
553 * @param hostname name of the machine where to run GNUnet
554 * (use NULL for localhost).
555 * @param cb function to call with the result
556 * @param cb_cls closure for cb
557 * @return handle to the daemon (actual start will be completed asynchronously)
559 struct GNUNET_TESTING_Daemon *
560 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
561 const struct GNUNET_CONFIGURATION_Handle *cfg,
562 const char *hostname,
563 GNUNET_TESTING_NotifyDaemonRunning cb,
566 struct GNUNET_TESTING_Daemon * ret;
570 ret = GNUNET_malloc (sizeof(struct GNUNET_TESTING_Daemon));
572 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
573 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "Setting up peer with configuration file `%s'.\n",
579 if (NULL == ret->cfgfile)
581 GNUNET_free_non_null (ret->hostname);
586 ret->cb_cls = cb_cls;
587 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
588 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
592 /* 1) write configuration to temporary file */
594 GNUNET_CONFIGURATION_write (ret->cfg,
597 if (0 != UNLINK (ret->cfgfile))
598 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
601 GNUNET_CONFIGURATION_destroy (ret->cfg);
602 GNUNET_free_non_null (ret->hostname);
603 GNUNET_free (ret->cfgfile);
608 GNUNET_CONFIGURATION_get_value_string (cfg,
613 if (NULL != getenv ("USER"))
614 username = GNUNET_strdup (getenv("USER"));
618 ret->username = username;
620 /* 2) copy file to remote host */
621 if (NULL != hostname)
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "Copying configuration file to host `%s'.\n",
628 ret->phase = SP_COPYING;
629 if (NULL != username)
630 GNUNET_asprintf (&arg,
636 GNUNET_asprintf (&arg,
640 ret->pid = GNUNET_OS_start_process ("scp",
648 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
649 _("Could not start `%s' process to copy configuration file.\n"),
651 if (0 != UNLINK (ret->cfgfile))
652 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
655 GNUNET_CONFIGURATION_destroy (ret->cfg);
656 GNUNET_free_non_null (ret->hostname);
657 GNUNET_free_non_null (ret->username);
658 GNUNET_free (ret->cfgfile);
663 = GNUNET_SCHEDULER_add_delayed (sched,
664 GNUNET_CONSTANTS_EXEC_WAIT,
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "No need to copy configuration file since we are running locally.\n");
673 ret->phase = SP_COPIED;
674 GNUNET_SCHEDULER_add_continuation (sched,
677 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
683 * Stops a GNUnet daemon.
685 * @param d the daemon that should be stopped
686 * @param cb function called once the daemon was stopped
687 * @param cb_cls closure for cb
689 void GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
690 GNUNET_TESTING_NotifyCompletion cb,
693 struct GNUNET_CLIENT_Connection *cc;
698 d->dead = GNUNET_YES;
700 d->dead_cb_cls = cb_cls;
703 if (d->phase == SP_CONFIG_UPDATE)
705 GNUNET_SCHEDULER_cancel (d->sched,
707 d->phase = SP_START_DONE;
709 if (d->server != NULL)
711 GNUNET_CORE_disconnect (d->server);
714 /* shutdown ARM process (will also terminate others) */
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 _("Terminating peer `%4s'\n"),
718 cc = GNUNET_CLIENT_connect (d->sched,
721 GNUNET_CLIENT_service_shutdown (cc);
723 /* state clean up and notifications */
724 if (0 != UNLINK (d->cfgfile))
725 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
728 if (d->hostname != NULL)
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "Removing configuration file on remote host `%s'.\n",
735 if (NULL != d->username)
736 GNUNET_asprintf (&dst,
741 dst = GNUNET_strdup (d->hostname);
742 d->pid = GNUNET_OS_start_process ("ssh",
751 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
752 _("Could not start `%s' process to delete configuration file.\n"),
754 GNUNET_free (d->cfgfile);
755 GNUNET_free_non_null (d->hostname);
756 GNUNET_free_non_null (d->username);
758 cb (cb_cls, _("Error cleaning up configuration file.\n"));
761 d->phase = SP_CLEANUP;
763 d->dead_cb_cls = cb_cls;
765 = GNUNET_SCHEDULER_add_delayed (d->sched,
766 GNUNET_CONSTANTS_EXEC_WAIT,
771 GNUNET_CONFIGURATION_destroy (d->cfg);
772 GNUNET_free (d->cfgfile);
773 GNUNET_free_non_null (d->hostname);
774 GNUNET_free_non_null (d->username);
782 * Changes the configuration of a GNUnet daemon.
784 * @param d the daemon that should be modified
785 * @param cfg the new configuration for the daemon
786 * @param cb function called once the configuration was changed
787 * @param cb_cls closure for cb
789 void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
790 struct GNUNET_CONFIGURATION_Handle *cfg,
791 GNUNET_TESTING_NotifyCompletion cb,
796 if (d->phase != SP_START_DONE)
800 _("Peer not yet running, can not change configuration at this point."));
804 /* 1) write configuration to temporary file */
806 GNUNET_CONFIGURATION_write (cfg,
811 _("Failed to write new configuration to disk."));
815 /* 2) copy file to remote host (if necessary) */
816 if (NULL == d->hostname)
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
825 "Copying updated configuration file to remote host `%s'.\n",
828 d->phase = SP_CONFIG_UPDATE;
829 if (NULL != d->username)
830 GNUNET_asprintf (&arg,
836 GNUNET_asprintf (&arg,
840 d->pid = GNUNET_OS_start_process ("scp",
848 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
849 _("Could not start `%s' process to copy configuration file.\n"),
853 _("Failed to copy new configuration to remote machine."));
854 d->phase = SP_START_DONE;
858 d->update_cb_cls = cb_cls;
860 = GNUNET_SCHEDULER_add_delayed (d->sched,
861 GNUNET_CONSTANTS_EXEC_WAIT,
868 * Data kept for each pair of peers that we try
871 struct ConnectContext
874 * Testing handle to the first daemon.
876 struct GNUNET_TESTING_Daemon *d1;
879 * Testing handle to the second daemon.
881 struct GNUNET_TESTING_Daemon *d2;
884 * Transport handle to the first daemon.
886 struct GNUNET_TRANSPORT_Handle *d1th;
889 * Transport handle to the second daemon.
891 struct GNUNET_TRANSPORT_Handle *d2th;
894 * Function to call once we are done (or have timed out).
896 GNUNET_TESTING_NotifyCompletion cb;
904 * Transmit handle for our request for transmission
905 * (as given to d2 asking to talk to d1).
907 struct GNUNET_CORE_TransmitHandle *ntr;
910 * When should this operation be complete (or we must trigger
913 struct GNUNET_TIME_Absolute timeout;
919 * Notify callback about success or failure of the attempt
920 * to connect the two peers
922 * @param cls our "struct ConnectContext" (freed)
923 * @param tc reason tells us if we succeeded or failed
926 notify_connect_result (void *cls,
927 const struct GNUNET_SCHEDULER_TaskContext *tc)
929 struct ConnectContext *ctx = cls;
933 if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
934 ctx->cb (ctx->cb_cls, _("Peers failed to connect"));
936 ctx->cb (ctx->cb_cls, NULL);
943 * Success, connection is up. Signal client our success.
945 * @param cls our "struct ConnectContext"
946 * @param size number of bytes available in buf
947 * @param buf where to copy the message, NULL on error
948 * @return number of bytes copied to buf
951 transmit_ready (void *cls, size_t size, void *buf)
953 struct ConnectContext *ctx = cls;
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 "Core notified us about readiness to transmit message, connection must be up!\n");
960 GNUNET_TRANSPORT_disconnect (ctx->d1th);
962 GNUNET_TRANSPORT_disconnect (ctx->d2th);
964 GNUNET_SCHEDULER_add_continuation (ctx->d1->sched,
965 ¬ify_connect_result,
968 GNUNET_SCHEDULER_REASON_TIMEOUT :
969 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
975 * Receive the HELLO from one peer, give it to the other
976 * and ask them to connect.
978 * @param cls "struct ConnectContext"
979 * @param latency how fast is the connection
980 * @param peer ID of peer giving us the HELLO
981 * @param message HELLO message of peer
984 process_hello (void *cls,
985 struct GNUNET_TIME_Relative latency,
986 const struct GNUNET_PeerIdentity *peer,
987 const struct GNUNET_MessageHeader *message)
989 struct ConnectContext *ctx = cls;
994 GNUNET_TRANSPORT_disconnect (ctx->d1th);
995 GNUNET_TRANSPORT_disconnect (ctx->d2th);
997 ctx->cb (ctx->cb_cls,
998 _("Failed to receive `HELLO' from peer\n"));
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004 "Received `%s' from transport service of `%4s'\n",
1005 "HELLO", GNUNET_i2s (peer));
1007 GNUNET_assert (message != NULL);
1008 GNUNET_TRANSPORT_offer_hello (ctx->d2th, message);
1010 = GNUNET_CORE_notify_transmit_ready (ctx->d2->server,
1012 GNUNET_TIME_absolute_get_remaining (ctx->timeout),
1014 sizeof (struct GNUNET_MessageHeader),
1015 &transmit_ready, ctx);
1020 * Establish a connection between two GNUnet daemons.
1022 * @param d1 handle for the first daemon
1023 * @param d2 handle for the second daemon
1024 * @param timeout how long is the connection attempt
1026 * @param cb function to call at the end
1027 * @param cb_cls closure for cb
1029 void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1030 struct GNUNET_TESTING_Daemon *d2,
1031 struct GNUNET_TIME_Relative timeout,
1032 GNUNET_TESTING_NotifyCompletion cb,
1035 struct ConnectContext *ctx;
1037 if ( (d1->server == NULL) ||
1038 (d2->server == NULL) )
1041 cb (cb_cls, _("Peers are not fully running yet, can not connect!\n"));
1044 ctx = GNUNET_malloc (sizeof(struct ConnectContext));
1047 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1049 ctx->cb_cls = cb_cls;
1050 ctx->d1th = GNUNET_TRANSPORT_connect (d1->sched,
1054 if (ctx->d1th == NULL)
1058 cb (cb_cls, _("Failed to connect to transport service!\n"));
1061 ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
1065 if (ctx->d2th == NULL)
1067 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1070 cb (cb_cls, _("Failed to connect to transport service!\n"));
1073 GNUNET_TRANSPORT_get_hello (ctx->d1th,
1080 /* end of testing.c */