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