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