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
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;
62 struct GNUNET_OS_Process *arm_proc;
68 setup_peer (struct PeerContext *p, const char *cfgname)
70 p->cfg = GNUNET_CONFIGURATION_create ();
72 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
79 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
84 stop_arm (struct PeerContext *p)
87 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
88 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
89 if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
90 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
92 "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
93 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)
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 (NULL != ns);
129 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
130 if (ksk_search == NULL)
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)
146 if (! GNUNET_FS_uri_test_equal (sks_expect_uri,
147 event->value.search.specifics.result.uri))
150 "Wrong result for sks search!\n");
153 /* give system 1ms to initiate update search! */
154 GNUNET_SCHEDULER_add_delayed (sched,
155 GNUNET_TIME_UNIT_MILLISECONDS,
156 &abort_sks_search_task,
159 else if (ksk_search == event->value.search.sc)
161 if (! GNUNET_FS_uri_test_equal (ksk_expect_uri,
162 event->value.search.specifics.result.uri))
165 "Wrong result for ksk search!\n");
168 GNUNET_SCHEDULER_add_continuation (sched,
169 &abort_ksk_search_task,
171 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
176 "Unexpected search result received!\n");
180 case GNUNET_FS_STATUS_SEARCH_ERROR:
182 "Error searching file: %s\n",
183 event->value.search.specifics.error.message);
184 if (sks_search == event->value.search.sc)
185 GNUNET_SCHEDULER_add_continuation (sched,
186 &abort_sks_search_task,
188 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
189 else if (ksk_search == event->value.search.sc)
190 GNUNET_SCHEDULER_add_continuation (sched,
191 &abort_ksk_search_task,
193 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
197 case GNUNET_FS_STATUS_SEARCH_START:
198 GNUNET_assert ( (NULL == event->value.search.cctx) ||
199 (0 == strcmp ("sks_search", event->value.search.cctx)) ||
200 (0 == strcmp ("ksk_search", event->value.search.cctx)));
201 if (NULL == event->value.search.cctx)
203 GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
204 update_started = GNUNET_YES;
206 GNUNET_assert (1 == event->value.search.anonymity);
208 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
210 case GNUNET_FS_STATUS_SEARCH_STOPPED:
214 "Unexpected event: %d\n",
218 return event->value.search.cctx;
223 publish_cont (void *cls,
224 const struct GNUNET_FS_Uri *ksk_uri,
228 struct GNUNET_FS_Uri *sks_uri;
230 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
234 fprintf (stderr, "Error publishing: %s\n", emsg);
239 GNUNET_CRYPTO_hash_to_enc (&nsid,
241 GNUNET_snprintf (sbuf,
243 "gnunet://fs/sks/%s/this",
245 sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
248 fprintf (stderr, "failed to parse URI `%s': %s\n",
256 ksk_search = GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "ksk_search");
257 sks_search = GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "sks_search");
258 GNUNET_FS_uri_destroy (sks_uri);
264 const struct GNUNET_FS_Uri *uri,
267 struct GNUNET_CONTAINER_MetaData *meta;
268 struct GNUNET_TIME_Absolute expiration;
269 struct GNUNET_FS_Uri *ksk_uri;
272 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
273 meta = GNUNET_CONTAINER_meta_data_create ();
275 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
276 GNUNET_assert (NULL == msg);
277 ksk_expect_uri = GNUNET_FS_uri_dup (uri);
278 GNUNET_FS_publish_ksk (fs,
284 GNUNET_FS_PUBLISH_OPTION_NONE,
287 GNUNET_FS_uri_destroy (ksk_uri);
288 GNUNET_CONTAINER_meta_data_destroy (meta);
294 const struct GNUNET_FS_Uri *uri,
297 struct GNUNET_CONTAINER_MetaData *meta;
298 struct GNUNET_FS_Namespace *ns;
299 struct GNUNET_TIME_Absolute expiration;
303 fprintf (stderr, "Error publishing: %s\n", emsg);
308 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
309 ns = GNUNET_FS_namespace_create (fs,
311 GNUNET_assert (NULL != ns);
312 meta = GNUNET_CONTAINER_meta_data_create ();
313 GNUNET_assert (NULL == emsg);
314 sks_expect_uri = GNUNET_FS_uri_dup (uri);
315 GNUNET_FS_publish_sks (fs,
320 uri, /* FIXME: this is non-sense (use CHK URI!?) */
323 GNUNET_FS_PUBLISH_OPTION_NONE,
326 GNUNET_CONTAINER_meta_data_destroy (meta);
327 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
332 ns_iterator (void *cls,
334 const GNUNET_HashCode *id)
338 if (0 != strcmp (name,
349 struct GNUNET_FS_Namespace *ns;
350 struct GNUNET_TIME_Absolute expiration;
351 struct GNUNET_CONTAINER_MetaData *meta;
352 struct GNUNET_FS_Uri *ksk_uri;
355 ns = GNUNET_FS_namespace_create (fs,
357 GNUNET_assert (NULL != ns);
359 GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
362 fprintf (stderr, "namespace_list failed to find namespace!\n");
363 GNUNET_FS_namespace_delete (ns, GNUNET_YES);
368 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
369 meta = GNUNET_CONTAINER_meta_data_create ();
370 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
371 GNUNET_FS_namespace_advertise (fs,
379 GNUNET_FS_uri_destroy (ksk_uri);
380 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
381 GNUNET_CONTAINER_meta_data_destroy (meta);
387 struct GNUNET_SCHEDULER_Handle *s,
390 const struct GNUNET_CONFIGURATION_Handle *cfg)
393 setup_peer (&p1, "test_fs_namespace_data.conf");
394 fs = GNUNET_FS_start (sched,
399 GNUNET_FS_FLAGS_NONE,
400 GNUNET_FS_OPTIONS_END);
406 main (int argc, char *argv[])
408 char *const argvx[] = {
411 "test_fs_namespace_data.conf",
417 struct GNUNET_GETOPT_CommandLineOption options[] = {
418 GNUNET_GETOPT_OPTION_END
421 GNUNET_log_setup ("test_fs_namespace",
428 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
429 argvx, "test-fs-namespace",
430 "nohelp", options, &run, NULL);
432 if (GNUNET_YES != update_started)
435 "Update search never started!\n");
438 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
443 /* end of test_fs_namespace.c */