61f304487ecae93100d99028ee3630d1aabcd7a8
[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_testing_lib.h"
29 #include "gnunet_fs_service.h"
30
31
32 static struct GNUNET_HashCode nsid;
33
34 static struct GNUNET_FS_Uri *sks_expect_uri;
35
36 static struct GNUNET_FS_Uri *ksk_expect_uri;
37
38 static struct GNUNET_FS_Handle *fs;
39
40 static struct GNUNET_FS_SearchContext *sks_search;
41
42 static struct GNUNET_FS_SearchContext *ksk_search;
43
44 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
45
46 static GNUNET_SCHEDULER_TaskIdentifier kill_ncc_task;
47
48 struct GNUNET_FS_NamespaceCreationContext *ncc;
49
50 static int update_started;
51
52 static int err;
53
54 static int phase;
55
56 const struct GNUNET_CONFIGURATION_Handle *config;
57
58 static void ns_created (void *cls, struct GNUNET_FS_Namespace *ns, const char *emsg);
59
60 static void do_ncc_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
61
62
63 static void
64 next_phase ()
65 {
66   switch (phase)
67   {
68   case 0:
69     phase += 1;
70     FPRINTF (stderr, "%s",  "Testing asynchronous namespace creation\n");
71     ncc = GNUNET_FS_namespace_create_start (fs, "testNamespace", ns_created, NULL);
72     if (NULL == ncc)
73     {
74       FPRINTF (stderr, "%s",  "Failed to start asynchronous namespace creation\n");
75       err = 1;
76       next_phase ();
77       return;
78     }
79     kill_ncc_task =
80       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &do_ncc_timeout,
81                                     NULL);
82     break;
83   case 1:
84     FPRINTF (stderr, "%s",  "Shutting down FS\n");
85     GNUNET_FS_stop (fs);
86     if (GNUNET_SCHEDULER_NO_TASK != kill_task)
87       GNUNET_SCHEDULER_cancel (kill_task);
88     kill_task = GNUNET_SCHEDULER_NO_TASK;
89   }
90 }
91
92 static void
93 abort_ksk_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
94 {
95   if (NULL == ksk_search)
96     return;
97   FPRINTF (stderr, "%s",  "Stopping KSK search\n");
98   GNUNET_FS_search_stop (ksk_search);
99   ksk_search = NULL;
100   if (sks_search == NULL)
101     next_phase ();
102 }
103
104
105 static void
106 abort_sks_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
107 {
108   struct GNUNET_FS_Namespace *ns;
109
110   if (sks_search == NULL)
111     return;
112   FPRINTF (stderr, "%s",  "Stopping SKS search\n");
113   GNUNET_FS_search_stop (sks_search);
114   sks_search = NULL;
115   ns = GNUNET_FS_namespace_create (fs, "testNamespace");
116   GNUNET_assert (NULL != ns);
117   GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
118   if (ksk_search == NULL)
119     next_phase ();
120 }
121
122
123 static void
124 do_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
125 {
126   FPRINTF (stderr, "%s",  "Operation timed out\n");
127   kill_task = GNUNET_SCHEDULER_NO_TASK;
128   abort_sks_search_task (NULL, tc);
129   abort_ksk_search_task (NULL, tc);
130 }
131
132
133 static void *
134 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
135 {
136   char *got;
137   switch (event->status)
138   {
139   case GNUNET_FS_STATUS_SEARCH_RESULT:
140     got = GNUNET_FS_uri_to_string (event->value.search.specifics.result.uri);
141     FPRINTF (stderr, "Got a search result `%s'\n", got);
142     if (sks_search == event->value.search.sc)
143     {
144       if (!GNUNET_FS_uri_test_equal
145           (sks_expect_uri, event->value.search.specifics.result.uri))
146       {
147         char *expected;
148         expected = GNUNET_FS_uri_to_string (sks_expect_uri);
149         FPRINTF (stderr, "Wrong result for sks search! Expected:\n%s\nGot:\n%s\n", expected, got);
150         GNUNET_free (expected);
151         err = 1;
152       }
153       /* give system 1ms to initiate update search! */
154       FPRINTF (stderr, "scheduling `%s'\n", "abort_sks_search_task");
155       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
156                                     &abort_sks_search_task, NULL);
157     }
158     else if (ksk_search == event->value.search.sc)
159     {
160       if (!GNUNET_FS_uri_test_equal
161           (ksk_expect_uri, event->value.search.specifics.result.uri))
162       {
163         char *expected;
164         expected = GNUNET_FS_uri_to_string (ksk_expect_uri);
165         FPRINTF (stderr, "Wrong result for ksk search! Expected:\n%s\nGot:\n%s\n", expected, got);
166         GNUNET_free (expected);
167         err = 1;
168       }
169       FPRINTF (stderr, "scheduling `%s'\n", "abort_ksk_search_task");
170       GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task, NULL,
171                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
172     }
173     else
174     {
175       FPRINTF (stderr, "Unexpected search result `%s' received!\n", got);
176       GNUNET_break (0);
177     }
178     GNUNET_free (got);
179     break;
180   case GNUNET_FS_STATUS_SEARCH_ERROR:
181     FPRINTF (stderr, "Error searching file: %s\n",
182              event->value.search.specifics.error.message);
183     if (sks_search == event->value.search.sc)
184       GNUNET_SCHEDULER_add_continuation (&abort_sks_search_task, NULL,
185                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
186     else if (ksk_search == event->value.search.sc)
187       GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task, NULL,
188                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
189     else
190       GNUNET_break (0);
191     break;
192   case GNUNET_FS_STATUS_SEARCH_START:
193     FPRINTF (stderr, "Search %s started\n", event->value.search.pctx);
194     GNUNET_assert ((NULL == event->value.search.cctx) ||
195                    (0 == strcmp ("sks_search", event->value.search.cctx)) ||
196                    (0 == strcmp ("ksk_search", event->value.search.cctx)));
197     if (NULL == event->value.search.cctx)
198     {
199       GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
200       update_started = GNUNET_YES;
201     }
202     GNUNET_assert (1 == event->value.search.anonymity);
203     break;
204   case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
205     FPRINTF (stderr, "%s",  "Search result stopped\n");
206     return NULL;
207   case GNUNET_FS_STATUS_SEARCH_STOPPED:
208     FPRINTF (stderr, "%s",  "Search stopped\n");
209     return NULL;
210   default:
211     FPRINTF (stderr, "Unexpected event: %d\n", event->status);
212     break;
213   }
214   return event->value.search.cctx;
215 }
216
217
218 static void
219 publish_cont (void *cls, const struct GNUNET_FS_Uri *ksk_uri, const char *emsg)
220 {
221   char *msg;
222   struct GNUNET_FS_Uri *sks_uri;
223   char sbuf[1024];
224   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
225
226   if (NULL != emsg)
227   {
228     FPRINTF (stderr, "Error publishing ksk: %s\n", emsg);
229     err = 1;
230     GNUNET_FS_stop (fs);
231     return;
232   }
233   FPRINTF (stderr, "%s",  "Published ksk\n");
234   GNUNET_CRYPTO_hash_to_enc (&nsid, &enc);
235   GNUNET_snprintf (sbuf, sizeof (sbuf), "gnunet://fs/sks/%s/this", &enc);
236   sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
237   if (NULL == sks_uri)
238   {
239     FPRINTF (stderr, "failed to parse URI `%s': %s\n", sbuf, msg);
240     err = 1;
241     GNUNET_FS_stop (fs);
242     GNUNET_free_non_null (msg);
243     return;
244   }
245   FPRINTF (stderr, "%s",  "Starting searches\n");
246   ksk_search =
247       GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
248                               "ksk_search");
249   sks_search =
250       GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
251                               "sks_search");
252   GNUNET_FS_uri_destroy (sks_uri);
253 }
254
255
256 static void
257 sks_cont (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
258 {
259   struct GNUNET_CONTAINER_MetaData *meta;
260   struct GNUNET_FS_Uri *ksk_uri;
261   char *msg;
262   struct GNUNET_FS_BlockOptions bo;
263
264   if (NULL == uri)
265   {
266     fprintf (stderr, "Error publishing sks: %s\n", emsg);
267     err = 1;
268     GNUNET_FS_stop (fs);
269     return;
270   }
271   FPRINTF (stderr, "%s",  "Published sks\n");
272   meta = GNUNET_CONTAINER_meta_data_create ();
273   msg = NULL;
274   ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
275   GNUNET_assert (NULL == msg);
276   ksk_expect_uri = GNUNET_FS_uri_dup (uri);
277   bo.content_priority = 1;
278   bo.anonymity_level = 1;
279   bo.replication_level = 0;
280   bo.expiration_time =
281       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
282   GNUNET_FS_publish_ksk (fs, ksk_uri, meta, uri, &bo,
283                          GNUNET_FS_PUBLISH_OPTION_NONE, &publish_cont, NULL);
284   GNUNET_FS_uri_destroy (ksk_uri);
285   GNUNET_CONTAINER_meta_data_destroy (meta);
286 }
287
288
289 static void
290 adv_cont (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
291 {
292   struct GNUNET_CONTAINER_MetaData *meta;
293   struct GNUNET_FS_Namespace *ns;
294   struct GNUNET_FS_BlockOptions bo;
295
296   if (NULL != emsg)
297   {
298     FPRINTF (stderr, "Error advertising: %s\n", emsg);
299     err = 1;
300     GNUNET_FS_stop (fs);
301     return;
302   }
303   FPRINTF (stderr, "%s",  "Created an advertising\n");
304   ns = GNUNET_FS_namespace_create (fs, "testNamespace");
305   GNUNET_assert (NULL != ns);
306   meta = GNUNET_CONTAINER_meta_data_create ();
307   GNUNET_assert (NULL == emsg);
308   sks_expect_uri = GNUNET_FS_uri_dup (uri);
309   bo.content_priority = 1;
310   bo.anonymity_level = 1;
311   bo.replication_level = 0;
312   bo.expiration_time =
313       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
314   GNUNET_FS_publish_sks (fs, ns, "this", "next", meta, uri,     /* FIXME: this is non-sense (use CHK URI!?) */
315                          &bo, GNUNET_FS_PUBLISH_OPTION_NONE, &sks_cont, NULL);
316   GNUNET_CONTAINER_meta_data_destroy (meta);
317   GNUNET_FS_namespace_delete (ns, GNUNET_NO);
318 }
319
320
321 static void
322 ns_iterator (void *cls, const char *name, const struct GNUNET_HashCode * id)
323 {
324   int *ok = cls;
325
326   FPRINTF (stderr, "Namespace in the list: %s\n", name);
327   if (0 != strcmp (name, "testNamespace"))
328     return;
329   *ok = GNUNET_YES;
330   nsid = *id;
331 }
332
333 static void
334 testCreatedNamespace (struct GNUNET_FS_Namespace *ns)
335 {
336   struct GNUNET_FS_BlockOptions bo;
337   struct GNUNET_CONTAINER_MetaData *meta;
338   struct GNUNET_FS_Uri *ksk_uri;
339   int ok;
340
341   FPRINTF (stderr, "%s",  "Listing namespaces\n");
342   ok = GNUNET_NO;
343   GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
344   if (GNUNET_NO == ok)
345   {
346     FPRINTF (stderr, "%s",  "namespace_list failed to find namespace!\n");
347     GNUNET_FS_namespace_delete (ns, GNUNET_YES);
348     GNUNET_FS_stop (fs);
349     err = 1;
350     return;
351   }
352   FPRINTF (stderr, "%s",  "Creating an advertising\n");
353   meta = GNUNET_CONTAINER_meta_data_create ();
354   ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
355   bo.content_priority = 1;
356   bo.anonymity_level = 1;
357   bo.replication_level = 0;
358   bo.expiration_time =
359       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
360   GNUNET_FS_namespace_advertise (fs, ksk_uri, ns, meta, &bo, "root", &adv_cont,
361                                  NULL);
362   GNUNET_FS_uri_destroy (ksk_uri);
363   GNUNET_FS_namespace_delete (ns, GNUNET_NO);
364   GNUNET_CONTAINER_meta_data_destroy (meta);
365 }
366
367 static void
368 do_ncc_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
369 {
370   FPRINTF (stderr, "%s",  "Asynchronous NS creation timed out\n");
371   kill_ncc_task = GNUNET_SCHEDULER_NO_TASK;
372   if (NULL == ncc)
373     return;
374   GNUNET_FS_namespace_create_stop (ncc);
375   ncc = NULL;
376   err = 1;
377 }
378
379 static void
380 ns_created (void *cls, struct GNUNET_FS_Namespace *ns, const char *emsg)
381 {
382   if (GNUNET_SCHEDULER_NO_TASK != kill_ncc_task)
383     GNUNET_SCHEDULER_cancel (kill_ncc_task);
384   kill_ncc_task = GNUNET_SCHEDULER_NO_TASK;
385   if (NULL == ns)
386   {
387     FPRINTF (stderr, "Asynchronous NS creation failed: %s\n", emsg);
388     err = 1;
389     return;
390   }
391
392   FPRINTF (stderr, "%s",  "Namespace created asynchronously\n");
393   testCreatedNamespace (ns);
394 }
395
396 static void
397 testNamespace ()
398 {
399   struct GNUNET_FS_Namespace *ns;
400
401   FPRINTF (stderr, "%s",  "Testing synchronous namespace creation\n");
402   ns = GNUNET_FS_namespace_create (fs, "testNamespace");
403   GNUNET_assert (NULL != ns);
404   testCreatedNamespace (ns);
405
406   kill_task =
407       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &do_timeout,
408                                     NULL);
409 }
410
411
412 static void
413 run (void *cls, 
414      const struct GNUNET_CONFIGURATION_Handle *cfg,
415      struct GNUNET_TESTING_Peer *peer)
416 {
417   config = cfg;
418   fs = GNUNET_FS_start (cfg, "test-fs-namespace", &progress_cb, NULL,
419                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
420   phase = 0;
421   testNamespace ();
422 }
423
424
425 int
426 main (int argc, char *argv[])
427 {
428   if (0 != GNUNET_TESTING_peer_run ("test-fs-namespace",
429                                     "test_fs_namespace_data.conf",
430                                     &run, NULL))
431     return 1;
432   return err;
433 }
434
435
436 /* end of test_fs_namespace.c */