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_TestContext'
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);
140 * Iterator called on each result obtained for a DHT
141 * operation that expects a reply
143 * @param cls closure with our 'struct GetOperation'
144 * @param exp when will this value expire
145 * @param key key of the result
146 * @param get_path peers on reply path (or NULL if not recorded)
147 * @param get_path_length number of entries in get_path
148 * @param put_path peers on the PUT path (or NULL if not recorded)
149 * @param put_path_length number of entries in get_path
150 * @param type type of the result
151 * @param size number of bytes in data
152 * @param data pointer to the result data
155 dht_get_handler (void *cls, struct GNUNET_TIME_Absolute exp,
156 const struct GNUNET_HashCode * key,
157 const struct GNUNET_PeerIdentity *get_path,
158 unsigned int get_path_length,
159 const struct GNUNET_PeerIdentity *put_path,
160 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
161 size_t size, const void *data)
163 struct GetOperation *get_op = cls;
164 struct GNUNET_HashCode want;
165 struct GNUNET_DHT_TestContext *ctx;
167 if (sizeof (struct GNUNET_HashCode) != size)
172 GNUNET_CRYPTO_hash (key, sizeof (*key), &want);
173 if (0 != memcmp (&want, data, sizeof (want)))
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 GNUNET_DHT_get_stop (get_op->get);
181 GNUNET_CONTAINER_DLL_remove (get_head,
184 GNUNET_free (get_op);
185 if (NULL != get_head)
187 /* all DHT GET operations successful; terminate! */
189 ctx = GNUNET_SCHEDULER_cancel (timeout_task);
190 timeout_task = GNUNET_SCHEDULER_add_now (&shutdown_task, ctx);
195 * Task to put the id of each peer into the DHT.
197 * @param cls array with NUM_PEERS DHT handles
202 struct GNUNET_DHT_Handle **hs = cls;
203 struct GNUNET_HashCode key;
204 struct GNUNET_HashCode value;
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "Putting values into DHT\n");
209 for (i = 0; i < NUM_PEERS; i++)
211 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
212 GNUNET_CRYPTO_hash (&key, sizeof (key), &value);
213 GNUNET_DHT_put (hs[i], &key, 10U,
214 GNUNET_DHT_RO_RECORD_ROUTE |
215 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
216 GNUNET_BLOCK_TYPE_TEST,
217 sizeof (value), &value,
218 GNUNET_TIME_UNIT_FOREVER_ABS,
219 GNUNET_TIME_UNIT_FOREVER_REL,
222 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
228 * Callback called on each GET request going through the DHT.
229 * Prints the info about the intercepted packet and increments a counter.
231 * @param cls Closure.
232 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
233 * @param type The type of data in the request.
234 * @param hop_count Hop count so far.
235 * @param path_length number of entries in path (or 0 if not recorded).
236 * @param path peers on the GET path (or NULL if not recorded).
237 * @param desired_replication_level Desired replication level.
238 * @param key Key of the requested data.
241 monitor_get_cb (void *cls,
242 enum GNUNET_DHT_RouteOption options,
243 enum GNUNET_BLOCK_Type type,
245 uint32_t desired_replication_level,
246 unsigned int path_length,
247 const struct GNUNET_PeerIdentity *path,
248 const struct GNUNET_HashCode * key)
252 i = (unsigned int) (long) cls;
253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
254 "%u got a GET message for key %s\n",
262 * Callback called on each PUT request going through the DHT.
263 * Prints the info about the intercepted packet and increments a counter.
265 * @param cls Closure.
266 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
267 * @param type The type of data in the request.
268 * @param hop_count Hop count so far.
269 * @param path_length number of entries in path (or 0 if not recorded).
270 * @param path peers on the PUT path (or NULL if not recorded).
271 * @param desired_replication_level Desired replication level.
272 * @param exp Expiration time of the data.
273 * @param key Key under which data is to be stored.
274 * @param data Pointer to the data carried.
275 * @param size Number of bytes in data.
278 monitor_put_cb (void *cls,
279 enum GNUNET_DHT_RouteOption options,
280 enum GNUNET_BLOCK_Type type,
282 uint32_t desired_replication_level,
283 unsigned int path_length,
284 const struct GNUNET_PeerIdentity *path,
285 struct GNUNET_TIME_Absolute exp,
286 const struct GNUNET_HashCode * key,
292 i = (unsigned int) (long) cls;
293 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
294 "%u got a PUT message for key %s with %u bytes\n",
297 (unsigned int) size);
303 * Callback called on each GET reply going through the DHT.
304 * Prints the info about the intercepted packet and increments a counter.
306 * @param cls Closure.
307 * @param type The type of data in the result.
308 * @param get_path Peers on GET path (or NULL if not recorded).
309 * @param get_path_length number of entries in get_path.
310 * @param put_path peers on the PUT path (or NULL if not recorded).
311 * @param put_path_length number of entries in get_path.
312 * @param exp Expiration time of the data.
313 * @param key Key of the data.
314 * @param data Pointer to the result data.
315 * @param size Number of bytes in data.
318 monitor_res_cb (void *cls,
319 enum GNUNET_BLOCK_Type type,
320 const struct GNUNET_PeerIdentity *get_path,
321 unsigned int get_path_length,
322 const struct GNUNET_PeerIdentity *put_path,
323 unsigned int put_path_length,
324 struct GNUNET_TIME_Absolute exp,
325 const struct GNUNET_HashCode * key,
331 i = (unsigned int) (long) cls;
332 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
333 "%u got a REPLY message for key %s with %u bytes\n",
336 (unsigned int) size);
342 * Main function of the test.
344 * @param cls closure (NULL)
345 * @param ctx argument to give to GNUNET_DHT_TEST_cleanup on test end
346 * @param num_peers number of peers that are running
347 * @param peers array of peers
348 * @param dhts handle to each of the DHTs of the peers
352 struct GNUNET_DHT_TEST_Context *ctx,
353 unsigned int num_peers,
354 struct GNUNET_TESTBED_Peer **peers,
355 struct GNUNET_DHT_Handle **dhts)
359 struct GNUNET_HashCode key;
360 struct GetOperation *get_op;
362 GNUNET_assert (NUM_PEERS == num_peers);
364 monitors = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_MonitorHandle *));
365 for (i = 0; i < num_peers; i++)
366 monitors[i] = GNUNET_DHT_monitor_start (dhts[i],
367 GNUNET_BLOCK_TYPE_ANY,
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374 "Peers setup, starting test\n");
375 put_task = GNUNET_SCHEDULER_add_now (&do_puts, dhts);
376 for (i=0;i<num_peers;i++)
378 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
379 for (j=0;j<num_peers;j++)
381 get_op = GNUNET_new (struct GetOperation);
382 GNUNET_CONTAINER_DLL_insert (get_head,
385 get_op->get = GNUNET_DHT_get_start (dhts[j],
386 GNUNET_BLOCK_TYPE_TEST, /* type */
387 &key, /*key to search */
388 4U, /* replication level */
389 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
392 &dht_get_handler, get_op);
395 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
396 &shutdown_task, ctx);
404 main (int xargc, char *xargv[])
406 GNUNET_DHT_TEST_run ("test-dht-monitor",
407 "test_dht_monitor.conf",
414 /* end of test_dht_monitor.c */