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_NO
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)
109 fprintf (stderr, "initiating shutdown\n");
117 abort_sks_search_task (void *cls,
118 const struct GNUNET_SCHEDULER_TaskContext *tc)
120 struct GNUNET_FS_Namespace *ns;
122 if (sks_search == NULL)
124 GNUNET_FS_search_stop (sks_search);
126 ns = GNUNET_FS_namespace_create (fs,
128 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
129 if (ksk_search == NULL)
131 fprintf (stderr, "initiating shutdown\n");
138 progress_cb (void *cls,
139 const struct GNUNET_FS_ProgressInfo *event)
141 switch (event->status)
143 case GNUNET_FS_STATUS_SEARCH_RESULT:
144 if (sks_search == event->value.search.sc)
147 "Search result for sks received\n");
148 if (! GNUNET_FS_uri_test_equal (sks_expect_uri,
149 event->value.search.specifics.result.uri))
152 "Wrong result for sks search!\n");
155 /* give system 1ms to initiate update search! */
156 GNUNET_SCHEDULER_add_delayed (sched,
157 GNUNET_TIME_UNIT_MILLISECONDS,
158 &abort_sks_search_task,
161 else if (ksk_search == event->value.search.sc)
164 "Search result for ksk received\n");
165 if (! GNUNET_FS_uri_test_equal (ksk_expect_uri,
166 event->value.search.specifics.result.uri))
169 "Wrong result for ksk search!\n");
172 GNUNET_SCHEDULER_add_continuation (sched,
173 &abort_ksk_search_task,
175 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
180 "Search result received!?\n");
184 case GNUNET_FS_STATUS_SEARCH_ERROR:
186 "Error searching file: %s\n",
187 event->value.search.specifics.error.message);
188 if (sks_search == event->value.search.sc)
189 GNUNET_SCHEDULER_add_continuation (sched,
190 &abort_sks_search_task,
192 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
193 else if (ksk_search == event->value.search.sc)
194 GNUNET_SCHEDULER_add_continuation (sched,
195 &abort_ksk_search_task,
197 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
201 case GNUNET_FS_STATUS_SEARCH_START:
203 "Search start event for `%s' received\n",
204 (const char*) event->value.search.cctx);
205 GNUNET_assert ( (NULL == event->value.search.cctx) ||
206 (0 == strcmp ("sks_search", event->value.search.cctx)) ||
207 (0 == strcmp ("ksk_search", event->value.search.cctx)));
208 if (NULL == event->value.search.cctx)
210 GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
211 update_started = GNUNET_YES;
213 GNUNET_assert (1 == event->value.search.anonymity);
215 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
217 case GNUNET_FS_STATUS_SEARCH_STOPPED:
219 "Search stop event received\n");
223 "Unexpected event: %d\n",
227 return event->value.search.cctx;
232 publish_cont (void *cls,
233 const struct GNUNET_FS_Uri *ksk_uri,
237 struct GNUNET_FS_Uri *sks_uri;
239 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
243 fprintf (stderr, "Error publishing: %s\n", emsg);
248 GNUNET_CRYPTO_hash_to_enc (&nsid,
250 GNUNET_snprintf (sbuf,
252 "gnunet://fs/sks/%s/this",
254 sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
257 fprintf (stderr, "failed to parse URI `%s': %s\n",
265 fprintf (stderr, "Starting keyword search...\n");
266 ksk_search = GNUNET_FS_search_start (fs, ksk_uri, 1, "ksk_search");
267 fprintf (stderr, "Starting namespace search...\n") ;
268 sks_search = GNUNET_FS_search_start (fs, sks_uri, 1, "sks_search");
269 GNUNET_FS_uri_destroy (sks_uri);
275 const struct GNUNET_FS_Uri *uri,
278 struct GNUNET_CONTAINER_MetaData *meta;
279 struct GNUNET_TIME_Absolute expiration;
280 struct GNUNET_FS_Uri *ksk_uri;
283 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
284 meta = GNUNET_CONTAINER_meta_data_create ();
286 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
287 GNUNET_assert (NULL == msg);
288 fprintf (stderr, "Advertising update 'this' namespace entry under keyword...\n");
289 ksk_expect_uri = GNUNET_FS_uri_dup (uri);
290 GNUNET_FS_publish_ksk (fs,
296 GNUNET_FS_PUBLISH_OPTION_NONE,
299 GNUNET_FS_uri_destroy (ksk_uri);
300 GNUNET_CONTAINER_meta_data_destroy (meta);
306 const struct GNUNET_FS_Uri *uri,
309 struct GNUNET_CONTAINER_MetaData *meta;
310 struct GNUNET_FS_Namespace *ns;
311 struct GNUNET_TIME_Absolute expiration;
315 fprintf (stderr, "Error publishing: %s\n", emsg);
320 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
321 ns = GNUNET_FS_namespace_create (fs,
323 meta = GNUNET_CONTAINER_meta_data_create ();
324 GNUNET_assert (NULL == emsg);
325 fprintf (stderr, "Advertising update 'this->next' namespace update...\n");
326 sks_expect_uri = GNUNET_FS_uri_dup (uri);
327 GNUNET_FS_publish_sks (fs,
332 uri, /* FIXME: this is non-sense (use CHK URI!?) */
335 GNUNET_FS_PUBLISH_OPTION_NONE,
338 GNUNET_CONTAINER_meta_data_destroy (meta);
339 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
344 ns_iterator (void *cls,
346 const GNUNET_HashCode *id)
350 if (0 != strcmp (name,
361 struct GNUNET_FS_Namespace *ns;
362 struct GNUNET_TIME_Absolute expiration;
363 struct GNUNET_CONTAINER_MetaData *meta;
366 ns = GNUNET_FS_namespace_create (fs,
369 GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
372 fprintf (stderr, "namespace_list failed to find namespace!\n");
373 GNUNET_FS_namespace_delete (ns, GNUNET_YES);
378 fprintf (stderr, "Advertising namespace root...\n");
379 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
380 meta = GNUNET_CONTAINER_meta_data_create ();
381 GNUNET_FS_namespace_advertise (fs,
388 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
389 GNUNET_CONTAINER_meta_data_destroy (meta);
395 struct GNUNET_SCHEDULER_Handle *s,
398 const struct GNUNET_CONFIGURATION_Handle *cfg)
401 setup_peer (&p1, "test_fs_namespace_data.conf");
402 fs = GNUNET_FS_start (sched,
407 GNUNET_FS_FLAGS_NONE,
408 GNUNET_FS_OPTIONS_END);
414 main (int argc, char *argv[])
416 char *const argvx[] = {
419 "test_fs_namespace_data.conf",
425 struct GNUNET_GETOPT_CommandLineOption options[] = {
426 GNUNET_GETOPT_OPTION_END
429 GNUNET_log_setup ("test_fs_namespace",
436 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
437 argvx, "test-fs-namespace",
438 "nohelp", options, &run, NULL);
440 if (GNUNET_YES != update_started)
443 "Update search never started!\n");
446 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
451 /* end of test_fs_namespace.c */