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_arm_service.h"
29 #include "gnunet_fs_service.h"
31 #define VERBOSE GNUNET_NO
33 #define START_ARM GNUNET_YES
35 static struct PeerContext p1;
37 static GNUNET_HashCode nsid;
39 static struct GNUNET_FS_Uri *sks_expect_uri;
41 static struct GNUNET_FS_Uri *ksk_expect_uri;
43 static struct GNUNET_FS_Handle *fs;
45 static struct GNUNET_FS_SearchContext *sks_search;
47 static struct GNUNET_FS_SearchContext *ksk_search;
49 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
51 static int update_started;
57 struct GNUNET_CONFIGURATION_Handle *cfg;
59 struct GNUNET_OS_Process *arm_proc;
65 setup_peer (struct PeerContext *p, const char *cfgname)
67 p->cfg = GNUNET_CONFIGURATION_create ();
70 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
74 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
79 stop_arm (struct PeerContext *p)
82 if (NULL != p->arm_proc)
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, "ARM process %u stopped\n",
89 GNUNET_OS_process_get_pid (p->arm_proc));
90 GNUNET_OS_process_destroy (p->arm_proc);
94 GNUNET_CONFIGURATION_destroy (p->cfg);
99 abort_ksk_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 if (ksk_search != NULL)
103 GNUNET_FS_search_stop (ksk_search);
105 if (sks_search == NULL)
108 if (GNUNET_SCHEDULER_NO_TASK != kill_task)
109 GNUNET_SCHEDULER_cancel (kill_task);
116 abort_sks_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
118 struct GNUNET_FS_Namespace *ns;
120 if (sks_search == NULL)
122 GNUNET_FS_search_stop (sks_search);
124 ns = GNUNET_FS_namespace_create (fs, "testNamespace");
125 GNUNET_assert (NULL != ns);
126 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
127 if (ksk_search == NULL)
130 if (GNUNET_SCHEDULER_NO_TASK != kill_task)
131 GNUNET_SCHEDULER_cancel (kill_task);
137 do_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
139 FPRINTF (stderr, "%s", "Operation timed out\n");
140 kill_task = GNUNET_SCHEDULER_NO_TASK;
141 abort_sks_search_task (NULL, tc);
142 abort_ksk_search_task (NULL, tc);
148 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
150 switch (event->status)
152 case GNUNET_FS_STATUS_SEARCH_RESULT:
153 if (sks_search == event->value.search.sc)
155 if (!GNUNET_FS_uri_test_equal
156 (sks_expect_uri, event->value.search.specifics.result.uri))
158 FPRINTF (stderr, "%s", "Wrong result for sks search!\n");
161 /* give system 1ms to initiate update search! */
162 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
163 &abort_sks_search_task, NULL);
165 else if (ksk_search == event->value.search.sc)
167 if (!GNUNET_FS_uri_test_equal
168 (ksk_expect_uri, event->value.search.specifics.result.uri))
170 FPRINTF (stderr, "%s", "Wrong result for ksk search!\n");
173 GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task, NULL,
174 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
178 FPRINTF (stderr, "%s", "Unexpected search result received!\n");
182 case GNUNET_FS_STATUS_SEARCH_ERROR:
183 FPRINTF (stderr, "Error searching file: %s\n",
184 event->value.search.specifics.error.message);
185 if (sks_search == event->value.search.sc)
186 GNUNET_SCHEDULER_add_continuation (&abort_sks_search_task, NULL,
187 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
188 else if (ksk_search == event->value.search.sc)
189 GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task, NULL,
190 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
194 case GNUNET_FS_STATUS_SEARCH_START:
195 GNUNET_assert ((NULL == event->value.search.cctx) ||
196 (0 == strcmp ("sks_search", event->value.search.cctx)) ||
197 (0 == strcmp ("ksk_search", event->value.search.cctx)));
198 if (NULL == event->value.search.cctx)
200 GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
201 update_started = GNUNET_YES;
203 GNUNET_assert (1 == event->value.search.anonymity);
205 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
207 case GNUNET_FS_STATUS_SEARCH_STOPPED:
210 FPRINTF (stderr, "Unexpected event: %d\n", event->status);
213 return event->value.search.cctx;
218 publish_cont (void *cls, const struct GNUNET_FS_Uri *ksk_uri, const char *emsg)
221 struct GNUNET_FS_Uri *sks_uri;
223 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
227 FPRINTF (stderr, "Error publishing: %s\n", emsg);
232 GNUNET_CRYPTO_hash_to_enc (&nsid, &enc);
233 GNUNET_snprintf (sbuf, sizeof (sbuf), "gnunet://fs/sks/%s/this", &enc);
234 sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
237 FPRINTF (stderr, "failed to parse URI `%s': %s\n", sbuf, msg);
240 GNUNET_free_non_null (msg);
244 GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
247 GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
249 GNUNET_FS_uri_destroy (sks_uri);
254 sks_cont (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
256 struct GNUNET_CONTAINER_MetaData *meta;
257 struct GNUNET_FS_Uri *ksk_uri;
259 struct GNUNET_FS_BlockOptions bo;
261 meta = GNUNET_CONTAINER_meta_data_create ();
263 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
264 GNUNET_assert (NULL == msg);
265 ksk_expect_uri = GNUNET_FS_uri_dup (uri);
266 bo.content_priority = 1;
267 bo.anonymity_level = 1;
268 bo.replication_level = 0;
270 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
271 GNUNET_FS_publish_ksk (fs, ksk_uri, meta, uri, &bo,
272 GNUNET_FS_PUBLISH_OPTION_NONE, &publish_cont, NULL);
273 GNUNET_FS_uri_destroy (ksk_uri);
274 GNUNET_CONTAINER_meta_data_destroy (meta);
279 adv_cont (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
281 struct GNUNET_CONTAINER_MetaData *meta;
282 struct GNUNET_FS_Namespace *ns;
283 struct GNUNET_FS_BlockOptions bo;
287 FPRINTF (stderr, "Error publishing: %s\n", emsg);
292 ns = GNUNET_FS_namespace_create (fs, "testNamespace");
293 GNUNET_assert (NULL != ns);
294 meta = GNUNET_CONTAINER_meta_data_create ();
295 GNUNET_assert (NULL == emsg);
296 sks_expect_uri = GNUNET_FS_uri_dup (uri);
297 bo.content_priority = 1;
298 bo.anonymity_level = 1;
299 bo.replication_level = 0;
301 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
302 GNUNET_FS_publish_sks (fs, ns, "this", "next", meta, uri, /* FIXME: this is non-sense (use CHK URI!?) */
303 &bo, GNUNET_FS_PUBLISH_OPTION_NONE, &sks_cont, NULL);
304 GNUNET_CONTAINER_meta_data_destroy (meta);
305 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
310 ns_iterator (void *cls, const char *name, const GNUNET_HashCode * id)
314 if (0 != strcmp (name, "testNamespace"))
324 struct GNUNET_FS_Namespace *ns;
325 struct GNUNET_FS_BlockOptions bo;
326 struct GNUNET_CONTAINER_MetaData *meta;
327 struct GNUNET_FS_Uri *ksk_uri;
330 ns = GNUNET_FS_namespace_create (fs, "testNamespace");
331 GNUNET_assert (NULL != ns);
333 GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
336 FPRINTF (stderr, "%s", "namespace_list failed to find namespace!\n");
337 GNUNET_FS_namespace_delete (ns, GNUNET_YES);
342 meta = GNUNET_CONTAINER_meta_data_create ();
343 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
344 bo.content_priority = 1;
345 bo.anonymity_level = 1;
346 bo.replication_level = 0;
348 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
349 GNUNET_FS_namespace_advertise (fs, ksk_uri, ns, meta, &bo, "root", &adv_cont,
352 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &do_timeout,
354 GNUNET_FS_uri_destroy (ksk_uri);
355 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
356 GNUNET_CONTAINER_meta_data_destroy (meta);
361 run (void *cls, char *const *args, const char *cfgfile,
362 const struct GNUNET_CONFIGURATION_Handle *cfg)
364 setup_peer (&p1, "test_fs_namespace_data.conf");
365 fs = GNUNET_FS_start (cfg, "test-fs-namespace", &progress_cb, NULL,
366 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
372 main (int argc, char *argv[])
374 char *const argvx[] = {
377 "test_fs_namespace_data.conf",
380 struct GNUNET_GETOPT_CommandLineOption options[] = {
381 GNUNET_GETOPT_OPTION_END
384 GNUNET_log_setup ("test_fs_namespace",
387 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
388 "test-fs-namespace", "nohelp", options, &run, NULL);
390 if (GNUNET_YES != update_started)
392 FPRINTF (stderr, "%s", "Update search never started!\n");
395 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
400 /* end of test_fs_namespace.c */