-fix
[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-new.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 int update_started;
47
48 static int err;
49
50
51 static void
52 abort_ksk_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
53 {
54   if (ksk_search != NULL)
55   {
56     GNUNET_FS_search_stop (ksk_search);
57     ksk_search = NULL;
58     if (sks_search == NULL)
59     {
60       GNUNET_FS_stop (fs);
61       if (GNUNET_SCHEDULER_NO_TASK != kill_task)
62         GNUNET_SCHEDULER_cancel (kill_task);
63     }
64   }
65 }
66
67
68 static void
69 abort_sks_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
70 {
71   struct GNUNET_FS_Namespace *ns;
72
73   if (sks_search == NULL)
74     return;
75   GNUNET_FS_search_stop (sks_search);
76   sks_search = NULL;
77   ns = GNUNET_FS_namespace_create (fs, "testNamespace");
78   GNUNET_assert (NULL != ns);
79   GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
80   if (ksk_search == NULL)
81   {
82     GNUNET_FS_stop (fs);
83     if (GNUNET_SCHEDULER_NO_TASK != kill_task)
84       GNUNET_SCHEDULER_cancel (kill_task);
85   }
86 }
87
88
89 static void
90 do_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 {
92   FPRINTF (stderr, "%s",  "Operation timed out\n");
93   kill_task = GNUNET_SCHEDULER_NO_TASK;
94   abort_sks_search_task (NULL, tc);
95   abort_ksk_search_task (NULL, tc);
96 }
97
98
99 static void *
100 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
101 {
102   switch (event->status)
103   {
104   case GNUNET_FS_STATUS_SEARCH_RESULT:
105     if (sks_search == event->value.search.sc)
106     {
107       if (!GNUNET_FS_uri_test_equal
108           (sks_expect_uri, event->value.search.specifics.result.uri))
109       {
110         FPRINTF (stderr, "%s",  "Wrong result for sks search!\n");
111         err = 1;
112       }
113       /* give system 1ms to initiate update search! */
114       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
115                                     &abort_sks_search_task, NULL);
116     }
117     else if (ksk_search == event->value.search.sc)
118     {
119       if (!GNUNET_FS_uri_test_equal
120           (ksk_expect_uri, event->value.search.specifics.result.uri))
121       {
122         FPRINTF (stderr, "%s",  "Wrong result for ksk search!\n");
123         err = 1;
124       }
125       GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task, NULL,
126                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
127     }
128     else
129     {
130       FPRINTF (stderr, "%s",  "Unexpected search result received!\n");
131       GNUNET_break (0);
132     }
133     break;
134   case GNUNET_FS_STATUS_SEARCH_ERROR:
135     FPRINTF (stderr, "Error searching file: %s\n",
136              event->value.search.specifics.error.message);
137     if (sks_search == event->value.search.sc)
138       GNUNET_SCHEDULER_add_continuation (&abort_sks_search_task, NULL,
139                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
140     else if (ksk_search == event->value.search.sc)
141       GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task, NULL,
142                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
143     else
144       GNUNET_break (0);
145     break;
146   case GNUNET_FS_STATUS_SEARCH_START:
147     GNUNET_assert ((NULL == event->value.search.cctx) ||
148                    (0 == strcmp ("sks_search", event->value.search.cctx)) ||
149                    (0 == strcmp ("ksk_search", event->value.search.cctx)));
150     if (NULL == event->value.search.cctx)
151     {
152       GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
153       update_started = GNUNET_YES;
154     }
155     GNUNET_assert (1 == event->value.search.anonymity);
156     break;
157   case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
158     return NULL;
159   case GNUNET_FS_STATUS_SEARCH_STOPPED:
160     return NULL;
161   default:
162     FPRINTF (stderr, "Unexpected event: %d\n", event->status);
163     break;
164   }
165   return event->value.search.cctx;
166 }
167
168
169 static void
170 publish_cont (void *cls, const struct GNUNET_FS_Uri *ksk_uri, const char *emsg)
171 {
172   char *msg;
173   struct GNUNET_FS_Uri *sks_uri;
174   char sbuf[1024];
175   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
176
177   if (NULL != emsg)
178   {
179     FPRINTF (stderr, "Error publishing: %s\n", emsg);
180     err = 1;
181     GNUNET_FS_stop (fs);
182     return;
183   }
184   GNUNET_CRYPTO_hash_to_enc (&nsid, &enc);
185   GNUNET_snprintf (sbuf, sizeof (sbuf), "gnunet://fs/sks/%s/this", &enc);
186   sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
187   if (NULL == sks_uri)
188   {
189     FPRINTF (stderr, "failed to parse URI `%s': %s\n", sbuf, msg);
190     err = 1;
191     GNUNET_FS_stop (fs);
192     GNUNET_free_non_null (msg);
193     return;
194   }
195   ksk_search =
196       GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
197                               "ksk_search");
198   sks_search =
199       GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
200                               "sks_search");
201   GNUNET_FS_uri_destroy (sks_uri);
202 }
203
204
205 static void
206 sks_cont (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
207 {
208   struct GNUNET_CONTAINER_MetaData *meta;
209   struct GNUNET_FS_Uri *ksk_uri;
210   char *msg;
211   struct GNUNET_FS_BlockOptions bo;
212
213   if (NULL == uri)
214   {
215     fprintf (stderr, "Error publishing: %s\n", emsg);
216     err = 1;
217     GNUNET_FS_stop (fs);
218     return;
219   }
220   meta = GNUNET_CONTAINER_meta_data_create ();
221   msg = NULL;
222   ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
223   GNUNET_assert (NULL == msg);
224   ksk_expect_uri = GNUNET_FS_uri_dup (uri);
225   bo.content_priority = 1;
226   bo.anonymity_level = 1;
227   bo.replication_level = 0;
228   bo.expiration_time =
229       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
230   GNUNET_FS_publish_ksk (fs, ksk_uri, meta, uri, &bo,
231                          GNUNET_FS_PUBLISH_OPTION_NONE, &publish_cont, NULL);
232   GNUNET_FS_uri_destroy (ksk_uri);
233   GNUNET_CONTAINER_meta_data_destroy (meta);
234 }
235
236
237 static void
238 adv_cont (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
239 {
240   struct GNUNET_CONTAINER_MetaData *meta;
241   struct GNUNET_FS_Namespace *ns;
242   struct GNUNET_FS_BlockOptions bo;
243
244   if (NULL != emsg)
245   {
246     FPRINTF (stderr, "Error publishing: %s\n", emsg);
247     err = 1;
248     GNUNET_FS_stop (fs);
249     return;
250   }
251   ns = GNUNET_FS_namespace_create (fs, "testNamespace");
252   GNUNET_assert (NULL != ns);
253   meta = GNUNET_CONTAINER_meta_data_create ();
254   GNUNET_assert (NULL == emsg);
255   sks_expect_uri = GNUNET_FS_uri_dup (uri);
256   bo.content_priority = 1;
257   bo.anonymity_level = 1;
258   bo.replication_level = 0;
259   bo.expiration_time =
260       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
261   GNUNET_FS_publish_sks (fs, ns, "this", "next", meta, uri,     /* FIXME: this is non-sense (use CHK URI!?) */
262                          &bo, GNUNET_FS_PUBLISH_OPTION_NONE, &sks_cont, NULL);
263   GNUNET_CONTAINER_meta_data_destroy (meta);
264   GNUNET_FS_namespace_delete (ns, GNUNET_NO);
265 }
266
267
268 static void
269 ns_iterator (void *cls, const char *name, const struct GNUNET_HashCode * id)
270 {
271   int *ok = cls;
272
273   if (0 != strcmp (name, "testNamespace"))
274     return;
275   *ok = GNUNET_YES;
276   nsid = *id;
277 }
278
279
280 static void
281 testNamespace ()
282 {
283   struct GNUNET_FS_Namespace *ns;
284   struct GNUNET_FS_BlockOptions bo;
285   struct GNUNET_CONTAINER_MetaData *meta;
286   struct GNUNET_FS_Uri *ksk_uri;
287   int ok;
288
289   ns = GNUNET_FS_namespace_create (fs, "testNamespace");
290   GNUNET_assert (NULL != ns);
291   ok = GNUNET_NO;
292   GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
293   if (GNUNET_NO == ok)
294   {
295     FPRINTF (stderr, "%s",  "namespace_list failed to find namespace!\n");
296     GNUNET_FS_namespace_delete (ns, GNUNET_YES);
297     GNUNET_FS_stop (fs);
298     err = 1;
299     return;
300   }
301   meta = GNUNET_CONTAINER_meta_data_create ();
302   ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
303   bo.content_priority = 1;
304   bo.anonymity_level = 1;
305   bo.replication_level = 0;
306   bo.expiration_time =
307       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
308   GNUNET_FS_namespace_advertise (fs, ksk_uri, ns, meta, &bo, "root", &adv_cont,
309                                  NULL);
310   kill_task =
311       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &do_timeout,
312                                     NULL);
313   GNUNET_FS_uri_destroy (ksk_uri);
314   GNUNET_FS_namespace_delete (ns, GNUNET_NO);
315   GNUNET_CONTAINER_meta_data_destroy (meta);
316 }
317
318
319 static void
320 run (void *cls, 
321      const struct GNUNET_CONFIGURATION_Handle *cfg,
322      struct GNUNET_TESTING_Peer *peer)
323 {
324   fs = GNUNET_FS_start (cfg, "test-fs-namespace", &progress_cb, NULL,
325                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
326   testNamespace ();
327 }
328
329
330 int
331 main (int argc, char *argv[])
332 {
333   if (0 != GNUNET_TESTING_peer_run ("test-fs-namespace",
334                                     "test_fs_namespace_data.conf",
335                                     &run, NULL))
336     return 1;
337   return err;
338 }
339
340
341 /* end of test_fs_namespace.c */