96caa8dfaab1353288f807932c192a48dfd48012
[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 = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
70                                          "gnunet-service-arm",
71 #if VERBOSE
72                                          "-L", "DEBUG",
73 #endif
74                                          "-c", cfgname, NULL);
75 #endif
76   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
77 }
78
79
80 static void
81 stop_arm (struct PeerContext *p)
82 {
83 #if START_ARM
84   if (NULL != p->arm_proc)
85   {
86     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
87       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
88     if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
89       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
90     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91                 "ARM process %u stopped\n",
92                 GNUNET_OS_process_get_pid (p->arm_proc));
93     GNUNET_OS_process_close (p->arm_proc);
94     p->arm_proc = NULL;
95   }
96 #endif
97   GNUNET_CONFIGURATION_destroy (p->cfg);
98 }
99
100
101 static void
102 abort_ksk_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 {
104   if (ksk_search != NULL)
105   {
106     GNUNET_FS_search_stop (ksk_search);
107     ksk_search = NULL;
108     if (sks_search == NULL)
109     {
110       GNUNET_FS_stop (fs);
111       if (GNUNET_SCHEDULER_NO_TASK != kill_task)
112         GNUNET_SCHEDULER_cancel (kill_task);
113     }
114   }
115 }
116
117
118 static void
119 abort_sks_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 {
121   struct GNUNET_FS_Namespace *ns;
122
123   if (sks_search == NULL)
124     return;
125   GNUNET_FS_search_stop (sks_search);
126   sks_search = NULL;
127   ns = GNUNET_FS_namespace_create (fs, "testNamespace");
128   GNUNET_assert (NULL != ns);
129   GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
130   if (ksk_search == NULL)
131   {
132     GNUNET_FS_stop (fs);
133     if (GNUNET_SCHEDULER_NO_TASK != kill_task)
134       GNUNET_SCHEDULER_cancel (kill_task);
135   }
136 }
137
138
139 static void
140 do_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
141 {
142   fprintf (stderr, "Operation timed out\n");
143   kill_task = GNUNET_SCHEDULER_NO_TASK;
144   abort_sks_search_task (NULL, tc);
145   abort_ksk_search_task (NULL, tc);
146 }
147
148
149
150 static void *
151 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
152 {
153   switch (event->status)
154   {
155   case GNUNET_FS_STATUS_SEARCH_RESULT:
156     if (sks_search == event->value.search.sc)
157     {
158       if (!GNUNET_FS_uri_test_equal (sks_expect_uri,
159                                      event->value.search.specifics.result.uri))
160       {
161         fprintf (stderr, "Wrong result for sks search!\n");
162         err = 1;
163       }
164       /* give system 1ms to initiate update search! */
165       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
166                                     &abort_sks_search_task, NULL);
167     }
168     else if (ksk_search == event->value.search.sc)
169     {
170       if (!GNUNET_FS_uri_test_equal (ksk_expect_uri,
171                                      event->value.search.specifics.result.uri))
172       {
173         fprintf (stderr, "Wrong result for ksk search!\n");
174         err = 1;
175       }
176       GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task,
177                                          NULL,
178                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
179     }
180     else
181     {
182       fprintf (stderr, "Unexpected search result received!\n");
183       GNUNET_break (0);
184     }
185     break;
186   case GNUNET_FS_STATUS_SEARCH_ERROR:
187     fprintf (stderr,
188              "Error searching file: %s\n",
189              event->value.search.specifics.error.message);
190     if (sks_search == event->value.search.sc)
191       GNUNET_SCHEDULER_add_continuation (&abort_sks_search_task,
192                                          NULL,
193                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
194     else if (ksk_search == event->value.search.sc)
195       GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task,
196                                          NULL,
197                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
198     else
199       GNUNET_break (0);
200     break;
201   case GNUNET_FS_STATUS_SEARCH_START:
202     GNUNET_assert ((NULL == event->value.search.cctx) ||
203                    (0 == strcmp ("sks_search", event->value.search.cctx)) ||
204                    (0 == strcmp ("ksk_search", event->value.search.cctx)));
205     if (NULL == event->value.search.cctx)
206     {
207       GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
208       update_started = GNUNET_YES;
209     }
210     GNUNET_assert (1 == event->value.search.anonymity);
211     break;
212   case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
213     return NULL;
214   case GNUNET_FS_STATUS_SEARCH_STOPPED:
215     return NULL;
216   default:
217     fprintf (stderr, "Unexpected event: %d\n", event->status);
218     break;
219   }
220   return event->value.search.cctx;
221 }
222
223
224 static void
225 publish_cont (void *cls, const struct GNUNET_FS_Uri *ksk_uri, const char *emsg)
226 {
227   char *msg;
228   struct GNUNET_FS_Uri *sks_uri;
229   char sbuf[1024];
230   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
231
232   if (NULL != emsg)
233   {
234     fprintf (stderr, "Error publishing: %s\n", emsg);
235     err = 1;
236     GNUNET_FS_stop (fs);
237     return;
238   }
239   GNUNET_CRYPTO_hash_to_enc (&nsid, &enc);
240   GNUNET_snprintf (sbuf, sizeof (sbuf), "gnunet://fs/sks/%s/this", &enc);
241   sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
242   if (msg != NULL)
243   {
244     fprintf (stderr, "failed to parse URI `%s': %s\n", sbuf, msg);
245     err = 1;
246     GNUNET_FS_stop (fs);
247     GNUNET_free (msg);
248     return;
249   }
250   ksk_search =
251       GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
252                               "ksk_search");
253   sks_search =
254       GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
255                               "sks_search");
256   GNUNET_FS_uri_destroy (sks_uri);
257 }
258
259
260 static void
261 sks_cont (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
262 {
263   struct GNUNET_CONTAINER_MetaData *meta;
264   struct GNUNET_FS_Uri *ksk_uri;
265   char *msg;
266   struct GNUNET_FS_BlockOptions bo;
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, "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,
369      char *const *args,
370      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
371 {
372   setup_peer (&p1, "test_fs_namespace_data.conf");
373   fs = GNUNET_FS_start (cfg,
374                         "test-fs-namespace",
375                         &progress_cb,
376                         NULL, GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
377   testNamespace ();
378 }
379
380
381 int
382 main (int argc, char *argv[])
383 {
384   char *const argvx[] = {
385     "test-fs-namespace",
386     "-c",
387     "test_fs_namespace_data.conf",
388 #if VERBOSE
389     "-L", "DEBUG",
390 #endif
391     NULL
392   };
393   struct GNUNET_GETOPT_CommandLineOption options[] = {
394     GNUNET_GETOPT_OPTION_END
395   };
396
397   GNUNET_log_setup ("test_fs_namespace",
398 #if VERBOSE
399                     "DEBUG",
400 #else
401                     "WARNING",
402 #endif
403                     NULL);
404   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
405                       argvx, "test-fs-namespace",
406                       "nohelp", options, &run, NULL);
407   stop_arm (&p1);
408   if (GNUNET_YES != update_started)
409   {
410     fprintf (stderr, "Update search never started!\n");
411     err = 1;
412   }
413   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
414   return err;
415 }
416
417
418 /* end of test_fs_namespace.c */