2 This file is part of GNUnet.
3 Copyright (C) 2011, 2012 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file dht/test_dht_monitor.c
22 * @brief Test for the dht monitoring API; checks that we receive "some" monitor events
23 * @author Christian Grothoff
26 #include "gnunet_testbed_service.h"
27 #include "gnunet_dht_service.h"
28 #include "dht_test_lib.h"
32 * How long do we run the test at most?
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
37 * How often do we run the PUTs?
39 #define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
43 * Information we keep for each GET operation.
50 struct GetOperation *next;
55 struct GetOperation *prev;
58 * Handle for the operation.
60 struct GNUNET_DHT_GetHandle *get;
66 * Return value from 'main'.
71 * Head of list of active GET operations.
73 static struct GetOperation *get_head;
76 * Tail of list of active GET operations.
78 static struct GetOperation *get_tail;
81 * Array of the testbed's peers.
83 static struct GNUNET_TESTBED_Peer **my_peers;
86 * Number of peers to run.
88 static unsigned int NUM_PEERS = 3;
91 * Task called to disconnect peers.
93 static struct GNUNET_SCHEDULER_Task *timeout_task;
98 static struct GNUNET_SCHEDULER_Task * put_task;
100 static struct GNUNET_DHT_MonitorHandle **monitors;
102 static unsigned int monitor_counter;
106 * Task run on success or timeout to clean up.
107 * Terminates active get operations and shuts down
110 * @param cls the `struct GNUNET_DHT_TEST_Context`
113 shutdown_task (void *cls)
115 struct GNUNET_DHT_TEST_Context *ctx = cls;
117 struct GetOperation *get_op;
119 ok = (monitor_counter > NUM_PEERS) ? 0 : 2;
120 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
121 "Received %u monitor events\n",
123 while (NULL != (get_op = get_tail))
125 GNUNET_DHT_get_stop (get_op->get);
126 GNUNET_CONTAINER_DLL_remove (get_head,
129 GNUNET_free (get_op);
131 for (i=0;i<NUM_PEERS;i++)
132 GNUNET_DHT_monitor_stop (monitors[i]);
133 GNUNET_free (monitors);
134 GNUNET_SCHEDULER_cancel (put_task);
135 GNUNET_DHT_TEST_cleanup (ctx);
136 if (NULL != timeout_task)
138 GNUNET_SCHEDULER_cancel (timeout_task);
145 * Task run on success or timeout to clean up.
146 * Terminates active get operations and shuts down
152 timeout_task_cb (void *cls)
155 GNUNET_SCHEDULER_shutdown ();
160 * Iterator called on each result obtained for a DHT
161 * operation that expects a reply
163 * @param cls closure with our 'struct GetOperation'
164 * @param exp when will this value expire
165 * @param key key of the result
166 * @param get_path peers on reply path (or NULL if not recorded)
167 * @param get_path_length number of entries in get_path
168 * @param put_path peers on the PUT path (or NULL if not recorded)
169 * @param put_path_length number of entries in get_path
170 * @param type type of the result
171 * @param size number of bytes in data
172 * @param data pointer to the result data
175 dht_get_handler (void *cls, struct GNUNET_TIME_Absolute exp,
176 const struct GNUNET_HashCode * key,
177 const struct GNUNET_PeerIdentity *get_path,
178 unsigned int get_path_length,
179 const struct GNUNET_PeerIdentity *put_path,
180 unsigned int put_path_length,
181 enum GNUNET_BLOCK_Type type,
182 size_t size, const void *data)
184 struct GetOperation *get_op = cls;
185 struct GNUNET_HashCode want;
187 if (sizeof (struct GNUNET_HashCode) != size)
192 GNUNET_CRYPTO_hash (key, sizeof (*key), &want);
193 if (0 != memcmp (&want, data, sizeof (want)))
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 GNUNET_DHT_get_stop (get_op->get);
201 GNUNET_CONTAINER_DLL_remove (get_head,
204 GNUNET_free (get_op);
205 if (NULL != get_head)
207 /* all DHT GET operations successful; terminate! */
209 GNUNET_SCHEDULER_shutdown ();
214 * Task to put the id of each peer into the DHT.
216 * @param cls array with NUM_PEERS DHT handles
221 struct GNUNET_DHT_Handle **hs = cls;
222 struct GNUNET_HashCode key;
223 struct GNUNET_HashCode value;
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Putting values into DHT\n");
227 for (unsigned int i = 0; i < NUM_PEERS; i++)
229 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
230 GNUNET_CRYPTO_hash (&key, sizeof (key), &value);
231 GNUNET_DHT_put (hs[i], &key, 10U,
232 GNUNET_DHT_RO_RECORD_ROUTE |
233 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
234 GNUNET_BLOCK_TYPE_TEST,
235 sizeof (value), &value,
236 GNUNET_TIME_UNIT_FOREVER_ABS,
239 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
245 * Callback called on each GET request going through the DHT.
246 * Prints the info about the intercepted packet and increments a counter.
248 * @param cls Closure.
249 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
250 * @param type The type of data in the request.
251 * @param hop_count Hop count so far.
252 * @param path_length number of entries in path (or 0 if not recorded).
253 * @param path peers on the GET path (or NULL if not recorded).
254 * @param desired_replication_level Desired replication level.
255 * @param key Key of the requested data.
258 monitor_get_cb (void *cls,
259 enum GNUNET_DHT_RouteOption options,
260 enum GNUNET_BLOCK_Type type,
262 uint32_t desired_replication_level,
263 unsigned int path_length,
264 const struct GNUNET_PeerIdentity *path,
265 const struct GNUNET_HashCode * key)
269 i = (unsigned int) (long) cls;
270 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
271 "%u got a GET message for key %s\n",
279 * Callback called on each PUT request going through the DHT.
280 * Prints the info about the intercepted packet and increments a counter.
282 * @param cls Closure.
283 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
284 * @param type The type of data in the request.
285 * @param hop_count Hop count so far.
286 * @param path_length number of entries in path (or 0 if not recorded).
287 * @param path peers on the PUT path (or NULL if not recorded).
288 * @param desired_replication_level Desired replication level.
289 * @param exp Expiration time of the data.
290 * @param key Key under which data is to be stored.
291 * @param data Pointer to the data carried.
292 * @param size Number of bytes in data.
295 monitor_put_cb (void *cls,
296 enum GNUNET_DHT_RouteOption options,
297 enum GNUNET_BLOCK_Type type,
299 uint32_t desired_replication_level,
300 unsigned int path_length,
301 const struct GNUNET_PeerIdentity *path,
302 struct GNUNET_TIME_Absolute exp,
303 const struct GNUNET_HashCode * key,
309 i = (unsigned int) (long) cls;
310 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
311 "%u got a PUT message for key %s with %u bytes\n",
314 (unsigned int) size);
320 * Callback called on each GET reply going through the DHT.
321 * Prints the info about the intercepted packet and increments a counter.
323 * @param cls Closure.
324 * @param type The type of data in the result.
325 * @param get_path Peers on GET path (or NULL if not recorded).
326 * @param get_path_length number of entries in get_path.
327 * @param put_path peers on the PUT path (or NULL if not recorded).
328 * @param put_path_length number of entries in get_path.
329 * @param exp Expiration time of the data.
330 * @param key Key of the data.
331 * @param data Pointer to the result data.
332 * @param size Number of bytes in data.
335 monitor_res_cb (void *cls,
336 enum GNUNET_BLOCK_Type type,
337 const struct GNUNET_PeerIdentity *get_path,
338 unsigned int get_path_length,
339 const struct GNUNET_PeerIdentity *put_path,
340 unsigned int put_path_length,
341 struct GNUNET_TIME_Absolute exp,
342 const struct GNUNET_HashCode * key,
348 i = (unsigned int) (long) cls;
349 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
350 "%u got a REPLY message for key %s with %u bytes\n",
353 (unsigned int) size);
359 * Main function of the test.
361 * @param cls closure (NULL)
362 * @param ctx argument to give to GNUNET_DHT_TEST_cleanup on test end
363 * @param num_peers number of peers that are running
364 * @param peers array of peers
365 * @param dhts handle to each of the DHTs of the peers
369 struct GNUNET_DHT_TEST_Context *ctx,
370 unsigned int num_peers,
371 struct GNUNET_TESTBED_Peer **peers,
372 struct GNUNET_DHT_Handle **dhts)
376 struct GNUNET_HashCode key;
377 struct GetOperation *get_op;
379 GNUNET_assert (NUM_PEERS == num_peers);
381 monitors = GNUNET_new_array (num_peers,
382 struct GNUNET_DHT_MonitorHandle *);
383 for (i = 0; i < num_peers; i++)
384 monitors[i] = GNUNET_DHT_monitor_start (dhts[i],
385 GNUNET_BLOCK_TYPE_ANY,
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Peers setup, starting test\n");
393 put_task = GNUNET_SCHEDULER_add_now (&do_puts, dhts);
394 for (i=0;i<num_peers;i++)
396 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
397 for (j=0;j<num_peers;j++)
399 get_op = GNUNET_new (struct GetOperation);
400 GNUNET_CONTAINER_DLL_insert (get_head,
403 get_op->get = GNUNET_DHT_get_start (dhts[j],
404 GNUNET_BLOCK_TYPE_TEST, /* type */
405 &key, /*key to search */
406 4U, /* replication level */
407 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
410 &dht_get_handler, get_op);
413 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
416 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
425 main (int xargc, char *xargv[])
427 GNUNET_DHT_TEST_run ("test-dht-monitor",
428 "test_dht_monitor.conf",
435 /* end of test_dht_monitor.c */