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 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.
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      Affero General Public License for more details.
14     
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/>.
17 */
18 /**
19  * @file dht/test_dht_monitor.c
20  * @brief Test for the dht monitoring API; checks that we receive "some" monitor events
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_testbed_service.h"
25 #include "gnunet_dht_service.h"
26 #include "dht_test_lib.h"
27
28
29 /**
30  * How long do we run the test at most?
31  */
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
33
34 /**
35  * How often do we run the PUTs?
36  */
37 #define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
38
39
40 /**
41  * Information we keep for each GET operation.
42  */
43 struct GetOperation
44 {
45   /**
46    * DLL.
47    */
48   struct GetOperation *next;
49
50   /**
51    * DLL.
52    */
53   struct GetOperation *prev;
54
55   /**
56    * Handle for the operation.
57    */
58   struct GNUNET_DHT_GetHandle *get;
59
60 };
61
62
63 /**
64  * Return value from 'main'.
65  */
66 static int ok;
67
68 /**
69  * Head of list of active GET operations.
70  */
71 static struct GetOperation *get_head;
72
73 /**
74  * Tail of list of active GET operations.
75  */
76 static struct GetOperation *get_tail;
77
78 /**
79  * Array of the testbed's peers.
80  */
81 static struct GNUNET_TESTBED_Peer **my_peers;
82
83 /**
84  * Number of peers to run.
85  */
86 static unsigned int NUM_PEERS = 3;
87
88 /**
89  * Task called to disconnect peers.
90  */
91 static struct GNUNET_SCHEDULER_Task *timeout_task;
92
93 /**
94  * Task to do DHT_puts
95  */
96 static struct GNUNET_SCHEDULER_Task * put_task;
97
98 static struct GNUNET_DHT_MonitorHandle **monitors;
99
100 static unsigned int monitor_counter;
101
102
103 /**
104  * Task run on success or timeout to clean up.
105  * Terminates active get operations and shuts down
106  * the testbed.
107  *
108  * @param cls the `struct GNUNET_DHT_TEST_Context`
109  */
110 static void
111 shutdown_task (void *cls)
112 {
113   struct GNUNET_DHT_TEST_Context *ctx = cls;
114   unsigned int i;
115   struct GetOperation *get_op;
116
117   ok = (monitor_counter > NUM_PEERS) ? 0 : 2;
118   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
119               "Received %u monitor events\n",
120               monitor_counter);
121   while (NULL != (get_op = get_tail))
122   {
123     GNUNET_DHT_get_stop (get_op->get);
124     GNUNET_CONTAINER_DLL_remove (get_head,
125                                  get_tail,
126                                  get_op);
127     GNUNET_free (get_op);
128   }
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)
135   {
136     GNUNET_SCHEDULER_cancel (timeout_task);
137     timeout_task = NULL;
138   }
139 }
140
141
142 /**
143  * Task run on success or timeout to clean up.
144  * Terminates active get operations and shuts down
145  * the testbed.
146  *
147  * @param cls NULL
148  */
149 static void
150 timeout_task_cb (void *cls)
151 {
152   timeout_task = NULL;
153   GNUNET_SCHEDULER_shutdown ();
154 }
155
156
157 /**
158  * Iterator called on each result obtained for a DHT
159  * operation that expects a reply
160  *
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
171  */
172 static void
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)
181 {
182   struct GetOperation *get_op = cls;
183   struct GNUNET_HashCode want;
184
185   if (sizeof (struct GNUNET_HashCode) != size)
186   {
187     GNUNET_break (0);
188     return;
189   }
190   GNUNET_CRYPTO_hash (key, sizeof (*key), &want);
191   if (0 != memcmp (&want, data, sizeof (want)))
192   {
193     GNUNET_break (0);
194     return;
195   }
196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197               "Get successful\n");
198   GNUNET_DHT_get_stop (get_op->get);
199   GNUNET_CONTAINER_DLL_remove (get_head,
200                                get_tail,
201                                get_op);
202   GNUNET_free (get_op);
203   if (NULL != get_head)
204     return;
205   /* all DHT GET operations successful; terminate! */
206   ok = 0;
207   GNUNET_SCHEDULER_shutdown ();
208 }
209
210
211 /**
212  * Task to put the id of each peer into the DHT.
213  *
214  * @param cls array with NUM_PEERS DHT handles
215  */
216 static void
217 do_puts (void *cls)
218 {
219   struct GNUNET_DHT_Handle **hs = cls;
220   struct GNUNET_HashCode key;
221   struct GNUNET_HashCode value;
222
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224               "Putting values into DHT\n");
225   for (unsigned int i = 0; i < NUM_PEERS; i++)
226   {
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,
235                     NULL, NULL);
236   }
237   put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
238                                            &do_puts, hs);
239 }
240
241
242 /**
243  * Callback called on each GET request going through the DHT.
244  * Prints the info about the intercepted packet and increments a counter.
245  *
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.
254  */
255 static void
256 monitor_get_cb (void *cls,
257                 enum GNUNET_DHT_RouteOption options,
258                 enum GNUNET_BLOCK_Type type,
259                 uint32_t hop_count,
260                 uint32_t desired_replication_level,
261                 unsigned int path_length,
262                 const struct GNUNET_PeerIdentity *path,
263                 const struct GNUNET_HashCode * key)
264 {
265   unsigned int i;
266
267   i = (unsigned int) (long) cls;
268   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
269               "%u got a GET message for key %s\n",
270               i,
271               GNUNET_h2s (key));
272   monitor_counter++;
273 }
274
275
276 /**
277  * Callback called on each PUT request going through the DHT.
278  * Prints the info about the intercepted packet and increments a counter.
279  *
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.
291  */
292 static void
293 monitor_put_cb (void *cls,
294                 enum GNUNET_DHT_RouteOption options,
295                 enum GNUNET_BLOCK_Type type,
296                 uint32_t hop_count,
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,
302                 const void *data,
303                 size_t size)
304 {
305   unsigned int i;
306
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",
310               i,
311               GNUNET_h2s (key),
312               (unsigned int) size);
313   monitor_counter++;
314 }
315
316
317 /**
318  * Callback called on each GET reply going through the DHT.
319  * Prints the info about the intercepted packet and increments a counter.
320  *
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.
331  */
332 static void
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,
341                 const void *data,
342                 size_t size)
343 {
344   unsigned int i;
345
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",
349               i,
350               GNUNET_h2s (key),
351               (unsigned int) size);
352   monitor_counter++;
353 }
354
355
356 /**
357  * Main function of the test.
358  *
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
364  */
365 static void
366 run (void *cls,
367      struct GNUNET_DHT_TEST_Context *ctx,
368      unsigned int num_peers,
369      struct GNUNET_TESTBED_Peer **peers,
370      struct GNUNET_DHT_Handle **dhts)
371 {
372   unsigned int i;
373   unsigned int j;
374   struct GNUNET_HashCode key;
375   struct GetOperation *get_op;
376
377   GNUNET_assert (NUM_PEERS == num_peers);
378   my_peers = 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,
384                                             NULL,
385                                             &monitor_get_cb,
386                                             &monitor_res_cb,
387                                             &monitor_put_cb,
388                                             (void *)(long)i);
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++)
393   {
394     GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
395     for (j=0;j<num_peers;j++)
396     {
397       get_op = GNUNET_new (struct GetOperation);
398       GNUNET_CONTAINER_DLL_insert (get_head,
399                                    get_tail,
400                                    get_op);
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,
406                                           NULL,        /* xquery */
407                                           0,      /* xquery bits */
408                                           &dht_get_handler, get_op);
409     }
410   }
411   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
412                                                &timeout_task_cb,
413                                                NULL);
414   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
415                                  ctx);
416 }
417
418
419 /**
420  * Main: start test
421  */
422 int
423 main (int xargc, char *xargv[])
424 {
425   GNUNET_DHT_TEST_run ("test-dht-monitor",
426                        "test_dht_monitor.conf",
427                        NUM_PEERS,
428                        &run, NULL);
429   return ok;
430 }
431
432
433 /* end of test_dht_monitor.c */