c5fae9257fc3af50ed920c3e5a7e7657f3183f33
[oweals/gnunet.git] / src / testbed / test_testbed_api_controllerlink.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2013 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 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., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, 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    * Destory master peer and mark test as success
185    */
186   SUCCESS,
187
188   /**
189    * Marks test as skipped
190    */
191   SKIP
192 };
193
194 /**
195  * Host for running master controller
196  */
197 static struct GNUNET_TESTBED_Host *host;
198
199 /**
200  * The master controller process
201  */
202 static struct GNUNET_TESTBED_ControllerProc *cp;
203
204 /**
205  * Handle to master controller
206  */
207 static struct GNUNET_TESTBED_Controller *mc;
208
209 /**
210  * Slave host for running slave controller
211  */
212 static struct GNUNET_TESTBED_Host *slave;
213
214 /**
215  * Another slave host for running another slave controller
216  */
217 static struct GNUNET_TESTBED_Host *slave2;
218
219 /**
220  * Host for slave 3
221  */
222 static struct GNUNET_TESTBED_Host *slave3;
223
224 /**
225  * Slave host registration handle
226  */
227 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
228
229 /**
230  * Handle to global configuration
231  */
232 static struct GNUNET_CONFIGURATION_Handle *cfg;
233
234 /**
235  * Configuration of slave 3 controller
236  */
237 static struct GNUNET_CONFIGURATION_Handle *cfg3;
238
239 /**
240  * Abort task
241  */
242 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
243
244 /**
245  * Operation handle for linking controllers
246  */
247 static struct GNUNET_TESTBED_Operation *op;
248
249 /**
250  * Handle to peer started at slave 1
251  */
252 static struct GNUNET_TESTBED_Peer *slave1_peer;
253
254 /**
255  * Handle to peer started at slave 2
256  */
257 static struct GNUNET_TESTBED_Peer *slave2_peer;
258
259 /**
260  * Handle to peer started at slave 2
261  */
262 static struct GNUNET_TESTBED_Peer *slave3_peer;
263
264 /**
265  * Handle to a peer started at master controller
266  */
267 static struct GNUNET_TESTBED_Peer *master_peer;
268
269 /**
270  * The handle for whether a host is habitable or not
271  */
272 struct GNUNET_TESTBED_HostHabitableCheckHandle *hc_handle;
273
274 /**
275  * The task handle for the delay task
276  */
277 GNUNET_SCHEDULER_TaskIdentifier delay_task_id;
278
279 /**
280  * Event mask
281  */
282 uint64_t event_mask;
283
284 /**
285  * Global testing status
286  */
287 static enum Stage result;
288
289 /**
290  * shortcut to exit during failure
291  */
292 #define FAIL_TEST(cond) do {                                    \
293     if (!(cond)) {                                              \
294       GNUNET_break(0);                                          \
295       if (GNUNET_SCHEDULER_NO_TASK != abort_task)               \
296         GNUNET_SCHEDULER_cancel (abort_task);                   \
297       abort_task = GNUNET_SCHEDULER_NO_TASK;                    \
298       GNUNET_SCHEDULER_add_now (do_shutdown, NULL);             \
299       return;                                                   \
300     }                                                          \
301   } while (0)
302
303
304 /**
305  * Shutdown nicely
306  *
307  * @param cls NULL
308  * @param tc the task context
309  */
310 static void
311 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
312 {
313   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
314     GNUNET_SCHEDULER_cancel (abort_task);
315   if (GNUNET_SCHEDULER_NO_TASK != delay_task_id)
316   {
317     GNUNET_SCHEDULER_cancel (delay_task_id);
318     delay_task_id = GNUNET_SCHEDULER_NO_TASK;
319   }
320   if (NULL != hc_handle)
321     GNUNET_TESTBED_is_host_habitable_cancel (hc_handle);
322   if (NULL != mc)
323     GNUNET_TESTBED_controller_disconnect (mc);
324   if (NULL != cp)
325     GNUNET_TESTBED_controller_stop (cp);
326   if (NULL != slave3)
327     GNUNET_TESTBED_host_destroy (slave3);
328   if (NULL != slave2)
329     GNUNET_TESTBED_host_destroy (slave2);
330   if (NULL != slave)
331     GNUNET_TESTBED_host_destroy (slave);
332   if (NULL != host)
333     GNUNET_TESTBED_host_destroy (host);
334   if (NULL != cfg)
335     GNUNET_CONFIGURATION_destroy (cfg);
336   if (NULL != cfg3)
337     GNUNET_CONFIGURATION_destroy (cfg3);
338   if (NULL != rh)
339     GNUNET_TESTBED_cancel_registration (rh);
340 }
341
342
343 /**
344  * abort task to run on test timed out
345  *
346  * @param cls NULL
347  * @param tc the task context
348  */
349 static void
350 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
351 {
352   LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
353   abort_task = GNUNET_SCHEDULER_NO_TASK;
354   do_shutdown (cls, tc);
355 }
356
357
358 /**
359  * Calls abort now
360  *
361  * @param
362  * @return
363  */
364 static void
365 do_abort_now (void *cls)
366 {
367   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
368     GNUNET_SCHEDULER_cancel (abort_task);
369   abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
370 }
371
372
373 /**
374  * Callback which will be called to after a host registration succeeded or failed
375  *
376  * @param cls the host which has been registered
377  * @param emsg the error message; NULL if host registration is successful
378  */
379 static void
380 registration_cont (void *cls, const char *emsg);
381
382
383 /**
384  * Task for inserting delay between tests
385  *
386  * @param
387  * @return
388  */
389 static void
390 delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
391 {
392   delay_task_id = GNUNET_SCHEDULER_NO_TASK;
393   switch (result)
394   {
395   case SLAVE2_PEER_CREATE_SUCCESS:
396     op = GNUNET_TESTBED_peer_stop (NULL, slave1_peer, NULL, NULL);
397     FAIL_TEST (NULL != op);
398     break;
399   case MASTER_SLAVE2_PEERS_CONNECTED:
400     slave3 = GNUNET_TESTBED_host_create_with_id (3, "127.0.0.1", NULL, cfg, 0);
401     rh = GNUNET_TESTBED_register_host (mc, slave3, &registration_cont, NULL);
402     break;
403   case SLAVE2_SLAVE3_PEERS_CONNECTED:
404     op = GNUNET_TESTBED_peer_stop (NULL, slave2_peer, NULL, NULL);
405     FAIL_TEST (NULL != op);
406     break;
407   default:
408     FAIL_TEST (0);
409   }
410 }
411
412
413 /**
414  * Functions of this signature are called when a peer has been successfully
415  * created
416  *
417  * @param cls the closure from GNUNET_TESTBED_peer_create()
418  * @param peer the handle for the created peer; NULL on any error during
419  *          creation
420  * @param emsg NULL if peer is not NULL; else MAY contain the error description
421  */
422 static void
423 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
424 {
425   FAIL_TEST (NULL != peer);
426   FAIL_TEST (NULL == emsg);
427   switch (result)
428   {
429   case MASTER_STARTED:
430     result = MASTER_PEER_CREATE_SUCCESS;
431     master_peer = peer;
432     GNUNET_TESTBED_operation_done (op);
433     op = GNUNET_TESTBED_peer_start (NULL, master_peer, NULL, NULL);
434     break;
435   case SLAVE1_LINK_SUCCESS:
436     result = SLAVE1_PEER_CREATE_SUCCESS;
437     slave1_peer = peer;
438     GNUNET_TESTBED_operation_done (op);
439     op = GNUNET_TESTBED_peer_start (NULL, slave1_peer, NULL, NULL);
440     break;
441   case SLAVE2_LINK_SUCCESS:
442     result = SLAVE2_PEER_CREATE_SUCCESS;
443     slave2_peer = peer;
444     GNUNET_TESTBED_operation_done (op);
445     delay_task_id =
446         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
447                                       (GNUNET_TIME_UNIT_SECONDS, 1),
448                                       &delay_task,
449                                       NULL);
450     break;
451   case SLAVE3_STARTED:
452     result = SLAVE3_PEER_CREATE_SUCCESS;
453     slave3_peer = peer;
454     GNUNET_TESTBED_operation_done (op);
455     op = GNUNET_TESTBED_peer_start (NULL, slave3_peer, NULL, NULL);
456     break;
457   default:
458     FAIL_TEST (0);
459   }
460   FAIL_TEST (NULL != op);
461 }
462
463
464 /**
465  * Checks the event if it is an operation finished event and if indicates a
466  * successfull completion of operation
467  *
468  * @param event the event information to check
469  */
470 static void
471 check_operation_success (const struct GNUNET_TESTBED_EventInformation *event)
472 {
473   FAIL_TEST (NULL != event);
474   FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
475   FAIL_TEST (event->op == op);
476   FAIL_TEST (NULL == event->op_cls);
477   FAIL_TEST (NULL == event->details.operation_finished.emsg);
478   FAIL_TEST (NULL == event->details.operation_finished.generic);
479 }
480
481
482 /**
483  * Signature of the event handler function called by the
484  * respective event controller.
485  *
486  * @param cls closure
487  * @param event information about the event
488  */
489 static void
490 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
491 {
492   switch (result)
493   {
494   case SLAVE2_REGISTERED:
495     check_operation_success (event);
496     GNUNET_TESTBED_operation_done (op);
497     op = NULL;
498     result = SLAVE1_LINK_SUCCESS;
499     FAIL_TEST (NULL != slave2);
500     FAIL_TEST (NULL != slave);
501     op = GNUNET_TESTBED_peer_create (mc, slave, cfg, peer_create_cb, NULL);
502     FAIL_TEST (NULL != op);
503     break;
504   case SLAVE1_PEER_START_SUCCESS:
505     check_operation_success (event);
506     GNUNET_TESTBED_operation_done (op);
507     result = SLAVE2_LINK_SUCCESS;
508     op = GNUNET_TESTBED_peer_create (mc, slave2, cfg, peer_create_cb, NULL);
509     FAIL_TEST (NULL != op);
510     break;
511   case MASTER_PEER_CREATE_SUCCESS:
512     FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
513     FAIL_TEST (event->details.peer_start.host == host);
514     FAIL_TEST (event->details.peer_start.peer == master_peer);
515     GNUNET_TESTBED_operation_done (op);
516     result = MASTER_PEER_START_SUCCESS;
517     slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, cfg, 0);
518     FAIL_TEST (NULL != slave);
519     rh = GNUNET_TESTBED_register_host (mc, slave, &registration_cont, NULL);
520     FAIL_TEST (NULL != rh);
521     break;
522   case SLAVE1_PEER_CREATE_SUCCESS:
523     FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
524     FAIL_TEST (event->details.peer_start.host == slave);
525     FAIL_TEST (event->details.peer_start.peer == slave1_peer);
526     GNUNET_TESTBED_operation_done (op);
527     result = SLAVE1_PEER_START_SUCCESS;
528     op = GNUNET_TESTBED_controller_link (NULL, mc, slave2, slave, GNUNET_YES);
529     break;
530   case SLAVE2_PEER_CREATE_SUCCESS:
531     FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
532     FAIL_TEST (event->details.peer_stop.peer == slave1_peer);
533     GNUNET_TESTBED_operation_done (op);
534     result = SLAVE1_PEER_STOP_SUCCESS;
535     op = GNUNET_TESTBED_peer_start (NULL, slave2_peer, NULL, NULL);
536     FAIL_TEST (NULL != op);
537     break;
538   case SLAVE3_PEER_CREATE_SUCCESS:
539     FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
540     FAIL_TEST (event->details.peer_start.host == slave3);
541     FAIL_TEST (event->details.peer_start.peer == slave3_peer);
542     GNUNET_TESTBED_operation_done (op);
543     result = SLAVE3_PEER_START_SUCCESS;
544     op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, slave2_peer,
545                                          slave3_peer);
546     FAIL_TEST (NULL != op);
547     break;
548   case SLAVE3_PEER_START_SUCCESS:
549     FAIL_TEST (NULL != event);
550     FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
551     FAIL_TEST (event->details.peer_connect.peer1 == slave2_peer);
552     FAIL_TEST (event->details.peer_connect.peer2 == slave3_peer);
553     result = SLAVE2_SLAVE3_PEERS_CONNECTED;
554     GNUNET_TESTBED_operation_done (op);
555     op = NULL;
556     delay_task_id =
557         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
558                                       (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
559                                       NULL);
560     break;
561   case SLAVE1_PEER_STOP_SUCCESS:
562     FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
563     FAIL_TEST (event->details.peer_start.host == slave2);
564     FAIL_TEST (event->details.peer_start.peer == slave2_peer);
565     GNUNET_TESTBED_operation_done (op);
566     result = SLAVE2_PEER_START_SUCCESS;
567     op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, master_peer,
568                                          slave2_peer);
569     break;
570   case SLAVE2_PEER_START_SUCCESS:
571     FAIL_TEST (NULL != event);
572     FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
573     FAIL_TEST (event->details.peer_connect.peer1 == master_peer);
574     FAIL_TEST (event->details.peer_connect.peer2 == slave2_peer);
575     result = MASTER_SLAVE2_PEERS_CONNECTED;
576     GNUNET_TESTBED_operation_done (op);
577     op = NULL;
578     delay_task_id =
579         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
580                                       (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
581                                       NULL);
582     break;
583   case SLAVE2_SLAVE3_PEERS_CONNECTED:
584     FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
585     FAIL_TEST (event->details.peer_stop.peer == slave2_peer);
586     GNUNET_TESTBED_operation_done (op);
587     result = SLAVE2_PEER_STOP_SUCCESS;
588     op = GNUNET_TESTBED_peer_destroy (slave1_peer);
589     FAIL_TEST (NULL != op);
590     break;
591   case SLAVE2_PEER_STOP_SUCCESS:
592     check_operation_success (event);
593     GNUNET_TESTBED_operation_done (op);
594     result = SLAVE1_PEER_DESTROY_SUCCESS;
595     op = GNUNET_TESTBED_peer_destroy (slave2_peer);
596     FAIL_TEST (NULL != op);
597     break;
598   case SLAVE1_PEER_DESTROY_SUCCESS:
599     check_operation_success (event);
600     GNUNET_TESTBED_operation_done (op);
601     op = NULL;
602     result = SLAVE2_PEER_DESTROY_SUCCESS;
603     op = GNUNET_TESTBED_get_slave_config (NULL, mc, slave3);
604     FAIL_TEST (NULL != op);
605     break;
606   case SLAVE2_PEER_DESTROY_SUCCESS:
607     FAIL_TEST (NULL != event);
608     FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
609     FAIL_TEST (event->op == op);
610     FAIL_TEST (NULL == event->op_cls);
611     FAIL_TEST (NULL == event->details.operation_finished.emsg);
612     cfg3 = GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic);
613     GNUNET_TESTBED_operation_done (op);
614     result = SLAVE3_GET_CONFIG_SUCCESS;
615     op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, slave, GNUNET_NO);
616     break;
617   case SLAVE3_REGISTERED:
618     check_operation_success (event);
619     GNUNET_TESTBED_operation_done (op);
620     op = NULL;
621     result = SLAVE3_STARTED;
622     op = GNUNET_TESTBED_peer_create (mc, slave3, cfg, peer_create_cb, NULL);
623     FAIL_TEST (NULL != op);
624     break;
625   case SLAVE3_GET_CONFIG_SUCCESS:
626     result = SLAVE3_LINK_SUCCESS;
627     GNUNET_TESTBED_operation_done (op);
628     op = GNUNET_TESTBED_peer_destroy (master_peer);
629     break;
630   case SLAVE3_LINK_SUCCESS:
631     check_operation_success (event);
632     result = SUCCESS;
633     GNUNET_TESTBED_operation_done (op);
634     op = NULL;
635     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
636                                   (GNUNET_TIME_UNIT_SECONDS, 1), &do_shutdown,
637                                   NULL);
638     break;
639   default:
640     FAIL_TEST (0);
641   }
642 }
643
644
645 /**
646  * Callback which will be called to after a host registration succeeded or failed
647  *
648  * @param cls the host which has been registered
649  * @param emsg the error message; NULL if host registration is successful
650  */
651 static void
652 registration_cont (void *cls, const char *emsg)
653 {
654   rh = NULL;
655   switch (result)
656   {
657   case MASTER_PEER_START_SUCCESS:
658     FAIL_TEST (NULL == emsg);
659     FAIL_TEST (NULL != mc);
660     result = SLAVE1_REGISTERED;
661     slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, cfg, 0);
662     FAIL_TEST (NULL != slave2);
663     rh = GNUNET_TESTBED_register_host (mc, slave2, &registration_cont, NULL);
664     FAIL_TEST (NULL != rh);
665     break;
666   case SLAVE1_REGISTERED:
667     FAIL_TEST (NULL == emsg);
668     FAIL_TEST (NULL != mc);
669     result = SLAVE2_REGISTERED;
670     FAIL_TEST (NULL != cfg);
671     op = GNUNET_TESTBED_controller_link (NULL, mc, slave, NULL, GNUNET_YES);
672     FAIL_TEST (NULL != op);
673     break;
674   case MASTER_SLAVE2_PEERS_CONNECTED:
675     FAIL_TEST (NULL == emsg);
676     FAIL_TEST (NULL != mc);
677     FAIL_TEST (NULL == op);
678     result = SLAVE3_REGISTERED;
679     op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, NULL, GNUNET_YES);
680     FAIL_TEST (NULL != op);
681     break;
682   default:
683     GNUNET_break (0);
684     do_abort_now (NULL);
685   }
686 }
687
688 /**
689  * Callback to signal successfull startup of the controller process
690  *
691  * @param cls the closure from GNUNET_TESTBED_controller_start()
692  * @param cfg the configuration with which the controller has been started;
693  *          NULL if status is not GNUNET_OK
694  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
695  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
696  */
697 static void
698 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
699            int status)
700 {
701   switch (result)
702   {
703   case INIT:
704     FAIL_TEST (GNUNET_OK == status);
705     event_mask = 0;
706     event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
707     event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
708     event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
709     event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
710     mc = GNUNET_TESTBED_controller_connect (host, event_mask,
711                                             &controller_cb, NULL);
712     FAIL_TEST (NULL != mc);
713     result = MASTER_STARTED;
714     op = GNUNET_TESTBED_peer_create (mc, host, cfg, peer_create_cb, NULL);
715     FAIL_TEST (NULL != op);
716     break;
717   default:
718     GNUNET_break (0);
719     cp = NULL;
720     do_abort_now (NULL);
721   }
722 }
723
724
725 /**
726  * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
727  * inform whether the given host is habitable or not. The Handle returned by
728  * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
729  *
730  * @param cls NULL
731  * @param host the host whose status is being reported; will be NULL if the host
732  *          given to GNUNET_TESTBED_is_host_habitable() is NULL
733  * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
734  */
735 static void
736 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *_host,
737                    int status)
738 {
739   hc_handle = NULL;
740   if (GNUNET_NO == status)
741   {
742     (void) PRINTF ("%s",
743                    "Unable to run the test as this system is not configured "
744                    "to use password less SSH logins to localhost.\n"
745                    "Skipping test\n");
746     GNUNET_SCHEDULER_cancel (abort_task);
747     abort_task = GNUNET_SCHEDULER_NO_TASK;
748     (void) GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
749     result = SKIP;
750     return;
751   }
752   cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb,
753                                         NULL);
754 }
755
756
757 /**
758  * Main run function.
759  *
760  * @param cls NULL
761  * @param args arguments passed to GNUNET_PROGRAM_run
762  * @param cfgfile the path to configuration file
763  * @param cfg the configuration file handle
764  */
765 static void
766 run (void *cls, char *const *args, const char *cfgfile,
767      const struct GNUNET_CONFIGURATION_Handle *config)
768 {
769   cfg = GNUNET_CONFIGURATION_dup (config);
770   host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
771   FAIL_TEST (NULL != host);
772   if (NULL ==
773       (hc_handle =
774        GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
775                                          NULL)))
776   {
777     GNUNET_TESTBED_host_destroy (host);
778     GNUNET_CONFIGURATION_destroy (cfg);
779     cfg = NULL;
780     host = NULL;
781     (void) PRINTF ("%s",
782                    "Unable to run the test as this system is not configured "
783                    "to use password less SSH logins to localhost.\n"
784                    "Marking test as successful\n");
785     result = SKIP;
786     return;
787   }
788   abort_task =
789       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
790                                     (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
791                                     NULL);
792 }
793
794
795 /**
796  * Main function
797  */
798 int
799 main (int argc, char **argv)
800 {
801   char *const argv2[] = { "test_testbed_api_controllerlink",
802     "-c", "test_testbed_api.conf",
803     NULL
804   };
805   struct GNUNET_GETOPT_CommandLineOption options[] = {
806     GNUNET_GETOPT_OPTION_END
807   };
808   int ret;
809
810   result = INIT;
811   ret =
812       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
813                           "test_testbed_api_controllerlink", "nohelp", options,
814                           &run, NULL);
815   if (GNUNET_OK != ret)
816     return 1;
817   switch (result)
818   {
819   case SUCCESS:
820     return 0;
821   case SKIP:
822     return 77;                  /* Mark test as skipped */
823   default:
824     return 1;
825   }
826 }
827
828 /* end of test_testbed_api_controllerlink.c */