aboutsummaryrefslogtreecommitdiff
path: root/src/dht/test_dht_twopeer_path_tracking.c
diff options
context:
space:
mode:
authorNathan S. Evans <evans@in.tum.de>2011-02-02 11:54:21 +0000
committerNathan S. Evans <evans@in.tum.de>2011-02-02 11:54:21 +0000
commit0aa66524bd2ef85668ba8de14792eb39480b59ba (patch)
treec84691a6fad3cbdbcc963e6194ba2b49971963a3 /src/dht/test_dht_twopeer_path_tracking.c
parent3b3eb36e3a01c7046fe4c9a3c4bb7834d83442d6 (diff)
downloadgnunet-0aa66524bd2ef85668ba8de14792eb39480b59ba.tar.gz
gnunet-0aa66524bd2ef85668ba8de14792eb39480b59ba.zip
path tracking commit, hopefully a fix for dht_api connect bug, needs tested
Diffstat (limited to 'src/dht/test_dht_twopeer_path_tracking.c')
-rw-r--r--src/dht/test_dht_twopeer_path_tracking.c513
1 files changed, 513 insertions, 0 deletions
diff --git a/src/dht/test_dht_twopeer_path_tracking.c b/src/dht/test_dht_twopeer_path_tracking.c
new file mode 100644
index 000000000..ab4bc81e0
--- /dev/null
+++ b/src/dht/test_dht_twopeer_path_tracking.c
@@ -0,0 +1,513 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 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 * @file dht/test_dht_twopeer_path_tracking.c
22 * @brief testcase for testing DHT service with
23 * two running peers, logging the path of the dht requests.
24 */
25#include "platform.h"
26#include "gnunet_testing_lib.h"
27#include "gnunet_core_service.h"
28#include "gnunet_dht_service.h"
29
30/* DEFINES */
31#define VERBOSE GNUNET_NO
32
33/* Timeout for entire testcase */
34#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
35
36/* Timeout for waiting for replies to get requests */
37#define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
38
39/* If number of peers not in config file, use this number */
40#define DEFAULT_NUM_PEERS 2
41
42/* Globals */
43
44/**
45 * Directory to store temp data in, defined in config file
46 */
47static char *test_directory;
48
49/**
50 * Variable used to store the number of connections we should wait for.
51 */
52static unsigned int expected_connections;
53
54/**
55 * Variable used to keep track of how many peers aren't yet started.
56 */
57static unsigned long long peers_left;
58
59/**
60 * Handle to the set of all peers run for this test.
61 */
62static struct GNUNET_TESTING_PeerGroup *pg;
63
64/**
65 * Global handle we will use for GET requests.
66 */
67struct GNUNET_DHT_GetHandle *global_get_handle;
68
69
70/**
71 * Total number of peers to run, set based on config file.
72 */
73static unsigned long long num_peers;
74
75/**
76 * Global used to count how many connections we have currently
77 * been notified about (how many times has topology_callback been called
78 * with success?)
79 */
80static unsigned int total_connections;
81
82/**
83 * Global used to count how many failed connections we have
84 * been notified about (how many times has topology_callback
85 * been called with failure?)
86 */
87static unsigned int failed_connections;
88
89/* Task handle to use to schedule test failure */
90GNUNET_SCHEDULER_TaskIdentifier die_task;
91
92/* Global return value (0 for success, anything else for failure) */
93static int ok;
94
95/**
96 * Peer identity of the first peer started.
97 */
98static struct GNUNET_PeerIdentity peer1id;
99
100/**
101 * Peer identity of the second peer started.
102 */
103static struct GNUNET_PeerIdentity peer2id;
104
105/**
106 * Handle to the first peers DHT service (via the API)
107 */
108static struct GNUNET_DHT_Handle *peer1dht;
109
110/**
111 * Handle to the second peers DHT service (via the API)
112 */
113static struct GNUNET_DHT_Handle *peer2dht;
114
115/**
116 * Check whether peers successfully shut down.
117 */
118void shutdown_callback (void *cls,
119 const char *emsg)
120{
121 if (emsg != NULL)
122 {
123 if (ok == 0)
124 ok = 2;
125 }
126}
127
128/**
129 * Function scheduled to be run on the successful completion of this
130 * testcase. Specifically, called when our get request completes.
131 */
132static void
133finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
134{
135 GNUNET_assert (pg != NULL);
136 GNUNET_assert (peer1dht != NULL);
137 GNUNET_assert (peer2dht != NULL);
138 GNUNET_DHT_disconnect(peer1dht);
139 GNUNET_DHT_disconnect(peer2dht);
140 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
141 ok = 0;
142}
143
144/**
145 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
146 * down the peers without freeing memory associated with GET request.
147 */
148static void
149end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
150{
151 if (peer1dht != NULL)
152 GNUNET_DHT_disconnect(peer1dht);
153
154 if (peer2dht != NULL)
155 GNUNET_DHT_disconnect(peer2dht);
156
157 if (pg != NULL)
158 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
159}
160
161/**
162 * Check if the get_handle is being used, if so stop the request. Either
163 * way, schedule the end_badly_cont function which actually shuts down the
164 * test.
165 */
166static void
167end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
168{
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failing test with error: `%s'!\n", (char *)cls);
170 if (global_get_handle != NULL)
171 {
172 GNUNET_DHT_get_stop(global_get_handle);
173 global_get_handle = NULL;
174 }
175 GNUNET_SCHEDULER_add_now(&end_badly_cont, NULL);
176 ok = 1;
177}
178
179/**
180 * Iterator called if the GET request initiated returns a response.
181 *
182 * @param cls closure
183 * @param exp when will this value expire
184 * @param key key of the result
185 * @param type type of the result
186 * @param size number of bytes in data
187 * @param data pointer to the result data
188 */
189void get_result_iterator (void *cls,
190 struct GNUNET_TIME_Absolute exp,
191 const GNUNET_HashCode * key,
192 const struct GNUNET_PeerIdentity * const *get_path,
193 const struct GNUNET_PeerIdentity * const *put_path,
194 enum GNUNET_BLOCK_Type type,
195 size_t size,
196 const void *data)
197{
198 GNUNET_HashCode original_key; /* Key data was stored data under */
199 char original_data[4]; /* Made up data that was stored */
200 memset(&original_key, 42, sizeof(GNUNET_HashCode)); /* Set the key to what it was set to previously */
201 memset(original_data, 43, sizeof(original_data));
202#if VERBOSE
203 unsigned int i;
204#endif
205
206 if ((0 != memcmp(&original_key, key, sizeof (GNUNET_HashCode))) || (0 != memcmp(original_data, data, sizeof(original_data))))
207 {
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Key or data is not the same as was inserted!\n");
209 GNUNET_SCHEDULER_cancel(die_task);
210 GNUNET_SCHEDULER_add_now(&end_badly, "key or data mismatch in get response!\n");
211 return;
212 }
213
214#if VERBOSE
215 if (put_path != NULL)
216 {
217 fprintf(stderr, "PUT Path: ");
218 for (i = 0; put_path[i] != NULL; i++)
219 fprintf(stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s(put_path[i]));
220 fprintf(stderr, "\n");
221 }
222 if (get_path != NULL)
223 {
224 fprintf(stderr, "GET Path: ");
225 for (i = 0; get_path[i] != NULL; i++)
226 fprintf(stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s(get_path[i]));
227 fprintf(stderr, "\n");
228 }
229#endif
230
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
232 GNUNET_SCHEDULER_cancel(die_task);
233 GNUNET_DHT_get_stop(global_get_handle);
234 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
235}
236
237/**
238 * Start the GET request for the same key/data that was inserted.
239 */
240static void
241do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
242{
243 GNUNET_HashCode key; /* Key for data lookup */
244 memset(&key, 42, sizeof(GNUNET_HashCode)); /* Set the key to the same thing as when data was inserted */
245 global_get_handle = GNUNET_DHT_get_start(peer2dht, GNUNET_TIME_relative_get_forever(),
246 GNUNET_BLOCK_TYPE_TEST,
247 &key,
248 DEFAULT_GET_REPLICATION,
249 GNUNET_DHT_RO_RECORD_ROUTE,
250 NULL, 0,
251 NULL, 0,
252 &get_result_iterator, NULL);
253}
254
255/**
256 * Called when the PUT request has been transmitted to the DHT service.
257 * Schedule the GET request for some time in the future.
258 */
259static void
260put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
261{
262 GNUNET_SCHEDULER_cancel (die_task);
263 die_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
264 &end_badly, "waiting for get response (data not found)");
265 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10), &do_get, NULL);
266}
267
268/**
269 * Set up some data, and call API PUT function
270 */
271static void
272do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
273{
274 GNUNET_HashCode key; /* Made up key to store data under */
275 char data[4]; /* Made up data to store */
276 memset(&key, 42, sizeof(GNUNET_HashCode)); /* Set the key to something simple so we can issue GET request */
277 memset(data, 43, sizeof(data));
278
279 /* Insert the data at the first peer */
280 GNUNET_DHT_put(peer1dht,
281 &key,
282 DEFAULT_PUT_REPLICATION,
283 GNUNET_DHT_RO_RECORD_ROUTE,
284 GNUNET_BLOCK_TYPE_TEST,
285 sizeof(data), data,
286 GNUNET_TIME_UNIT_FOREVER_ABS,
287 GNUNET_TIME_UNIT_FOREVER_REL,
288 &put_finished, NULL);
289}
290
291/**
292 * This function is called whenever a connection attempt is finished between two of
293 * the started peers (started with GNUNET_TESTING_daemons_start). The total
294 * number of times this function is called should equal the number returned
295 * from the GNUNET_TESTING_connect_topology call.
296 *
297 * The emsg variable is NULL on success (peers connected), and non-NULL on
298 * failure (peers failed to connect).
299 */
300void
301topology_callback (void *cls,
302 const struct GNUNET_PeerIdentity *first,
303 const struct GNUNET_PeerIdentity *second,
304 uint32_t distance,
305 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
306 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
307 struct GNUNET_TESTING_Daemon *first_daemon,
308 struct GNUNET_TESTING_Daemon *second_daemon,
309 const char *emsg)
310{
311 if (emsg == NULL)
312 {
313 total_connections++;
314#if VERBOSE
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
316 first_daemon->shortname,
317 second_daemon->shortname,
318 distance);
319#endif
320 }
321#if VERBOSE
322 else
323 {
324 failed_connections++;
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
326 first_daemon->shortname,
327 second_daemon->shortname, emsg);
328 }
329#endif
330
331 if (total_connections == expected_connections)
332 {
333#if VERBOSE
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
336 total_connections);
337#endif
338 GNUNET_SCHEDULER_cancel (die_task);
339 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
340 &end_badly, "from test gets");
341
342 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2), &do_put, NULL);
343 }
344 else if (total_connections + failed_connections == expected_connections)
345 {
346 GNUNET_SCHEDULER_cancel (die_task);
347 die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from topology_callback (too many failed connections)");
348 }
349}
350
351
352/**
353 * Callback which is called whenever a peer is started (as a result of the
354 * GNUNET_TESTING_daemons_start call.
355 *
356 * @param cls closure argument given to GNUNET_TESTING_daemons_start
357 * @param id the GNUNET_PeerIdentity of the started peer
358 * @param cfg the configuration for this specific peer (needed to connect
359 * to the DHT)
360 * @param d the handle to the daemon started
361 * @param emsg NULL if peer started, non-NULL on error
362 */
363static void
364peers_started_callback (void *cls,
365 const struct GNUNET_PeerIdentity *id,
366 const struct GNUNET_CONFIGURATION_Handle *cfg,
367 struct GNUNET_TESTING_Daemon *d, const char *emsg)
368{
369 if (emsg != NULL)
370 {
371 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to start daemon with error: `%s'\n",
372 emsg);
373 return;
374 }
375 GNUNET_assert (id != NULL);
376
377 /* This is the first peer started */
378 if (peers_left == num_peers)
379 {
380 memcpy(&peer1id, id, sizeof(struct GNUNET_PeerIdentity)); /* Save the peer id */
381 peer1dht = GNUNET_DHT_connect(cfg, 100); /* Connect to the first peers DHT service */
382 if (peer1dht == NULL) /* If DHT connect failed */
383 {
384 GNUNET_SCHEDULER_cancel (die_task);
385 GNUNET_SCHEDULER_add_now(&end_badly, "Failed to get dht handle!\n");
386 }
387 }
388 else /* This is the second peer started */
389 {
390 memcpy(&peer2id, id, sizeof(struct GNUNET_PeerIdentity)); /* Same as for first peer... */
391 peer2dht = GNUNET_DHT_connect(cfg, 100);
392 if (peer2dht == NULL)
393 {
394 GNUNET_SCHEDULER_cancel (die_task);
395 GNUNET_SCHEDULER_add_now(&end_badly, "Failed to get dht handle!\n");
396 }
397 }
398
399 /* Decrement number of peers left to start */
400 peers_left--;
401
402 if (peers_left == 0) /* Indicates all peers started */
403 {
404#if VERBOSE
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "All %d daemons started, now connecting peers!\n",
407 num_peers);
408#endif
409 expected_connections = -1;
410 if ((pg != NULL)) /* Sanity check */
411 {
412 /* Connect peers in a "straight line" topology, return the number of expected connections */
413 expected_connections = GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_LINE, GNUNET_TESTING_TOPOLOGY_OPTION_ALL, 0.0, NULL, NULL);
414 }
415
416 /* Cancel current timeout fail task */
417 GNUNET_SCHEDULER_cancel (die_task);
418 if (expected_connections == GNUNET_SYSERR) /* Some error happened */
419 die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from connect topology (bad return)");
420
421 /* Schedule timeout on failure task */
422 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
423 &end_badly, "from connect topology (timeout)");
424 ok = 0;
425 }
426}
427
428static void
429run (void *cls,
430 char *const *args,
431 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
432{
433
434 /* Get path from configuration file */
435 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
436 {
437 ok = 404;
438 return;
439 }
440
441 /* Get number of peers to start from configuration (should be two) */
442 if (GNUNET_SYSERR ==
443 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
444 &num_peers))
445 num_peers = DEFAULT_NUM_PEERS;
446
447 /* Set peers_left so we know when all peers started */
448 peers_left = num_peers;
449
450 /* Set up a task to end testing if peer start fails */
451 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
452 &end_badly, "didn't start all daemons in reasonable amount of time!!!");
453
454 /* Start num_peers peers, call peers_started_callback on peer start, topology_callback on peer connect */
455 /* Read the API documentation for other parameters! */
456 pg = GNUNET_TESTING_daemons_start (cfg,
457 num_peers, TIMEOUT, NULL, NULL, &peers_started_callback, NULL,
458 &topology_callback, NULL, NULL);
459
460}
461
462static int
463check ()
464{
465 int ret;
466 /* Arguments for GNUNET_PROGRAM_run */
467 char *const argv[] = {"test-dht-twopeer-put-get", /* Name to give running binary */
468 "-c",
469 "test_dht_twopeer_data.conf", /* Config file to use */
470#if VERBOSE
471 "-L", "DEBUG",
472#endif
473 NULL
474 };
475 struct GNUNET_GETOPT_CommandLineOption options[] = {
476 GNUNET_GETOPT_OPTION_END
477 };
478 /* Run the run function as a new program */
479 ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
480 argv, "test-dht-twopeer-put-get", "nohelp",
481 options, &run, &ok);
482 if (ret != GNUNET_OK)
483 {
484 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`test-dht-twopeer': Failed with error code %d\n", ret);
485 }
486 return ok;
487}
488
489int
490main (int argc, char *argv[])
491{
492 int ret;
493
494 GNUNET_log_setup ("test-dht-twopeer",
495#if VERBOSE
496 "DEBUG",
497#else
498 "WARNING",
499#endif
500 NULL);
501 ret = check ();
502 /**
503 * Need to remove base directory, subdirectories taken care
504 * of by the testing framework.
505 */
506 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
507 {
508 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
509 }
510 return ret;
511}
512
513/* end of test_dht_twopeer_put_get.c */