-ensure stats queues do not grow too big
[oweals/gnunet.git] / src / testbed / test_testbed_api_controllerlink.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2013 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18       Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @file testbed/test_testbed_api_controllerlink.c
23  * @brief testcase for testing controller to subcontroller linking
24  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25  */
26
27
28 /**
29  * The controller architecture we try to achieve in this test case:
30  *
31  *                    Master Controller
32  *                    //             \\
33  *                   //               \\
34  *         Slave Controller 1---------Slave Controller 3
35  *                  ||
36  *                  ||
37  *         Slave Controller 2
38  */
39
40 #include "platform.h"
41 #include "gnunet_util_lib.h"
42 #include "gnunet_testing_lib.h"
43 #include "gnunet_testbed_service.h"
44
45 /**
46  * Generic logging shortcut
47  */
48 #define LOG(kind,...)                           \
49   GNUNET_log (kind, __VA_ARGS__)
50
51 /**
52  * Debug logging shorthand
53  */
54 #define LOG_DEBUG(...)                          \
55   LOG(GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
56
57 /**
58  * Different stages in testing
59  */
60 enum Stage
61 {
62
63   /**
64    * Initial stage
65    */
66   INIT,
67
68   /**
69    * Master controller has started
70    */
71   MASTER_STARTED,
72
73   /**
74    * A peer has been created on master
75    */
76   MASTER_PEER_CREATE_SUCCESS,
77
78   /**
79    * Peer on master controller has been started successfully.
80    */
81   MASTER_PEER_START_SUCCESS,
82
83   /**
84    * The first slave has been registered at master controller
85    */
86   SLAVE1_REGISTERED,
87
88   /**
89    * The second slave has been registered at the master controller
90    */
91   SLAVE2_REGISTERED,
92
93   /**
94    * Link from master to slave 1 has been successfully created
95    */
96   SLAVE1_LINK_SUCCESS,
97
98   /**
99    * Peer create on slave 1 successful
100    */
101   SLAVE1_PEER_CREATE_SUCCESS,
102
103   /**
104    * Peer startup on slave 1 successful
105    */
106   SLAVE1_PEER_START_SUCCESS,
107
108   /**
109    * Link from slave 1 to slave 2 has been successfully created.
110    */
111   SLAVE2_LINK_SUCCESS,
112
113   /**
114    * Peer create on slave 2 successful
115    */
116   SLAVE2_PEER_CREATE_SUCCESS,
117
118   /**
119    * Peer on slave 1 successfully stopped
120    */
121   SLAVE1_PEER_STOP_SUCCESS,
122
123   /**
124    * Peer startup on slave 2 successful
125    */
126   SLAVE2_PEER_START_SUCCESS,
127
128   /**
129    * Try to connect peers on master and slave 2.
130    */
131   MASTER_SLAVE2_PEERS_CONNECTED,
132
133   /**
134    * Slave 3 has successfully registered
135    */
136   SLAVE3_REGISTERED,
137
138   /**
139    * Slave 3 has successfully started
140    */
141   SLAVE3_STARTED,
142
143   /**
144    * Peer created on slave 3
145    */
146   SLAVE3_PEER_CREATE_SUCCESS,
147
148   /**
149    * Peer started at slave 3
150    */
151   SLAVE3_PEER_START_SUCCESS,
152
153   /**
154    * Try to connect peers on slave2 and slave3
155    */
156   SLAVE2_SLAVE3_PEERS_CONNECTED,
157
158   /**
159    * Peer on slave 2 successfully stopped
160    */
161   SLAVE2_PEER_STOP_SUCCESS,
162
163   /**
164    * Peer destroy on slave 1 successful
165    */
166   SLAVE1_PEER_DESTROY_SUCCESS,
167
168   /**
169    * Peer destory on slave 2 successful
170    */
171   SLAVE2_PEER_DESTROY_SUCCESS,
172
173   /**
174    * The configuration of slave 3 is acquired
175    */
176   SLAVE3_GET_CONFIG_SUCCESS,
177
178   /**
179    * Slave 1 has linked to slave 3;
180    */
181   SLAVE3_LINK_SUCCESS,
182
183   /**
184    * Master peer destoryed.  Destory slave 3 peer
185    */
186   MASTER_PEER_DESTROY_SUCCESS,
187
188   /**
189    * Slave 3 peer destroyed.  Mark test as success
190    */
191   SUCCESS,
192
193   /**
194    * Marks test as skipped
195    */
196   SKIP
197 };
198
199 /**
200  * Host for running master controller
201  */
202 static struct GNUNET_TESTBED_Host *host;
203
204 /**
205  * The master controller process
206  */
207 static struct GNUNET_TESTBED_ControllerProc *cp;
208
209 /**
210  * Handle to master controller
211  */
212 static struct GNUNET_TESTBED_Controller *mc;
213
214 /**
215  * Slave host for running slave controller
216  */
217 static struct GNUNET_TESTBED_Host *slave;
218
219 /**
220  * Another slave host for running another slave controller
221  */
222 static struct GNUNET_TESTBED_Host *slave2;
223
224 /**
225  * Host for slave 3
226  */
227 static struct GNUNET_TESTBED_Host *slave3;
228
229 /**
230  * Slave host registration handle
231  */
232 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
233
234 /**
235  * Handle to global configuration
236  */
237 static struct GNUNET_CONFIGURATION_Handle *cfg;
238
239 /**
240  * Configuration of slave 3 controller
241  */
242 static struct GNUNET_CONFIGURATION_Handle *cfg3;
243
244 /**
245  * Abort task
246  */
247 static struct GNUNET_SCHEDULER_Task *abort_task;
248
249 /**
250  * Operation handle for linking controllers
251  */
252 static struct GNUNET_TESTBED_Operation *op;
253
254 /**
255  * Handle to peer started at slave 1
256  */
257 static struct GNUNET_TESTBED_Peer *slave1_peer;
258
259 /**
260  * Handle to peer started at slave 2
261  */
262 static struct GNUNET_TESTBED_Peer *slave2_peer;
263
264 /**
265  * Handle to peer started at slave 2
266  */
267 static struct GNUNET_TESTBED_Peer *slave3_peer;
268
269 /**
270  * Handle to a peer started at master controller
271  */
272 static struct GNUNET_TESTBED_Peer *master_peer;
273
274 /**
275  * The handle for whether a host is habitable or not
276  */
277 static struct GNUNET_TESTBED_HostHabitableCheckHandle *hc_handle;
278
279 /**
280  * The task handle for the delay task
281  */
282 static struct GNUNET_SCHEDULER_Task *delay_task_id;
283
284 /**
285  * Event mask
286  */
287 static uint64_t event_mask;
288
289 /**
290  * Global testing status
291  */
292 static enum Stage result;
293
294 /**
295  * shortcut to exit during failure
296  */
297 #define FAIL_TEST(cond) do {                                    \
298     if (!(cond)) {                                              \
299       GNUNET_break(0);                                          \
300       if (NULL != abort_task)               \
301         GNUNET_SCHEDULER_cancel (abort_task);                   \
302       abort_task = NULL;                    \
303       GNUNET_SCHEDULER_shutdown ();              \
304       return;                                                   \
305     }                                                          \
306   } while (0)
307
308
309 /**
310  * Shutdown nicely
311  *
312  * @param cls NULL
313  */
314 static void
315 do_shutdown (void *cls)
316 {
317   if (NULL != abort_task)
318     GNUNET_SCHEDULER_cancel (abort_task);
319   if (NULL != delay_task_id)
320   {
321     GNUNET_SCHEDULER_cancel (delay_task_id);
322     delay_task_id = NULL;
323   }
324   if (NULL != hc_handle)
325     GNUNET_TESTBED_is_host_habitable_cancel (hc_handle);
326   if (NULL != op)
327   {
328     GNUNET_TESTBED_operation_done (op);
329     op = NULL;
330   }
331   if (NULL != mc)
332     GNUNET_TESTBED_controller_disconnect (mc);
333   if (NULL != cp)
334     GNUNET_TESTBED_controller_stop (cp);
335   if (NULL != slave3)
336     GNUNET_TESTBED_host_destroy (slave3);
337   if (NULL != slave2)
338     GNUNET_TESTBED_host_destroy (slave2);
339   if (NULL != slave)
340     GNUNET_TESTBED_host_destroy (slave);
341   if (NULL != host)
342     GNUNET_TESTBED_host_destroy (host);
343   if (NULL != cfg)
344     GNUNET_CONFIGURATION_destroy (cfg);
345   if (NULL != cfg3)
346     GNUNET_CONFIGURATION_destroy (cfg3);
347   if (NULL != rh)
348     GNUNET_TESTBED_cancel_registration (rh);
349 }
350
351
352 /**
353  * abort task to run on test timed out
354  *
355  * @param cls NULL
356  */
357 static void
358 do_abort (void *cls)
359 {
360   LOG (GNUNET_ERROR_TYPE_WARNING,
361        "Aborting in stage %d\n",
362        result);
363   abort_task = NULL;
364   GNUNET_SCHEDULER_shutdown ();
365 }
366
367
368 /**
369  * Calls abort now
370  *
371  * @param
372  * @return
373  */
374 static void
375 do_abort_now (void *cls)
376 {
377   if (NULL != abort_task)
378     GNUNET_SCHEDULER_cancel (abort_task);
379   abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
380 }
381
382
383 /**
384  * Callback which will be called to after a host registration succeeded or failed
385  *
386  * @param cls the host which has been registered
387  * @param emsg the error message; NULL if host registration is successful
388  */
389 static void
390 registration_cont (void *cls, const char *emsg);
391
392
393 /**
394  * Task for inserting delay between tests
395  *
396  * @param
397  * @return
398  */
399 static void
400 delay_task (void *cls)
401 {
402   delay_task_id = NULL;
403   switch (result)
404   {
405   case SLAVE2_PEER_CREATE_SUCCESS:
406     op = GNUNET_TESTBED_peer_stop (NULL, slave1_peer, NULL, NULL);
407     FAIL_TEST (NULL != op);
408     break;
409   case MASTER_SLAVE2_PEERS_CONNECTED:
410     slave3 = GNUNET_TESTBED_host_create_with_id (3, "127.0.0.1", NULL, cfg, 0);
411     rh = GNUNET_TESTBED_register_host (mc, slave3, &registration_cont, NULL);
412     break;
413   case SLAVE2_SLAVE3_PEERS_CONNECTED:
414     op = GNUNET_TESTBED_peer_stop (NULL, slave2_peer, NULL, NULL);
415     FAIL_TEST (NULL != op);
416     break;
417   default:
418     FAIL_TEST (0);
419   }
420 }
421
422
423 /**
424  * Functions of this signature are called when a peer has been successfully
425  * created
426  *
427  * @param cls the closure from GNUNET_TESTBED_peer_create()
428  * @param peer the handle for the created peer; NULL on any error during
429  *          creation
430  * @param emsg NULL if peer is not NULL; else MAY contain the error description
431  */
432 static void
433 peer_create_cb (void *cls,
434                 struct GNUNET_TESTBED_Peer *peer,
435                 const char *emsg)
436 {
437   FAIL_TEST (NULL != peer);
438   FAIL_TEST (NULL == emsg);
439   switch (result)
440   {
441   case MASTER_STARTED:
442     result = MASTER_PEER_CREATE_SUCCESS;
443     master_peer = peer;
444     GNUNET_TESTBED_operation_done (op);
445     op = GNUNET_TESTBED_peer_start (NULL, master_peer, NULL, NULL);
446     break;
447   case SLAVE1_LINK_SUCCESS:
448     result = SLAVE1_PEER_CREATE_SUCCESS;
449     slave1_peer = peer;
450     GNUNET_TESTBED_operation_done (op);
451     op = GNUNET_TESTBED_peer_start (NULL, slave1_peer, NULL, NULL);
452     break;
453   case SLAVE2_LINK_SUCCESS:
454     result = SLAVE2_PEER_CREATE_SUCCESS;
455     slave2_peer = peer;
456     GNUNET_TESTBED_operation_done (op);
457     op = NULL;
458     delay_task_id =
459         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
460                                       (GNUNET_TIME_UNIT_SECONDS, 1),
461                                       &delay_task,
462                                       NULL);
463     return;
464   case SLAVE3_STARTED:
465     result = SLAVE3_PEER_CREATE_SUCCESS;
466     slave3_peer = peer;
467     GNUNET_TESTBED_operation_done (op);
468     op = GNUNET_TESTBED_peer_start (NULL, slave3_peer, NULL, NULL);
469     break;
470   default:
471     FAIL_TEST (0);
472   }
473   FAIL_TEST (NULL != op);
474 }
475
476
477 /**
478  * Checks the event if it is an operation finished event and if indicates a
479  * successfull completion of operation
480  *
481  * @param event the event information to check
482  */
483 static void
484 check_operation_success (const struct GNUNET_TESTBED_EventInformation *event)
485 {
486   FAIL_TEST (NULL != event);
487   FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
488   FAIL_TEST (event->op == op);
489   FAIL_TEST (NULL == event->op_cls);
490   FAIL_TEST (NULL == event->details.operation_finished.emsg);
491   FAIL_TEST (NULL == event->details.operation_finished.generic);
492 }
493
494
495 /**
496  * Signature of the event handler function called by the
497  * respective event controller.
498  *
499  * @param cls closure
500  * @param event information about the event
501  */
502 static void
503 controller_cb (void *cls,
504                const struct GNUNET_TESTBED_EventInformation *event)
505 {
506   switch (result)
507   {
508   case SLAVE2_REGISTERED:
509     check_operation_success (event);
510     GNUNET_TESTBED_operation_done (op);
511     op = NULL;
512     result = SLAVE1_LINK_SUCCESS;
513     FAIL_TEST (NULL != slave2);
514     FAIL_TEST (NULL != slave);
515     op = GNUNET_TESTBED_peer_create (mc, slave, cfg, peer_create_cb, NULL);
516     FAIL_TEST (NULL != op);
517     break;
518   case SLAVE1_PEER_START_SUCCESS:
519     check_operation_success (event);
520     GNUNET_TESTBED_operation_done (op);
521     result = SLAVE2_LINK_SUCCESS;
522     op = GNUNET_TESTBED_peer_create (mc, slave2, cfg, peer_create_cb, NULL);
523     FAIL_TEST (NULL != op);
524     break;
525   case MASTER_PEER_CREATE_SUCCESS:
526     FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
527     FAIL_TEST (event->details.peer_start.host == host);
528     FAIL_TEST (event->details.peer_start.peer == master_peer);
529     GNUNET_TESTBED_operation_done (op);
530     op = NULL;
531     result = MASTER_PEER_START_SUCCESS;
532     slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, cfg, 0);
533     FAIL_TEST (NULL != slave);
534     rh = GNUNET_TESTBED_register_host (mc, slave, &registration_cont, NULL);
535     FAIL_TEST (NULL != rh);
536     break;
537   case SLAVE1_PEER_CREATE_SUCCESS:
538     FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
539     FAIL_TEST (event->details.peer_start.host == slave);
540     FAIL_TEST (event->details.peer_start.peer == slave1_peer);
541     GNUNET_TESTBED_operation_done (op);
542     result = SLAVE1_PEER_START_SUCCESS;
543     op = GNUNET_TESTBED_controller_link (NULL, mc, slave2, slave, GNUNET_YES);
544     break;
545   case SLAVE2_PEER_CREATE_SUCCESS:
546     FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
547     FAIL_TEST (event->details.peer_stop.peer == slave1_peer);
548     GNUNET_TESTBED_operation_done (op);
549     result = SLAVE1_PEER_STOP_SUCCESS;
550     op = GNUNET_TESTBED_peer_start (NULL, slave2_peer, NULL, NULL);
551     FAIL_TEST (NULL != op);
552     break;
553   case SLAVE3_PEER_CREATE_SUCCESS:
554     FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
555     FAIL_TEST (event->details.peer_start.host == slave3);
556     FAIL_TEST (event->details.peer_start.peer == slave3_peer);
557     GNUNET_TESTBED_operation_done (op);
558     result = SLAVE3_PEER_START_SUCCESS;
559     sleep (1);
560     LOG_DEBUG ("**************************************\n");
561     op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, slave2_peer,
562                                          slave3_peer);
563     FAIL_TEST (NULL != op);
564     break;
565   case SLAVE3_PEER_START_SUCCESS:
566     FAIL_TEST (NULL != event);
567     FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
568     FAIL_TEST (event->details.peer_connect.peer1 == slave2_peer);
569     FAIL_TEST (event->details.peer_connect.peer2 == slave3_peer);
570     result = SLAVE2_SLAVE3_PEERS_CONNECTED;
571     GNUNET_TESTBED_operation_done (op);
572     op = NULL;
573     delay_task_id =
574         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
575                                       (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
576                                       NULL);
577     break;
578   case SLAVE1_PEER_STOP_SUCCESS:
579     FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
580     FAIL_TEST (event->details.peer_start.host == slave2);
581     FAIL_TEST (event->details.peer_start.peer == slave2_peer);
582     GNUNET_TESTBED_operation_done (op);
583     result = SLAVE2_PEER_START_SUCCESS;
584     op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, master_peer,
585                                          slave2_peer);
586     break;
587   case SLAVE2_PEER_START_SUCCESS:
588     FAIL_TEST (NULL != event);
589     FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
590     FAIL_TEST (event->details.peer_connect.peer1 == master_peer);
591     FAIL_TEST (event->details.peer_connect.peer2 == slave2_peer);
592     result = MASTER_SLAVE2_PEERS_CONNECTED;
593     GNUNET_TESTBED_operation_done (op);
594     op = NULL;
595     delay_task_id =
596         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
597                                       (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
598                                       NULL);
599     break;
600   case SLAVE2_SLAVE3_PEERS_CONNECTED:
601     FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
602     FAIL_TEST (event->details.peer_stop.peer == slave2_peer);
603     GNUNET_TESTBED_operation_done (op);
604     result = SLAVE2_PEER_STOP_SUCCESS;
605     op = GNUNET_TESTBED_peer_destroy (slave1_peer);
606     FAIL_TEST (NULL != op);
607     break;
608   case SLAVE2_PEER_STOP_SUCCESS:
609     check_operation_success (event);
610     GNUNET_TESTBED_operation_done (op);
611     result = SLAVE1_PEER_DESTROY_SUCCESS;
612     op = GNUNET_TESTBED_peer_destroy (slave2_peer);
613     FAIL_TEST (NULL != op);
614     break;
615   case SLAVE1_PEER_DESTROY_SUCCESS:
616     check_operation_success (event);
617     GNUNET_TESTBED_operation_done (op);
618     op = NULL;
619     result = SLAVE2_PEER_DESTROY_SUCCESS;
620     op = GNUNET_TESTBED_get_slave_config (NULL, mc, slave3);
621     FAIL_TEST (NULL != op);
622     break;
623   case SLAVE2_PEER_DESTROY_SUCCESS:
624     FAIL_TEST (NULL != event);
625     FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
626     FAIL_TEST (event->op == op);
627     FAIL_TEST (NULL == event->op_cls);
628     FAIL_TEST (NULL == event->details.operation_finished.emsg);
629     cfg3 = GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic);
630     GNUNET_TESTBED_operation_done (op);
631     result = SLAVE3_GET_CONFIG_SUCCESS;
632     op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, slave, GNUNET_NO);
633     break;
634   case SLAVE3_REGISTERED:
635     check_operation_success (event);
636     GNUNET_TESTBED_operation_done (op);
637     result = SLAVE3_STARTED;
638     op = GNUNET_TESTBED_peer_create (mc, slave3, cfg, peer_create_cb, NULL);
639     FAIL_TEST (NULL != op);
640     break;
641   case SLAVE3_GET_CONFIG_SUCCESS:
642     result = SLAVE3_LINK_SUCCESS;
643     GNUNET_TESTBED_operation_done (op);
644     op = GNUNET_TESTBED_peer_destroy (master_peer);
645     break;
646   case SLAVE3_LINK_SUCCESS:
647     check_operation_success (event);
648     result = MASTER_PEER_DESTROY_SUCCESS;
649     GNUNET_TESTBED_operation_done (op);
650     op = GNUNET_TESTBED_peer_destroy (slave3_peer);
651     break;
652   case MASTER_PEER_DESTROY_SUCCESS:
653     result = SUCCESS;
654     GNUNET_TESTBED_operation_done (op);
655     op = NULL;
656     GNUNET_SCHEDULER_shutdown ();
657     break;
658   default:
659     FAIL_TEST (0);
660   }
661 }
662
663
664 /**
665  * Callback which will be called to after a host registration succeeded or failed
666  *
667  * @param cls the host which has been registered
668  * @param emsg the error message; NULL if host registration is successful
669  */
670 static void
671 registration_cont (void *cls, const char *emsg)
672 {
673   rh = NULL;
674   switch (result)
675   {
676   case MASTER_PEER_START_SUCCESS:
677     FAIL_TEST (NULL == emsg);
678     FAIL_TEST (NULL != mc);
679     result = SLAVE1_REGISTERED;
680     slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, cfg, 0);
681     FAIL_TEST (NULL != slave2);
682     rh = GNUNET_TESTBED_register_host (mc, slave2, &registration_cont, NULL);
683     FAIL_TEST (NULL != rh);
684     break;
685   case SLAVE1_REGISTERED:
686     FAIL_TEST (NULL == emsg);
687     FAIL_TEST (NULL != mc);
688     result = SLAVE2_REGISTERED;
689     FAIL_TEST (NULL != cfg);
690     op = GNUNET_TESTBED_controller_link (NULL, mc, slave, NULL, GNUNET_YES);
691     FAIL_TEST (NULL != op);
692     break;
693   case MASTER_SLAVE2_PEERS_CONNECTED:
694     FAIL_TEST (NULL == emsg);
695     FAIL_TEST (NULL != mc);
696     FAIL_TEST (NULL == op);
697     result = SLAVE3_REGISTERED;
698     op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, NULL, GNUNET_YES);
699     FAIL_TEST (NULL != op);
700     break;
701   default:
702     GNUNET_break (0);
703     do_abort_now (NULL);
704   }
705 }
706
707 /**
708  * Callback to signal successfull startup of the controller process
709  *
710  * @param cls the closure from GNUNET_TESTBED_controller_start()
711  * @param cfg the configuration with which the controller has been started;
712  *          NULL if status is not GNUNET_OK
713  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
714  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
715  */
716 static void
717 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
718            int status)
719 {
720   switch (result)
721   {
722   case INIT:
723     FAIL_TEST (GNUNET_OK == status);
724     event_mask = 0;
725     event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
726     event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
727     event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
728     event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
729     mc = GNUNET_TESTBED_controller_connect (host, event_mask,
730                                             &controller_cb, NULL);
731     FAIL_TEST (NULL != mc);
732     result = MASTER_STARTED;
733     op = GNUNET_TESTBED_peer_create (mc, host, cfg, peer_create_cb, NULL);
734     FAIL_TEST (NULL != op);
735     break;
736   default:
737     GNUNET_break (0);
738     cp = NULL;
739     do_abort_now (NULL);
740   }
741 }
742
743
744 /**
745  * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
746  * inform whether the given host is habitable or not. The Handle returned by
747  * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
748  *
749  * @param cls NULL
750  * @param host the host whose status is being reported; will be NULL if the host
751  *          given to GNUNET_TESTBED_is_host_habitable() is NULL
752  * @param status #GNUNET_YES if it is habitable; #GNUNET_NO if not
753  */
754 static void
755 host_habitable_cb (void *cls,
756                    const struct GNUNET_TESTBED_Host *_host,
757                    int status)
758 {
759   hc_handle = NULL;
760   if (GNUNET_NO == status)
761   {
762     (void) PRINTF ("%s",
763                    "Unable to run the test as this system is not configured "
764                    "to use password less SSH logins to localhost.\n"
765                    "Skipping test\n");
766     GNUNET_SCHEDULER_cancel (abort_task);
767     abort_task = NULL;
768     GNUNET_SCHEDULER_shutdown ();
769     result = SKIP;
770     return;
771   }
772   cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb,
773                                         NULL);
774 }
775
776
777 /**
778  * Main run function.
779  *
780  * @param cls NULL
781  * @param args arguments passed to GNUNET_PROGRAM_run
782  * @param cfgfile the path to configuration file
783  * @param cfg the configuration file handle
784  */
785 static void
786 run (void *cls, char *const *args, const char *cfgfile,
787      const struct GNUNET_CONFIGURATION_Handle *config)
788 {
789   cfg = GNUNET_CONFIGURATION_dup (config);
790   host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
791   FAIL_TEST (NULL != host);
792   if (NULL ==
793       (hc_handle =
794        GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
795                                          NULL)))
796   {
797     GNUNET_TESTBED_host_destroy (host);
798     GNUNET_CONFIGURATION_destroy (cfg);
799     cfg = NULL;
800     host = NULL;
801     (void) PRINTF ("%s",
802                    "Unable to run the test as this system is not configured "
803                    "to use password less SSH logins to localhost.\n"
804                    "Marking test as successful\n");
805     result = SKIP;
806     return;
807   }
808   abort_task =
809       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
810                                     (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
811                                     NULL);
812   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
813                                  NULL);
814 }
815
816
817 /**
818  * Main function
819  */
820 int
821 main (int argc, char **argv)
822 {
823   char *const argv2[] = { "test_testbed_api_controllerlink",
824     "-c", "test_testbed_api.conf",
825     NULL
826   };
827   struct GNUNET_GETOPT_CommandLineOption options[] = {
828     GNUNET_GETOPT_OPTION_END
829   };
830   int ret;
831
832   result = INIT;
833   ret =
834       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
835                           "test_testbed_api_controllerlink", "nohelp", options,
836                           &run, NULL);
837   if (GNUNET_OK != ret)
838     return 1;
839   switch (result)
840   {
841   case SUCCESS:
842     return 0;
843   case SKIP:
844     return 77;                  /* Mark test as skipped */
845   default:
846     return 1;
847   }
848 }
849
850 /* end of test_testbed_api_controllerlink.c */