Merge branch 'master' of ssh://gnunet.org/gnunet
[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_TEST_Context`
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   if (NULL != timeout_task)
137   {
138     GNUNET_SCHEDULER_cancel (timeout_task);
139     timeout_task = NULL;
140   }
141 }
142
143
144 /**
145  * Task run on success or timeout to clean up.
146  * Terminates active get operations and shuts down
147  * the testbed.
148  *
149  * @param cls NULL
150  */
151 static void
152 timeout_task_cb (void *cls)
153 {
154   timeout_task = NULL;
155   GNUNET_SCHEDULER_shutdown ();
156 }
157
158
159 /**
160  * Iterator called on each result obtained for a DHT
161  * operation that expects a reply
162  *
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
173  */
174 static void
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)
183 {
184   struct GetOperation *get_op = cls;
185   struct GNUNET_HashCode want;
186
187   if (sizeof (struct GNUNET_HashCode) != size)
188   {
189     GNUNET_break (0);
190     return;
191   }
192   GNUNET_CRYPTO_hash (key, sizeof (*key), &want);
193   if (0 != memcmp (&want, data, sizeof (want)))
194   {
195     GNUNET_break (0);
196     return;
197   }
198   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199               "Get successful\n");
200   GNUNET_DHT_get_stop (get_op->get);
201   GNUNET_CONTAINER_DLL_remove (get_head,
202                                get_tail,
203                                get_op);
204   GNUNET_free (get_op);
205   if (NULL != get_head)
206     return;
207   /* all DHT GET operations successful; terminate! */
208   ok = 0;
209   GNUNET_SCHEDULER_shutdown ();
210 }
211
212
213 /**
214  * Task to put the id of each peer into the DHT.
215  *
216  * @param cls array with NUM_PEERS DHT handles
217  */
218 static void
219 do_puts (void *cls)
220 {
221   struct GNUNET_DHT_Handle **hs = cls;
222   struct GNUNET_HashCode key;
223   struct GNUNET_HashCode value;
224
225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226               "Putting values into DHT\n");
227   for (unsigned int i = 0; i < NUM_PEERS; i++)
228   {
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,
237                     NULL, NULL);
238   }
239   put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
240                                            &do_puts, hs);
241 }
242
243
244 /**
245  * Callback called on each GET request going through the DHT.
246  * Prints the info about the intercepted packet and increments a counter.
247  *
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.
256  */
257 static void
258 monitor_get_cb (void *cls,
259                 enum GNUNET_DHT_RouteOption options,
260                 enum GNUNET_BLOCK_Type type,
261                 uint32_t hop_count,
262                 uint32_t desired_replication_level,
263                 unsigned int path_length,
264                 const struct GNUNET_PeerIdentity *path,
265                 const struct GNUNET_HashCode * key)
266 {
267   unsigned int i;
268
269   i = (unsigned int) (long) cls;
270   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
271               "%u got a GET message for key %s\n",
272               i,
273               GNUNET_h2s (key));
274   monitor_counter++;
275 }
276
277
278 /**
279  * Callback called on each PUT request going through the DHT.
280  * Prints the info about the intercepted packet and increments a counter.
281  *
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.
293  */
294 static void
295 monitor_put_cb (void *cls,
296                 enum GNUNET_DHT_RouteOption options,
297                 enum GNUNET_BLOCK_Type type,
298                 uint32_t hop_count,
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,
304                 const void *data,
305                 size_t size)
306 {
307   unsigned int i;
308
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",
312               i,
313               GNUNET_h2s (key),
314               (unsigned int) size);
315   monitor_counter++;
316 }
317
318
319 /**
320  * Callback called on each GET reply going through the DHT.
321  * Prints the info about the intercepted packet and increments a counter.
322  *
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.
333  */
334 static void
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,
343                 const void *data,
344                 size_t size)
345 {
346   unsigned int i;
347
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",
351               i,
352               GNUNET_h2s (key),
353               (unsigned int) size);
354   monitor_counter++;
355 }
356
357
358 /**
359  * Main function of the test.
360  *
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
366  */
367 static void
368 run (void *cls,
369      struct GNUNET_DHT_TEST_Context *ctx,
370      unsigned int num_peers,
371      struct GNUNET_TESTBED_Peer **peers,
372      struct GNUNET_DHT_Handle **dhts)
373 {
374   unsigned int i;
375   unsigned int j;
376   struct GNUNET_HashCode key;
377   struct GetOperation *get_op;
378
379   GNUNET_assert (NUM_PEERS == num_peers);
380   my_peers = 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,
386                                             NULL,
387                                             &monitor_get_cb,
388                                             &monitor_res_cb,
389                                             &monitor_put_cb,
390                                             (void *)(long)i);
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++)
395   {
396     GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
397     for (j=0;j<num_peers;j++)
398     {
399       get_op = GNUNET_new (struct GetOperation);
400       GNUNET_CONTAINER_DLL_insert (get_head,
401                                    get_tail,
402                                    get_op);
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,
408                                           NULL,        /* xquery */
409                                           0,      /* xquery bits */
410                                           &dht_get_handler, get_op);
411     }
412   }
413   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
414                                                &timeout_task_cb,
415                                                NULL);
416   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
417                                  ctx);
418 }
419
420
421 /**
422  * Main: start test
423  */
424 int
425 main (int xargc, char *xargv[])
426 {
427   GNUNET_DHT_TEST_run ("test-dht-monitor",
428                        "test_dht_monitor.conf",
429                        NUM_PEERS,
430                        &run, NULL);
431   return ok;
432 }
433
434
435 /* end of test_dht_monitor.c */