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_YES
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 (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,
89 "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
90 GNUNET_OS_process_close (p->arm_proc);
93 if (GNUNET_SCHEDULER_NO_TASK != kill_task)
94 GNUNET_SCHEDULER_cancel (kill_task);
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)
116 abort_sks_search_task (void *cls,
117 const struct GNUNET_SCHEDULER_TaskContext *tc)
119 struct GNUNET_FS_Namespace *ns;
121 if (sks_search == NULL)
123 GNUNET_FS_search_stop (sks_search);
125 ns = GNUNET_FS_namespace_create (fs,
127 GNUNET_assert (NULL != ns);
128 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
129 if (ksk_search == NULL)
137 do_timeout (void *cls,
138 const struct GNUNET_SCHEDULER_TaskContext *tc)
141 "Operation timed out\n");
142 kill_task = GNUNET_SCHEDULER_NO_TASK;
143 abort_sks_search_task (NULL, tc);
144 abort_ksk_search_task (NULL, tc);
150 progress_cb (void *cls,
151 const struct GNUNET_FS_ProgressInfo *event)
153 switch (event->status)
155 case GNUNET_FS_STATUS_SEARCH_RESULT:
156 if (sks_search == event->value.search.sc)
158 if (! GNUNET_FS_uri_test_equal (sks_expect_uri,
159 event->value.search.specifics.result.uri))
162 "Wrong result for sks search!\n");
165 /* give system 1ms to initiate update search! */
166 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
167 &abort_sks_search_task,
170 else if (ksk_search == event->value.search.sc)
172 if (! GNUNET_FS_uri_test_equal (ksk_expect_uri,
173 event->value.search.specifics.result.uri))
176 "Wrong result for ksk search!\n");
179 GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task,
181 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
186 "Unexpected search result received!\n");
190 case GNUNET_FS_STATUS_SEARCH_ERROR:
192 "Error searching file: %s\n",
193 event->value.search.specifics.error.message);
194 if (sks_search == event->value.search.sc)
195 GNUNET_SCHEDULER_add_continuation (&abort_sks_search_task,
197 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
198 else if (ksk_search == event->value.search.sc)
199 GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task,
201 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
205 case GNUNET_FS_STATUS_SEARCH_START:
206 GNUNET_assert ( (NULL == event->value.search.cctx) ||
207 (0 == strcmp ("sks_search", event->value.search.cctx)) ||
208 (0 == strcmp ("ksk_search", event->value.search.cctx)));
209 if (NULL == event->value.search.cctx)
211 GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
212 update_started = GNUNET_YES;
214 GNUNET_assert (1 == event->value.search.anonymity);
216 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
218 case GNUNET_FS_STATUS_SEARCH_STOPPED:
222 "Unexpected event: %d\n",
226 return event->value.search.cctx;
231 publish_cont (void *cls,
232 const struct GNUNET_FS_Uri *ksk_uri,
236 struct GNUNET_FS_Uri *sks_uri;
238 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
242 fprintf (stderr, "Error publishing: %s\n", emsg);
247 GNUNET_CRYPTO_hash_to_enc (&nsid,
249 GNUNET_snprintf (sbuf,
251 "gnunet://fs/sks/%s/this",
253 sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
256 fprintf (stderr, "failed to parse URI `%s': %s\n",
264 ksk_search = GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "ksk_search");
265 sks_search = GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "sks_search");
266 GNUNET_FS_uri_destroy (sks_uri);
272 const struct GNUNET_FS_Uri *uri,
275 struct GNUNET_CONTAINER_MetaData *meta;
276 struct GNUNET_TIME_Absolute expiration;
277 struct GNUNET_FS_Uri *ksk_uri;
280 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
281 meta = GNUNET_CONTAINER_meta_data_create ();
283 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
284 GNUNET_assert (NULL == msg);
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 GNUNET_assert (NULL != ns);
320 meta = GNUNET_CONTAINER_meta_data_create ();
321 GNUNET_assert (NULL == emsg);
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;
360 struct GNUNET_FS_Uri *ksk_uri;
363 ns = GNUNET_FS_namespace_create (fs,
365 GNUNET_assert (NULL != ns);
367 GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
370 fprintf (stderr, "namespace_list failed to find namespace!\n");
371 GNUNET_FS_namespace_delete (ns, GNUNET_YES);
376 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
377 meta = GNUNET_CONTAINER_meta_data_create ();
378 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
379 GNUNET_FS_namespace_advertise (fs,
387 kill_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
390 GNUNET_FS_uri_destroy (ksk_uri);
391 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
392 GNUNET_CONTAINER_meta_data_destroy (meta);
400 const struct GNUNET_CONFIGURATION_Handle *cfg)
402 setup_peer (&p1, "test_fs_namespace_data.conf");
403 fs = GNUNET_FS_start (cfg,
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 */