- give out run handle through which master controller's handle can be retrieved
[oweals/gnunet.git] / src / testbed / test_testbed_api_2peers_1controller.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_2peers_1controller.c
23  * @brief testcases for the testbed api: 2 peers are configured, started and
24  *          connected together. The 2 peer reside on a single controller.
25  * @author Sree Harsha Totakura
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_testbed_service.h"
32
33
34 /**
35  * Generic logging shortcut
36  */
37 #define LOG(kind,...)                           \
38   GNUNET_log (kind, __VA_ARGS__)
39
40 /**
41  * Relative time seconds shorthand
42  */
43 #define TIME_REL_SECS(sec) \
44   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
45
46 /**
47  * Peer context
48  */
49 struct PeerContext
50 {
51   /**
52    * The peer handle
53    */
54   struct GNUNET_TESTBED_Peer *peer;
55
56   /**
57    * Operations involving this peer
58    */
59   struct GNUNET_TESTBED_Operation *operation;
60
61   /**
62    * set to GNUNET_YES when peer is started
63    */
64   int is_running;
65 };
66
67 /**
68  * Our localhost
69  */
70 static struct GNUNET_TESTBED_Host *host;
71
72 /**
73  * The controller process
74  */
75 static struct GNUNET_TESTBED_ControllerProc *cp;
76
77 /**
78  * The controller handle
79  */
80 static struct GNUNET_TESTBED_Controller *controller;
81
82 /**
83  * A neighbouring host
84  */
85 static struct GNUNET_TESTBED_Host *neighbour;
86
87 /**
88  * Handle for neighbour registration
89  */
90 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
91
92 /**
93  * peer 1
94  */
95 static struct PeerContext peer1;
96
97 /**
98  * peer2
99  */
100 static struct PeerContext peer2;
101
102 /**
103  * Handle to configuration
104  */
105 static struct GNUNET_CONFIGURATION_Handle *cfg;
106
107 /**
108  * Handle to operations involving both peers
109  */
110 static struct GNUNET_TESTBED_Operation *common_operation;
111
112 /**
113  * Abort task identifier
114  */
115 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
116
117 /**
118  * Delayed connect job identifier
119  */
120 static GNUNET_SCHEDULER_TaskIdentifier delayed_connect_task;
121
122 /**
123  * Different stages in testing
124  */
125 enum Stage
126 {
127
128   /**
129    * Initial stage
130    */
131   INIT,
132
133   /**
134    * peers are created
135    */
136   PEERS_CREATED,
137
138   /**
139    * peers are started
140    */
141   PEERS_STARTED,
142
143   /**
144    * peers are connected
145    */
146   PEERS_CONNECTED,
147
148   /**
149    * Peers are connected once again (this should not fail as they are already connected)
150    */
151   PEERS_CONNECTED_2,
152
153   /**
154    * peers are stopped
155    */
156   PEERS_STOPPED,
157
158   /**
159    * Final success stage
160    */
161   SUCCESS
162 };
163
164 /**
165  * The testing result
166  */
167 static enum Stage result;
168
169
170 /**
171  * shortcut to exit during failure
172  */
173 #define FAIL_TEST(cond) do {                                    \
174     if (!(cond)) {                                              \
175       GNUNET_break(0);                                          \
176       if (GNUNET_SCHEDULER_NO_TASK != abort_task)               \
177         GNUNET_SCHEDULER_cancel (abort_task);                   \
178       abort_task = GNUNET_SCHEDULER_NO_TASK;                    \
179       GNUNET_SCHEDULER_add_now (do_shutdown, NULL);             \
180       return;                                                   \
181     }                                                          \
182   } while (0)
183
184
185 /**
186  * Shutdown nicely
187  *
188  * @param cls NULL
189  * @param tc the task context
190  */
191 static void
192 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
193 {
194   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
195     GNUNET_SCHEDULER_cancel (abort_task);
196   if (GNUNET_SCHEDULER_NO_TASK != delayed_connect_task)
197     GNUNET_SCHEDULER_cancel (delayed_connect_task);
198   if (NULL != reg_handle)
199     GNUNET_TESTBED_cancel_registration (reg_handle);
200   GNUNET_TESTBED_controller_disconnect (controller);
201   GNUNET_CONFIGURATION_destroy (cfg);
202   if (NULL != cp)
203     GNUNET_TESTBED_controller_stop (cp);
204   GNUNET_TESTBED_host_destroy (neighbour);
205   GNUNET_TESTBED_host_destroy (host);
206 }
207
208
209 /**
210  * abort task to run on test timed out
211  *
212  * @param cls NULL
213  * @param tc the task context
214  */
215 static void
216 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
217 {
218   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
219   abort_task = GNUNET_SCHEDULER_NO_TASK;
220   do_shutdown (cls, tc);
221 }
222
223
224 /**
225  * Callback to be called when an operation is completed
226  *
227  * @param cls the callback closure from functions generating an operation
228  * @param op the operation that has been finished
229  * @param emsg error message in case the operation has failed; will be NULL if
230  *          operation has executed successfully.
231  */
232 static void
233 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
234
235
236 /**
237  * task for delaying a connect
238  *
239  * @param cls NULL
240  * @param tc the task context
241  */
242 static void
243 do_delayed_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
244 {
245   delayed_connect_task = GNUNET_SCHEDULER_NO_TASK;
246   FAIL_TEST (NULL == common_operation);
247   common_operation =
248       GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
249                                       peer2.peer);
250 }
251
252
253 /**
254  * Callback to be called when an operation is completed
255  *
256  * @param cls the callback closure from functions generating an operation
257  * @param op the operation that has been finished
258  * @param emsg error message in case the operation has failed; will be NULL if
259  *          operation has executed successfully.
260  */
261 static void
262 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
263 {
264   FAIL_TEST (common_operation == op);
265   switch (result)
266   {
267   case PEERS_STARTED:
268     FAIL_TEST (NULL == peer1.operation);
269     FAIL_TEST (NULL == peer2.operation);
270     FAIL_TEST (NULL != common_operation);
271     break;
272   case PEERS_CONNECTED:
273     FAIL_TEST (NULL == peer1.operation);
274     FAIL_TEST (NULL == peer2.operation);
275     FAIL_TEST (NULL != common_operation);
276     break;
277   default:
278     FAIL_TEST (0);
279   }
280 }
281
282
283 /**
284  * Signature of the event handler function called by the
285  * respective event controller.
286  *
287  * @param cls closure
288  * @param event information about the event
289  */
290 static void
291 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
292 {
293   switch (event->type)
294   {
295   case GNUNET_TESTBED_ET_OPERATION_FINISHED:   /* Will be reached when we destroy peers */
296     FAIL_TEST (PEERS_STOPPED == result);
297     FAIL_TEST (NULL == event->op_cls);
298     FAIL_TEST (NULL == event->details.operation_finished.emsg);
299     FAIL_TEST (NULL == event->details.operation_finished.generic);
300     if (event->op == peer1.operation)
301     {
302       GNUNET_TESTBED_operation_done (peer1.operation);
303       peer1.operation = NULL;
304       peer1.peer = NULL;
305     }
306     else if (event->op == peer2.operation)
307     {
308       GNUNET_TESTBED_operation_done (peer2.operation);
309       peer2.operation = NULL;
310       peer2.peer = NULL;
311     }
312     else
313       FAIL_TEST (0);
314     if ((NULL == peer1.peer) && (NULL == peer2.peer))
315     {
316       result = SUCCESS;
317       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
318     }
319     break;
320   case GNUNET_TESTBED_ET_PEER_START:
321     FAIL_TEST (INIT == result);
322     FAIL_TEST (event->details.peer_start.host == host);
323     if (event->details.peer_start.peer == peer1.peer)
324     {
325       peer1.is_running = GNUNET_YES;
326       GNUNET_TESTBED_operation_done (peer1.operation);
327       peer1.operation = NULL;
328     }
329     else if (event->details.peer_start.peer == peer2.peer)
330     {
331       peer2.is_running = GNUNET_YES;
332       GNUNET_TESTBED_operation_done (peer2.operation);
333       peer2.operation = NULL;
334     }
335     else
336       FAIL_TEST (0);
337     if ((GNUNET_YES == peer1.is_running) && (GNUNET_YES == peer2.is_running))
338     {
339       result = PEERS_STARTED;
340       common_operation =
341           GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
342                                           peer2.peer);
343     }
344     break;
345   case GNUNET_TESTBED_ET_PEER_STOP:
346     FAIL_TEST (PEERS_CONNECTED_2 == result);
347     if (event->details.peer_stop.peer == peer1.peer)
348     {
349       peer1.is_running = GNUNET_NO;
350       GNUNET_TESTBED_operation_done (peer1.operation);
351       peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
352     }
353     else if (event->details.peer_stop.peer == peer2.peer)
354     {
355       peer2.is_running = GNUNET_NO;
356       GNUNET_TESTBED_operation_done (peer2.operation);
357       peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
358     }
359     else
360       FAIL_TEST (0);
361     if ((GNUNET_NO == peer1.is_running) && (GNUNET_NO == peer2.is_running))
362       result = PEERS_STOPPED;
363     break;
364   case GNUNET_TESTBED_ET_CONNECT:
365     switch (result)
366     {
367     case PEERS_STARTED:
368       FAIL_TEST (NULL == peer1.operation);
369       FAIL_TEST (NULL == peer2.operation);
370       FAIL_TEST (NULL != common_operation);
371       FAIL_TEST ((event->details.peer_connect.peer1 == peer1.peer) &&
372                  (event->details.peer_connect.peer2 == peer2.peer));
373       GNUNET_TESTBED_operation_done (common_operation);
374       common_operation = NULL;
375       result = PEERS_CONNECTED;
376       LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
377       delayed_connect_task =
378           GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3), &do_delayed_connect,
379                                         NULL);
380       break;
381     case PEERS_CONNECTED:
382       FAIL_TEST (NULL == peer1.operation);
383       FAIL_TEST (NULL == peer2.operation);
384       FAIL_TEST (NULL != common_operation);
385       GNUNET_TESTBED_operation_done (common_operation);
386       common_operation = NULL;
387       result = PEERS_CONNECTED_2;
388       LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
389       peer1.operation = GNUNET_TESTBED_peer_stop (NULL, peer1.peer, NULL, NULL);
390       peer2.operation = GNUNET_TESTBED_peer_stop (NULL, peer2.peer, NULL, NULL);
391       break;
392     default:
393       FAIL_TEST (0);
394     }
395     break;
396   default:
397     FAIL_TEST (0);
398   };
399 }
400
401
402 /**
403  * Functions of this signature are called when a peer has been successfully
404  * created
405  *
406  * @param cls the closure from GNUNET_TESTBED_peer_create()
407  * @param peer the handle for the created peer; NULL on any error during
408  *          creation
409  * @param emsg NULL if peer is not NULL; else MAY contain the error description
410  */
411 static void
412 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
413 {
414   struct PeerContext *pc = cls;
415
416   FAIL_TEST (NULL != pc->operation);
417   FAIL_TEST (NULL != peer);
418   FAIL_TEST (NULL == pc->peer);
419   pc->peer = peer;
420   GNUNET_TESTBED_operation_done (pc->operation);
421   pc->operation = GNUNET_TESTBED_peer_start (NULL, pc->peer, NULL, NULL);
422 }
423
424
425 /**
426  * Callback which will be called to after a host registration succeeded or failed
427  *
428  * @param cls the host which has been registered
429  * @param emsg the error message; NULL if host registration is successful
430  */
431 static void
432 registration_comp (void *cls, const char *emsg)
433 {
434   FAIL_TEST (cls == neighbour);
435   reg_handle = NULL;
436   peer1.operation =
437       GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
438                                   &peer1);
439   peer2.operation =
440       GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
441                                   &peer2);
442   FAIL_TEST (NULL != peer1.operation);
443   FAIL_TEST (NULL != peer2.operation);
444 }
445
446
447 /**
448  * Callback to signal successfull startup of the controller process
449  *
450  * @param cls the closure from GNUNET_TESTBED_controller_start()
451  * @param cfg the configuration with which the controller has been started;
452  *          NULL if status is not GNUNET_OK
453  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
454  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
455  */
456 static void
457 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg_, int status)
458 {
459   uint64_t event_mask;
460
461   if (GNUNET_OK != status)
462   {
463     cp = NULL;
464     FAIL_TEST (0);
465   }
466   event_mask = 0;
467   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
468   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
469   event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
470   event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
471   controller =
472       GNUNET_TESTBED_controller_connect (host, event_mask, &controller_cb,
473                                          NULL);
474   FAIL_TEST (NULL != controller);
475   neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0);
476   FAIL_TEST (NULL != neighbour);
477   reg_handle =
478       GNUNET_TESTBED_register_host (controller, neighbour, &registration_comp,
479                                     neighbour);
480   FAIL_TEST (NULL != reg_handle);
481 }
482
483
484
485 /**
486  * Main run function.
487  *
488  * @param cls NULL
489  * @param args arguments passed to GNUNET_PROGRAM_run
490  * @param cfgfile the path to configuration file
491  * @param cfg the configuration file handle
492  */
493 static void
494 run (void *cls, char *const *args, const char *cfgfile,
495      const struct GNUNET_CONFIGURATION_Handle *config)
496 {
497   cfg = GNUNET_CONFIGURATION_dup (config);
498   host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
499   FAIL_TEST (NULL != host);
500   cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb,
501                                         NULL);
502   abort_task =
503       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
504                                     (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
505                                     NULL);
506 }
507
508
509 /**
510  * Main function
511  */
512 int
513 main (int argc, char **argv)
514 {
515   int ret;
516
517   char *const argv2[] = { "test_testbed_api_2peers_1controller",
518     "-c", "test_testbed_api.conf",
519     NULL
520   };
521   struct GNUNET_GETOPT_CommandLineOption options[] = {
522     GNUNET_GETOPT_OPTION_END
523   };
524   result = INIT;
525   ret =
526       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
527                           "test_testbed_api_2peers_1controller", "nohelp",
528                           options, &run, NULL);
529   if ((GNUNET_OK != ret) || (SUCCESS != result))
530     return 1;
531   return 0;
532 }
533
534 /* end of test_testbed_api_2peers_1controller.c */