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