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