- opaque mq structs
[oweals/gnunet.git] / src / testbed / test_testbed_api.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.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_dht_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 DHT service
87  */
88 static struct GNUNET_DHT_Handle *dht_handle;
89
90 /**
91  * Abort task identifier
92  */
93 static GNUNET_SCHEDULER_TaskIdentifier 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, const struct GNUNET_SCHEDULER_TaskContext *tc)
140 {
141   LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down...\n");
142   if (GNUNET_SCHEDULER_NO_TASK != 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 (GNUNET_SCHEDULER_NO_TASK != abort_task)               \
166         GNUNET_SCHEDULER_cancel (abort_task);                   \
167       abort_task = GNUNET_SCHEDULER_NO_TASK;                    \
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, const struct GNUNET_SCHEDULER_TaskContext *tc)
182 {
183   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
184   abort_task = GNUNET_SCHEDULER_NO_TASK;
185   do_shutdown (cls, tc);
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 dht_connect_adapter (void *cls, 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   dht_handle = GNUNET_DHT_connect (cfg, 10);
206   return dht_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 dht_disconnect_adapter (void *cls, void *op_result)
219 {
220   FAIL_TEST (NULL != op_result, return);
221   FAIL_TEST (op_result == dht_handle, return);
222   GNUNET_DHT_disconnect (dht_handle);
223   dht_handle = NULL;
224   FAIL_TEST (PEER_SERVICE_CONNECT == sub_test, return);
225   FAIL_TEST (NULL != operation, return);
226   operation = GNUNET_TESTBED_peer_stop (NULL, peer, NULL, NULL);
227   FAIL_TEST (NULL != operation, return);
228 }
229
230
231 /**
232  * Callback to be called when a service connect operation is completed
233  *
234  * @param cls the callback closure from functions generating an operation
235  * @param op the operation that has been finished
236  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
237  * @param emsg error message in case the operation has failed; will be NULL if
238  *          operation has executed successfully.
239  */
240 static void
241 service_connect_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
242                          void *ca_result, const char *emsg)
243 {
244   switch (sub_test)
245   {
246   case PEER_SERVICE_CONNECT:
247     FAIL_TEST (operation == op, return);
248     FAIL_TEST (NULL == emsg, return);
249     FAIL_TEST (NULL == cls, return);
250     FAIL_TEST (ca_result == dht_handle, return);
251     GNUNET_TESTBED_operation_done (operation);  /* This results in call to
252                                                  * disconnect adapter */
253     break;
254   default:
255     FAIL_TEST (0, return);
256   }
257 }
258
259
260
261 /**
262  * Callback to be called when the requested peer information is available
263  *
264  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
265  * @param op the operation this callback corresponds to
266  * @param pinfo the result; will be NULL if the operation has failed
267  * @param emsg error message if the operation has failed; will be NULL if the
268  *          operation is successfull
269  */
270 static void
271 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
272              const struct GNUNET_TESTBED_PeerInformation *pinfo,
273              const char *emsg)
274 {
275   switch (sub_test)
276   {
277   case PEER_GETCONFIG:
278     FAIL_TEST (NULL != pinfo, return);
279     FAIL_TEST (NULL == emsg, return);
280     FAIL_TEST (NULL == cb_cls, return);
281     FAIL_TEST (operation == op, return);
282     FAIL_TEST (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit, return);
283     FAIL_TEST (NULL != pinfo->result.cfg, return);
284     sub_test = PEER_DESTROY;
285     GNUNET_TESTBED_operation_done (operation);
286     operation = GNUNET_TESTBED_peer_destroy (peer);
287     break;
288   default:
289     FAIL_TEST (0, return);
290   }
291 }
292
293
294 /**
295  * Signature of the event handler function called by the
296  * respective event controller.
297  *
298  * @param cls closure
299  * @param event information about the event
300  */
301 static void
302 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
303 {
304   switch (event->type)
305   {
306   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
307     switch (sub_test)
308     {
309     case PEER_DESTROY:
310       FAIL_TEST (event->op == operation, return);
311       FAIL_TEST (NULL == event->op_cls, return);
312       FAIL_TEST (NULL == event->details.operation_finished.emsg, return);
313       FAIL_TEST (NULL == event->details.operation_finished.generic, return);
314       GNUNET_TESTBED_operation_done (operation);
315       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
316       break;
317     case PEER_SERVICE_CONNECT:
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 != dht_handle, return);
322       FAIL_TEST (event->details.operation_finished.generic == dht_handle, return);
323       break;
324     default:
325       FAIL_TEST (0, return);
326       break;
327     }
328     break;
329   case GNUNET_TESTBED_ET_PEER_START:
330     FAIL_TEST (event->details.peer_start.host == host, return);
331     FAIL_TEST (event->details.peer_start.peer == peer, return);
332     FAIL_TEST (OTHER == sub_test, return);
333     GNUNET_TESTBED_operation_done (operation);
334     operation =
335         GNUNET_TESTBED_service_connect (NULL, peer, "dht",
336                                         &service_connect_comp_cb, NULL,
337                                         &dht_connect_adapter,
338                                         &dht_disconnect_adapter, NULL);
339     FAIL_TEST (NULL != operation, return);
340     break;
341   case GNUNET_TESTBED_ET_PEER_STOP:
342     FAIL_TEST (event->details.peer_stop.peer == peer, return);
343     FAIL_TEST (PEER_SERVICE_CONNECT == sub_test, return);
344     result = GNUNET_YES;
345     sub_test = PEER_GETCONFIG;
346     GNUNET_TESTBED_operation_done (operation);
347     operation =
348         GNUNET_TESTBED_peer_get_information (peer,
349                                              GNUNET_TESTBED_PIT_CONFIGURATION,
350                                              &peerinfo_cb, NULL);
351     break;
352   default:
353     FAIL_TEST (0, return);          /* We should never reach this state */
354   }
355 }
356
357
358 /**
359  * Functions of this signature are called when a peer has been successfully
360  * created
361  *
362  * @param cls the closure from GNUNET_TESTBED_peer_create()
363  * @param peer the handle for the created peer; NULL on any error during
364  *          creation
365  * @param emsg NULL if peer is not NULL; else MAY contain the error description
366  */
367 static void
368 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
369 {
370   struct GNUNET_TESTBED_Peer **peer_ptr;
371
372   peer_ptr = cls;
373   FAIL_TEST (NULL != peer, return);
374   FAIL_TEST (NULL != peer_ptr, return);
375   *peer_ptr = peer;
376   GNUNET_TESTBED_operation_done (operation);
377   operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
378   FAIL_TEST (NULL != operation, return);
379 }
380
381
382 /**
383  * Callback which will be called to after a host registration succeeded or failed
384  *
385  * @param cls the host which has been registered
386  * @param emsg the error message; NULL if host registration is successful
387  */
388 static void
389 registration_comp (void *cls, const char *emsg)
390 {
391   FAIL_TEST (cls == neighbour, return);
392   reg_handle = NULL;
393   operation =
394       GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
395                                   &peer);
396   FAIL_TEST (NULL != operation, return);
397 }
398
399
400 /**
401  * Callback to signal successfull startup of the controller process
402  *
403  * @param cls the closure from GNUNET_TESTBED_controller_start()
404  * @param cfg the configuration with which the controller has been started;
405  *          NULL if status is not GNUNET_OK
406  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
407  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
408  */
409 static void
410 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg_, int status)
411 {
412   uint64_t event_mask;
413
414   if (GNUNET_OK != status)
415   {
416     cp = NULL;
417     FAIL_TEST (0, return);
418     return;
419   }  
420   event_mask = 0;
421   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
422   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
423   event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
424   event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
425   controller =
426       GNUNET_TESTBED_controller_connect (host, event_mask, &controller_cb,
427                                          NULL);
428   FAIL_TEST (NULL != controller, return);
429   neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0);
430   FAIL_TEST (NULL != neighbour, return);
431   reg_handle =
432       GNUNET_TESTBED_register_host (controller, neighbour, &registration_comp,
433                                     neighbour);
434   FAIL_TEST (NULL != reg_handle, return);
435 }
436
437
438
439 /**
440  * Main run function.
441  *
442  * @param cls NULL
443  * @param args arguments passed to GNUNET_PROGRAM_run
444  * @param cfgfile the path to configuration file
445  * @param cfg the configuration file handle
446  */
447 static void
448 run (void *cls, char *const *args, const char *cfgfile,
449      const struct GNUNET_CONFIGURATION_Handle *config)
450 {
451   cfg = GNUNET_CONFIGURATION_dup (config);
452   host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
453   FAIL_TEST (NULL != host, return);
454   cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb,
455                                         NULL);
456   abort_task =
457       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
458                                     (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
459                                     NULL);
460 }
461
462
463 /**
464  * Main function
465  */
466 int
467 main (int argc, char **argv)
468 {
469   int ret;
470
471   char *const argv2[] = { "test_testbed_api",
472     "-c", "test_testbed_api.conf",
473     NULL
474   };
475   struct GNUNET_GETOPT_CommandLineOption options[] = {
476     GNUNET_GETOPT_OPTION_END
477   };
478
479   result = GNUNET_SYSERR;
480   ret =
481       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
482                           "test_testbed_api", "nohelp", options, &run, NULL);
483   if ((GNUNET_OK != ret) || (GNUNET_OK != result))
484     return 1;
485   return 0;
486 }
487
488 /* end of test_testbed_api.c */