basic find peer functionality, added to test_dht_api testcase
[oweals/gnunet.git] / src / dht / test_dht_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file dht/test_dht_api.c
22  * @brief base test case for dht api
23  *
24  * This test case tests DHT api to DUMMY DHT service communication.
25  *
26  */
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_program_lib.h"
33 #include "gnunet_scheduler_lib.h"
34 #include "gnunet_dht_service.h"
35
36 #define VERBOSE GNUNET_NO
37
38 #define VERBOSE_ARM GNUNET_NO
39
40 #define START_ARM GNUNET_YES
41
42 /**
43  * How long until we give up on transmitting the message?
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 50)
46
47 #define MTYPE 12345
48
49 struct PeerContext
50 {
51   struct GNUNET_CONFIGURATION_Handle *cfg;
52   struct GNUNET_DHT_Handle *dht_handle;
53   struct GNUNET_PeerIdentity id;
54   struct GNUNET_DHT_GetHandle *get_handle;
55   struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
56
57 #if START_ARM
58   pid_t arm_pid;
59 #endif
60 };
61
62 static struct PeerContext p1;
63
64 static struct GNUNET_SCHEDULER_Handle *sched;
65
66 static int ok;
67
68 GNUNET_SCHEDULER_TaskIdentifier die_task;
69
70 #if VERBOSE
71 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
72 #else
73 #define OKPP do { ok++; } while (0)
74 #endif
75
76
77 static void
78 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79 {
80   /* do work here */
81   GNUNET_SCHEDULER_cancel (sched, die_task);
82
83   GNUNET_DHT_disconnect (p1.dht_handle);
84
85   die_task = GNUNET_SCHEDULER_NO_TASK;
86
87   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
88     {
89       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90                   "DHT disconnected, returning FAIL!\n");
91       ok = 365;
92     }
93   else
94     {
95       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96                   "DHT disconnected, returning success!\n");
97       ok = 0;
98     }
99 }
100
101 static void
102 stop_arm (struct PeerContext *p)
103 {
104 #if START_ARM
105   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
106     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
107   GNUNET_OS_process_wait (p->arm_pid);
108 #endif
109   GNUNET_CONFIGURATION_destroy (p->cfg);
110 }
111
112
113 static void
114 end_badly ()
115 {
116   /* do work here */
117 #if VERBOSE
118   fprintf (stderr, "Ending on an unhappy note.\n");
119 #endif
120
121   GNUNET_DHT_disconnect (p1.dht_handle);
122
123   ok = 1;
124   return;
125 }
126
127 /**
128  * Signature of the main function of a task.
129  *
130  * @param cls closure
131  * @param tc context information (why was this task triggered now)
132  */
133 void
134 test_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 {
136   struct PeerContext *peer = cls;
137
138   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer_stop!\n");
139   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
140     GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
141
142   GNUNET_assert (peer->dht_handle != NULL);
143
144   GNUNET_DHT_find_peer_stop (peer->find_peer_handle, &end, &p1);
145
146   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &end, &p1);
147
148 }
149
150
151 /**
152  * Iterator called on each result obtained from a find peer
153  * operation
154  *
155  * @param cls closure (NULL)
156  * @param peer the peer we learned about
157  * @param reply response
158  */
159 void test_find_peer_processor (void *cls,
160                           const struct GNUNET_PeerIdentity *peer,
161                           const struct GNUNET_MessageHeader *reply)
162 {
163
164   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165               "test_find_peer_processor called (peer `%s'), stopping find peer request!\n", GNUNET_i2s(peer));
166
167   GNUNET_SCHEDULER_add_continuation (sched, &test_find_peer_stop, &p1,
168                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
169 }
170
171
172 /**
173  * Signature of the main function of a task.
174  *
175  * @param cls closure
176  * @param tc context information (why was this task triggered now)
177  */
178 void
179 test_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
180 {
181   struct PeerContext *peer = cls;
182   GNUNET_HashCode hash;
183   memset (&hash, 42, sizeof (GNUNET_HashCode));
184
185   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer!\n");
186   GNUNET_assert (peer->dht_handle != NULL);
187
188   peer->find_peer_handle =
189     GNUNET_DHT_find_peer_start (peer->dht_handle, TIMEOUT, 0, NULL, &hash,
190                                 &test_find_peer_processor, NULL, NULL, NULL);
191
192   if (peer->find_peer_handle == NULL)
193     GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
194 }
195
196 /**
197  * Signature of the main function of a task.
198  *
199  * @param cls closure
200  * @param tc context information (why was this task triggered now)
201  */
202 void
203 test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
204 {
205   struct PeerContext *peer = cls;
206
207   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
208   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
209     GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
210
211   GNUNET_assert (peer->dht_handle != NULL);
212
213   GNUNET_DHT_get_stop (peer->get_handle, &test_find_peer, &p1);
214
215   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
216
217 }
218
219 void
220 test_get_iterator (void *cls,
221                    struct GNUNET_TIME_Absolute exp,
222                    const GNUNET_HashCode * key,
223                    uint32_t type, uint32_t size, const void *data)
224 {
225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226               "test_get_iterator called (we got a result), stopping get request!\n");
227
228   GNUNET_SCHEDULER_add_continuation (sched, &test_get_stop, &p1,
229                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
230 }
231
232 /**
233  * Signature of the main function of a task.
234  *
235  * @param cls closure
236  * @param tc context information (why was this task triggered now)
237  */
238 void
239 test_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
240 {
241   struct PeerContext *peer = cls;
242   GNUNET_HashCode hash;
243   memset (&hash, 42, sizeof (GNUNET_HashCode));
244
245   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
246
247   GNUNET_assert (peer->dht_handle != NULL);
248
249   peer->get_handle =
250     GNUNET_DHT_get_start (peer->dht_handle, TIMEOUT, 42, &hash,
251                           &test_get_iterator, NULL, NULL, NULL);
252
253   if (peer->get_handle == NULL)
254     GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
255 }
256
257 /**
258  * Signature of the main function of a task.
259  *
260  * @param cls closure
261  * @param tc context information (why was this task triggered now)
262  */
263 void
264 test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
265 {
266   struct PeerContext *peer = cls;
267   GNUNET_HashCode hash;
268   char *data;
269   size_t data_size = 42;
270   memset (&hash, 42, sizeof (GNUNET_HashCode));
271   data = GNUNET_malloc (data_size);
272   memset (data, 43, data_size);
273   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
274   peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg, 100);
275
276   GNUNET_assert (peer->dht_handle != NULL);
277
278   GNUNET_DHT_put (peer->dht_handle, &hash, 0, data_size, data,
279                   GNUNET_TIME_relative_to_absolute (TIMEOUT), TIMEOUT,
280                   &test_get, &p1);
281
282 }
283
284 static void
285 setup_peer (struct PeerContext *p, const char *cfgname)
286 {
287   p->cfg = GNUNET_CONFIGURATION_create ();
288 #if START_ARM
289   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
290                                         "gnunet-service-arm",
291 #if VERBOSE_ARM
292                                         "-L", "DEBUG",
293 #endif
294                                         "-c", cfgname, NULL);
295 #endif
296   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
297
298 }
299
300 static void
301 run (void *cls,
302      struct GNUNET_SCHEDULER_Handle *s,
303      char *const *args,
304      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
305 {
306   GNUNET_assert (ok == 1);
307   OKPP;
308   sched = s;
309
310   die_task = GNUNET_SCHEDULER_add_delayed (sched,
311                                            GNUNET_TIME_relative_multiply
312                                            (GNUNET_TIME_UNIT_MINUTES, 1),
313                                            &end_badly, NULL);
314
315   setup_peer (&p1, "test_dht_api_peer1.conf");
316
317   GNUNET_SCHEDULER_add_delayed (sched,
318                                 GNUNET_TIME_relative_multiply
319                                 (GNUNET_TIME_UNIT_SECONDS, 1), &test_put,
320                                 &p1);
321 }
322
323 static int
324 check ()
325 {
326
327   char *const argv[] = { "test-dht-api",
328     "-c",
329     "test_dht_api_data.conf",
330 #if VERBOSE
331     "-L", "DEBUG",
332 #endif
333     NULL
334   };
335
336   struct GNUNET_GETOPT_CommandLineOption options[] = {
337     GNUNET_GETOPT_OPTION_END
338   };
339
340   ok = 1;
341   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
342                       argv, "test-dht-api", "nohelp", options, &run, &ok);
343   stop_arm (&p1);
344   return ok;
345 }
346
347
348 int
349 main (int argc, char *argv[])
350 {
351   int ret;
352 #ifdef MINGW
353   return GNUNET_SYSERR;
354 #endif
355
356   GNUNET_log_setup ("test-dht-api",
357 #if VERBOSE
358                     "DEBUG",
359 #else
360                     "WARNING",
361 #endif
362                     NULL);
363   ret = check ();
364
365   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
366
367   return ret;
368 }
369
370 /* end of test_dht_api.c */