increase default timeout for startup (for SLOW machines)
[oweals/gnunet.git] / src / testing / testing.c
1 /*
2       This file is part of GNUnet
3       (C) 2008, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
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
30  *
31  */
32 #include "platform.h"
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"
39
40 #define DEBUG_TESTING GNUNET_NO
41
42 /**
43  * How long do we wait after starting gnunet-service-arm
44  * for the core service to be alive?
45  */
46 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
47
48 /**
49  * How many times are we willing to try to wait for "scp" or
50  * "gnunet-service-arm" to complete (waitpid) before giving up?
51  */
52 #define MAX_EXEC_WAIT_RUNS 50
53
54 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
55
56 /**
57  * Receive the HELLO from one peer, give it to the other
58  * and ask them to connect.
59  *
60  * @param cls "struct ConnectContext"
61  * @param message HELLO message of peer
62  */
63 static void
64 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
65 {
66   struct GNUNET_TESTING_Daemon *daemon = cls;
67   GNUNET_TRANSPORT_get_hello_cancel(daemon->th, &process_hello, daemon);
68 #if DEBUG_TESTING
69   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70               "Received `%s' from transport service of `%4s'\n",
71               "HELLO", GNUNET_i2s (&daemon->id));
72 #endif
73
74   GNUNET_assert (message != NULL);
75   GNUNET_free_non_null(daemon->hello);
76   daemon->hello = GNUNET_malloc(ntohs(message->size));
77   memcpy(daemon->hello, message, ntohs(message->size));
78
79   if (daemon->th != NULL)
80     {
81       GNUNET_TRANSPORT_disconnect(daemon->th);
82       daemon->th = NULL;
83     }
84
85 }
86
87 /**
88  * Function called after GNUNET_CORE_connect has succeeded
89  * (or failed for good).  Note that the private key of the
90  * peer is intentionally not exposed here; if you need it,
91  * your process should try to read the private key file
92  * directly (which should work if you are authorized...).
93  *
94  * @param cls closure
95  * @param server handle to the server, NULL if we failed
96  * @param my_identity ID of this peer, NULL if we failed
97  * @param publicKey public key of this peer, NULL if we failed
98  */
99 static void
100 testing_init (void *cls,
101               struct GNUNET_CORE_Handle *server,
102               const struct GNUNET_PeerIdentity *my_identity,
103               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
104 {
105   struct GNUNET_TESTING_Daemon *d = cls;
106   GNUNET_TESTING_NotifyDaemonRunning cb;
107
108   GNUNET_assert (d->phase == SP_START_CORE);
109   d->phase = SP_START_DONE;
110   cb = d->cb;
111   d->cb = NULL;
112   if (server == NULL)
113     {
114       d->server = NULL;
115       if (GNUNET_YES == d->dead)
116         GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
117       else if (NULL != cb)
118         cb (d->cb_cls, NULL, d->cfg, d,
119             _("Failed to connect to core service\n"));
120       return;
121     }
122 #if DEBUG_TESTING
123   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
124               "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
125 #endif
126   d->id = *my_identity;
127   d->shortname = strdup (GNUNET_i2s (my_identity));
128   d->server = server;
129   if (GNUNET_YES == d->dead)
130     GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
131   else if (NULL != cb)
132     cb (d->cb_cls, my_identity, d->cfg, d, NULL);
133 #if DEBUG_TESTING
134   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135               "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
136 #endif
137
138
139   d->th = GNUNET_TRANSPORT_connect (d->sched,
140                                     d->cfg, d, NULL, NULL, NULL);
141   if (d->th == NULL)
142     {
143       if (GNUNET_YES == d->dead)
144         GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
145       else if (NULL != d->cb)
146         d->cb (d->cb_cls, &d->id, d->cfg, d,
147             _("Failed to connect to transport service!\n"));
148       return;
149     }
150
151   GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
152 }
153
154
155 /**
156  * Finite-state machine for starting GNUnet.
157  *
158  * @param cls our "struct GNUNET_TESTING_Daemon"
159  * @param tc unused
160  */
161 static void
162 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
163 {
164   struct GNUNET_TESTING_Daemon *d = cls;
165   GNUNET_TESTING_NotifyDaemonRunning cb;
166   enum GNUNET_OS_ProcessStatusType type;
167   unsigned long code;
168   char *dst;
169
170 #if DEBUG_TESTING
171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172               "Peer FSM is in phase %u.\n", d->phase);
173 #endif
174
175   d->task = GNUNET_SCHEDULER_NO_TASK;
176   switch (d->phase)
177     {
178     case SP_COPYING:
179       /* confirm copying complete */
180       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
181         {
182           d->wait_runs++;
183           if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
184             {
185               cb = d->cb;
186               d->cb = NULL;
187               if (NULL != cb)
188                 cb (d->cb_cls,
189                     NULL,
190                     d->cfg, d, _("`scp' does not seem to terminate.\n"));
191               return;
192             }
193           /* wait some more */
194           d->task
195             = GNUNET_SCHEDULER_add_delayed (d->sched,
196                                             GNUNET_CONSTANTS_EXEC_WAIT,
197                                             &start_fsm, d);
198           return;
199         }
200       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
201         {
202           cb = d->cb;
203           d->cb = NULL;
204           if (NULL != cb)
205             cb (d->cb_cls,
206                 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
207           return;
208         }
209 #if DEBUG_TESTING
210       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211                   "Successfully copied configuration file.\n");
212 #endif
213       d->phase = SP_COPIED;
214       /* fall-through */
215     case SP_COPIED:
216       /* start GNUnet on remote host */
217       if (NULL == d->hostname)
218         {
219 #if DEBUG_TESTING
220           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221                       "Starting `%s', with command `%s %s %s %s %s %s'.\n",
222                       "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
223                       "-L", "DEBUG",
224                       "-s");
225 #endif
226           d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
227                                             "gnunet-arm",
228                                             "-c", d->cfgfile,
229 #if DEBUG_TESTING
230                                             "-L", "DEBUG",
231 #endif
232                                             "-s", "-q", NULL);
233         }
234       else
235         {
236           if (d->username != NULL)
237             GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
238           else
239             dst = GNUNET_strdup (d->hostname);
240
241 #if DEBUG_TESTING
242           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243                       "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
244                       "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
245                       "-L", "DEBUG", "-s", "-q");
246 #endif
247           d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
248                                             "ssh",
249                                             dst,
250                                             "gnunet-arm",
251 #if DEBUG_TESTING
252                                             "-L", "DEBUG",
253 #endif
254                                             "-c", d->cfgfile, "-s", "-q", NULL);
255           GNUNET_free (dst);
256         }
257       if (-1 == d->pid)
258         {
259           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260                       _("Could not start `%s' process to start GNUnet.\n"),
261                       (NULL == d->hostname) ? "gnunet-arm" : "ssh");
262           cb = d->cb;
263           d->cb = NULL;
264           if (NULL != cb)
265             cb (d->cb_cls,
266                 NULL,
267                 d->cfg,
268                 d,
269                 (NULL == d->hostname)
270                 ? _("Failed to start `gnunet-arm' process.\n")
271                 : _("Failed to start `ssh' process.\n"));
272         }
273 #if DEBUG_TESTING
274       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275                   "Started `%s', waiting for `%s' to be up.\n",
276                   "gnunet-arm", "gnunet-service-core");
277 #endif
278       d->phase = SP_START_ARMING;
279       d->wait_runs = 0;
280       d->task
281         = GNUNET_SCHEDULER_add_delayed (d->sched,
282                                         GNUNET_CONSTANTS_EXEC_WAIT,
283                                         &start_fsm, d);
284       break;
285     case SP_START_ARMING:
286       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
287         {
288           d->wait_runs++;
289           if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
290             {
291               cb = d->cb;
292               d->cb = NULL;
293               if (NULL != cb)
294                 cb (d->cb_cls,
295                     NULL,
296                     d->cfg,
297                     d,
298                     (NULL == d->hostname)
299                     ? _("`gnunet-arm' does not seem to terminate.\n")
300                     : _("`ssh' does not seem to terminate.\n"));
301               return;
302             }
303           /* wait some more */
304           d->task
305             = GNUNET_SCHEDULER_add_delayed (d->sched,
306                                             GNUNET_CONSTANTS_EXEC_WAIT,
307                                             &start_fsm, d);
308           return;
309         }
310 #if DEBUG_TESTING
311       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312                   "Successfully started `%s'.\n", "gnunet-arm");
313 #endif
314       d->phase = SP_START_CORE;
315       d->server = GNUNET_CORE_connect (d->sched,
316                                        d->cfg,
317                                        ARM_START_WAIT,
318                                        d,
319                                        &testing_init,
320                                        NULL, NULL,
321                                        NULL, GNUNET_NO,
322                                        NULL, GNUNET_NO, no_handlers);
323       break;
324     case SP_START_CORE:
325       GNUNET_break (0);
326       break;
327     case SP_START_DONE:
328       GNUNET_break (0);
329       break;
330     case SP_SHUTDOWN_START:
331       /* confirm copying complete */
332       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
333         {
334           d->wait_runs++;
335           if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
336             {
337               d->dead_cb (d->dead_cb_cls,
338                           _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
339               if (d->th != NULL)
340                 {
341                   GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
342                   GNUNET_TRANSPORT_disconnect(d->th);
343                   d->th = NULL;
344                 }
345               GNUNET_CONFIGURATION_destroy (d->cfg);
346               GNUNET_free (d->cfgfile);
347               GNUNET_free_non_null (d->hostname);
348               GNUNET_free_non_null (d->username);
349               GNUNET_free_non_null (d->shortname);
350               GNUNET_free (d);
351               return;
352             }
353           /* wait some more */
354           d->task
355             = GNUNET_SCHEDULER_add_delayed (d->sched,
356                                             GNUNET_CONSTANTS_EXEC_WAIT,
357                                             &start_fsm, d);
358           return;
359         }
360       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
361         {
362           if (NULL != d->dead_cb)
363             d->dead_cb (d->dead_cb_cls,
364                         _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
365           if (d->th != NULL)
366             {
367               GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
368               GNUNET_TRANSPORT_disconnect(d->th);
369               d->th = NULL;
370             }
371           GNUNET_CONFIGURATION_destroy (d->cfg);
372           GNUNET_free (d->cfgfile);
373           GNUNET_free_non_null (d->hostname);
374           GNUNET_free_non_null (d->username);
375           GNUNET_free_non_null (d->shortname);
376           GNUNET_free (d);
377           return;
378         }
379 #if DEBUG_TESTING
380       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
381 #endif
382       if (d->th != NULL)
383         {
384           GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
385           GNUNET_TRANSPORT_disconnect(d->th);
386           d->th = NULL;
387         }
388       /* state clean up and notifications */
389       GNUNET_CONFIGURATION_destroy (d->cfg);
390       GNUNET_free (d->cfgfile);
391       GNUNET_free_non_null (d->hostname);
392       GNUNET_free_non_null (d->username);
393       GNUNET_free_non_null (d->shortname);
394       if (NULL != d->dead_cb)
395         d->dead_cb (d->dead_cb_cls, NULL);
396       GNUNET_free (d);
397       break;
398     case SP_CONFIG_UPDATE:
399       /* confirm copying complete */
400       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
401         {
402           d->wait_runs++;
403           if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
404             {
405               cb = d->cb;
406               d->cb = NULL;
407               if (NULL != cb)
408                 cb (d->cb_cls,
409                     NULL,
410                     d->cfg, d, _("`scp' does not seem to terminate.\n"));
411               return;
412             }
413           /* wait some more */
414           d->task
415             = GNUNET_SCHEDULER_add_delayed (d->sched,
416                                             GNUNET_CONSTANTS_EXEC_WAIT,
417                                             &start_fsm, d);
418           return;
419         }
420       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
421         {
422           if (NULL != d->update_cb)
423             d->update_cb (d->update_cb_cls,
424                           _("`scp' did not complete cleanly.\n"));
425           return;
426         }
427 #if DEBUG_TESTING
428       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429                   "Successfully copied configuration file.\n");
430 #endif
431       if (NULL != d->update_cb)
432         d->update_cb (d->update_cb_cls, NULL);
433       d->phase = SP_START_DONE;
434       break;
435     }
436 }
437
438
439 /**
440  * Starts a GNUnet daemon.  GNUnet must be installed on the target
441  * system and available in the PATH.  The machine must furthermore be
442  * reachable via "ssh" (unless the hostname is "NULL") without the
443  * need to enter a password.
444  *
445  * @param sched scheduler to use
446  * @param cfg configuration to use
447  * @param hostname name of the machine where to run GNUnet
448  *        (use NULL for localhost).
449  * @param cb function to call with the result
450  * @param cb_cls closure for cb
451  * @return handle to the daemon (actual start will be completed asynchronously)
452  */
453 struct GNUNET_TESTING_Daemon *
454 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
455                              const struct GNUNET_CONFIGURATION_Handle *cfg,
456                              const char *hostname,
457                              GNUNET_TESTING_NotifyDaemonRunning cb,
458                              void *cb_cls)
459 {
460   struct GNUNET_TESTING_Daemon *ret;
461   char *arg;
462   char *username;
463
464   ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
465   ret->sched = sched;
466   ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
467   ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
468 #if DEBUG_TESTING
469   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470               "Setting up peer with configuration file `%s'.\n",
471               ret->cfgfile);
472 #endif
473   if (NULL == ret->cfgfile)
474     {
475       GNUNET_free_non_null (ret->hostname);
476       GNUNET_free (ret);
477       return NULL;
478     }
479   ret->cb = cb;
480   ret->cb_cls = cb_cls;
481   ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
482   GNUNET_CONFIGURATION_set_value_string (ret->cfg,
483                                          "PATHS",
484                                          "DEFAULTCONFIG", ret->cfgfile);
485   /* 1) write configuration to temporary file */
486   if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
487     {
488       if (0 != UNLINK (ret->cfgfile))
489         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
490                                   "unlink", ret->cfgfile);
491       GNUNET_CONFIGURATION_destroy (ret->cfg);
492       GNUNET_free_non_null (ret->hostname);
493       GNUNET_free (ret->cfgfile);
494       GNUNET_free (ret);
495       return NULL;
496     }
497   if (GNUNET_OK !=
498       GNUNET_CONFIGURATION_get_value_string (cfg,
499                                              "TESTING",
500                                              "USERNAME", &username))
501     {
502       if (NULL != getenv ("USER"))
503         username = GNUNET_strdup (getenv ("USER"));
504       else
505         username = NULL;
506     }
507   ret->username = username;
508
509   /* 2) copy file to remote host */
510   if (NULL != hostname)
511     {
512 #if DEBUG_TESTING
513       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514                   "Copying configuration file to host `%s'.\n", hostname);
515 #endif
516       ret->phase = SP_COPYING;
517       if (NULL != username)
518         GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
519       else
520         GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
521       ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
522                                           "scp", ret->cfgfile, arg, NULL);
523       GNUNET_free (arg);
524       if (-1 == ret->pid)
525         {
526           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
527                       _
528                       ("Could not start `%s' process to copy configuration file.\n"),
529                       "scp");
530           if (0 != UNLINK (ret->cfgfile))
531             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
532                                       "unlink", ret->cfgfile);
533           GNUNET_CONFIGURATION_destroy (ret->cfg);
534           GNUNET_free_non_null (ret->hostname);
535           GNUNET_free_non_null (ret->username);
536           GNUNET_free (ret->cfgfile);
537           GNUNET_free (ret);
538           return NULL;
539         }
540       ret->task
541         = GNUNET_SCHEDULER_add_delayed (sched,
542                                         GNUNET_CONSTANTS_EXEC_WAIT,
543                                         &start_fsm, ret);
544       return ret;
545     }
546 #if DEBUG_TESTING
547   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548               "No need to copy configuration file since we are running locally.\n");
549 #endif
550   ret->phase = SP_COPIED;
551   GNUNET_SCHEDULER_add_continuation (sched,
552                                      &start_fsm,
553                                      ret,
554                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
555   return ret;
556 }
557
558
559 /**
560  * Stops a GNUnet daemon.
561  *
562  * @param d the daemon that should be stopped
563  * @param cb function called once the daemon was stopped
564  * @param cb_cls closure for cb
565  */
566 void
567 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
568                             GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
569 {
570   char *arg;
571
572   d->dead_cb = cb;
573   d->dead_cb_cls = cb_cls;
574
575   if (NULL != d->cb)
576     {
577       d->dead = GNUNET_YES;
578       return;
579     }
580   if (d->phase == SP_CONFIG_UPDATE)
581     {
582       GNUNET_SCHEDULER_cancel (d->sched, d->task);
583       d->phase = SP_START_DONE;
584     }
585   if (d->server != NULL)
586     {
587       GNUNET_CORE_disconnect (d->server);
588       d->server = NULL;
589     }
590   /* shutdown ARM process (will terminate others) */
591 #if DEBUG_TESTING
592   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593               _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
594   /* sleep(15); Manual check for running */
595 #endif
596
597   d->phase = SP_SHUTDOWN_START;
598
599   /* Check if this is a local or remote process */
600   if (NULL != d->hostname)
601     {
602 #if DEBUG_TESTING
603       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604                   "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
605 #endif
606
607       if (d->username != NULL)
608         GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
609       else
610         arg = GNUNET_strdup (d->hostname);
611
612       d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
613                                               arg, "gnunet-arm",
614 #if DEBUG_TESTING
615                                               "-L", "DEBUG",
616 #endif
617                                               "-c", d->cfgfile, "-e", "-d", "-q", NULL);
618       /* Use -e to end arm, and -d to remove temp files */
619
620       GNUNET_free (arg);
621     }
622   else
623   {
624 #if DEBUG_TESTING
625       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626                   "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
627 #endif
628     d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
629                                             "gnunet-arm",
630 #if DEBUG_TESTING
631                                             "-L", "DEBUG",
632 #endif
633                                             "-c", d->cfgfile, "-e", "-d", "-q", NULL);
634   }
635
636   GNUNET_free_non_null(d->hello);
637
638   d->wait_runs = 0;
639   d->task
640     = GNUNET_SCHEDULER_add_delayed (d->sched,
641                                     GNUNET_CONSTANTS_EXEC_WAIT,
642                                     &start_fsm, d);
643   return;
644 }
645
646
647 /**
648  * Changes the configuration of a GNUnet daemon.
649  *
650  * @param d the daemon that should be modified
651  * @param cfg the new configuration for the daemon
652  * @param cb function called once the configuration was changed
653  * @param cb_cls closure for cb
654  */
655 void
656 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
657                                    struct GNUNET_CONFIGURATION_Handle *cfg,
658                                    GNUNET_TESTING_NotifyCompletion cb,
659                                    void *cb_cls)
660 {
661   char *arg;
662
663   if (d->phase != SP_START_DONE)
664     {
665       if (NULL != cb)
666         cb (cb_cls,
667             _
668             ("Peer not yet running, can not change configuration at this point."));
669       return;
670     }
671
672   /* 1) write configuration to temporary file */
673   if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
674     {
675       if (NULL != cb)
676         cb (cb_cls, _("Failed to write new configuration to disk."));
677       return;
678     }
679
680   /* 2) copy file to remote host (if necessary) */
681   if (NULL == d->hostname)
682     {
683       /* signal success */
684       if (NULL != cb)
685         cb (cb_cls, NULL);
686       return;
687     }
688 #if DEBUG_TESTING
689   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690               "Copying updated configuration file to remote host `%s'.\n",
691               d->hostname);
692 #endif
693   d->phase = SP_CONFIG_UPDATE;
694   if (NULL != d->username)
695     GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
696   else
697     GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
698   d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", d->cfgfile, arg, NULL);
699   GNUNET_free (arg);
700   if (-1 == d->pid)
701     {
702       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
703                   _
704                   ("Could not start `%s' process to copy configuration file.\n"),
705                   "scp");
706       if (NULL != cb)
707         cb (cb_cls, _("Failed to copy new configuration to remote machine."));
708       d->phase = SP_START_DONE;
709       return;
710     }
711   d->update_cb = cb;
712   d->update_cb_cls = cb_cls;
713   d->task
714     = GNUNET_SCHEDULER_add_delayed (d->sched,
715                                     GNUNET_CONSTANTS_EXEC_WAIT,
716                                     &start_fsm, d);
717 }
718
719
720 /**
721  * Data kept for each pair of peers that we try
722  * to connect.
723  */
724 struct ConnectContext
725 {
726   /**
727    * Testing handle to the first daemon.
728    */
729   struct GNUNET_TESTING_Daemon *d1;
730
731   /**
732    * Handle to core of first daemon (to check connect)
733    */
734   struct GNUNET_CORE_Handle * d1core;
735
736   /**
737    * Testing handle to the second daemon.
738    */
739   struct GNUNET_TESTING_Daemon *d2;
740
741   /**
742    * Transport handle to the second daemon.
743    */
744   struct GNUNET_TRANSPORT_Handle *d2th;
745
746   /**
747    * Function to call once we are done (or have timed out).
748    */
749   GNUNET_TESTING_NotifyConnection cb;
750
751   /**
752    * Closure for "nb".
753    */
754   void *cb_cls;
755
756   /**
757    * When should this operation be complete (or we must trigger
758    * a timeout).
759    */
760   struct GNUNET_TIME_Absolute timeout;
761
762   /**
763    * The relative timeout from whence this connect attempt was
764    * started.  Allows for reconnect attempts.
765    */
766   struct GNUNET_TIME_Relative relative_timeout;
767
768   /**
769    * Maximum number of connect attempts, will retry connection
770    * this number of times on failures.
771    */
772   unsigned int max_connect_attempts;
773
774   /**
775    * Hello timeout task
776    */
777   GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
778
779   /**
780    * Connect timeout task
781    */
782   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
783
784   /**
785    * When should this operation be complete (or we must trigger
786    * a timeout).
787    */
788   struct GNUNET_TIME_Relative timeout_hello;
789
790
791   /**
792    * Was the connection attempt successful?
793    */
794   int connected;
795 };
796
797
798 /** Forward declaration **/
799 static void
800 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
801
802
803 /**
804  * Notify callback about success or failure of the attempt
805  * to connect the two peers
806  *
807  * @param cls our "struct ConnectContext" (freed)
808  * @param tc reason tells us if we succeeded or failed
809  */
810 static void
811 notify_connect_result (void *cls,
812                        const struct GNUNET_SCHEDULER_TaskContext *tc)
813 {
814   struct ConnectContext *ctx = cls;
815   struct GNUNET_TIME_Relative remaining;
816
817   if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
818     {
819       GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
820       ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
821     }
822
823   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
824     {
825       if (ctx->d2th != NULL)
826         GNUNET_TRANSPORT_disconnect (ctx->d2th);
827       ctx->d2th = NULL;
828       if (ctx->d1core != NULL)
829         GNUNET_CORE_disconnect (ctx->d1core);
830
831       ctx->d1core = NULL;
832       GNUNET_free (ctx);
833       return;
834     }
835
836   remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
837
838   if (ctx->connected == GNUNET_YES)
839     {
840       if (ctx->cb != NULL)
841         {
842           ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
843                    ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
844         }
845       GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
846     }
847   else if (remaining.value > 0)
848     {
849       GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
850       return;
851     }
852   else
853     {
854       if (ctx->cb != NULL)
855         {
856           ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
857                    ctx->d2->cfg, ctx->d1, ctx->d2,
858                    _("Peers failed to connect"));
859         }
860     }
861
862
863   GNUNET_TRANSPORT_disconnect (ctx->d2th);
864   ctx->d2th = NULL;
865   GNUNET_CORE_disconnect (ctx->d1core);
866   ctx->d1core = NULL;
867   GNUNET_free (ctx);
868 }
869
870
871 /**
872  * Success, connection is up.  Signal client our success.
873  *
874  * @param cls our "struct ConnectContext"
875  * @param peer identity of the peer that has connected
876  * @param latency the round trip latency of the connection to this peer
877  * @param distance distance the transport level distance to this peer
878  *
879  */
880 static void
881 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
882                 uint32_t distance)
883 {
884   struct ConnectContext *ctx = cls;
885
886   if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
887     {
888       ctx->connected = GNUNET_YES;
889       GNUNET_SCHEDULER_add_now (ctx->d1->sched,
890                                 &notify_connect_result,
891                                 ctx);
892     }
893
894 }
895
896 static void
897 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
898 {
899   struct ConnectContext *ctx = cls;
900
901   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
902     return;
903
904   if (ctx->d1->hello != NULL)
905     {
906       GNUNET_TRANSPORT_offer_hello (ctx->d2th, GNUNET_HELLO_get_header(ctx->d1->hello));
907       ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
908                                                     GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
909                                                                                   500));
910     }
911   ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
912                                                       ctx->timeout_hello,
913                                                       &send_hello, ctx);
914 }
915
916 /**
917  * Establish a connection between two GNUnet daemons.
918  *
919  * @param d1 handle for the first daemon
920  * @param d2 handle for the second daemon
921  * @param timeout how long is the connection attempt
922  *        allowed to take?
923  * @param max_connect_attempts how many times should we try to reconnect
924  *        (within timeout)
925  * @param cb function to call at the end
926  * @param cb_cls closure for cb
927  */
928 void
929 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
930                                 struct GNUNET_TESTING_Daemon *d2,
931                                 struct GNUNET_TIME_Relative timeout,
932                                 unsigned int max_connect_attempts,
933                                 GNUNET_TESTING_NotifyConnection cb,
934                                 void *cb_cls)
935 {
936   struct ConnectContext *ctx;
937
938   if ((d1->server == NULL) || (d2->server == NULL))
939     {
940       if (NULL != cb)
941         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
942             _("Peers are not fully running yet, can not connect!\n"));
943       return;
944     }
945   ctx = GNUNET_malloc (sizeof (struct ConnectContext));
946   ctx->d1 = d1;
947   ctx->d2 = d2;
948   ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
949   ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
950   ctx->relative_timeout = timeout;
951   ctx->cb = cb;
952   ctx->cb_cls = cb_cls;
953   ctx->max_connect_attempts = max_connect_attempts;
954   ctx->connected = GNUNET_NO;
955 #if DEBUG_TESTING
956   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957               "Asked to connect peer %s to peer %s\n",
958               d1->shortname, d2->shortname);
959 #endif
960
961   ctx->d1core = GNUNET_CORE_connect (d1->sched,
962                                      d1->cfg,
963                                      timeout,
964                                      ctx,
965                                      NULL,
966                                      &connect_notify, NULL,
967                                      NULL, GNUNET_NO,
968                                      NULL, GNUNET_NO, no_handlers);
969   if (ctx->d1core == NULL)
970     {
971       GNUNET_free (ctx);
972       if (NULL != cb)
973         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
974             _("Failed to connect to core service of first peer!\n"));
975       return;
976     }
977
978 #if DEBUG_TESTING > 2
979   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980               "Asked to connect peer %s to peer %s\n",
981               d1->shortname, d2->shortname);
982   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983               "Connecting to transport service of peer %s\n", d2->shortname);
984
985 #endif
986
987   ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
988                                         d2->cfg, d2, NULL, NULL, NULL);
989   if (ctx->d2th == NULL)
990     {
991       GNUNET_free (ctx);
992       if (NULL != cb)
993         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
994             _("Failed to connect to transport service!\n"));
995       return;
996     }
997
998   ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
999                                                     GNUNET_TIME_relative_divide(ctx->relative_timeout, max_connect_attempts), /* Allow up to 8 reconnect attempts */
1000                                                     &notify_connect_result, ctx);
1001
1002   ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1003 }
1004
1005 static void
1006 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1007 {
1008
1009   struct ConnectContext *ctx = cls;
1010   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1011     {
1012       return;
1013     }
1014 #if DEBUG_TESTING
1015   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "re-attempting connect of peer %s to peer %s\n",
1016               ctx->d1->shortname, ctx->d2->shortname);
1017 #endif
1018
1019   if (ctx->d1core != NULL)
1020     {
1021       GNUNET_CORE_disconnect(ctx->d1core);
1022       ctx->d1core = NULL;
1023     }
1024
1025   if (ctx->d2th != NULL)
1026     {
1027       GNUNET_TRANSPORT_disconnect(ctx->d2th);
1028       ctx->d2th = NULL;
1029     }
1030
1031   ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1032                                      ctx->d1->cfg,
1033                                      GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1034                                      ctx,
1035                                      NULL,
1036                                      &connect_notify, NULL,
1037                                      NULL, GNUNET_NO,
1038                                      NULL, GNUNET_NO, no_handlers);
1039   if (ctx->d1core == NULL)
1040     {
1041       if (NULL != ctx->cb)
1042         ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1043                  _("Failed to connect to core service of first peer!\n"));
1044       GNUNET_free (ctx);
1045       return;
1046     }
1047
1048   ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1049                                         ctx->d2->cfg, ctx->d2, NULL, NULL, NULL);
1050   if (ctx->d2th == NULL)
1051     {
1052       GNUNET_free (ctx);
1053       if (NULL != ctx->cb)
1054         ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1055             _("Failed to connect to transport service!\n"));
1056       return;
1057     }
1058
1059   ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1060                                                     GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1061                                                     &notify_connect_result, ctx);
1062
1063   ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1064 }
1065
1066 /* end of testing.c */