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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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.
49 struct GetOperation *next;
54 struct GetOperation *prev;
57 * Handle for the operation.
59 struct GNUNET_DHT_GetHandle *get;
64 * Return value from 'main'.
69 * Head of list of active GET operations.
71 static struct GetOperation *get_head;
74 * Tail of list of active GET operations.
76 static struct GetOperation *get_tail;
79 * Array of the testbed's peers.
81 static struct GNUNET_TESTBED_Peer **my_peers;
84 * Number of peers to run.
86 static unsigned int NUM_PEERS = 3;
89 * Task called to disconnect peers.
91 static struct GNUNET_SCHEDULER_Task *timeout_task;
96 static struct GNUNET_SCHEDULER_Task * put_task;
98 static struct GNUNET_DHT_MonitorHandle **monitors;
100 static unsigned int monitor_counter;
104 * Task run on success or timeout to clean up.
105 * Terminates active get operations and shuts down
108 * @param cls the `struct GNUNET_DHT_TEST_Context`
111 shutdown_task(void *cls)
113 struct GNUNET_DHT_TEST_Context *ctx = cls;
115 struct GetOperation *get_op;
117 ok = (monitor_counter > NUM_PEERS) ? 0 : 2;
118 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
119 "Received %u monitor events\n",
121 while (NULL != (get_op = get_tail))
123 GNUNET_DHT_get_stop(get_op->get);
124 GNUNET_CONTAINER_DLL_remove(get_head,
129 for (i = 0; i < NUM_PEERS; i++)
130 GNUNET_DHT_monitor_stop(monitors[i]);
131 GNUNET_free(monitors);
132 GNUNET_SCHEDULER_cancel(put_task);
133 GNUNET_DHT_TEST_cleanup(ctx);
134 if (NULL != timeout_task)
136 GNUNET_SCHEDULER_cancel(timeout_task);
143 * Task run on success or timeout to clean up.
144 * Terminates active get operations and shuts down
150 timeout_task_cb(void *cls)
153 GNUNET_SCHEDULER_shutdown();
158 * Iterator called on each result obtained for a DHT
159 * operation that expects a reply
161 * @param cls closure with our 'struct GetOperation'
162 * @param exp when will this value expire
163 * @param key key of the result
164 * @param get_path peers on reply path (or NULL if not recorded)
165 * @param get_path_length number of entries in get_path
166 * @param put_path peers on the PUT path (or NULL if not recorded)
167 * @param put_path_length number of entries in get_path
168 * @param type type of the result
169 * @param size number of bytes in data
170 * @param data pointer to the result data
173 dht_get_handler(void *cls, struct GNUNET_TIME_Absolute exp,
174 const struct GNUNET_HashCode * key,
175 const struct GNUNET_PeerIdentity *get_path,
176 unsigned int get_path_length,
177 const struct GNUNET_PeerIdentity *put_path,
178 unsigned int put_path_length,
179 enum GNUNET_BLOCK_Type type,
180 size_t size, const void *data)
182 struct GetOperation *get_op = cls;
183 struct GNUNET_HashCode want;
185 if (sizeof(struct GNUNET_HashCode) != size)
190 GNUNET_CRYPTO_hash(key, sizeof(*key), &want);
191 if (0 != memcmp(&want, data, sizeof(want)))
196 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
198 GNUNET_DHT_get_stop(get_op->get);
199 GNUNET_CONTAINER_DLL_remove(get_head,
203 if (NULL != get_head)
205 /* all DHT GET operations successful; terminate! */
207 GNUNET_SCHEDULER_shutdown();
212 * Task to put the id of each peer into the DHT.
214 * @param cls array with NUM_PEERS DHT handles
219 struct GNUNET_DHT_Handle **hs = cls;
220 struct GNUNET_HashCode key;
221 struct GNUNET_HashCode value;
223 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
224 "Putting values into DHT\n");
225 for (unsigned int i = 0; i < NUM_PEERS; i++)
227 GNUNET_CRYPTO_hash(&i, sizeof(i), &key);
228 GNUNET_CRYPTO_hash(&key, sizeof(key), &value);
229 GNUNET_DHT_put(hs[i], &key, 10U,
230 GNUNET_DHT_RO_RECORD_ROUTE |
231 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
232 GNUNET_BLOCK_TYPE_TEST,
233 sizeof(value), &value,
234 GNUNET_TIME_UNIT_FOREVER_ABS,
237 put_task = GNUNET_SCHEDULER_add_delayed(PUT_FREQUENCY,
243 * Callback called on each GET request going through the DHT.
244 * Prints the info about the intercepted packet and increments a counter.
246 * @param cls Closure.
247 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
248 * @param type The type of data in the request.
249 * @param hop_count Hop count so far.
250 * @param path_length number of entries in path (or 0 if not recorded).
251 * @param path peers on the GET path (or NULL if not recorded).
252 * @param desired_replication_level Desired replication level.
253 * @param key Key of the requested data.
256 monitor_get_cb(void *cls,
257 enum GNUNET_DHT_RouteOption options,
258 enum GNUNET_BLOCK_Type type,
260 uint32_t desired_replication_level,
261 unsigned int path_length,
262 const struct GNUNET_PeerIdentity *path,
263 const struct GNUNET_HashCode * key)
267 i = (unsigned int)(long)cls;
268 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
269 "%u got a GET message for key %s\n",
277 * Callback called on each PUT request going through the DHT.
278 * Prints the info about the intercepted packet and increments a counter.
280 * @param cls Closure.
281 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
282 * @param type The type of data in the request.
283 * @param hop_count Hop count so far.
284 * @param path_length number of entries in path (or 0 if not recorded).
285 * @param path peers on the PUT path (or NULL if not recorded).
286 * @param desired_replication_level Desired replication level.
287 * @param exp Expiration time of the data.
288 * @param key Key under which data is to be stored.
289 * @param data Pointer to the data carried.
290 * @param size Number of bytes in data.
293 monitor_put_cb(void *cls,
294 enum GNUNET_DHT_RouteOption options,
295 enum GNUNET_BLOCK_Type type,
297 uint32_t desired_replication_level,
298 unsigned int path_length,
299 const struct GNUNET_PeerIdentity *path,
300 struct GNUNET_TIME_Absolute exp,
301 const struct GNUNET_HashCode * key,
307 i = (unsigned int)(long)cls;
308 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
309 "%u got a PUT message for key %s with %u bytes\n",
318 * Callback called on each GET reply going through the DHT.
319 * Prints the info about the intercepted packet and increments a counter.
321 * @param cls Closure.
322 * @param type The type of data in the result.
323 * @param get_path Peers on GET path (or NULL if not recorded).
324 * @param get_path_length number of entries in get_path.
325 * @param put_path peers on the PUT path (or NULL if not recorded).
326 * @param put_path_length number of entries in get_path.
327 * @param exp Expiration time of the data.
328 * @param key Key of the data.
329 * @param data Pointer to the result data.
330 * @param size Number of bytes in data.
333 monitor_res_cb(void *cls,
334 enum GNUNET_BLOCK_Type type,
335 const struct GNUNET_PeerIdentity *get_path,
336 unsigned int get_path_length,
337 const struct GNUNET_PeerIdentity *put_path,
338 unsigned int put_path_length,
339 struct GNUNET_TIME_Absolute exp,
340 const struct GNUNET_HashCode * key,
346 i = (unsigned int)(long)cls;
347 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
348 "%u got a REPLY message for key %s with %u bytes\n",
357 * Main function of the test.
359 * @param cls closure (NULL)
360 * @param ctx argument to give to GNUNET_DHT_TEST_cleanup on test end
361 * @param num_peers number of peers that are running
362 * @param peers array of peers
363 * @param dhts handle to each of the DHTs of the peers
367 struct GNUNET_DHT_TEST_Context *ctx,
368 unsigned int num_peers,
369 struct GNUNET_TESTBED_Peer **peers,
370 struct GNUNET_DHT_Handle **dhts)
374 struct GNUNET_HashCode key;
375 struct GetOperation *get_op;
377 GNUNET_assert(NUM_PEERS == num_peers);
379 monitors = GNUNET_new_array(num_peers,
380 struct GNUNET_DHT_MonitorHandle *);
381 for (i = 0; i < num_peers; i++)
382 monitors[i] = GNUNET_DHT_monitor_start(dhts[i],
383 GNUNET_BLOCK_TYPE_ANY,
389 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
390 "Peers setup, starting test\n");
391 put_task = GNUNET_SCHEDULER_add_now(&do_puts, dhts);
392 for (i = 0; i < num_peers; i++)
394 GNUNET_CRYPTO_hash(&i, sizeof(i), &key);
395 for (j = 0; j < num_peers; j++)
397 get_op = GNUNET_new(struct GetOperation);
398 GNUNET_CONTAINER_DLL_insert(get_head,
401 get_op->get = GNUNET_DHT_get_start(dhts[j],
402 GNUNET_BLOCK_TYPE_TEST, /* type */
403 &key, /*key to search */
404 4U, /* replication level */
405 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
408 &dht_get_handler, get_op);
411 timeout_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,
414 GNUNET_SCHEDULER_add_shutdown(&shutdown_task,
423 main(int xargc, char *xargv[])
425 GNUNET_DHT_TEST_run("test-dht-monitor",
426 "test_dht_monitor.conf",
433 /* end of test_dht_monitor.c */