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