fix test
[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 (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,
89               "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
90   GNUNET_OS_process_close (p->arm_proc);
91   p->arm_proc = NULL;
92 #endif
93   GNUNET_CONFIGURATION_destroy (p->cfg);
94 }
95
96
97 static void
98 abort_ksk_search_task (void *cls,
99                        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,
117                        const struct GNUNET_SCHEDULER_TaskContext *tc)
118 {
119   struct GNUNET_FS_Namespace *ns;
120
121   if (sks_search == NULL)
122     return;
123   GNUNET_FS_search_stop (sks_search); 
124   sks_search = NULL;
125   ns = GNUNET_FS_namespace_create (fs,
126                                    "testNamespace");
127   GNUNET_assert (NULL != ns);
128   GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
129   if (ksk_search == NULL)
130     {
131       GNUNET_FS_stop (fs);
132       if (GNUNET_SCHEDULER_NO_TASK  != kill_task)
133         GNUNET_SCHEDULER_cancel (kill_task);
134     }    
135 }
136
137
138 static void
139 do_timeout (void *cls,
140             const struct GNUNET_SCHEDULER_TaskContext *tc)
141 {
142   fprintf (stderr,
143            "Operation timed out\n");
144   kill_task = GNUNET_SCHEDULER_NO_TASK;
145   abort_sks_search_task (NULL, tc);
146   abort_ksk_search_task (NULL, tc);
147 }
148
149
150
151 static void *
152 progress_cb (void *cls, 
153              const struct GNUNET_FS_ProgressInfo *event)
154 {
155   switch (event->status)
156     {
157     case GNUNET_FS_STATUS_SEARCH_RESULT:
158       if (sks_search == event->value.search.sc)
159         {
160           if (! GNUNET_FS_uri_test_equal (sks_expect_uri,
161                                           event->value.search.specifics.result.uri))
162             {
163               fprintf (stderr,
164                        "Wrong result for sks search!\n");
165               err = 1;
166             }
167           /* give system 1ms to initiate update search! */
168           GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
169                                         &abort_sks_search_task,
170                                         NULL);
171         }
172       else if (ksk_search == event->value.search.sc)
173         {
174           if (! GNUNET_FS_uri_test_equal (ksk_expect_uri,
175                                           event->value.search.specifics.result.uri))
176             {
177               fprintf (stderr,
178                        "Wrong result for ksk search!\n");
179               err = 1;
180             }
181           GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task,
182                                              NULL,
183                                              GNUNET_SCHEDULER_REASON_PREREQ_DONE);
184         }
185       else 
186         {
187           fprintf (stderr,
188                    "Unexpected search result received!\n");
189           GNUNET_break (0);
190         }
191       break;
192     case GNUNET_FS_STATUS_SEARCH_ERROR:
193       fprintf (stderr,
194                "Error searching file: %s\n",
195                event->value.search.specifics.error.message);
196       if (sks_search == event->value.search.sc)
197         GNUNET_SCHEDULER_add_continuation (&abort_sks_search_task,
198                                            NULL,
199                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
200       else if (ksk_search == event->value.search.sc)
201         GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task,
202                                            NULL,
203                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
204       else
205         GNUNET_break (0);
206       break;
207     case GNUNET_FS_STATUS_SEARCH_START:
208       GNUNET_assert ( (NULL == event->value.search.cctx) ||
209                       (0 == strcmp ("sks_search", event->value.search.cctx)) ||
210                       (0 == strcmp ("ksk_search", event->value.search.cctx)));
211       if (NULL == event->value.search.cctx)
212         {
213           GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
214           update_started = GNUNET_YES;
215         }
216       GNUNET_assert (1 == event->value.search.anonymity);
217       break;
218     case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
219       return NULL;
220     case GNUNET_FS_STATUS_SEARCH_STOPPED:
221       return NULL;
222     default:
223       fprintf (stderr,
224                "Unexpected event: %d\n", 
225                event->status);
226       break;
227     }
228   return event->value.search.cctx;
229 }
230
231
232 static void
233 publish_cont (void *cls,
234               const struct GNUNET_FS_Uri *ksk_uri,
235               const char *emsg)
236 {
237   char *msg;
238   struct GNUNET_FS_Uri *sks_uri;
239   char sbuf[1024];
240   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
241   
242   if (NULL != emsg)
243     {
244       fprintf (stderr, "Error publishing: %s\n", emsg);
245       err = 1;
246       GNUNET_FS_stop (fs);
247       return;
248     }  
249   GNUNET_CRYPTO_hash_to_enc (&nsid,
250                              &enc);
251   GNUNET_snprintf (sbuf,
252                    sizeof (sbuf),
253                    "gnunet://fs/sks/%s/this",
254                    &enc);
255   sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
256   if (msg != NULL)
257     {
258       fprintf (stderr, "failed to parse URI `%s': %s\n",
259                sbuf,
260                msg);
261       err = 1;
262       GNUNET_FS_stop (fs);
263       GNUNET_free (msg);
264       return;
265     }
266   ksk_search = GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "ksk_search");
267   sks_search = GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "sks_search");
268   GNUNET_FS_uri_destroy (sks_uri);
269 }
270
271
272 static void
273 sks_cont (void *cls,
274           const struct GNUNET_FS_Uri *uri,
275           const char *emsg)
276 {
277   struct GNUNET_CONTAINER_MetaData *meta;
278   struct GNUNET_TIME_Absolute expiration;
279   struct GNUNET_FS_Uri *ksk_uri;
280   char * msg;
281
282   expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
283   meta = GNUNET_CONTAINER_meta_data_create ();
284   msg = NULL;
285   ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
286   GNUNET_assert (NULL == msg);
287   ksk_expect_uri = GNUNET_FS_uri_dup (uri);
288   GNUNET_FS_publish_ksk (fs,
289                          ksk_uri,
290                          meta,
291                          uri,
292                          expiration,
293                          1, 1,
294                          GNUNET_FS_PUBLISH_OPTION_NONE,
295                          &publish_cont,
296                          NULL);
297   GNUNET_FS_uri_destroy (ksk_uri);
298   GNUNET_CONTAINER_meta_data_destroy (meta);
299 }
300
301
302 static void
303 adv_cont (void *cls,
304           const struct GNUNET_FS_Uri *uri,
305           const char *emsg)
306 {
307   struct GNUNET_CONTAINER_MetaData *meta;
308   struct GNUNET_FS_Namespace *ns;
309   struct GNUNET_TIME_Absolute expiration;
310
311   if (NULL != emsg)
312     {
313       fprintf (stderr, "Error publishing: %s\n", emsg);
314       err = 1;
315       GNUNET_FS_stop (fs);
316       return;
317     }
318   expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
319   ns = GNUNET_FS_namespace_create (fs,
320                                    "testNamespace");
321   GNUNET_assert (NULL != ns);
322   meta = GNUNET_CONTAINER_meta_data_create ();
323   GNUNET_assert (NULL == emsg);
324   sks_expect_uri = GNUNET_FS_uri_dup (uri);
325   GNUNET_FS_publish_sks (fs,
326                          ns,
327                          "this",
328                          "next",
329                          meta,
330                          uri, /* FIXME: this is non-sense (use CHK URI!?) */
331                          expiration,
332                          1, 1,
333                          GNUNET_FS_PUBLISH_OPTION_NONE,
334                          &sks_cont,
335                          NULL);
336   GNUNET_CONTAINER_meta_data_destroy (meta);
337   GNUNET_FS_namespace_delete (ns, GNUNET_NO);
338 }
339
340
341 static void
342 ns_iterator (void *cls,
343              const char *name,
344              const GNUNET_HashCode *id)
345 {
346   int *ok = cls;
347
348   if (0 != strcmp (name,
349                    "testNamespace"))
350     return;
351   *ok = GNUNET_YES;
352   nsid = *id;
353 }
354
355
356 static void
357 testNamespace ()
358 {
359   struct GNUNET_FS_Namespace *ns;
360   struct GNUNET_TIME_Absolute expiration;
361   struct GNUNET_CONTAINER_MetaData *meta;
362   struct GNUNET_FS_Uri *ksk_uri;
363   int ok;
364
365   ns = GNUNET_FS_namespace_create (fs,
366                                    "testNamespace");
367   GNUNET_assert (NULL != ns);
368   ok = GNUNET_NO;
369   GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
370   if (GNUNET_NO == ok)
371     {
372       fprintf (stderr, "namespace_list failed to find namespace!\n");
373       GNUNET_FS_namespace_delete (ns, GNUNET_YES);
374       GNUNET_FS_stop (fs);
375       err = 1;
376       return;
377     }
378   expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
379   meta = GNUNET_CONTAINER_meta_data_create ();
380   ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
381   GNUNET_FS_namespace_advertise (fs,
382                                  ksk_uri,
383                                  ns,
384                                  meta,
385                                  1, 1,
386                                  expiration,                                       
387                                  "root",
388                                  &adv_cont, NULL);
389   kill_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
390                                             &do_timeout,
391                                             NULL);
392   GNUNET_FS_uri_destroy (ksk_uri);
393   GNUNET_FS_namespace_delete (ns, GNUNET_NO);
394   GNUNET_CONTAINER_meta_data_destroy (meta);
395 }
396
397
398 static void
399 run (void *cls,
400      char *const *args,
401      const char *cfgfile,
402      const struct GNUNET_CONFIGURATION_Handle *cfg)
403 {
404   setup_peer (&p1, "test_fs_namespace_data.conf");
405   fs = GNUNET_FS_start (cfg,
406                         "test-fs-namespace",
407                         &progress_cb,
408                         NULL,
409                         GNUNET_FS_FLAGS_NONE,
410                         GNUNET_FS_OPTIONS_END);
411   testNamespace ();
412 }
413
414
415 int
416 main (int argc, char *argv[])
417 {
418   char *const argvx[] = { 
419     "test-fs-namespace",
420     "-c",
421     "test_fs_namespace_data.conf",
422 #if VERBOSE
423     "-L", "DEBUG",
424 #endif
425     NULL
426   };
427   struct GNUNET_GETOPT_CommandLineOption options[] = {
428     GNUNET_GETOPT_OPTION_END
429   };
430
431   GNUNET_log_setup ("test_fs_namespace", 
432 #if VERBOSE
433                     "DEBUG",
434 #else
435                     "WARNING",
436 #endif
437                     NULL);
438   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
439                       argvx, "test-fs-namespace",
440                       "nohelp", options, &run, NULL);
441   stop_arm (&p1);
442   if (GNUNET_YES != update_started)
443     {
444       fprintf (stderr,
445                "Update search never started!\n");
446       err = 1;
447     }
448   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
449   return err;
450 }
451
452
453 /* end of test_fs_namespace.c */