2 This file is part of GNUnet.
3 (C) 2005, 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file fs/test_fs_namespace.c
23 * @brief Test for fs_namespace.c
24 * @author Christian Grothoff
30 #include "gnunet_util_lib.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_fs_service.h"
34 #define VERBOSE GNUNET_YES
36 #define START_ARM GNUNET_YES
38 static struct GNUNET_SCHEDULER_Handle *sched;
40 static struct PeerContext p1;
42 static GNUNET_HashCode nsid;
44 static struct GNUNET_FS_Uri *sks_expect_uri;
46 static struct GNUNET_FS_Uri *ksk_expect_uri;
48 static struct GNUNET_FS_Handle *fs;
50 static struct GNUNET_FS_SearchContext *sks_search;
52 static struct GNUNET_FS_SearchContext *ksk_search;
54 static int update_started;
60 struct GNUNET_CONFIGURATION_Handle *cfg;
68 setup_peer (struct PeerContext *p, const char *cfgname)
70 p->cfg = GNUNET_CONFIGURATION_create ();
72 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
79 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
80 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
85 stop_arm (struct PeerContext *p)
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);
95 GNUNET_CONFIGURATION_destroy (p->cfg);
100 abort_ksk_search_task (void *cls,
101 const struct GNUNET_SCHEDULER_TaskContext *tc)
103 if (ksk_search != NULL)
105 GNUNET_FS_search_stop (ksk_search);
107 if (sks_search == NULL)
114 abort_sks_search_task (void *cls,
115 const struct GNUNET_SCHEDULER_TaskContext *tc)
117 struct GNUNET_FS_Namespace *ns;
119 if (sks_search != NULL)
121 GNUNET_FS_search_stop (sks_search);
123 ns = GNUNET_FS_namespace_create (fs,
125 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
126 if (ksk_search == NULL)
133 progress_cb (void *cls,
134 const struct GNUNET_FS_ProgressInfo *event)
136 switch (event->status)
138 case GNUNET_FS_STATUS_SEARCH_RESULT:
139 if (sks_search == event->value.search.sc)
142 "Search result for sks received\n");
143 if (! GNUNET_FS_uri_test_equal (sks_expect_uri,
144 event->value.search.specifics.result.uri))
147 "Wrong result for sks search!\n");
150 /* give system 1ms to initiate update search! */
151 GNUNET_SCHEDULER_add_delayed (sched,
152 GNUNET_TIME_UNIT_MILLISECONDS,
153 &abort_sks_search_task,
156 else if (ksk_search == event->value.search.sc)
159 "Search result for ksk received\n");
160 if (! GNUNET_FS_uri_test_equal (ksk_expect_uri,
161 event->value.search.specifics.result.uri))
164 "Wrong result for ksk search!\n");
167 GNUNET_SCHEDULER_add_continuation (sched,
168 &abort_ksk_search_task,
170 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
175 "Search result received!?\n");
179 case GNUNET_FS_STATUS_SEARCH_ERROR:
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,
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,
192 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
196 case GNUNET_FS_STATUS_SEARCH_START:
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)
205 GNUNET_assert (sks_search == event->value.search.pctx);
206 update_started = GNUNET_YES;
208 GNUNET_assert (1 == event->value.search.anonymity);
210 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
212 case GNUNET_FS_STATUS_SEARCH_STOPPED:
214 "Search stop event received\n");
215 GNUNET_assert ( (ksk_search == event->value.search.sc) ||
216 (sks_search == event->value.search.sc));
220 "Unexpected event: %d\n",
229 publish_cont (void *cls,
230 const struct GNUNET_FS_Uri *ksk_uri,
234 struct GNUNET_FS_Uri *sks_uri;
236 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
240 fprintf (stderr, "Error publishing: %s\n", emsg);
245 GNUNET_CRYPTO_hash_to_enc (&nsid,
247 GNUNET_snprintf (sbuf,
249 "gnunet://fs/sks/%s/this",
251 sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
254 fprintf (stderr, "failed to parse URI `%s': %s\n",
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);
271 const struct GNUNET_FS_Uri *uri,
274 struct GNUNET_CONTAINER_MetaData *meta;
275 struct GNUNET_TIME_Absolute expiration;
276 struct GNUNET_FS_Uri *ksk_uri;
279 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
280 meta = GNUNET_CONTAINER_meta_data_create ();
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,
292 GNUNET_FS_PUBLISH_OPTION_NONE,
295 GNUNET_FS_uri_destroy (ksk_uri);
296 GNUNET_CONTAINER_meta_data_destroy (meta);
302 const struct GNUNET_FS_Uri *uri,
305 struct GNUNET_CONTAINER_MetaData *meta;
306 struct GNUNET_FS_Namespace *ns;
307 struct GNUNET_TIME_Absolute expiration;
311 fprintf (stderr, "Error publishing: %s\n", emsg);
316 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
317 ns = GNUNET_FS_namespace_create (fs,
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,
328 uri, /* FIXME: this is non-sense (use CHK URI!?) */
331 GNUNET_FS_PUBLISH_OPTION_NONE,
334 GNUNET_CONTAINER_meta_data_destroy (meta);
335 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
340 ns_iterator (void *cls,
342 const GNUNET_HashCode *id)
346 if (0 != strcmp (name,
357 struct GNUNET_FS_Namespace *ns;
358 struct GNUNET_TIME_Absolute expiration;
359 struct GNUNET_CONTAINER_MetaData *meta;
362 ns = GNUNET_FS_namespace_create (fs,
365 GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
368 fprintf (stderr, "namespace_list failed to find namespace!\n");
369 GNUNET_FS_namespace_delete (ns, GNUNET_YES);
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,
384 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
385 GNUNET_CONTAINER_meta_data_destroy (meta);
391 struct GNUNET_SCHEDULER_Handle *s,
394 const struct GNUNET_CONFIGURATION_Handle *cfg)
397 setup_peer (&p1, "test_fs_namespace_data.conf");
398 fs = GNUNET_FS_start (sched,
403 GNUNET_FS_FLAGS_NONE,
404 GNUNET_FS_OPTIONS_END);
410 main (int argc, char *argv[])
412 char *const argvx[] = {
415 "test_fs_namespace_data.conf",
421 struct GNUNET_GETOPT_CommandLineOption options[] = {
422 GNUNET_GETOPT_OPTION_END
425 GNUNET_log_setup ("test_fs_namespace",
432 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
433 argvx, "test-fs-namespace",
434 "nohelp", options, &run, NULL);
436 if (GNUNET_YES != update_started)
439 "Update search never started!\n");
442 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
447 /* end of test_fs_namespace.c */