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