aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/cadet_test_lib.c
diff options
context:
space:
mode:
authorSchanzenbach, Martin <mschanzenbach@posteo.de>2017-03-29 14:26:33 +0200
committerSchanzenbach, Martin <mschanzenbach@posteo.de>2017-03-29 14:26:33 +0200
commitab281595eeb270120f89ec954a572f4fcf78fc53 (patch)
tree335a2caf503596adc400c5ebb9fb742f097bc5a3 /src/cadet/cadet_test_lib.c
parent59d393a1124cfd1aaffdf994bf6f8a9baaac8361 (diff)
parent2b87f173e360aaf4a3bac3fbc6e5b4dc44cf58cd (diff)
downloadgnunet-ab281595eeb270120f89ec954a572f4fcf78fc53.tar.gz
gnunet-ab281595eeb270120f89ec954a572f4fcf78fc53.zip
- merge with master
Diffstat (limited to 'src/cadet/cadet_test_lib.c')
-rw-r--r--src/cadet/cadet_test_lib.c133
1 files changed, 96 insertions, 37 deletions
diff --git a/src/cadet/cadet_test_lib.c b/src/cadet/cadet_test_lib.c
index 9a70dad49..1df6bff0d 100644
--- a/src/cadet/cadet_test_lib.c
+++ b/src/cadet/cadet_test_lib.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V. 3 Copyright (C) 2012, 2017 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -27,6 +27,7 @@
27#include "cadet_test_lib.h" 27#include "cadet_test_lib.h"
28#include "gnunet_cadet_service.h" 28#include "gnunet_cadet_service.h"
29 29
30
30/** 31/**
31 * Test context for a CADET Test. 32 * Test context for a CADET Test.
32 */ 33 */
@@ -40,7 +41,7 @@ struct GNUNET_CADET_TEST_Context
40 /** 41 /**
41 * Array of handles to the CADET for each peer. 42 * Array of handles to the CADET for each peer.
42 */ 43 */
43 struct GNUNET_CADET_Handle **cadetes; 44 struct GNUNET_CADET_Handle **cadets;
44 45
45 /** 46 /**
46 * Operation associated with the connection to the CADET. 47 * Operation associated with the connection to the CADET.
@@ -48,6 +49,11 @@ struct GNUNET_CADET_TEST_Context
48 struct GNUNET_TESTBED_Operation **ops; 49 struct GNUNET_TESTBED_Operation **ops;
49 50
50 /** 51 /**
52 * Number of peers running, size of the arrays above.
53 */
54 unsigned int num_peers;
55
56 /**
51 * Main function of the test to run once all CADETs are available. 57 * Main function of the test to run once all CADETs are available.
52 */ 58 */
53 GNUNET_CADET_TEST_AppMain app_main; 59 GNUNET_CADET_TEST_AppMain app_main;
@@ -58,30 +64,35 @@ struct GNUNET_CADET_TEST_Context
58 void *app_main_cls; 64 void *app_main_cls;
59 65
60 /** 66 /**
61 * Number of peers running, size of the arrays above. 67 * Handler for incoming tunnels.
62 */ 68 */
63 unsigned int num_peers; 69 GNUNET_CADET_ConnectEventHandler connects;
64 70
65 /** 71 /**
66 * Handler for incoming tunnels. 72 * Function called when the transmit window size changes.
67 */ 73 */
68 GNUNET_CADET_InboundChannelNotificationHandler *new_channel; 74 GNUNET_CADET_WindowSizeEventHandler window_changes;
69 75
70 /** 76 /**
71 * Cleaner for destroyed incoming tunnels. 77 * Cleaner for destroyed incoming tunnels.
72 */ 78 */
73 GNUNET_CADET_ChannelEndHandler *cleaner; 79 GNUNET_CADET_DisconnectEventHandler disconnects;
74 80
75 /** 81 /**
76 * Message handlers. 82 * Message handlers.
77 */ 83 */
78 struct GNUNET_CADET_MessageHandler* handlers; 84 struct GNUNET_MQ_MessageHandler *handlers;
79 85
80 /** 86 /**
81 * Application ports. 87 * Application ports.
82 */ 88 */
83 const struct GNUNET_HashCode **ports; 89 const struct GNUNET_HashCode **ports;
84 90
91 /**
92 * Number of ports in #ports.
93 */
94 unsigned int port_count;
95
85}; 96};
86 97
87 98
@@ -94,6 +105,11 @@ struct GNUNET_CADET_TEST_AdapterContext
94 * Peer number for the particular peer. 105 * Peer number for the particular peer.
95 */ 106 */
96 unsigned int peer; 107 unsigned int peer;
108
109 /**
110 * Port handlers for open ports.
111 */
112 struct GNUNET_CADET_Port **ports;
97 113
98 /** 114 /**
99 * General context. 115 * General context.
@@ -114,26 +130,28 @@ struct GNUNET_CADET_TEST_AdapterContext
114 */ 130 */
115static void * 131static void *
116cadet_connect_adapter (void *cls, 132cadet_connect_adapter (void *cls,
117 const struct GNUNET_CONFIGURATION_Handle *cfg) 133 const struct GNUNET_CONFIGURATION_Handle *cfg)
118{ 134{
119 struct GNUNET_CADET_TEST_AdapterContext *actx = cls; 135 struct GNUNET_CADET_TEST_AdapterContext *actx = cls;
120 struct GNUNET_CADET_TEST_Context *ctx = actx->ctx; 136 struct GNUNET_CADET_TEST_Context *ctx = actx->ctx;
121 struct GNUNET_CADET_Handle *h; 137 struct GNUNET_CADET_Handle *h;
138 unsigned int i;
122 139
123 h = GNUNET_CADET_connect (cfg, 140 h = GNUNET_CADET_connect (cfg);
124 (void *) (long) actx->peer,
125 ctx->cleaner,
126 ctx->handlers);
127 if (NULL == ctx->ports) 141 if (NULL == ctx->ports)
128 return h; 142 return h;
129 143
130 for (int i = 0; NULL != ctx->ports[i]; i++) 144 actx->ports = GNUNET_new_array (ctx->port_count, struct GNUNET_CADET_Port *);
145 for (i = 0; i < ctx->port_count; i++)
131 { 146 {
132 (void ) GNUNET_CADET_open_port (h, ctx->ports[i], 147 actx->ports[i] = GNUNET_CADET_open_port (h,
133 ctx->new_channel, 148 ctx->ports[i],
134 (void *) (long) actx->peer); 149 ctx->connects,
150 (void *) (long) actx->peer,
151 ctx->window_changes,
152 ctx->disconnects,
153 ctx->handlers);
135 } 154 }
136
137 return h; 155 return h;
138} 156}
139 157
@@ -152,6 +170,15 @@ cadet_disconnect_adapter (void *cls,
152 struct GNUNET_CADET_Handle *cadet = op_result; 170 struct GNUNET_CADET_Handle *cadet = op_result;
153 struct GNUNET_CADET_TEST_AdapterContext *actx = cls; 171 struct GNUNET_CADET_TEST_AdapterContext *actx = cls;
154 172
173 if (NULL != actx->ports)
174 {
175 for (int i = 0; i < actx->ctx->port_count; i++)
176 {
177 GNUNET_CADET_close_port (actx->ports[i]);
178 actx->ports[i] = NULL;
179 }
180 GNUNET_free (actx->ports);
181 }
155 GNUNET_free (actx); 182 GNUNET_free (actx);
156 GNUNET_CADET_disconnect (cadet); 183 GNUNET_CADET_disconnect (cadet);
157} 184}
@@ -186,18 +213,18 @@ cadet_connect_cb (void *cls,
186 for (i = 0; i < ctx->num_peers; i++) 213 for (i = 0; i < ctx->num_peers; i++)
187 if (op == ctx->ops[i]) 214 if (op == ctx->ops[i])
188 { 215 {
189 ctx->cadetes[i] = ca_result; 216 ctx->cadets[i] = ca_result;
190 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "...cadet %u connected\n", i); 217 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "...cadet %u connected\n", i);
191 } 218 }
192 for (i = 0; i < ctx->num_peers; i++) 219 for (i = 0; i < ctx->num_peers; i++)
193 if (NULL == ctx->cadetes[i]) 220 if (NULL == ctx->cadets[i])
194 return; /* still some CADET connections missing */ 221 return; /* still some CADET connections missing */
195 /* all CADET connections ready! */ 222 /* all CADET connections ready! */
196 ctx->app_main (ctx->app_main_cls, 223 ctx->app_main (ctx->app_main_cls,
197 ctx, 224 ctx,
198 ctx->num_peers, 225 ctx->num_peers,
199 ctx->peers, 226 ctx->peers,
200 ctx->cadetes); 227 ctx->cadets);
201} 228}
202 229
203 230
@@ -213,7 +240,7 @@ GNUNET_CADET_TEST_cleanup (struct GNUNET_CADET_TEST_Context *ctx)
213 ctx->ops[i] = NULL; 240 ctx->ops[i] = NULL;
214 } 241 }
215 GNUNET_free (ctx->ops); 242 GNUNET_free (ctx->ops);
216 GNUNET_free (ctx->cadetes); 243 GNUNET_free (ctx->cadets);
217 GNUNET_free (ctx); 244 GNUNET_free (ctx);
218 GNUNET_SCHEDULER_shutdown (); 245 GNUNET_SCHEDULER_shutdown ();
219} 246}
@@ -243,12 +270,23 @@ cadet_test_run (void *cls,
243 struct GNUNET_CADET_TEST_Context *ctx = cls; 270 struct GNUNET_CADET_TEST_Context *ctx = cls;
244 unsigned int i; 271 unsigned int i;
245 272
273 if (0 != links_failed)
274 {
275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Some links failed (%u), ending\n",
276 links_failed);
277 exit (2);
278 }
279
246 if (num_peers != ctx->num_peers) 280 if (num_peers != ctx->num_peers)
247 { 281 {
248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers started %u/%u, ending\n", 282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers started %u/%u, ending\n",
249 num_peers, ctx->num_peers); 283 num_peers, ctx->num_peers);
250 exit (1); 284 exit (1);
251 } 285 }
286
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "Testbed up, %u peers and %u links\n",
289 num_peers, links_succeeded);
252 ctx->peers = peers; 290 ctx->peers = peers;
253 for (i = 0; i < num_peers; i++) 291 for (i = 0; i < num_peers; i++)
254 { 292 {
@@ -270,31 +308,52 @@ cadet_test_run (void *cls,
270} 308}
271 309
272 310
311/**
312 * Run a test using the given name, configuration file and number of peers.
313 * All cadet callbacks will receive the peer number (long) as the closure.
314 *
315 * @param testname Name of the test (for logging).
316 * @param cfgfile Name of the configuration file.
317 * @param num_peers Number of peers to start.
318 * @param tmain Main function to run once the testbed is ready.
319 * @param tmain_cls Closure for @a tmain.
320 * @param connects Handler for incoming channels.
321 * @param window_changes Handler for the window size change notification.
322 * @param disconnects Cleaner for destroyed incoming channels.
323 * @param handlers Message handlers.
324 * @param ports Ports the peers offer, NULL-terminated.
325 */
273void 326void
274GNUNET_CADET_TEST_run (const char *testname, 327GNUNET_CADET_TEST_ruN (const char *testname,
275 const char *cfgname, 328 const char *cfgfile,
276 unsigned int num_peers, 329 unsigned int num_peers,
277 GNUNET_CADET_TEST_AppMain tmain, 330 GNUNET_CADET_TEST_AppMain tmain,
278 void *tmain_cls, 331 void *tmain_cls,
279 GNUNET_CADET_InboundChannelNotificationHandler new_channel, 332 GNUNET_CADET_ConnectEventHandler connects,
280 GNUNET_CADET_ChannelEndHandler cleaner, 333 GNUNET_CADET_WindowSizeEventHandler window_changes,
281 struct GNUNET_CADET_MessageHandler* handlers, 334 GNUNET_CADET_DisconnectEventHandler disconnects,
282 const struct GNUNET_HashCode **ports) 335 struct GNUNET_MQ_MessageHandler *handlers,
336 const struct GNUNET_HashCode **ports)
283{ 337{
284 struct GNUNET_CADET_TEST_Context *ctx; 338 struct GNUNET_CADET_TEST_Context *ctx;
285 339
286 ctx = GNUNET_new (struct GNUNET_CADET_TEST_Context); 340 ctx = GNUNET_new (struct GNUNET_CADET_TEST_Context);
287 ctx->num_peers = num_peers; 341 ctx->num_peers = num_peers;
288 ctx->ops = GNUNET_malloc (num_peers * sizeof (struct GNUNET_TESTBED_Operation *)); 342 ctx->ops = GNUNET_new_array (num_peers, struct GNUNET_TESTBED_Operation *);
289 ctx->cadetes = GNUNET_malloc (num_peers * sizeof (struct GNUNET_CADET_Handle *)); 343 ctx->cadets = GNUNET_new_array (num_peers, struct GNUNET_CADET_Handle *);
290 ctx->app_main = tmain; 344 ctx->app_main = tmain;
291 ctx->app_main_cls = tmain_cls; 345 ctx->app_main_cls = tmain_cls;
292 ctx->new_channel = new_channel; 346 ctx->connects = connects;
293 ctx->cleaner = cleaner; 347 ctx->window_changes = window_changes;
294 ctx->handlers = handlers; 348 ctx->disconnects = disconnects;
349 ctx->handlers = GNUNET_MQ_copy_handlers (handlers);
295 ctx->ports = ports; 350 ctx->ports = ports;
351 ctx->port_count = 0;
352 while (NULL != ctx->ports[ctx->port_count])
353 ctx->port_count++;
354
296 GNUNET_TESTBED_test_run (testname, 355 GNUNET_TESTBED_test_run (testname,
297 cfgname, 356 cfgfile,
298 num_peers, 357 num_peers,
299 0LL, NULL, NULL, 358 0LL, NULL, NULL,
300 &cadet_test_run, ctx); 359 &cadet_test_run, ctx);