error reporting in service_connect
[oweals/gnunet.git] / src / testbed / test_testbed_api_2peers.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_2peers.c
23  * @brief testcases for the testbed api: 2 peers are configured, started and
24  *          connected together
25  * @author Sree Harsha Totakura
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testing_lib-new.h"
31 #include "gnunet_testbed_service.h"
32
33
34 /**
35  * Generic logging shortcut
36  */
37 #define LOG(kind,...)                           \
38   GNUNET_log (kind, __VA_ARGS__)
39
40 /**
41  * Relative time seconds shorthand
42  */
43 #define TIME_REL_SECS(sec) \
44   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
45
46
47 /**
48  * Peer context
49  */
50 struct PeerContext
51 {
52   /**
53    * The peer handle
54    */
55   struct GNUNET_TESTBED_Peer *peer;
56
57   /**
58    * Operations involving this peer
59    */
60   struct GNUNET_TESTBED_Operation *operation;
61
62   /**
63    * set to GNUNET_YES when peer is started
64    */
65   int is_running;
66 };
67
68 /**
69  * Our localhost
70  */
71 static struct GNUNET_TESTBED_Host *host;
72
73 /**
74  * The controller process
75  */
76 static struct GNUNET_TESTBED_ControllerProc *cp;
77
78 /**
79  * The controller handle
80  */
81 static struct GNUNET_TESTBED_Controller *controller;
82
83 /**
84  * A neighbouring host
85  */
86 static struct GNUNET_TESTBED_Host *neighbour;
87
88 /**
89  * Handle for neighbour registration
90  */
91 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
92
93 /**
94  * peer 1
95  */
96 static struct PeerContext peer1;
97
98 /**
99  * peer2
100  */
101 static struct PeerContext peer2;
102
103 /**
104  * Handle to configuration
105  */
106 static struct GNUNET_CONFIGURATION_Handle *cfg;
107
108 /**
109  * Handle to operations involving both peers
110  */
111 static struct GNUNET_TESTBED_Operation *common_operation;
112
113 /**
114  * Abort task identifier
115  */
116 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
117
118 /**
119  * Different stages in testing
120  */
121 enum Stage
122 {
123
124     /**
125      * Initial stage
126      */
127   INIT,
128
129     /**
130      * peers are created
131      */
132   PEERS_CREATED,
133
134     /**
135      * peers are started
136      */
137   PEERS_STARTED,
138
139     /**
140      * peers are connected
141      */
142   PEERS_CONNECTED,
143
144     /**
145      * peers are stopped
146      */
147   PEERS_STOPPED,
148
149     /**
150      * Final success stage
151      */
152   SUCCESS
153 };
154
155 /**
156  * The testing result
157  */
158 static enum Stage result;
159
160
161 /**
162  * Shutdown nicely
163  *
164  * @param cls NULL
165  * @param tc the task context
166  */
167 static void
168 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
169 {
170   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
171     GNUNET_SCHEDULER_cancel (abort_task);
172   if (NULL != reg_handle)
173     GNUNET_TESTBED_cancel_registration (reg_handle);
174   GNUNET_TESTBED_controller_disconnect (controller);
175   GNUNET_CONFIGURATION_destroy (cfg);
176   if (NULL != cp)
177     GNUNET_TESTBED_controller_stop (cp);
178   GNUNET_TESTBED_host_destroy (neighbour);
179   GNUNET_TESTBED_host_destroy (host);
180 }
181
182
183 /**
184  * abort task to run on test timed out
185  *
186  * @param cls NULL
187  * @param tc the task context
188  */
189 static void
190 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
191 {
192   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
193   abort_task = GNUNET_SCHEDULER_NO_TASK;
194   do_shutdown (cls, tc);
195 }
196
197
198 /**
199  * Callback to be called when an operation is completed
200  *
201  * @param cls the callback closure from functions generating an operation
202  * @param op the operation that has been finished
203  * @param emsg error message in case the operation has failed; will be NULL if
204  *          operation has executed successfully.
205  */
206 static void 
207 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
208 {
209   GNUNET_assert (PEERS_STARTED == result);
210   GNUNET_assert (NULL == peer1.operation);
211   GNUNET_assert (NULL == peer2.operation);
212   GNUNET_assert (NULL != common_operation);
213   GNUNET_TESTBED_operation_done (common_operation);
214   common_operation = NULL;
215   result = PEERS_CONNECTED;
216   LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
217   peer1.operation = GNUNET_TESTBED_peer_stop (peer1.peer, NULL, NULL);
218   peer2.operation = GNUNET_TESTBED_peer_stop (peer2.peer, NULL, NULL);
219 }
220
221
222 /**
223  * Signature of the event handler function called by the
224  * respective event controller.
225  *
226  * @param cls closure
227  * @param event information about the event
228  */
229 static void
230 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
231 {
232   switch (event->type)
233   {
234   case GNUNET_TESTBED_ET_OPERATION_FINISHED:   /* Will be reached when we destroy peers */
235     GNUNET_assert (PEERS_STOPPED == result);
236     GNUNET_assert (NULL == event->details.operation_finished.op_cls);
237     GNUNET_assert (NULL == event->details.operation_finished.emsg);
238     GNUNET_assert (NULL == event->details.operation_finished.generic);
239     if (event->details.operation_finished.operation == peer1.operation)
240     {
241       GNUNET_TESTBED_operation_done (peer1.operation);
242       peer1.operation = NULL;
243       peer1.peer = NULL;
244     }
245     else if (event->details.operation_finished.operation == peer2.operation)
246     {
247       GNUNET_TESTBED_operation_done (peer2.operation);
248       peer2.operation = NULL;
249       peer2.peer = NULL;
250     }
251     else
252       GNUNET_assert (0);
253     if ((NULL == peer1.peer) && (NULL == peer2.peer))
254     {
255       result = SUCCESS;
256       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
257     }
258     break;
259   case GNUNET_TESTBED_ET_PEER_START:
260     GNUNET_assert (INIT == result);
261     GNUNET_assert (event->details.peer_start.host == host);
262     if (event->details.peer_start.peer == peer1.peer)
263     {
264       peer1.is_running = GNUNET_YES;
265       GNUNET_TESTBED_operation_done (peer1.operation);
266       peer1.operation = NULL;
267     }
268     else if (event->details.peer_start.peer == peer2.peer)
269     {
270       peer2.is_running = GNUNET_YES;
271       GNUNET_TESTBED_operation_done (peer2.operation);
272       peer2.operation = NULL;
273     }
274     else
275       GNUNET_assert (0);
276     if ((GNUNET_YES == peer1.is_running) && (GNUNET_YES == peer2.is_running))
277     {
278       result = PEERS_STARTED;
279       common_operation =
280           GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
281                                           peer2.peer);
282     }
283     break;
284   case GNUNET_TESTBED_ET_PEER_STOP:
285     GNUNET_assert (PEERS_CONNECTED == result);
286     if (event->details.peer_stop.peer == peer1.peer)
287     {
288       peer1.is_running = GNUNET_NO;
289       GNUNET_TESTBED_operation_done (peer1.operation);
290       peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
291     }
292     else if (event->details.peer_stop.peer == peer2.peer)
293     {
294       peer2.is_running = GNUNET_NO;
295       GNUNET_TESTBED_operation_done (peer2.operation);
296       peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
297     }
298     else
299       GNUNET_assert (0);
300     if ((GNUNET_NO == peer1.is_running) && (GNUNET_NO == peer2.is_running))
301       result = PEERS_STOPPED;
302     break;
303   case GNUNET_TESTBED_ET_CONNECT:
304     GNUNET_assert (PEERS_STARTED == result);
305     GNUNET_assert (NULL == peer1.operation);
306     GNUNET_assert (NULL == peer2.operation);
307     GNUNET_assert (NULL != common_operation);
308     GNUNET_assert ((event->details.peer_connect.peer1 == peer1.peer) &&
309                    (event->details.peer_connect.peer2 == peer2.peer));
310     break;
311   default:
312     GNUNET_assert (0);
313   };
314 }
315
316
317 /**
318  * Functions of this signature are called when a peer has been successfully
319  * created
320  *
321  * @param cls the closure from GNUNET_TESTBED_peer_create()
322  * @param peer the handle for the created peer; NULL on any error during
323  *          creation
324  * @param emsg NULL if peer is not NULL; else MAY contain the error description
325  */
326 static void
327 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
328 {
329   struct PeerContext *pc = cls;
330
331   GNUNET_assert (NULL != pc->operation);
332   GNUNET_assert (NULL != peer);
333   GNUNET_assert (NULL == pc->peer);
334   pc->peer = peer;
335   GNUNET_TESTBED_operation_done (pc->operation);
336   pc->operation = GNUNET_TESTBED_peer_start (pc->peer, NULL, NULL);
337 }
338
339
340 /**
341  * Callback which will be called to after a host registration succeeded or failed
342  *
343  * @param cls the host which has been registered
344  * @param emsg the error message; NULL if host registration is successful
345  */
346 static void
347 registration_comp (void *cls, const char *emsg)
348 {
349   GNUNET_assert (cls == neighbour);
350   reg_handle = NULL;
351   peer1.operation =
352       GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
353                                   &peer1);
354   peer2.operation =
355       GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
356                                   &peer2);
357   GNUNET_assert (NULL != peer1.operation);
358   GNUNET_assert (NULL != peer2.operation);
359 }
360
361
362 /**
363  * Callback to signal successfull startup of the controller process
364  *
365  * @param cls the closure from GNUNET_TESTBED_controller_start()
366  * @param cfg the configuration with which the controller has been started;
367  *          NULL if status is not GNUNET_OK
368  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
369  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
370  */
371 static void
372 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, int status)
373 {
374   uint64_t event_mask;
375
376   GNUNET_assert (GNUNET_OK == status);
377   event_mask = 0;
378   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
379   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
380   event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
381   event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
382   controller =
383       GNUNET_TESTBED_controller_connect (cfg, host, event_mask, &controller_cb,
384                                          NULL);
385   GNUNET_assert (NULL != controller);
386   neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
387   GNUNET_assert (NULL != neighbour);
388   reg_handle =
389       GNUNET_TESTBED_register_host (controller, neighbour, &registration_comp,
390                                     neighbour);
391   GNUNET_assert (NULL != reg_handle);
392 }
393
394
395
396 /**
397  * Main run function.
398  *
399  * @param cls NULL
400  * @param args arguments passed to GNUNET_PROGRAM_run
401  * @param cfgfile the path to configuration file
402  * @param cfg the configuration file handle
403  */
404 static void
405 run (void *cls, char *const *args, const char *cfgfile,
406      const struct GNUNET_CONFIGURATION_Handle *config)
407 {
408   host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
409   GNUNET_assert (NULL != host);
410   cfg = GNUNET_CONFIGURATION_dup (config);
411   cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
412                                         NULL);
413   abort_task =
414       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
415                                     (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
416                                     NULL);
417 }
418
419
420 /**
421  * Main function
422  */
423 int
424 main (int argc, char **argv)
425 {
426   int ret;
427
428   char *const argv2[] = { "test_testbed_api_2peers",
429     "-c", "test_testbed_api.conf",
430     NULL
431   };
432   struct GNUNET_GETOPT_CommandLineOption options[] = {
433     GNUNET_GETOPT_OPTION_END
434   };
435   result = INIT;
436   ret =
437       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
438                           "test_testbed_api_2peers", "nohelp", options, &run,
439                           NULL);
440   if ((GNUNET_OK != ret) || (SUCCESS != result))
441     return 1;
442   return 0;
443 }