-reducing testing peers and links
[oweals/gnunet.git] / src / testbed / test_testbed_api_controllerlink.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 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-new.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    * Peer on slave 2 successfully stopped
135    */
136   SLAVE2_PEER_STOP_SUCCESS,
137
138   /**
139    * Peer destroy on slave 1 successful
140    */
141   SLAVE1_PEER_DESTROY_SUCCESS,
142
143   /**
144    * Peer destory on slave 2 successful
145    */
146   SLAVE2_PEER_DESTROY_SUCCESS,
147
148   /**
149    * Slave 3 has successfully registered
150    */
151   SLAVE3_REGISTERED,
152
153   /**
154    * Slave 3 has successfully started
155    */
156   SLAVE3_STARTED,
157
158   /**
159    * The configuration of slave 3 is acquired
160    */
161   SLAVE3_GET_CONFIG_SUCCESS,
162
163   /**
164    * Slave 1 has linked to slave 3;
165    */
166   SLAVE3_LINK_SUCCESS,
167
168   /**
169    * Destory master peer and mark test as success
170    */
171   SUCCESS
172
173 };
174
175 /**
176  * Host for running master controller
177  */
178 static struct GNUNET_TESTBED_Host *host;
179
180 /**
181  * The master controller process
182  */
183 static struct GNUNET_TESTBED_ControllerProc *cp;
184
185 /**
186  * Handle to master controller
187  */
188 static struct GNUNET_TESTBED_Controller *mc;
189
190 /**
191  * Slave host for running slave controller
192  */
193 static struct GNUNET_TESTBED_Host *slave;
194
195 /**
196  * Another slave host for running another slave controller
197  */
198 static struct GNUNET_TESTBED_Host *slave2;
199
200 /**
201  * Host for slave 3
202  */
203 static struct GNUNET_TESTBED_Host *slave3;
204
205 /**
206  * Slave host registration handle
207  */
208 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
209
210 /**
211  * Handle to global configuration
212  */
213 static struct GNUNET_CONFIGURATION_Handle *cfg;
214
215 /**
216  * Configuration of slave 3 controller
217  */
218 static struct GNUNET_CONFIGURATION_Handle *cfg3;
219
220 /**
221  * Abort task
222  */
223 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
224
225 /**
226  * Operation handle for linking controllers
227  */
228 static struct GNUNET_TESTBED_Operation *op;
229
230 /**
231  * Handle to peer started at slave 1
232  */
233 static struct GNUNET_TESTBED_Peer *slave1_peer;
234
235 /**
236  * Handle to peer started at slave 2
237  */
238 static struct GNUNET_TESTBED_Peer *slave2_peer;
239
240 /**
241  * Handle to a peer started at master controller
242  */
243 static struct GNUNET_TESTBED_Peer *master_peer;
244
245 /**
246  * Event mask
247  */
248 uint64_t event_mask;
249
250 /**
251  * Global testing status
252  */
253 static enum Stage result;
254
255
256 /**
257  * Shutdown nicely
258  *
259  * @param cls NULL
260  * @param tc the task context
261  */
262 static void
263 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
264 {
265   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
266     GNUNET_SCHEDULER_cancel (abort_task);
267   if (NULL != slave3)
268     GNUNET_TESTBED_host_destroy (slave3);
269   if (NULL != slave2)
270     GNUNET_TESTBED_host_destroy (slave2);
271   if (NULL != slave)
272     GNUNET_TESTBED_host_destroy (slave);
273   if (NULL != host)
274     GNUNET_TESTBED_host_destroy (host);
275   if (NULL != mc)
276     GNUNET_TESTBED_controller_disconnect (mc);
277   if (NULL != cfg)
278     GNUNET_CONFIGURATION_destroy (cfg);
279   if (NULL != cfg3)
280     GNUNET_CONFIGURATION_destroy (cfg3);
281   if (NULL != cp)
282     GNUNET_TESTBED_controller_stop (cp);
283   if (NULL != rh)
284     GNUNET_TESTBED_cancel_registration (rh);
285 }
286
287
288 /**
289  * abort task to run on test timed out
290  *
291  * @param cls NULL
292  * @param tc the task context
293  */
294 static void
295 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
296 {
297   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
298   abort_task = GNUNET_SCHEDULER_NO_TASK;
299   do_shutdown (cls, tc);
300 }
301
302
303 /**
304  * Calls abort now
305  *
306  * @param 
307  * @return 
308  */
309 static void
310 do_abort_now (void *cls)
311 {
312   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
313     GNUNET_SCHEDULER_cancel (abort_task);
314   abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
315 }
316
317
318 /**
319  * Task for inserting delay between tests
320  *
321  * @param
322  * @return
323  */
324 static void
325 delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
326 {
327   switch (result)
328   {
329   case SLAVE2_PEER_CREATE_SUCCESS:
330     op = GNUNET_TESTBED_peer_stop (slave1_peer, NULL, NULL);
331     GNUNET_assert (NULL != op);
332     break;
333   case MASTER_SLAVE2_PEERS_CONNECTED:
334     op = GNUNET_TESTBED_peer_stop (slave2_peer, NULL, NULL);
335     GNUNET_assert (NULL != op);
336     break;
337   default:
338     GNUNET_assert (0);
339   }
340 }
341
342
343 /**
344  * Functions of this signature are called when a peer has been successfully
345  * created
346  *
347  * @param cls the closure from GNUNET_TESTBED_peer_create()
348  * @param peer the handle for the created peer; NULL on any error during
349  *          creation
350  * @param emsg NULL if peer is not NULL; else MAY contain the error description
351  */
352 static void
353 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
354 {
355   GNUNET_assert (NULL != peer);
356   GNUNET_assert (NULL == emsg);
357   switch (result)
358   {
359   case MASTER_STARTED:
360     result = MASTER_PEER_CREATE_SUCCESS;
361     master_peer = peer;
362     GNUNET_TESTBED_operation_done (op);
363     op = GNUNET_TESTBED_peer_start (NULL, master_peer, NULL, NULL);
364     break;
365   case SLAVE1_LINK_SUCCESS:
366     result = SLAVE1_PEER_CREATE_SUCCESS;
367     slave1_peer = peer;
368     GNUNET_TESTBED_operation_done (op);   
369     op = GNUNET_TESTBED_peer_start (NULL, slave1_peer, NULL, NULL);
370     break;
371   case SLAVE2_LINK_SUCCESS:
372     result = SLAVE2_PEER_CREATE_SUCCESS;
373     slave2_peer = peer;
374     GNUNET_TESTBED_operation_done (op);
375     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
376                                   (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
377                                   NULL);
378     break;
379   default:
380     GNUNET_assert (0);
381   }
382   GNUNET_assert (NULL != op);
383 }
384
385
386 /**
387  * Checks the event if it is an operation finished event and if indicates a
388  * successfull completion of operation
389  *
390  * @param event the event information to check
391  */
392 static void
393 check_operation_success (const struct GNUNET_TESTBED_EventInformation *event)
394 {
395   GNUNET_assert (NULL != event);
396   GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
397   GNUNET_assert (event->details.operation_finished.operation == op);
398   GNUNET_assert (NULL == event->details.operation_finished.op_cls);
399   GNUNET_assert (NULL == event->details.operation_finished.emsg);
400   GNUNET_assert (NULL == event->details.operation_finished.generic);
401 }
402
403
404 /**
405  * Callback which will be called to after a host registration succeeded or failed
406  *
407  * @param cls the host which has been registered
408  * @param emsg the error message; NULL if host registration is successful
409  */
410 static void
411 registration_cont (void *cls, const char *emsg);
412
413
414 /**
415  * Signature of the event handler function called by the
416  * respective event controller.
417  *
418  * @param cls closure
419  * @param event information about the event
420  */
421 static void
422 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
423 {
424   switch (result)
425   {
426   case SLAVE2_REGISTERED:
427     check_operation_success (event);
428     GNUNET_TESTBED_operation_done (op);
429     op = NULL;
430     result = SLAVE1_LINK_SUCCESS;
431     GNUNET_assert (NULL != slave2);
432     GNUNET_assert (NULL != slave);
433     op = GNUNET_TESTBED_peer_create (mc, slave, cfg, peer_create_cb, NULL);
434     GNUNET_assert (NULL != op);
435     break;
436   case SLAVE1_PEER_START_SUCCESS:
437     check_operation_success (event);
438     GNUNET_TESTBED_operation_done (op);
439     result = SLAVE2_LINK_SUCCESS;
440     op = GNUNET_TESTBED_peer_create (mc, slave2, cfg, peer_create_cb, NULL);    
441     GNUNET_assert (NULL != op);
442     break;
443   case MASTER_PEER_CREATE_SUCCESS:
444     GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type);
445     GNUNET_assert (event->details.peer_start.host == host);
446     GNUNET_assert (event->details.peer_start.peer == master_peer);
447     GNUNET_TESTBED_operation_done (op);
448     result = MASTER_PEER_START_SUCCESS;
449     slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, 0);
450     GNUNET_assert (NULL != slave);
451     rh = GNUNET_TESTBED_register_host (mc, slave, &registration_cont, NULL);
452     GNUNET_assert (NULL != rh);
453     break;
454   case SLAVE1_PEER_CREATE_SUCCESS:
455     GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type);
456     GNUNET_assert (event->details.peer_start.host == slave);
457     GNUNET_assert (event->details.peer_start.peer == slave1_peer);
458     GNUNET_TESTBED_operation_done (op);
459     result = SLAVE1_PEER_START_SUCCESS;
460     op = GNUNET_TESTBED_controller_link (NULL, mc, slave2, slave, cfg,
461                                          GNUNET_YES);  
462     break;
463   case SLAVE2_PEER_CREATE_SUCCESS:
464     GNUNET_assert (GNUNET_TESTBED_ET_PEER_STOP == event->type);
465     GNUNET_assert (event->details.peer_stop.peer == slave1_peer);
466     GNUNET_TESTBED_operation_done (op);
467     result = SLAVE1_PEER_STOP_SUCCESS;
468     op = GNUNET_TESTBED_peer_start (NULL, slave2_peer, NULL, NULL);
469     GNUNET_assert (NULL != op);
470     break;
471   case SLAVE1_PEER_STOP_SUCCESS:
472     GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type);
473     GNUNET_assert (event->details.peer_start.host == slave2);
474     GNUNET_assert (event->details.peer_start.peer == slave2_peer);
475     GNUNET_TESTBED_operation_done (op);
476     result = SLAVE2_PEER_START_SUCCESS;
477     op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, master_peer,
478                                          slave2_peer);
479     break;
480   case SLAVE2_PEER_START_SUCCESS:
481     GNUNET_assert (NULL != event);
482     GNUNET_assert (GNUNET_TESTBED_ET_CONNECT == event->type);
483     GNUNET_assert (event->details.peer_connect.peer1 == master_peer);
484     GNUNET_assert (event->details.peer_connect.peer2 == slave2_peer);
485     result = MASTER_SLAVE2_PEERS_CONNECTED;
486     GNUNET_TESTBED_operation_done (op);
487     op = NULL;
488     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
489                                   (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
490                                   NULL);
491     break;
492   case MASTER_SLAVE2_PEERS_CONNECTED:
493     GNUNET_assert (GNUNET_TESTBED_ET_PEER_STOP == event->type);
494     GNUNET_assert (event->details.peer_stop.peer == slave2_peer);
495     GNUNET_TESTBED_operation_done (op);
496     result = SLAVE2_PEER_STOP_SUCCESS;
497     op = GNUNET_TESTBED_peer_destroy (slave1_peer);
498     GNUNET_assert (NULL != op);
499     break;
500   case SLAVE2_PEER_STOP_SUCCESS:
501     check_operation_success (event);
502     GNUNET_TESTBED_operation_done (op);
503     result = SLAVE1_PEER_DESTROY_SUCCESS;
504     op = GNUNET_TESTBED_peer_destroy (slave2_peer);
505     GNUNET_assert (NULL != op);
506     break;
507   case SLAVE1_PEER_DESTROY_SUCCESS:
508     check_operation_success (event);
509     GNUNET_TESTBED_operation_done (op);
510     op = NULL;
511     result = SLAVE2_PEER_DESTROY_SUCCESS;
512     slave3 = GNUNET_TESTBED_host_create_with_id (3, "127.0.0.1", NULL, 0);
513     rh = GNUNET_TESTBED_register_host (mc, slave3, &registration_cont, NULL);
514     break;
515   case SLAVE3_REGISTERED:
516     check_operation_success (event);
517     GNUNET_TESTBED_operation_done (op);
518     op = NULL;
519     result = SLAVE3_STARTED;
520     op = GNUNET_TESTBED_get_slave_config (NULL, mc, slave3);
521     GNUNET_assert (NULL != op);    
522     break;
523   case SLAVE3_STARTED:
524     GNUNET_assert (NULL != event);
525     GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
526     GNUNET_assert (event->details.operation_finished.operation == op);
527     GNUNET_assert (NULL == event->details.operation_finished.op_cls);
528     GNUNET_assert (NULL == event->details.operation_finished.emsg);
529     cfg3 = GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic);
530     GNUNET_TESTBED_operation_done (op);
531     result = SLAVE3_GET_CONFIG_SUCCESS;
532     op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, slave, cfg3, GNUNET_NO);
533     break;
534   case SLAVE3_GET_CONFIG_SUCCESS:
535     result = SLAVE3_LINK_SUCCESS;
536     GNUNET_TESTBED_operation_done (op);
537     op = GNUNET_TESTBED_peer_destroy (master_peer);
538     break;
539  case SLAVE3_LINK_SUCCESS:
540    check_operation_success (event);
541    result = SUCCESS;
542    GNUNET_TESTBED_operation_done (op);
543    op = NULL;       
544    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
545                                  (GNUNET_TIME_UNIT_SECONDS, 3),
546                                  &do_shutdown, NULL);
547    break;
548   default:
549     GNUNET_assert (0);
550   }
551 }
552
553
554 /**
555  * Callback which will be called to after a host registration succeeded or failed
556  *
557  * @param cls the host which has been registered
558  * @param emsg the error message; NULL if host registration is successful
559  */
560 static void
561 registration_cont (void *cls, const char *emsg)
562 {
563   rh = NULL;
564   switch (result)
565   {
566   case MASTER_PEER_START_SUCCESS:
567     GNUNET_assert (NULL == emsg);
568     GNUNET_assert (NULL != mc);
569     result = SLAVE1_REGISTERED;
570     slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, 0);
571     GNUNET_assert (NULL != slave2);
572     rh = GNUNET_TESTBED_register_host (mc, slave2, &registration_cont, NULL);
573     GNUNET_assert (NULL != rh);
574     break;
575   case SLAVE1_REGISTERED:
576     GNUNET_assert (NULL == emsg);
577     GNUNET_assert (NULL != mc);
578     result = SLAVE2_REGISTERED;
579     GNUNET_assert (NULL != cfg);
580     op = GNUNET_TESTBED_controller_link (NULL, mc, slave, NULL, cfg, GNUNET_YES);
581     GNUNET_assert (NULL != op);
582     break;
583   case SLAVE2_PEER_DESTROY_SUCCESS:
584     GNUNET_assert (NULL == emsg);
585     GNUNET_assert (NULL != mc);
586     GNUNET_assert (NULL == op);
587     result = SLAVE3_REGISTERED;
588     op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, NULL, cfg, GNUNET_YES);
589     GNUNET_assert (NULL != op);
590     break;
591   default:
592     GNUNET_break (0);
593     do_abort_now (NULL);
594   }
595 }
596
597 /**
598  * Callback to signal successfull startup of the controller process
599  *
600  * @param cls the closure from GNUNET_TESTBED_controller_start()
601  * @param cfg the configuration with which the controller has been started;
602  *          NULL if status is not GNUNET_OK
603  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
604  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
605  */
606 static void
607 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
608            int status)
609 {
610   switch (result)
611   {
612   case INIT:
613     GNUNET_assert (GNUNET_OK == status);
614     event_mask = 0;
615     event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
616     event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
617     event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
618     event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
619     mc = GNUNET_TESTBED_controller_connect (config, host, event_mask,
620                                             &controller_cb, NULL);
621     GNUNET_assert (NULL != mc);
622     result = MASTER_STARTED;
623     op = GNUNET_TESTBED_peer_create (mc, host, cfg, peer_create_cb, NULL);
624     GNUNET_assert (NULL != op);
625     break;
626   default:    
627     GNUNET_break (0);
628     cp = NULL;
629     do_abort_now (NULL);
630   }
631 }
632
633
634 /**
635  * Main run function.
636  *
637  * @param cls NULL
638  * @param args arguments passed to GNUNET_PROGRAM_run
639  * @param cfgfile the path to configuration file
640  * @param cfg the configuration file handle
641  */
642 static void
643 run (void *cls, char *const *args, const char *cfgfile,
644      const struct GNUNET_CONFIGURATION_Handle *config)
645 {
646   host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
647   GNUNET_assert (NULL != host);
648   if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (host, config))
649   {
650     GNUNET_TESTBED_host_destroy (host);
651     host = NULL;
652     (void) PRINTF ("%s",
653                    "Unable to run the test as this system is not configured "
654                    "to use password less SSH logins to localhost.\n"
655                    "Marking test as successful\n");
656     result = SUCCESS;
657     return;
658   }
659   cfg = GNUNET_CONFIGURATION_dup (config);
660   cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
661                                         NULL);
662   abort_task =
663       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
664                                     (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
665                                     NULL);
666 }
667
668
669 /**
670  * Main function
671  */
672 int
673 main (int argc, char **argv)
674 {
675   char *const argv2[] = { "test_testbed_api_controllerlink",
676     "-c", "test_testbed_api.conf",
677     NULL
678   };
679   struct GNUNET_GETOPT_CommandLineOption options[] = {
680     GNUNET_GETOPT_OPTION_END
681   };
682   int ret;
683
684   result = INIT;
685   ret =
686       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
687                           "test_testbed_api_controllerlink", "nohelp", options,
688                           &run, NULL);
689   if ((GNUNET_OK != ret) || (SUCCESS != result))
690     return 1;
691   return 0;
692 }
693
694 /* end of test_testbed_api_controllerlink.c */
695