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 ();
69 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
76 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
81 stop_arm (struct PeerContext *p)
84 if (NULL != p->arm_proc)
86 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
87 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
88 if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
89 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91 "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
92 GNUNET_OS_process_close (p->arm_proc);
96 GNUNET_CONFIGURATION_destroy (p->cfg);
101 abort_ksk_search_task (void *cls,
102 const struct GNUNET_SCHEDULER_TaskContext *tc)
104 if (ksk_search != NULL)
106 GNUNET_FS_search_stop (ksk_search);
108 if (sks_search == NULL)
111 if (GNUNET_SCHEDULER_NO_TASK != kill_task)
112 GNUNET_SCHEDULER_cancel (kill_task);
119 abort_sks_search_task (void *cls,
120 const struct GNUNET_SCHEDULER_TaskContext *tc)
122 struct GNUNET_FS_Namespace *ns;
124 if (sks_search == NULL)
126 GNUNET_FS_search_stop (sks_search);
128 ns = GNUNET_FS_namespace_create (fs,
130 GNUNET_assert (NULL != ns);
131 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
132 if (ksk_search == NULL)
135 if (GNUNET_SCHEDULER_NO_TASK != kill_task)
136 GNUNET_SCHEDULER_cancel (kill_task);
142 do_timeout (void *cls,
143 const struct GNUNET_SCHEDULER_TaskContext *tc)
146 "Operation timed out\n");
147 kill_task = GNUNET_SCHEDULER_NO_TASK;
148 abort_sks_search_task (NULL, tc);
149 abort_ksk_search_task (NULL, tc);
155 progress_cb (void *cls,
156 const struct GNUNET_FS_ProgressInfo *event)
158 switch (event->status)
160 case GNUNET_FS_STATUS_SEARCH_RESULT:
161 if (sks_search == event->value.search.sc)
163 if (! GNUNET_FS_uri_test_equal (sks_expect_uri,
164 event->value.search.specifics.result.uri))
167 "Wrong result for sks search!\n");
170 /* give system 1ms to initiate update search! */
171 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
172 &abort_sks_search_task,
175 else if (ksk_search == event->value.search.sc)
177 if (! GNUNET_FS_uri_test_equal (ksk_expect_uri,
178 event->value.search.specifics.result.uri))
181 "Wrong result for ksk search!\n");
184 GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task,
186 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
191 "Unexpected search result received!\n");
195 case GNUNET_FS_STATUS_SEARCH_ERROR:
197 "Error searching file: %s\n",
198 event->value.search.specifics.error.message);
199 if (sks_search == event->value.search.sc)
200 GNUNET_SCHEDULER_add_continuation (&abort_sks_search_task,
202 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
203 else if (ksk_search == event->value.search.sc)
204 GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task,
206 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
210 case GNUNET_FS_STATUS_SEARCH_START:
211 GNUNET_assert ( (NULL == event->value.search.cctx) ||
212 (0 == strcmp ("sks_search", event->value.search.cctx)) ||
213 (0 == strcmp ("ksk_search", event->value.search.cctx)));
214 if (NULL == event->value.search.cctx)
216 GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
217 update_started = GNUNET_YES;
219 GNUNET_assert (1 == event->value.search.anonymity);
221 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
223 case GNUNET_FS_STATUS_SEARCH_STOPPED:
227 "Unexpected event: %d\n",
231 return event->value.search.cctx;
236 publish_cont (void *cls,
237 const struct GNUNET_FS_Uri *ksk_uri,
241 struct GNUNET_FS_Uri *sks_uri;
243 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
247 fprintf (stderr, "Error publishing: %s\n", emsg);
252 GNUNET_CRYPTO_hash_to_enc (&nsid,
254 GNUNET_snprintf (sbuf,
256 "gnunet://fs/sks/%s/this",
258 sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
261 fprintf (stderr, "failed to parse URI `%s': %s\n",
269 ksk_search = GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "ksk_search");
270 sks_search = GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "sks_search");
271 GNUNET_FS_uri_destroy (sks_uri);
277 const struct GNUNET_FS_Uri *uri,
280 struct GNUNET_CONTAINER_MetaData *meta;
281 struct GNUNET_FS_Uri *ksk_uri;
283 struct GNUNET_FS_BlockOptions bo;
285 meta = GNUNET_CONTAINER_meta_data_create ();
287 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
288 GNUNET_assert (NULL == msg);
289 ksk_expect_uri = GNUNET_FS_uri_dup (uri);
290 bo.content_priority = 1;
291 bo.anonymity_level = 1;
292 bo.replication_level = 0;
293 bo.expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
294 GNUNET_FS_publish_ksk (fs,
299 GNUNET_FS_PUBLISH_OPTION_NONE,
302 GNUNET_FS_uri_destroy (ksk_uri);
303 GNUNET_CONTAINER_meta_data_destroy (meta);
309 const struct GNUNET_FS_Uri *uri,
312 struct GNUNET_CONTAINER_MetaData *meta;
313 struct GNUNET_FS_Namespace *ns;
314 struct GNUNET_FS_BlockOptions bo;
318 fprintf (stderr, "Error publishing: %s\n", emsg);
323 ns = GNUNET_FS_namespace_create (fs,
325 GNUNET_assert (NULL != ns);
326 meta = GNUNET_CONTAINER_meta_data_create ();
327 GNUNET_assert (NULL == emsg);
328 sks_expect_uri = GNUNET_FS_uri_dup (uri);
329 bo.content_priority = 1;
330 bo.anonymity_level = 1;
331 bo.replication_level = 0;
332 bo.expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
333 GNUNET_FS_publish_sks (fs,
338 uri, /* FIXME: this is non-sense (use CHK URI!?) */
340 GNUNET_FS_PUBLISH_OPTION_NONE,
343 GNUNET_CONTAINER_meta_data_destroy (meta);
344 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
349 ns_iterator (void *cls,
351 const GNUNET_HashCode *id)
355 if (0 != strcmp (name,
366 struct GNUNET_FS_Namespace *ns;
367 struct GNUNET_FS_BlockOptions bo;
368 struct GNUNET_CONTAINER_MetaData *meta;
369 struct GNUNET_FS_Uri *ksk_uri;
372 ns = GNUNET_FS_namespace_create (fs,
374 GNUNET_assert (NULL != ns);
376 GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
379 fprintf (stderr, "namespace_list failed to find namespace!\n");
380 GNUNET_FS_namespace_delete (ns, GNUNET_YES);
385 meta = GNUNET_CONTAINER_meta_data_create ();
386 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
387 bo.content_priority = 1;
388 bo.anonymity_level = 1;
389 bo.replication_level = 0;
390 bo.expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
391 GNUNET_FS_namespace_advertise (fs,
398 kill_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
401 GNUNET_FS_uri_destroy (ksk_uri);
402 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
403 GNUNET_CONTAINER_meta_data_destroy (meta);
411 const struct GNUNET_CONFIGURATION_Handle *cfg)
413 setup_peer (&p1, "test_fs_namespace_data.conf");
414 fs = GNUNET_FS_start (cfg,
418 GNUNET_FS_FLAGS_NONE,
419 GNUNET_FS_OPTIONS_END);
425 main (int argc, char *argv[])
427 char *const argvx[] = {
430 "test_fs_namespace_data.conf",
436 struct GNUNET_GETOPT_CommandLineOption options[] = {
437 GNUNET_GETOPT_OPTION_END
440 GNUNET_log_setup ("test_fs_namespace",
447 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
448 argvx, "test-fs-namespace",
449 "nohelp", options, &run, NULL);
451 if (GNUNET_YES != update_started)
454 "Update search never started!\n");
457 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
462 /* end of test_fs_namespace.c */