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