peer start/stop with new operations handling
[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_testing_lib-new.h"
30 #include "gnunet_testbed_service.h"
31
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  * Abort task identifier
87  */
88 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
89
90 /**
91  * The testing result
92  */
93 static int result;
94
95
96 /**
97  * Enumeration of sub testcases
98  */
99 enum Test
100   {
101     /**
102      * Test cases which are not covered by the below ones
103      */
104     OTHER,
105
106     /**
107      * Test where we get a peer config from controller
108      */
109     PEER_GETCONFIG,
110
111     /**
112      * Test where we get a peer's identity from controller
113      */
114     PEER_DESTROY,
115   };
116
117 /**
118  * Testing status
119  */
120 static enum Test sub_test;
121
122 /**
123  * Shutdown nicely
124  *
125  * @param cls NULL
126  * @param tc the task context
127  */
128 static void
129 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
130 {
131   LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down...\n");
132   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
133     GNUNET_SCHEDULER_cancel (abort_task);
134   if (NULL != reg_handle)
135     GNUNET_TESTBED_cancel_registration (reg_handle);
136   GNUNET_TESTBED_controller_disconnect (controller);
137   GNUNET_CONFIGURATION_destroy (cfg);
138   if (NULL != cp)
139     GNUNET_TESTBED_controller_stop (cp);
140   GNUNET_TESTBED_host_destroy (neighbour);
141   GNUNET_TESTBED_host_destroy (host);
142 }
143
144
145 /**
146  * abort task to run on test timed out
147  *
148  * @param cls NULL
149  * @param tc the task context
150  */
151 static void
152 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
153 {
154   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
155   abort_task = GNUNET_SCHEDULER_NO_TASK;
156   do_shutdown (cls, tc);
157 }
158
159
160 /**
161  * Signature of the event handler function called by the
162  * respective event controller.
163  *
164  * @param cls closure
165  * @param event information about the event
166  */
167 static void 
168 controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
169 {
170   switch (event->type)
171   {
172   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
173     switch(sub_test)
174     {
175     case PEER_GETCONFIG:
176       GNUNET_assert (event->details.operation_finished.operation == operation);
177       GNUNET_assert (NULL == event->details.operation_finished.op_cls);
178       GNUNET_assert (NULL == event->details.operation_finished.emsg);
179       GNUNET_assert (GNUNET_TESTBED_PIT_CONFIGURATION ==
180                      event->details.operation_finished.pit);
181       GNUNET_assert (NULL != event->details.operation_finished.op_result.cfg);
182       sub_test = PEER_DESTROY;
183       GNUNET_TESTBED_operation_done (operation);
184       operation = GNUNET_TESTBED_peer_destroy (peer);
185       break;
186     case PEER_DESTROY:
187       GNUNET_assert (event->details.operation_finished.operation == operation);
188       GNUNET_assert (NULL == event->details.operation_finished.op_cls);
189       GNUNET_assert (NULL == event->details.operation_finished.emsg);
190       GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
191                      event->details.operation_finished.pit);
192       GNUNET_assert (NULL ==
193                      event->details.operation_finished.op_result.generic); 
194       GNUNET_TESTBED_operation_done (operation);
195       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
196       break;
197     case OTHER:
198       GNUNET_assert (0);
199       break;
200     }    
201     break;
202   case GNUNET_TESTBED_ET_PEER_START:
203     GNUNET_assert (event->details.peer_start.host == host);
204     GNUNET_assert (event->details.peer_start.peer == peer);
205     GNUNET_TESTBED_operation_done (operation);
206     operation = GNUNET_TESTBED_peer_stop (peer);
207     break;
208   case GNUNET_TESTBED_ET_PEER_STOP:
209     GNUNET_assert (event->details.peer_stop.peer == peer);    
210     result = GNUNET_YES;
211     sub_test = PEER_GETCONFIG;
212     GNUNET_TESTBED_operation_done (operation);
213     operation = 
214       GNUNET_TESTBED_peer_get_information (peer,
215                                            GNUNET_TESTBED_PIT_CONFIGURATION);
216     break;
217   default:
218     GNUNET_assert (0);          /* We should never reach this state */
219   }
220 }
221
222
223 /**
224  * Functions of this signature are called when a peer has been successfully
225  * created
226  *
227  * @param cls the closure from GNUNET_TESTBED_peer_create()
228  * @param peer the handle for the created peer; NULL on any error during
229  *          creation
230  * @param emsg NULL if peer is not NULL; else MAY contain the error description
231  */
232 static void
233 peer_create_cb (void *cls,
234                 struct GNUNET_TESTBED_Peer *peer, const char *emsg)
235 {
236   struct GNUNET_TESTBED_Peer **peer_ptr;
237   
238   peer_ptr = cls;
239   GNUNET_assert (NULL != peer);
240   GNUNET_assert (NULL != peer_ptr);
241   *peer_ptr = peer;
242   GNUNET_TESTBED_operation_done (operation);
243   operation = GNUNET_TESTBED_peer_start (peer);
244   GNUNET_assert (NULL != operation);
245 }
246
247
248 /**
249  * Callback which will be called to after a host registration succeeded or failed
250  *
251  * @param cls the host which has been registered
252  * @param emsg the error message; NULL if host registration is successful
253  */
254 static void 
255 registration_comp (void *cls, const char *emsg)
256 {
257   GNUNET_assert (cls == neighbour);
258   reg_handle = NULL;  
259   operation = GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb, &peer);
260   GNUNET_assert (NULL != operation);
261 }
262
263
264 /**
265  * Callback to signal successfull startup of the controller process
266  *
267  * @param cls the closure from GNUNET_TESTBED_controller_start()
268  * @param cfg the configuration with which the controller has been started;
269  *          NULL if status is not GNUNET_OK
270  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
271  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
272  */
273 static void 
274 status_cb (void *cls, 
275            const struct GNUNET_CONFIGURATION_Handle *cfg, int status)
276 {
277   uint64_t event_mask;
278
279   GNUNET_assert (GNUNET_OK == status);
280   event_mask = 0;
281   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
282   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
283   event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
284   event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
285   controller = GNUNET_TESTBED_controller_connect (cfg, host, event_mask,
286                                                   &controller_cb, NULL);
287   GNUNET_assert (NULL != controller);
288   neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
289   GNUNET_assert (NULL != neighbour);
290   reg_handle = 
291     GNUNET_TESTBED_register_host (controller, neighbour, &registration_comp,
292                                   neighbour);
293   GNUNET_assert (NULL != reg_handle);
294 }
295
296
297
298 /**
299  * Main run function. 
300  *
301  * @param cls NULL
302  * @param args arguments passed to GNUNET_PROGRAM_run
303  * @param cfgfile the path to configuration file
304  * @param cfg the configuration file handle
305  */
306 static void
307 run (void *cls, char *const *args, const char *cfgfile,
308      const struct GNUNET_CONFIGURATION_Handle *config)
309 {
310   host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
311   GNUNET_assert (NULL != host);
312   cfg = GNUNET_CONFIGURATION_dup (config);
313   cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb, NULL);
314   abort_task = GNUNET_SCHEDULER_add_delayed 
315     (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort, NULL);
316 }
317
318
319 /**
320  * Main function
321  */
322 int main (int argc, char **argv)
323 {
324   int ret;
325   char *const argv2[] = { "test_testbed_api",
326                           "-c", "test_testbed_api.conf",
327                           NULL
328   };
329   struct GNUNET_GETOPT_CommandLineOption options[] = {
330     GNUNET_GETOPT_OPTION_END
331   };
332
333   result = GNUNET_SYSERR;
334   ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
335                             "test_testbed_api", "nohelp", options, &run,
336                             NULL);
337   if ((GNUNET_OK != ret) || (GNUNET_OK != result))
338     return 1;
339   return 0;
340 }