-next test
[oweals/gnunet.git] / src / testbed / test_testbed_api.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 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-new.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 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   GNUNET_TESTBED_controller_disconnect (controller);
147   GNUNET_CONFIGURATION_destroy (cfg);
148   if (NULL != cp)
149     GNUNET_TESTBED_controller_stop (cp);
150   GNUNET_TESTBED_host_destroy (neighbour);
151   GNUNET_TESTBED_host_destroy (host);
152 }
153
154
155 /**
156  * abort task to run on test timed out
157  *
158  * @param cls NULL
159  * @param tc the task context
160  */
161 static void
162 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
163 {
164   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
165   abort_task = GNUNET_SCHEDULER_NO_TASK;
166   do_shutdown (cls, tc);
167 }
168
169
170 /**
171  * Adapter function called to establish a connection to
172  * a service.
173  *
174  * @param cls closure
175  * @param cfg configuration of the peer to connect to; will be available until
176  *          GNUNET_TESTBED_operation_done() is called on the operation returned
177  *          from GNUNET_TESTBED_service_connect()
178  * @return service handle to return in 'op_result', NULL on error
179  */
180 static void *
181 dht_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
182 {
183   GNUNET_assert (NULL == cls);
184   GNUNET_assert (OTHER == sub_test);
185   sub_test = PEER_SERVICE_CONNECT;
186   dht_handle = GNUNET_DHT_connect (cfg, 10);
187   return dht_handle;
188 }
189
190
191 /**
192  * Adapter function called to destroy a connection to
193  * a service.
194  *
195  * @param cls closure
196  * @param op_result service handle returned from the connect adapter
197  */
198 static void
199 dht_disconnect_adapter (void *cls, void *op_result)
200 {
201   if (NULL != op_result)
202     GNUNET_DHT_disconnect (op_result);
203   GNUNET_assert (PEER_SERVICE_CONNECT == sub_test);
204   GNUNET_assert (NULL != operation);
205   operation = GNUNET_TESTBED_peer_stop (peer);
206   GNUNET_assert (NULL != operation);
207 }
208
209
210
211 /**
212  * Signature of the event handler function called by the
213  * respective event controller.
214  *
215  * @param cls closure
216  * @param event information about the event
217  */
218 static void
219 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
220 {
221   switch (event->type)
222   {
223   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
224     switch (sub_test)
225     {
226     case PEER_GETCONFIG:
227       GNUNET_assert (event->details.operation_finished.operation == operation);
228       GNUNET_assert (NULL == event->details.operation_finished.op_cls);
229       GNUNET_assert (NULL == event->details.operation_finished.emsg);
230       GNUNET_assert (GNUNET_TESTBED_PIT_CONFIGURATION ==
231                      event->details.operation_finished.pit);
232       GNUNET_assert (NULL != event->details.operation_finished.op_result.cfg);
233       sub_test = PEER_DESTROY;
234       GNUNET_TESTBED_operation_done (operation);
235       operation = GNUNET_TESTBED_peer_destroy (peer);
236       break;
237     case PEER_DESTROY:
238       GNUNET_assert (event->details.operation_finished.operation == operation);
239       GNUNET_assert (NULL == event->details.operation_finished.op_cls);
240       GNUNET_assert (NULL == event->details.operation_finished.emsg);
241       GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
242                      event->details.operation_finished.pit);
243       GNUNET_assert (NULL ==
244                      event->details.operation_finished.op_result.generic);
245       GNUNET_TESTBED_operation_done (operation);
246       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
247       break;
248     case PEER_SERVICE_CONNECT:
249       GNUNET_assert (event->details.operation_finished.operation == operation);
250       GNUNET_assert (NULL == event->details.operation_finished.op_cls);
251       GNUNET_assert (NULL == event->details.operation_finished.emsg);
252       GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
253                      event->details.operation_finished.pit);
254       GNUNET_assert (NULL != dht_handle);
255       GNUNET_assert (event->details.operation_finished.op_result.generic ==
256                      dht_handle);
257       GNUNET_TESTBED_operation_done (operation);        /* This results in call to
258                                                          * disconnect adapter */
259       break;
260     case OTHER:
261       GNUNET_assert (0);
262       break;
263     }
264     break;
265   case GNUNET_TESTBED_ET_PEER_START:
266     GNUNET_assert (event->details.peer_start.host == host);
267     GNUNET_assert (event->details.peer_start.peer == peer);
268     GNUNET_assert (OTHER == sub_test);
269     GNUNET_TESTBED_operation_done (operation);
270     operation =
271         GNUNET_TESTBED_service_connect (NULL, peer, "dht", &dht_connect_adapter,
272                                         &dht_disconnect_adapter, NULL);
273     GNUNET_assert (NULL != operation);
274     break;
275   case GNUNET_TESTBED_ET_PEER_STOP:
276     GNUNET_assert (event->details.peer_stop.peer == peer);
277     GNUNET_assert (PEER_SERVICE_CONNECT == sub_test);
278     result = GNUNET_YES;
279     sub_test = PEER_GETCONFIG;
280     GNUNET_TESTBED_operation_done (operation);
281     operation =
282         GNUNET_TESTBED_peer_get_information (peer,
283                                              GNUNET_TESTBED_PIT_CONFIGURATION);
284     break;
285   default:
286     GNUNET_assert (0);          /* We should never reach this state */
287   }
288 }
289
290
291 /**
292  * Functions of this signature are called when a peer has been successfully
293  * created
294  *
295  * @param cls the closure from GNUNET_TESTBED_peer_create()
296  * @param peer the handle for the created peer; NULL on any error during
297  *          creation
298  * @param emsg NULL if peer is not NULL; else MAY contain the error description
299  */
300 static void
301 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
302 {
303   struct GNUNET_TESTBED_Peer **peer_ptr;
304
305   peer_ptr = cls;
306   GNUNET_assert (NULL != peer);
307   GNUNET_assert (NULL != peer_ptr);
308   *peer_ptr = peer;
309   GNUNET_TESTBED_operation_done (operation);
310   operation = GNUNET_TESTBED_peer_start (peer);
311   GNUNET_assert (NULL != operation);
312 }
313
314
315 /**
316  * Callback which will be called to after a host registration succeeded or failed
317  *
318  * @param cls the host which has been registered
319  * @param emsg the error message; NULL if host registration is successful
320  */
321 static void
322 registration_comp (void *cls, const char *emsg)
323 {
324   GNUNET_assert (cls == neighbour);
325   reg_handle = NULL;
326   operation =
327       GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
328                                   &peer);
329   GNUNET_assert (NULL != operation);
330 }
331
332
333 /**
334  * Callback to signal successfull startup of the controller process
335  *
336  * @param cls the closure from GNUNET_TESTBED_controller_start()
337  * @param cfg the configuration with which the controller has been started;
338  *          NULL if status is not GNUNET_OK
339  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
340  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
341  */
342 static void
343 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, int status)
344 {
345   uint64_t event_mask;
346
347   GNUNET_assert (GNUNET_OK == status);
348   event_mask = 0;
349   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
350   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
351   event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
352   event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
353   controller =
354       GNUNET_TESTBED_controller_connect (cfg, host, event_mask, &controller_cb,
355                                          NULL);
356   GNUNET_assert (NULL != controller);
357   neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
358   GNUNET_assert (NULL != neighbour);
359   reg_handle =
360       GNUNET_TESTBED_register_host (controller, neighbour, &registration_comp,
361                                     neighbour);
362   GNUNET_assert (NULL != reg_handle);
363 }
364
365
366
367 /**
368  * Main run function.
369  *
370  * @param cls NULL
371  * @param args arguments passed to GNUNET_PROGRAM_run
372  * @param cfgfile the path to configuration file
373  * @param cfg the configuration file handle
374  */
375 static void
376 run (void *cls, char *const *args, const char *cfgfile,
377      const struct GNUNET_CONFIGURATION_Handle *config)
378 {
379   host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
380   GNUNET_assert (NULL != host);
381   cfg = GNUNET_CONFIGURATION_dup (config);
382   cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
383                                         NULL);
384   abort_task =
385       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
386                                     (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
387                                     NULL);
388 }
389
390
391 /**
392  * Main function
393  */
394 int
395 main (int argc, char **argv)
396 {
397   int ret;
398
399   char *const argv2[] = { "test_testbed_api",
400     "-c", "test_testbed_api.conf",
401     NULL
402   };
403   struct GNUNET_GETOPT_CommandLineOption options[] = {
404     GNUNET_GETOPT_OPTION_END
405   };
406
407   result = GNUNET_SYSERR;
408   ret =
409       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
410                           "test_testbed_api", "nohelp", options, &run, NULL);
411   if ((GNUNET_OK != ret) || (GNUNET_OK != result))
412     return 1;
413   return 0;
414 }