nonews
[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 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., 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 #include "gnunet_hello_lib.h"
36
37 #define VERBOSE GNUNET_NO
38
39 #define VERBOSE_ARM GNUNET_NO
40
41 #define START_ARM GNUNET_YES
42
43 /**
44  * How long until we really give up on a particular testcase portion?
45  */
46 #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
47
48 /**
49  * How long until we give up on any particular operation (and retry)?
50  */
51 #define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
52
53 #define MTYPE 12345
54
55 struct RetryContext
56 {
57   /**
58    * When to really abort the operation.
59    */
60   struct GNUNET_TIME_Absolute real_timeout;
61
62   /**
63    * What timeout to set for the current attempt (increases)
64    */
65   struct GNUNET_TIME_Relative next_timeout;
66
67   /**
68    * The context of the peer we are dealing with.
69    */
70   struct PeerContext *peer_ctx;
71
72   /**
73    * The task identifier of the retry task, so it can be cancelled.
74    */
75   GNUNET_SCHEDULER_TaskIdentifier retry_task;
76
77 };
78
79 struct PeerContext
80 {
81   struct GNUNET_CONFIGURATION_Handle *cfg;
82   struct GNUNET_DHT_Handle *dht_handle;
83   struct GNUNET_PeerIdentity id;
84   struct GNUNET_DHT_GetHandle *get_handle;
85 #if START_ARM
86   struct GNUNET_OS_Process *arm_proc;
87 #endif
88 };
89
90 static struct PeerContext p1;
91
92 struct RetryContext retry_context;
93
94
95 static int ok;
96
97 static GNUNET_SCHEDULER_TaskIdentifier die_task;
98
99 #if VERBOSE
100 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
101 #else
102 #define OKPP do { ok++; } while (0)
103 #endif
104
105
106 static void
107 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108 {
109   GNUNET_SCHEDULER_cancel (die_task);
110   die_task = GNUNET_SCHEDULER_NO_TASK;
111   GNUNET_DHT_disconnect (p1.dht_handle);
112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113               "DHT disconnected, returning success!\n");
114   ok = 0;
115 }
116
117 static void
118 stop_arm (struct PeerContext *p)
119 {
120 #if START_ARM
121   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
122     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
123   GNUNET_OS_process_wait (p->arm_proc);
124   GNUNET_OS_process_close (p->arm_proc);
125   p->arm_proc = NULL;
126 #endif
127   GNUNET_CONFIGURATION_destroy (p->cfg);
128 }
129
130
131 static void
132 end_badly ()
133 {
134   /* do work here */
135 #if VERBOSE
136   fprintf (stderr, "Ending on an unhappy note.\n");
137 #endif
138
139   if ((retry_context.peer_ctx != NULL) &&
140       (retry_context.peer_ctx->get_handle != NULL))
141   {
142     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping get request!\n");
143     GNUNET_DHT_get_stop (retry_context.peer_ctx->get_handle);
144   }
145   if (retry_context.retry_task != GNUNET_SCHEDULER_NO_TASK)
146     GNUNET_SCHEDULER_cancel (retry_context.retry_task);
147   GNUNET_DHT_disconnect (p1.dht_handle);
148   ok = 1;
149 }
150
151
152 /**
153  * Signature of the main function of a task.
154  *
155  * @param cls closure
156  * @param tc context information (why was this task triggered now)
157  */
158 static void
159 test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 {
161   struct PeerContext *peer = cls;
162
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
164   if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
165   {
166     GNUNET_break (0);
167     GNUNET_SCHEDULER_cancel (die_task);
168     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
169     return;
170   }
171   GNUNET_assert (peer->dht_handle != NULL);
172   GNUNET_DHT_get_stop (peer->get_handle);
173   peer->get_handle = NULL;
174   GNUNET_SCHEDULER_add_now (&end, &p1);
175 }
176
177
178 static void
179 test_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
180                    const GNUNET_HashCode * key,
181                    const struct GNUNET_PeerIdentity *get_path,
182                    unsigned int get_path_length,
183                    const struct GNUNET_PeerIdentity *put_path,
184                    unsigned int put_path_length,
185                    enum GNUNET_BLOCK_Type type, size_t size, const void *data)
186 {
187   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188               "test_get_iterator called (we got a result), stopping get request!\n");
189
190   GNUNET_SCHEDULER_add_continuation (&test_get_stop, &p1,
191                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
192 }
193
194 /**
195  * Signature of the main function of a task.
196  *
197  * @param cls closure
198  * @param tc context information (why was this task triggered now)
199  */
200 static void
201 test_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
202 {
203   struct PeerContext *peer = cls;
204   GNUNET_HashCode hash;
205
206   memset (&hash, 42, sizeof (GNUNET_HashCode));
207
208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
209
210   GNUNET_assert (peer->dht_handle != NULL);
211   retry_context.real_timeout = GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT);
212   retry_context.next_timeout = BASE_TIMEOUT;
213
214   peer->get_handle =
215       GNUNET_DHT_get_start (peer->dht_handle, TOTAL_TIMEOUT,
216                             GNUNET_BLOCK_TYPE_TEST, &hash,
217                             1, GNUNET_DHT_RO_NONE, NULL,
218                             0, &test_get_iterator, NULL);
219
220   if (peer->get_handle == NULL)
221   {
222     GNUNET_break (0);
223     GNUNET_SCHEDULER_cancel (die_task);
224     die_task = GNUNET_SCHEDULER_add_now (&end_badly, &p1);
225     return;
226   }
227
228   retry_context.peer_ctx = peer;
229 }
230
231 /**
232  * Signature of the main function of a task.
233  *
234  * @param cls closure
235  * @param tc context information (why was this task triggered now)
236  */
237 static void
238 test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
239 {
240   struct PeerContext *peer = cls;
241   GNUNET_HashCode hash;
242   char *data;
243   size_t data_size = 42;
244
245   memset (&hash, 42, sizeof (GNUNET_HashCode));
246   data = GNUNET_malloc (data_size);
247   memset (data, 43, data_size);
248   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
249   peer->dht_handle = GNUNET_DHT_connect (peer->cfg, 100);
250
251   GNUNET_assert (peer->dht_handle != NULL);
252
253   GNUNET_DHT_put (peer->dht_handle, &hash, 1,
254                   GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_TEST, data_size, data,
255                   GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT),
256                   TOTAL_TIMEOUT, &test_get, &p1);
257   GNUNET_free (data);
258 }
259
260 static void
261 setup_peer (struct PeerContext *p, const char *cfgname)
262 {
263   p->cfg = GNUNET_CONFIGURATION_create ();
264 #if START_ARM
265   p->arm_proc =
266       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
267                                "gnunet-service-arm",
268 #if VERBOSE_ARM
269                                "-L", "DEBUG",
270 #endif
271                                "-c", cfgname, NULL);
272 #endif
273   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
274
275 }
276
277 static void
278 run (void *cls, char *const *args, const char *cfgfile,
279      const struct GNUNET_CONFIGURATION_Handle *cfg)
280 {
281   GNUNET_assert (ok == 1);
282   OKPP;
283
284   die_task =
285       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
286                                     (GNUNET_TIME_UNIT_MINUTES, 1), &end_badly,
287                                     NULL);
288
289   setup_peer (&p1, "test_dht_api_peer1.conf");
290
291   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
292                                 (GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
293 }
294
295 static int
296 check ()
297 {
298
299   char *const argv[] = { "test-dht-api",
300     "-c",
301     "test_dht_api_data.conf",
302 #if VERBOSE
303     "-L", "DEBUG",
304 #endif
305     NULL
306   };
307
308   struct GNUNET_GETOPT_CommandLineOption options[] = {
309     GNUNET_GETOPT_OPTION_END
310   };
311
312   ok = 1;
313   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
314                       "test-dht-api", "nohelp", options, &run, &ok);
315   stop_arm (&p1);
316   return ok;
317 }
318
319
320 int
321 main (int argc, char *argv[])
322 {
323   int ret;
324
325   GNUNET_log_setup ("test-dht-api",
326 #if VERBOSE
327                     "DEBUG",
328 #else
329                     "WARNING",
330 #endif
331                     NULL);
332   ret = check ();
333
334   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
335
336   return ret;
337 }
338
339 /* end of test_dht_api.c */