2 This file is part of GNUnet.
3 Copyright (C) 2011, 2012 Christian Grothoff (and other contributing authors)
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'
111 * @param tc scheduler context
114 shutdown_task (void *cls,
115 const struct GNUNET_SCHEDULER_TaskContext *tc)
117 struct GNUNET_DHT_TEST_Context *ctx = cls;
119 struct GetOperation *get_op;
121 ok = (monitor_counter > NUM_PEERS) ? 0 : 2;
122 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
123 "Received %u monitor events\n",
125 while (NULL != (get_op = get_tail))
127 GNUNET_DHT_get_stop (get_op->get);
128 GNUNET_CONTAINER_DLL_remove (get_head,
131 GNUNET_free (get_op);
133 for (i=0;i<NUM_PEERS;i++)
134 GNUNET_DHT_monitor_stop (monitors[i]);
135 GNUNET_free (monitors);
136 GNUNET_SCHEDULER_cancel (put_task);
137 GNUNET_DHT_TEST_cleanup (ctx);
142 * Iterator called on each result obtained for a DHT
143 * operation that expects a reply
145 * @param cls closure with our 'struct GetOperation'
146 * @param exp when will this value expire
147 * @param key key of the result
148 * @param get_path peers on reply path (or NULL if not recorded)
149 * @param get_path_length number of entries in get_path
150 * @param put_path peers on the PUT path (or NULL if not recorded)
151 * @param put_path_length number of entries in get_path
152 * @param type type of the result
153 * @param size number of bytes in data
154 * @param data pointer to the result data
157 dht_get_handler (void *cls, struct GNUNET_TIME_Absolute exp,
158 const struct GNUNET_HashCode * key,
159 const struct GNUNET_PeerIdentity *get_path,
160 unsigned int get_path_length,
161 const struct GNUNET_PeerIdentity *put_path,
162 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
163 size_t size, const void *data)
165 struct GetOperation *get_op = cls;
166 struct GNUNET_HashCode want;
167 struct GNUNET_DHT_TestContext *ctx;
169 if (sizeof (struct GNUNET_HashCode) != size)
174 GNUNET_CRYPTO_hash (key, sizeof (*key), &want);
175 if (0 != memcmp (&want, data, sizeof (want)))
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
182 GNUNET_DHT_get_stop (get_op->get);
183 GNUNET_CONTAINER_DLL_remove (get_head,
186 GNUNET_free (get_op);
187 if (NULL != get_head)
189 /* all DHT GET operations successful; terminate! */
191 ctx = GNUNET_SCHEDULER_cancel (timeout_task);
192 timeout_task = GNUNET_SCHEDULER_add_now (&shutdown_task, ctx);
197 * Task to put the id of each peer into the DHT.
199 * @param cls array with NUM_PEERS DHT handles
200 * @param tc Task context
204 const struct GNUNET_SCHEDULER_TaskContext *tc)
206 struct GNUNET_DHT_Handle **hs = cls;
207 struct GNUNET_HashCode key;
208 struct GNUNET_HashCode value;
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Putting values into DHT\n");
213 for (i = 0; i < NUM_PEERS; i++)
215 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
216 GNUNET_CRYPTO_hash (&key, sizeof (key), &value);
217 GNUNET_DHT_put (hs[i], &key, 10U,
218 GNUNET_DHT_RO_RECORD_ROUTE |
219 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
220 GNUNET_BLOCK_TYPE_TEST,
221 sizeof (value), &value,
222 GNUNET_TIME_UNIT_FOREVER_ABS,
223 GNUNET_TIME_UNIT_FOREVER_REL,
226 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
232 * Callback called on each GET request going through the DHT.
233 * Prints the info about the intercepted packet and increments a counter.
235 * @param cls Closure.
236 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
237 * @param type The type of data in the request.
238 * @param hop_count Hop count so far.
239 * @param path_length number of entries in path (or 0 if not recorded).
240 * @param path peers on the GET path (or NULL if not recorded).
241 * @param desired_replication_level Desired replication level.
242 * @param key Key of the requested data.
245 monitor_get_cb (void *cls,
246 enum GNUNET_DHT_RouteOption options,
247 enum GNUNET_BLOCK_Type type,
249 uint32_t desired_replication_level,
250 unsigned int path_length,
251 const struct GNUNET_PeerIdentity *path,
252 const struct GNUNET_HashCode * key)
256 i = (unsigned int) (long) cls;
257 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
258 "%u got a GET message for key %s\n",
266 * Callback called on each PUT request going through the DHT.
267 * Prints the info about the intercepted packet and increments a counter.
269 * @param cls Closure.
270 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
271 * @param type The type of data in the request.
272 * @param hop_count Hop count so far.
273 * @param path_length number of entries in path (or 0 if not recorded).
274 * @param path peers on the PUT path (or NULL if not recorded).
275 * @param desired_replication_level Desired replication level.
276 * @param exp Expiration time of the data.
277 * @param key Key under which data is to be stored.
278 * @param data Pointer to the data carried.
279 * @param size Number of bytes in data.
282 monitor_put_cb (void *cls,
283 enum GNUNET_DHT_RouteOption options,
284 enum GNUNET_BLOCK_Type type,
286 uint32_t desired_replication_level,
287 unsigned int path_length,
288 const struct GNUNET_PeerIdentity *path,
289 struct GNUNET_TIME_Absolute exp,
290 const struct GNUNET_HashCode * key,
296 i = (unsigned int) (long) cls;
297 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
298 "%u got a PUT message for key %s with %u bytes\n",
300 GNUNET_h2s (key), size);
306 * Callback called on each GET reply going through the DHT.
307 * Prints the info about the intercepted packet and increments a counter.
309 * @param cls Closure.
310 * @param type The type of data in the result.
311 * @param get_path Peers on GET path (or NULL if not recorded).
312 * @param get_path_length number of entries in get_path.
313 * @param put_path peers on the PUT path (or NULL if not recorded).
314 * @param put_path_length number of entries in get_path.
315 * @param exp Expiration time of the data.
316 * @param key Key of the data.
317 * @param data Pointer to the result data.
318 * @param size Number of bytes in data.
321 monitor_res_cb (void *cls,
322 enum GNUNET_BLOCK_Type type,
323 const struct GNUNET_PeerIdentity *get_path,
324 unsigned int get_path_length,
325 const struct GNUNET_PeerIdentity *put_path,
326 unsigned int put_path_length,
327 struct GNUNET_TIME_Absolute exp,
328 const struct GNUNET_HashCode * key,
334 i = (unsigned int) (long) cls;
335 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
336 "%u got a REPLY message for key %s with %u bytes\n",
338 GNUNET_h2s (key), size);
344 * Main function of the test.
346 * @param cls closure (NULL)
347 * @param ctx argument to give to GNUNET_DHT_TEST_cleanup on test end
348 * @param num_peers number of peers that are running
349 * @param peers array of peers
350 * @param dhts handle to each of the DHTs of the peers
354 struct GNUNET_DHT_TEST_Context *ctx,
355 unsigned int num_peers,
356 struct GNUNET_TESTBED_Peer **peers,
357 struct GNUNET_DHT_Handle **dhts)
361 struct GNUNET_HashCode key;
362 struct GetOperation *get_op;
364 GNUNET_assert (NUM_PEERS == num_peers);
366 monitors = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_MonitorHandle *));
367 for (i = 0; i < num_peers; i++)
368 monitors[i] = GNUNET_DHT_monitor_start (dhts[i],
369 GNUNET_BLOCK_TYPE_ANY,
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Peers setup, starting test\n");
377 put_task = GNUNET_SCHEDULER_add_now (&do_puts, dhts);
378 for (i=0;i<num_peers;i++)
380 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
381 for (j=0;j<num_peers;j++)
383 get_op = GNUNET_new (struct GetOperation);
384 GNUNET_CONTAINER_DLL_insert (get_head,
387 get_op->get = GNUNET_DHT_get_start (dhts[j],
388 GNUNET_BLOCK_TYPE_TEST, /* type */
389 &key, /*key to search */
390 4U, /* replication level */
391 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
394 &dht_get_handler, get_op);
397 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
398 &shutdown_task, ctx);
406 main (int xargc, char *xargv[])
408 GNUNET_DHT_TEST_run ("test-dht-monitor",
409 "test_dht_monitor.conf",
416 /* end of test_dht_monitor.c */