aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/test_testbed_api_2peers_1controller.c
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2012-09-16 12:33:41 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2012-09-16 12:33:41 +0000
commit92bc91f21ea5f201526775cd67df755eb50a0a78 (patch)
tree7a7f654e04b91316b43f9e28a48fcc4f52d470e4 /src/testbed/test_testbed_api_2peers_1controller.c
parent985c18baa426b7383faaba20651d793553f9a0cb (diff)
downloadgnunet-92bc91f21ea5f201526775cd67df755eb50a0a78.tar.gz
gnunet-92bc91f21ea5f201526775cd67df755eb50a0a78.zip
rename
Diffstat (limited to 'src/testbed/test_testbed_api_2peers_1controller.c')
-rw-r--r--src/testbed/test_testbed_api_2peers_1controller.c511
1 files changed, 511 insertions, 0 deletions
diff --git a/src/testbed/test_testbed_api_2peers_1controller.c b/src/testbed/test_testbed_api_2peers_1controller.c
new file mode 100644
index 000000000..0e1f83e98
--- /dev/null
+++ b/src/testbed/test_testbed_api_2peers_1controller.c
@@ -0,0 +1,511 @@
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_1controller.c
23 * @brief testcases for the testbed api: 2 peers are configured, started and
24 * connected together. The 2 peer reside on a single controller.
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 */
50struct 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 */
71static struct GNUNET_TESTBED_Host *host;
72
73/**
74 * The controller process
75 */
76static struct GNUNET_TESTBED_ControllerProc *cp;
77
78/**
79 * The controller handle
80 */
81static struct GNUNET_TESTBED_Controller *controller;
82
83/**
84 * A neighbouring host
85 */
86static struct GNUNET_TESTBED_Host *neighbour;
87
88/**
89 * Handle for neighbour registration
90 */
91static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
92
93/**
94 * peer 1
95 */
96static struct PeerContext peer1;
97
98/**
99 * peer2
100 */
101static struct PeerContext peer2;
102
103/**
104 * Handle to configuration
105 */
106static struct GNUNET_CONFIGURATION_Handle *cfg;
107
108/**
109 * Handle to operations involving both peers
110 */
111static struct GNUNET_TESTBED_Operation *common_operation;
112
113/**
114 * Abort task identifier
115 */
116static GNUNET_SCHEDULER_TaskIdentifier abort_task;
117
118/**
119 * Delayed connect job identifier
120 */
121static GNUNET_SCHEDULER_TaskIdentifier delayed_connect_task;
122
123/**
124 * Different stages in testing
125 */
126enum Stage
127{
128
129 /**
130 * Initial stage
131 */
132 INIT,
133
134 /**
135 * peers are created
136 */
137 PEERS_CREATED,
138
139 /**
140 * peers are started
141 */
142 PEERS_STARTED,
143
144 /**
145 * peers are connected
146 */
147 PEERS_CONNECTED,
148
149 /**
150 * Peers are connected once again (this should not fail as they are already connected)
151 */
152 PEERS_CONNECTED_2,
153
154 /**
155 * peers are stopped
156 */
157 PEERS_STOPPED,
158
159 /**
160 * Final success stage
161 */
162 SUCCESS
163};
164
165/**
166 * The testing result
167 */
168static enum Stage result;
169
170
171/**
172 * Shutdown nicely
173 *
174 * @param cls NULL
175 * @param tc the task context
176 */
177static void
178do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
179{
180 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
181 GNUNET_SCHEDULER_cancel (abort_task);
182 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
183 if (NULL != reg_handle)
184 GNUNET_TESTBED_cancel_registration (reg_handle);
185 GNUNET_TESTBED_controller_disconnect (controller);
186 GNUNET_CONFIGURATION_destroy (cfg);
187 if (NULL != cp)
188 GNUNET_TESTBED_controller_stop (cp);
189 GNUNET_TESTBED_host_destroy (neighbour);
190 GNUNET_TESTBED_host_destroy (host);
191}
192
193
194/**
195 * abort task to run on test timed out
196 *
197 * @param cls NULL
198 * @param tc the task context
199 */
200static void
201do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
202{
203 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
204 abort_task = GNUNET_SCHEDULER_NO_TASK;
205 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
206 do_shutdown (cls, tc);
207}
208
209
210/**
211 * Callback to be called when an operation is completed
212 *
213 * @param cls the callback closure from functions generating an operation
214 * @param op the operation that has been finished
215 * @param emsg error message in case the operation has failed; will be NULL if
216 * operation has executed successfully.
217 */
218static void
219op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
220
221
222/**
223 * task for delaying a connect
224 *
225 * @param cls NULL
226 * @param tc the task context
227 */
228static void
229do_delayed_connect (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
230{
231 delayed_connect_task = GNUNET_SCHEDULER_NO_TASK;
232 GNUNET_assert (NULL == common_operation);
233 common_operation = GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL,
234 peer1.peer, peer2.peer);
235}
236
237
238/**
239 * Callback to be called when an operation is completed
240 *
241 * @param cls the callback closure from functions generating an operation
242 * @param op the operation that has been finished
243 * @param emsg error message in case the operation has failed; will be NULL if
244 * operation has executed successfully.
245 */
246static void
247op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
248{
249 GNUNET_assert (common_operation == op);
250 switch(result)
251 {
252 case PEERS_STARTED:
253 GNUNET_assert (NULL == peer1.operation);
254 GNUNET_assert (NULL == peer2.operation);
255 GNUNET_assert (NULL != common_operation);
256 GNUNET_TESTBED_operation_done (common_operation);
257 common_operation = NULL;
258 result = PEERS_CONNECTED;
259 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
260 delayed_connect_task =
261 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3),
262 &do_delayed_connect, NULL);
263 break;
264 case PEERS_CONNECTED:
265 GNUNET_assert (NULL == peer1.operation);
266 GNUNET_assert (NULL == peer2.operation);
267 GNUNET_assert (NULL != common_operation);
268 GNUNET_TESTBED_operation_done (common_operation);
269 common_operation = NULL;
270 result = PEERS_CONNECTED_2;
271 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
272 peer1.operation = GNUNET_TESTBED_peer_stop (peer1.peer, NULL, NULL);
273 peer2.operation = GNUNET_TESTBED_peer_stop (peer2.peer, NULL, NULL);
274 break;
275 default:
276 GNUNET_assert (0);
277 }
278}
279
280
281/**
282 * Signature of the event handler function called by the
283 * respective event controller.
284 *
285 * @param cls closure
286 * @param event information about the event
287 */
288static void
289controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
290{
291 switch (event->type)
292 {
293 case GNUNET_TESTBED_ET_OPERATION_FINISHED: /* Will be reached when we destroy peers */
294 GNUNET_assert (PEERS_STOPPED == result);
295 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
296 GNUNET_assert (NULL == event->details.operation_finished.emsg);
297 GNUNET_assert (NULL == event->details.operation_finished.generic);
298 if (event->details.operation_finished.operation == peer1.operation)
299 {
300 GNUNET_TESTBED_operation_done (peer1.operation);
301 peer1.operation = NULL;
302 peer1.peer = NULL;
303 }
304 else if (event->details.operation_finished.operation == peer2.operation)
305 {
306 GNUNET_TESTBED_operation_done (peer2.operation);
307 peer2.operation = NULL;
308 peer2.peer = NULL;
309 }
310 else
311 GNUNET_assert (0);
312 if ((NULL == peer1.peer) && (NULL == peer2.peer))
313 {
314 result = SUCCESS;
315 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
316 }
317 break;
318 case GNUNET_TESTBED_ET_PEER_START:
319 GNUNET_assert (INIT == result);
320 GNUNET_assert (event->details.peer_start.host == host);
321 if (event->details.peer_start.peer == peer1.peer)
322 {
323 peer1.is_running = GNUNET_YES;
324 GNUNET_TESTBED_operation_done (peer1.operation);
325 peer1.operation = NULL;
326 }
327 else if (event->details.peer_start.peer == peer2.peer)
328 {
329 peer2.is_running = GNUNET_YES;
330 GNUNET_TESTBED_operation_done (peer2.operation);
331 peer2.operation = NULL;
332 }
333 else
334 GNUNET_assert (0);
335 if ((GNUNET_YES == peer1.is_running) && (GNUNET_YES == peer2.is_running))
336 {
337 result = PEERS_STARTED;
338 common_operation =
339 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
340 peer2.peer);
341 }
342 break;
343 case GNUNET_TESTBED_ET_PEER_STOP:
344 GNUNET_assert (PEERS_CONNECTED_2 == result);
345 if (event->details.peer_stop.peer == peer1.peer)
346 {
347 peer1.is_running = GNUNET_NO;
348 GNUNET_TESTBED_operation_done (peer1.operation);
349 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
350 }
351 else if (event->details.peer_stop.peer == peer2.peer)
352 {
353 peer2.is_running = GNUNET_NO;
354 GNUNET_TESTBED_operation_done (peer2.operation);
355 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
356 }
357 else
358 GNUNET_assert (0);
359 if ((GNUNET_NO == peer1.is_running) && (GNUNET_NO == peer2.is_running))
360 result = PEERS_STOPPED;
361 break;
362 case GNUNET_TESTBED_ET_CONNECT:
363 switch (result)
364 {
365 case PEERS_STARTED:
366 case PEERS_CONNECTED:
367 GNUNET_assert (NULL == peer1.operation);
368 GNUNET_assert (NULL == peer2.operation);
369 GNUNET_assert (NULL != common_operation);
370 GNUNET_assert ((event->details.peer_connect.peer1 == peer1.peer) &&
371 (event->details.peer_connect.peer2 == peer2.peer));
372 break;
373 default:
374 GNUNET_assert (0);
375 }
376 break;
377 default:
378 GNUNET_assert (0);
379 };
380}
381
382
383/**
384 * Functions of this signature are called when a peer has been successfully
385 * created
386 *
387 * @param cls the closure from GNUNET_TESTBED_peer_create()
388 * @param peer the handle for the created peer; NULL on any error during
389 * creation
390 * @param emsg NULL if peer is not NULL; else MAY contain the error description
391 */
392static void
393peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
394{
395 struct PeerContext *pc = cls;
396
397 GNUNET_assert (NULL != pc->operation);
398 GNUNET_assert (NULL != peer);
399 GNUNET_assert (NULL == pc->peer);
400 pc->peer = peer;
401 GNUNET_TESTBED_operation_done (pc->operation);
402 pc->operation = GNUNET_TESTBED_peer_start (pc->peer, NULL, NULL);
403}
404
405
406/**
407 * Callback which will be called to after a host registration succeeded or failed
408 *
409 * @param cls the host which has been registered
410 * @param emsg the error message; NULL if host registration is successful
411 */
412static void
413registration_comp (void *cls, const char *emsg)
414{
415 GNUNET_assert (cls == neighbour);
416 reg_handle = NULL;
417 peer1.operation =
418 GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
419 &peer1);
420 peer2.operation =
421 GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
422 &peer2);
423 GNUNET_assert (NULL != peer1.operation);
424 GNUNET_assert (NULL != peer2.operation);
425}
426
427
428/**
429 * Callback to signal successfull startup of the controller process
430 *
431 * @param cls the closure from GNUNET_TESTBED_controller_start()
432 * @param cfg the configuration with which the controller has been started;
433 * NULL if status is not GNUNET_OK
434 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
435 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
436 */
437static void
438status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, int status)
439{
440 uint64_t event_mask;
441
442 GNUNET_assert (GNUNET_OK == status);
443 event_mask = 0;
444 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
445 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
446 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
447 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
448 controller =
449 GNUNET_TESTBED_controller_connect (cfg, host, event_mask, &controller_cb,
450 NULL);
451 GNUNET_assert (NULL != controller);
452 neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
453 GNUNET_assert (NULL != neighbour);
454 reg_handle =
455 GNUNET_TESTBED_register_host (controller, neighbour, &registration_comp,
456 neighbour);
457 GNUNET_assert (NULL != reg_handle);
458}
459
460
461
462/**
463 * Main run function.
464 *
465 * @param cls NULL
466 * @param args arguments passed to GNUNET_PROGRAM_run
467 * @param cfgfile the path to configuration file
468 * @param cfg the configuration file handle
469 */
470static void
471run (void *cls, char *const *args, const char *cfgfile,
472 const struct GNUNET_CONFIGURATION_Handle *config)
473{
474 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
475 GNUNET_assert (NULL != host);
476 cfg = GNUNET_CONFIGURATION_dup (config);
477 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
478 NULL);
479 abort_task =
480 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
481 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
482 NULL);
483}
484
485
486/**
487 * Main function
488 */
489int
490main (int argc, char **argv)
491{
492 int ret;
493
494 char *const argv2[] = { "test_testbed_api_2peers_1controller",
495 "-c", "test_testbed_api.conf",
496 NULL
497 };
498 struct GNUNET_GETOPT_CommandLineOption options[] = {
499 GNUNET_GETOPT_OPTION_END
500 };
501 result = INIT;
502 ret =
503 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
504 "test_testbed_api_2peers_1controller", "nohelp",
505 options, &run, NULL);
506 if ((GNUNET_OK != ret) || (SUCCESS != result))
507 return 1;
508 return 0;
509}
510
511/* end of test_testbed_api_2peers_1controller.c */