-fix off-by-1
[oweals/gnunet.git] / src / dht / test_dht_monitor.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011, 2012 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
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
24  */
25 #include "platform.h"
26 #include "gnunet_testbed_service.h"
27 #include "gnunet_dht_service.h"
28 #include "dht_test_lib.h"
29
30
31 /**
32  * How long do we run the test at most?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
35
36 /**
37  * How often do we run the PUTs?
38  */
39 #define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
40
41
42 /**
43  * Information we keep for each GET operation.
44  */
45 struct GetOperation
46 {
47   /**
48    * DLL.
49    */
50   struct GetOperation *next;
51
52   /**
53    * DLL.
54    */
55   struct GetOperation *prev;
56
57   /**
58    * Handle for the operation.
59    */
60   struct GNUNET_DHT_GetHandle *get;
61
62 };
63
64
65 /**
66  * Return value from 'main'.
67  */
68 static int ok;
69
70 /**
71  * Head of list of active GET operations.
72  */
73 static struct GetOperation *get_head;
74
75 /**
76  * Tail of list of active GET operations.
77  */
78 static struct GetOperation *get_tail;
79
80 /**
81  * Array of the testbed's peers.
82  */
83 static struct GNUNET_TESTBED_Peer **my_peers;
84
85 /**
86  * Number of peers to run.
87  */
88 static unsigned int NUM_PEERS = 3;
89
90 /**
91  * Task called to disconnect peers.
92  */
93 static struct GNUNET_SCHEDULER_Task * timeout_task;
94
95 /**
96  * Task to do DHT_puts
97  */
98 static struct GNUNET_SCHEDULER_Task * put_task;
99
100 static struct GNUNET_DHT_MonitorHandle **monitors;
101
102 static unsigned int monitor_counter;
103
104
105 /**
106  * Task run on success or timeout to clean up.
107  * Terminates active get operations and shuts down
108  * the testbed.
109  *
110  * @param cls the 'struct GNUNET_DHT_TestContext'
111  */
112 static void
113 shutdown_task (void *cls)
114 {
115   struct GNUNET_DHT_TEST_Context *ctx = cls;
116   unsigned int i;
117   struct GetOperation *get_op;
118
119   ok = (monitor_counter > NUM_PEERS) ? 0 : 2;
120   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
121               "Received %u monitor events\n",
122               monitor_counter);
123   while (NULL != (get_op = get_tail))
124   {
125     GNUNET_DHT_get_stop (get_op->get);
126     GNUNET_CONTAINER_DLL_remove (get_head,
127                                  get_tail,
128                                  get_op);
129     GNUNET_free (get_op);
130   }
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 }
137
138
139 /**
140  * Iterator called on each result obtained for a DHT
141  * operation that expects a reply
142  *
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
153  */
154 static void
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)
162 {
163   struct GetOperation *get_op = cls;
164   struct GNUNET_HashCode want;
165   struct GNUNET_DHT_TestContext *ctx;
166
167   if (sizeof (struct GNUNET_HashCode) != size)
168   {
169     GNUNET_break (0);
170     return;
171   }
172   GNUNET_CRYPTO_hash (key, sizeof (*key), &want);
173   if (0 != memcmp (&want, data, sizeof (want)))
174   {
175     GNUNET_break (0);
176     return;
177   }
178   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179               "Get successful\n");
180   GNUNET_DHT_get_stop (get_op->get);
181   GNUNET_CONTAINER_DLL_remove (get_head,
182                                get_tail,
183                                get_op);
184   GNUNET_free (get_op);
185   if (NULL != get_head)
186     return;
187   /* all DHT GET operations successful; terminate! */
188   ok = 0;
189   ctx = GNUNET_SCHEDULER_cancel (timeout_task);
190   timeout_task = GNUNET_SCHEDULER_add_now (&shutdown_task, ctx);
191 }
192
193
194 /**
195  * Task to put the id of each peer into the DHT.
196  *
197  * @param cls array with NUM_PEERS DHT handles
198  */
199 static void
200 do_puts (void *cls)
201 {
202   struct GNUNET_DHT_Handle **hs = cls;
203   struct GNUNET_HashCode key;
204   struct GNUNET_HashCode value;
205   unsigned int i;
206
207   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208               "Putting values into DHT\n");
209   for (i = 0; i < NUM_PEERS; i++)
210   {
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,
220                     NULL, NULL);
221   }
222   put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
223                                            &do_puts, hs);
224 }
225
226
227 /**
228  * Callback called on each GET request going through the DHT.
229  * Prints the info about the intercepted packet and increments a counter.
230  *
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.
239  */
240 static void
241 monitor_get_cb (void *cls,
242                 enum GNUNET_DHT_RouteOption options,
243                 enum GNUNET_BLOCK_Type type,
244                 uint32_t hop_count,
245                 uint32_t desired_replication_level,
246                 unsigned int path_length,
247                 const struct GNUNET_PeerIdentity *path,
248                 const struct GNUNET_HashCode * key)
249 {
250   unsigned int i;
251
252   i = (unsigned int) (long) cls;
253   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
254               "%u got a GET message for key %s\n",
255               i,
256               GNUNET_h2s (key));
257   monitor_counter++;
258 }
259
260
261 /**
262  * Callback called on each PUT request going through the DHT.
263  * Prints the info about the intercepted packet and increments a counter.
264  *
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.
276  */
277 static void
278 monitor_put_cb (void *cls,
279                 enum GNUNET_DHT_RouteOption options,
280                 enum GNUNET_BLOCK_Type type,
281                 uint32_t hop_count,
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,
287                 const void *data,
288                 size_t size)
289 {
290   unsigned int i;
291
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",
295               i,
296               GNUNET_h2s (key),
297               (unsigned int) size);
298   monitor_counter++;
299 }
300
301
302 /**
303  * Callback called on each GET reply going through the DHT.
304  * Prints the info about the intercepted packet and increments a counter.
305  *
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.
316  */
317 static void
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,
326                 const void *data,
327                 size_t size)
328 {
329   unsigned int i;
330
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",
334               i,
335               GNUNET_h2s (key),
336               (unsigned int) size);
337   monitor_counter++;
338 }
339
340
341 /**
342  * Main function of the test.
343  *
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
349  */
350 static void
351 run (void *cls,
352      struct GNUNET_DHT_TEST_Context *ctx,
353      unsigned int num_peers,
354      struct GNUNET_TESTBED_Peer **peers,
355      struct GNUNET_DHT_Handle **dhts)
356 {
357   unsigned int i;
358   unsigned int j;
359   struct GNUNET_HashCode key;
360   struct GetOperation *get_op;
361
362   GNUNET_assert (NUM_PEERS == num_peers);
363   my_peers = 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,
368                                             NULL,
369                                             &monitor_get_cb,
370                                             &monitor_res_cb,
371                                             &monitor_put_cb,
372                                             (void *)(long)i);
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++)
377   {
378     GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
379     for (j=0;j<num_peers;j++)
380     {
381       get_op = GNUNET_new (struct GetOperation);
382       GNUNET_CONTAINER_DLL_insert (get_head,
383                                    get_tail,
384                                    get_op);
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,
390                                           NULL,        /* xquery */
391                                           0,      /* xquery bits */
392                                           &dht_get_handler, get_op);
393     }
394   }
395   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
396                                                &shutdown_task, ctx);
397 }
398
399
400 /**
401  * Main: start test
402  */
403 int
404 main (int xargc, char *xargv[])
405 {
406   GNUNET_DHT_TEST_run ("test-dht-monitor",
407                        "test_dht_monitor.conf",
408                        NUM_PEERS,
409                        &run, NULL);
410   return ok;
411 }
412
413
414 /* end of test_dht_monitor.c */