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