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