-trying to fix #2391: limit connect rate from topology
[oweals/gnunet.git] / src / fs / test_fs_namespace.c
1 /*
2      This file is part of GNUnet.
3      (C) 2005, 2006, 2008, 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 /**
22  * @file fs/test_fs_namespace.c
23  * @brief Test for fs_namespace.c
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_fs_service.h"
30
31 #define VERBOSE GNUNET_NO
32
33 #define START_ARM GNUNET_YES
34
35 static struct PeerContext p1;
36
37 static GNUNET_HashCode nsid;
38
39 static struct GNUNET_FS_Uri *sks_expect_uri;
40
41 static struct GNUNET_FS_Uri *ksk_expect_uri;
42
43 static struct GNUNET_FS_Handle *fs;
44
45 static struct GNUNET_FS_SearchContext *sks_search;
46
47 static struct GNUNET_FS_SearchContext *ksk_search;
48
49 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
50
51 static int update_started;
52
53 static int err;
54
55 struct PeerContext
56 {
57   struct GNUNET_CONFIGURATION_Handle *cfg;
58 #if START_ARM
59   struct GNUNET_OS_Process *arm_proc;
60 #endif
61 };
62
63
64 static void
65 setup_peer (struct PeerContext *p, const char *cfgname)
66 {
67   p->cfg = GNUNET_CONFIGURATION_create ();
68 #if START_ARM
69   p->arm_proc =
70     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
71                                "gnunet-service-arm",
72                                "-c", cfgname, NULL);
73 #endif
74   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
75 }
76
77
78 static void
79 stop_arm (struct PeerContext *p)
80 {
81 #if START_ARM
82   if (NULL != p->arm_proc)
83   {
84     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
85       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
86     if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
87       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
88     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
89                 GNUNET_OS_process_get_pid (p->arm_proc));
90     GNUNET_OS_process_destroy (p->arm_proc);
91     p->arm_proc = NULL;
92   }
93 #endif
94   GNUNET_CONFIGURATION_destroy (p->cfg);
95 }
96
97
98 static void
99 abort_ksk_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
100 {
101   if (ksk_search != NULL)
102   {
103     GNUNET_FS_search_stop (ksk_search);
104     ksk_search = NULL;
105     if (sks_search == NULL)
106     {
107       GNUNET_FS_stop (fs);
108       if (GNUNET_SCHEDULER_NO_TASK != kill_task)
109         GNUNET_SCHEDULER_cancel (kill_task);
110     }
111   }
112 }
113
114
115 static void
116 abort_sks_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 {
118   struct GNUNET_FS_Namespace *ns;
119
120   if (sks_search == NULL)
121     return;
122   GNUNET_FS_search_stop (sks_search);
123   sks_search = NULL;
124   ns = GNUNET_FS_namespace_create (fs, "testNamespace");
125   GNUNET_assert (NULL != ns);
126   GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
127   if (ksk_search == NULL)
128   {
129     GNUNET_FS_stop (fs);
130     if (GNUNET_SCHEDULER_NO_TASK != kill_task)
131       GNUNET_SCHEDULER_cancel (kill_task);
132   }
133 }
134
135
136 static void
137 do_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
138 {
139   FPRINTF (stderr, "%s",  "Operation timed out\n");
140   kill_task = GNUNET_SCHEDULER_NO_TASK;
141   abort_sks_search_task (NULL, tc);
142   abort_ksk_search_task (NULL, tc);
143 }
144
145
146
147 static void *
148 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
149 {
150   switch (event->status)
151   {
152   case GNUNET_FS_STATUS_SEARCH_RESULT:
153     if (sks_search == event->value.search.sc)
154     {
155       if (!GNUNET_FS_uri_test_equal
156           (sks_expect_uri, event->value.search.specifics.result.uri))
157       {
158         FPRINTF (stderr, "%s",  "Wrong result for sks search!\n");
159         err = 1;
160       }
161       /* give system 1ms to initiate update search! */
162       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
163                                     &abort_sks_search_task, NULL);
164     }
165     else if (ksk_search == event->value.search.sc)
166     {
167       if (!GNUNET_FS_uri_test_equal
168           (ksk_expect_uri, event->value.search.specifics.result.uri))
169       {
170         FPRINTF (stderr, "%s",  "Wrong result for ksk search!\n");
171         err = 1;
172       }
173       GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task, NULL,
174                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
175     }
176     else
177     {
178       FPRINTF (stderr, "%s",  "Unexpected search result received!\n");
179       GNUNET_break (0);
180     }
181     break;
182   case GNUNET_FS_STATUS_SEARCH_ERROR:
183     FPRINTF (stderr, "Error searching file: %s\n",
184              event->value.search.specifics.error.message);
185     if (sks_search == event->value.search.sc)
186       GNUNET_SCHEDULER_add_continuation (&abort_sks_search_task, NULL,
187                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
188     else if (ksk_search == event->value.search.sc)
189       GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task, NULL,
190                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
191     else
192       GNUNET_break (0);
193     break;
194   case GNUNET_FS_STATUS_SEARCH_START:
195     GNUNET_assert ((NULL == event->value.search.cctx) ||
196                    (0 == strcmp ("sks_search", event->value.search.cctx)) ||
197                    (0 == strcmp ("ksk_search", event->value.search.cctx)));
198     if (NULL == event->value.search.cctx)
199     {
200       GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
201       update_started = GNUNET_YES;
202     }
203     GNUNET_assert (1 == event->value.search.anonymity);
204     break;
205   case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
206     return NULL;
207   case GNUNET_FS_STATUS_SEARCH_STOPPED:
208     return NULL;
209   default:
210     FPRINTF (stderr, "Unexpected event: %d\n", event->status);
211     break;
212   }
213   return event->value.search.cctx;
214 }
215
216
217 static void
218 publish_cont (void *cls, const struct GNUNET_FS_Uri *ksk_uri, const char *emsg)
219 {
220   char *msg;
221   struct GNUNET_FS_Uri *sks_uri;
222   char sbuf[1024];
223   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
224
225   if (NULL != emsg)
226   {
227     FPRINTF (stderr, "Error publishing: %s\n", emsg);
228     err = 1;
229     GNUNET_FS_stop (fs);
230     return;
231   }
232   GNUNET_CRYPTO_hash_to_enc (&nsid, &enc);
233   GNUNET_snprintf (sbuf, sizeof (sbuf), "gnunet://fs/sks/%s/this", &enc);
234   sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
235   if (NULL == sks_uri)
236   {
237     FPRINTF (stderr, "failed to parse URI `%s': %s\n", sbuf, msg);
238     err = 1;
239     GNUNET_FS_stop (fs);
240     GNUNET_free_non_null (msg);
241     return;
242   }
243   ksk_search =
244       GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
245                               "ksk_search");
246   sks_search =
247       GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
248                               "sks_search");
249   GNUNET_FS_uri_destroy (sks_uri);
250 }
251
252
253 static void
254 sks_cont (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
255 {
256   struct GNUNET_CONTAINER_MetaData *meta;
257   struct GNUNET_FS_Uri *ksk_uri;
258   char *msg;
259   struct GNUNET_FS_BlockOptions bo;
260
261   if (NULL == uri)
262   {
263     fprintf (stderr, "Error publishing: %s\n", emsg);
264     err = 1;
265     GNUNET_FS_stop (fs);
266     return;
267   }
268   meta = GNUNET_CONTAINER_meta_data_create ();
269   msg = NULL;
270   ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
271   GNUNET_assert (NULL == msg);
272   ksk_expect_uri = GNUNET_FS_uri_dup (uri);
273   bo.content_priority = 1;
274   bo.anonymity_level = 1;
275   bo.replication_level = 0;
276   bo.expiration_time =
277       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
278   GNUNET_FS_publish_ksk (fs, ksk_uri, meta, uri, &bo,
279                          GNUNET_FS_PUBLISH_OPTION_NONE, &publish_cont, NULL);
280   GNUNET_FS_uri_destroy (ksk_uri);
281   GNUNET_CONTAINER_meta_data_destroy (meta);
282 }
283
284
285 static void
286 adv_cont (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
287 {
288   struct GNUNET_CONTAINER_MetaData *meta;
289   struct GNUNET_FS_Namespace *ns;
290   struct GNUNET_FS_BlockOptions bo;
291
292   if (NULL != emsg)
293   {
294     FPRINTF (stderr, "Error publishing: %s\n", emsg);
295     err = 1;
296     GNUNET_FS_stop (fs);
297     return;
298   }
299   ns = GNUNET_FS_namespace_create (fs, "testNamespace");
300   GNUNET_assert (NULL != ns);
301   meta = GNUNET_CONTAINER_meta_data_create ();
302   GNUNET_assert (NULL == emsg);
303   sks_expect_uri = GNUNET_FS_uri_dup (uri);
304   bo.content_priority = 1;
305   bo.anonymity_level = 1;
306   bo.replication_level = 0;
307   bo.expiration_time =
308       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
309   GNUNET_FS_publish_sks (fs, ns, "this", "next", meta, uri,     /* FIXME: this is non-sense (use CHK URI!?) */
310                          &bo, GNUNET_FS_PUBLISH_OPTION_NONE, &sks_cont, NULL);
311   GNUNET_CONTAINER_meta_data_destroy (meta);
312   GNUNET_FS_namespace_delete (ns, GNUNET_NO);
313 }
314
315
316 static void
317 ns_iterator (void *cls, const char *name, const GNUNET_HashCode * id)
318 {
319   int *ok = cls;
320
321   if (0 != strcmp (name, "testNamespace"))
322     return;
323   *ok = GNUNET_YES;
324   nsid = *id;
325 }
326
327
328 static void
329 testNamespace ()
330 {
331   struct GNUNET_FS_Namespace *ns;
332   struct GNUNET_FS_BlockOptions bo;
333   struct GNUNET_CONTAINER_MetaData *meta;
334   struct GNUNET_FS_Uri *ksk_uri;
335   int ok;
336
337   ns = GNUNET_FS_namespace_create (fs, "testNamespace");
338   GNUNET_assert (NULL != ns);
339   ok = GNUNET_NO;
340   GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
341   if (GNUNET_NO == ok)
342   {
343     FPRINTF (stderr, "%s",  "namespace_list failed to find namespace!\n");
344     GNUNET_FS_namespace_delete (ns, GNUNET_YES);
345     GNUNET_FS_stop (fs);
346     err = 1;
347     return;
348   }
349   meta = GNUNET_CONTAINER_meta_data_create ();
350   ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
351   bo.content_priority = 1;
352   bo.anonymity_level = 1;
353   bo.replication_level = 0;
354   bo.expiration_time =
355       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
356   GNUNET_FS_namespace_advertise (fs, ksk_uri, ns, meta, &bo, "root", &adv_cont,
357                                  NULL);
358   kill_task =
359       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &do_timeout,
360                                     NULL);
361   GNUNET_FS_uri_destroy (ksk_uri);
362   GNUNET_FS_namespace_delete (ns, GNUNET_NO);
363   GNUNET_CONTAINER_meta_data_destroy (meta);
364 }
365
366
367 static void
368 run (void *cls, char *const *args, const char *cfgfile,
369      const struct GNUNET_CONFIGURATION_Handle *cfg)
370 {
371   setup_peer (&p1, "test_fs_namespace_data.conf");
372   fs = GNUNET_FS_start (cfg, "test-fs-namespace", &progress_cb, NULL,
373                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
374   testNamespace ();
375 }
376
377
378 int
379 main (int argc, char *argv[])
380 {
381   char *const argvx[] = {
382     "test-fs-namespace",
383     "-c",
384     "test_fs_namespace_data.conf",
385     NULL
386   };
387   struct GNUNET_GETOPT_CommandLineOption options[] = {
388     GNUNET_GETOPT_OPTION_END
389   };
390
391   GNUNET_log_setup ("test_fs_namespace",
392                     "WARNING",
393                     NULL);
394   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
395                       "test-fs-namespace", "nohelp", options, &run, NULL);
396   stop_arm (&p1);
397   if (GNUNET_YES != update_started)
398   {
399     FPRINTF (stderr, "%s",  "Update search never started!\n");
400     err = 1;
401   }
402   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
403   return err;
404 }
405
406
407 /* end of test_fs_namespace.c */