3bb45f73085679712fc0b0c2a5fa7adb9355efea
[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  * Shutdown nicely
98  *
99  * @param cls NULL
100  * @param tc the task context
101  */
102 static void
103 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
104 {
105   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
106     GNUNET_SCHEDULER_cancel (abort_task);
107   if (NULL != reg_handle)
108     GNUNET_TESTBED_cancel_registration (reg_handle);
109   GNUNET_TESTBED_controller_disconnect (controller);
110   GNUNET_CONFIGURATION_destroy (cfg);
111   if (NULL != cp)
112     GNUNET_TESTBED_controller_stop (cp);
113   GNUNET_TESTBED_host_destroy (neighbour);
114   GNUNET_TESTBED_host_destroy (host);
115 }
116
117
118 /**
119  * abort task to run on test timed out
120  *
121  * @param cls NULL
122  * @param tc the task context
123  */
124 static void
125 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
126 {
127   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
128   abort_task = GNUNET_SCHEDULER_NO_TASK;
129   do_shutdown (cls, tc);
130 }
131
132
133 /**
134  * Signature of the event handler function called by the
135  * respective event controller.
136  *
137  * @param cls closure
138  * @param event information about the event
139  */
140 static void 
141 controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
142 {
143   switch (event->type)
144   {
145   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
146     GNUNET_assert (event->details.operation_finished.operation == operation);
147     GNUNET_assert (NULL == event->details.operation_finished.op_cls);
148     GNUNET_assert (NULL == event->details.operation_finished.emsg);
149     GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
150                    event->details.operation_finished.pit);
151     GNUNET_assert (NULL == event->details.operation_finished.op_result.generic);
152     break;
153   case GNUNET_TESTBED_ET_PEER_START:
154     GNUNET_assert (event->details.peer_start.host == host);
155     GNUNET_assert (event->details.peer_start.peer == peer);
156     operation = GNUNET_TESTBED_peer_stop (peer);
157     break;
158   case GNUNET_TESTBED_ET_PEER_STOP:
159     GNUNET_assert (event->details.peer_stop.peer == peer);    
160     result = GNUNET_YES;  
161     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
162     break;
163   default:
164     GNUNET_assert (0);          /* We should never reach this state */
165   }
166 }
167
168
169 /**
170  * Functions of this signature are called when a peer has been successfully
171  * created
172  *
173  * @param cls the closure from GNUNET_TESTBED_peer_create()
174  * @param peer the handle for the created peer; NULL on any error during
175  *          creation
176  * @param emsg NULL if peer is not NULL; else MAY contain the error description
177  */
178 static void
179 peer_create_cb (void *cls,
180                 struct GNUNET_TESTBED_Peer *peer, const char *emsg)
181 {
182   struct GNUNET_TESTBED_Peer **peer_ptr;
183   
184   peer_ptr = cls;
185   GNUNET_assert (NULL != peer);
186   GNUNET_assert (NULL != peer_ptr);
187   *peer_ptr = peer;
188   operation = GNUNET_TESTBED_peer_start (peer);
189   GNUNET_assert (NULL != operation);
190 }
191
192
193 /**
194  * Callback which will be called to after a host registration succeeded or failed
195  *
196  * @param cls the host which has been registered
197  * @param emsg the error message; NULL if host registration is successful
198  */
199 static void 
200 registration_comp (void *cls, const char *emsg)
201 {
202   GNUNET_assert (cls == neighbour);
203   reg_handle = NULL;  
204   operation = GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb, &peer);
205   GNUNET_assert (NULL != operation);
206 }
207
208
209 /**
210  * Callback to signal successfull startup of the controller process
211  *
212  * @param cls the closure from GNUNET_TESTBED_controller_start()
213  * @param cfg the configuration with which the controller has been started;
214  *          NULL if status is not GNUNET_OK
215  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
216  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
217  */
218 static void 
219 status_cb (void *cls, 
220            const struct GNUNET_CONFIGURATION_Handle *cfg, int status)
221 {
222   uint64_t event_mask;
223
224   GNUNET_assert (GNUNET_OK == status);
225   event_mask = 0;
226   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
227   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
228   event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
229   event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
230   controller = GNUNET_TESTBED_controller_connect (cfg, host, event_mask,
231                                                   &controller_cb, NULL);
232   GNUNET_assert (NULL != controller);
233   neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
234   GNUNET_assert (NULL != neighbour);
235   reg_handle = 
236     GNUNET_TESTBED_register_host (controller, neighbour, &registration_comp,
237                                   neighbour);
238   GNUNET_assert (NULL != reg_handle);
239 }
240
241
242
243 /**
244  * Main run function. 
245  *
246  * @param cls NULL
247  * @param args arguments passed to GNUNET_PROGRAM_run
248  * @param cfgfile the path to configuration file
249  * @param cfg the configuration file handle
250  */
251 static void
252 run (void *cls, char *const *args, const char *cfgfile,
253      const struct GNUNET_CONFIGURATION_Handle *config)
254 {
255   host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
256   GNUNET_assert (NULL != host);
257   cfg = GNUNET_CONFIGURATION_dup (config);
258   cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb, NULL);
259   abort_task = GNUNET_SCHEDULER_add_delayed 
260     (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort, NULL);
261 }
262
263
264 /**
265  * Main function
266  */
267 int main (int argc, char **argv)
268 {
269   int ret;
270
271   char *const argv2[] = { "test_testbed_api",
272                           "-c", "test_testbed_api.conf",
273                           NULL
274   };
275   struct GNUNET_GETOPT_CommandLineOption options[] = {
276     GNUNET_GETOPT_OPTION_END
277   };
278   result = GNUNET_SYSERR;
279   ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
280                             "test_testbed_api", "nohelp", options, &run,
281                             NULL);
282   if ((GNUNET_OK != ret) || (GNUNET_OK != result))
283     return 1;
284   return 0;
285 }