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 3, 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
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib-new.h"
29 #include "gnunet_fs_service.h"
32 static struct GNUNET_HashCode nsid;
34 static struct GNUNET_FS_Uri *sks_expect_uri;
36 static struct GNUNET_FS_Uri *ksk_expect_uri;
38 static struct GNUNET_FS_Handle *fs;
40 static struct GNUNET_FS_SearchContext *sks_search;
42 static struct GNUNET_FS_SearchContext *ksk_search;
44 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
46 static int update_started;
52 abort_ksk_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
54 if (ksk_search != NULL)
56 GNUNET_FS_search_stop (ksk_search);
58 if (sks_search == NULL)
61 if (GNUNET_SCHEDULER_NO_TASK != kill_task)
62 GNUNET_SCHEDULER_cancel (kill_task);
69 abort_sks_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 struct GNUNET_FS_Namespace *ns;
73 if (sks_search == NULL)
75 GNUNET_FS_search_stop (sks_search);
77 ns = GNUNET_FS_namespace_create (fs, "testNamespace");
78 GNUNET_assert (NULL != ns);
79 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
80 if (ksk_search == NULL)
83 if (GNUNET_SCHEDULER_NO_TASK != kill_task)
84 GNUNET_SCHEDULER_cancel (kill_task);
90 do_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 FPRINTF (stderr, "%s", "Operation timed out\n");
93 kill_task = GNUNET_SCHEDULER_NO_TASK;
94 abort_sks_search_task (NULL, tc);
95 abort_ksk_search_task (NULL, tc);
100 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
102 switch (event->status)
104 case GNUNET_FS_STATUS_SEARCH_RESULT:
105 if (sks_search == event->value.search.sc)
107 if (!GNUNET_FS_uri_test_equal
108 (sks_expect_uri, event->value.search.specifics.result.uri))
110 FPRINTF (stderr, "%s", "Wrong result for sks search!\n");
113 /* give system 1ms to initiate update search! */
114 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
115 &abort_sks_search_task, NULL);
117 else if (ksk_search == event->value.search.sc)
119 if (!GNUNET_FS_uri_test_equal
120 (ksk_expect_uri, event->value.search.specifics.result.uri))
122 FPRINTF (stderr, "%s", "Wrong result for ksk search!\n");
125 GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task, NULL,
126 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
130 FPRINTF (stderr, "%s", "Unexpected search result received!\n");
134 case GNUNET_FS_STATUS_SEARCH_ERROR:
135 FPRINTF (stderr, "Error searching file: %s\n",
136 event->value.search.specifics.error.message);
137 if (sks_search == event->value.search.sc)
138 GNUNET_SCHEDULER_add_continuation (&abort_sks_search_task, NULL,
139 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
140 else if (ksk_search == event->value.search.sc)
141 GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task, NULL,
142 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
146 case GNUNET_FS_STATUS_SEARCH_START:
147 GNUNET_assert ((NULL == event->value.search.cctx) ||
148 (0 == strcmp ("sks_search", event->value.search.cctx)) ||
149 (0 == strcmp ("ksk_search", event->value.search.cctx)));
150 if (NULL == event->value.search.cctx)
152 GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
153 update_started = GNUNET_YES;
155 GNUNET_assert (1 == event->value.search.anonymity);
157 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
159 case GNUNET_FS_STATUS_SEARCH_STOPPED:
162 FPRINTF (stderr, "Unexpected event: %d\n", event->status);
165 return event->value.search.cctx;
170 publish_cont (void *cls, const struct GNUNET_FS_Uri *ksk_uri, const char *emsg)
173 struct GNUNET_FS_Uri *sks_uri;
175 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
179 FPRINTF (stderr, "Error publishing: %s\n", emsg);
184 GNUNET_CRYPTO_hash_to_enc (&nsid, &enc);
185 GNUNET_snprintf (sbuf, sizeof (sbuf), "gnunet://fs/sks/%s/this", &enc);
186 sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
189 FPRINTF (stderr, "failed to parse URI `%s': %s\n", sbuf, msg);
192 GNUNET_free_non_null (msg);
196 GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
199 GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
201 GNUNET_FS_uri_destroy (sks_uri);
206 sks_cont (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
208 struct GNUNET_CONTAINER_MetaData *meta;
209 struct GNUNET_FS_Uri *ksk_uri;
211 struct GNUNET_FS_BlockOptions bo;
215 fprintf (stderr, "Error publishing: %s\n", emsg);
220 meta = GNUNET_CONTAINER_meta_data_create ();
222 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
223 GNUNET_assert (NULL == msg);
224 ksk_expect_uri = GNUNET_FS_uri_dup (uri);
225 bo.content_priority = 1;
226 bo.anonymity_level = 1;
227 bo.replication_level = 0;
229 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
230 GNUNET_FS_publish_ksk (fs, ksk_uri, meta, uri, &bo,
231 GNUNET_FS_PUBLISH_OPTION_NONE, &publish_cont, NULL);
232 GNUNET_FS_uri_destroy (ksk_uri);
233 GNUNET_CONTAINER_meta_data_destroy (meta);
238 adv_cont (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
240 struct GNUNET_CONTAINER_MetaData *meta;
241 struct GNUNET_FS_Namespace *ns;
242 struct GNUNET_FS_BlockOptions bo;
246 FPRINTF (stderr, "Error publishing: %s\n", emsg);
251 ns = GNUNET_FS_namespace_create (fs, "testNamespace");
252 GNUNET_assert (NULL != ns);
253 meta = GNUNET_CONTAINER_meta_data_create ();
254 GNUNET_assert (NULL == emsg);
255 sks_expect_uri = GNUNET_FS_uri_dup (uri);
256 bo.content_priority = 1;
257 bo.anonymity_level = 1;
258 bo.replication_level = 0;
260 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
261 GNUNET_FS_publish_sks (fs, ns, "this", "next", meta, uri, /* FIXME: this is non-sense (use CHK URI!?) */
262 &bo, GNUNET_FS_PUBLISH_OPTION_NONE, &sks_cont, NULL);
263 GNUNET_CONTAINER_meta_data_destroy (meta);
264 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
269 ns_iterator (void *cls, const char *name, const struct GNUNET_HashCode * id)
273 if (0 != strcmp (name, "testNamespace"))
283 struct GNUNET_FS_Namespace *ns;
284 struct GNUNET_FS_BlockOptions bo;
285 struct GNUNET_CONTAINER_MetaData *meta;
286 struct GNUNET_FS_Uri *ksk_uri;
289 ns = GNUNET_FS_namespace_create (fs, "testNamespace");
290 GNUNET_assert (NULL != ns);
292 GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
295 FPRINTF (stderr, "%s", "namespace_list failed to find namespace!\n");
296 GNUNET_FS_namespace_delete (ns, GNUNET_YES);
301 meta = GNUNET_CONTAINER_meta_data_create ();
302 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
303 bo.content_priority = 1;
304 bo.anonymity_level = 1;
305 bo.replication_level = 0;
307 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
308 GNUNET_FS_namespace_advertise (fs, ksk_uri, ns, meta, &bo, "root", &adv_cont,
311 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &do_timeout,
313 GNUNET_FS_uri_destroy (ksk_uri);
314 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
315 GNUNET_CONTAINER_meta_data_destroy (meta);
321 const struct GNUNET_CONFIGURATION_Handle *cfg,
322 struct GNUNET_TESTING_Peer *peer)
324 fs = GNUNET_FS_start (cfg, "test-fs-namespace", &progress_cb, NULL,
325 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
331 main (int argc, char *argv[])
333 if (0 != GNUNET_TESTING_peer_run ("test-fs-namespace",
334 "test_fs_namespace_data.conf",
341 /* end of test_fs_namespace.c */