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 2, 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;
68 setup_peer (struct PeerContext *p, const char *cfgname)
70 p->cfg = GNUNET_CONFIGURATION_create ();
72 p->arm_pid = 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 != PLIBC_KILL (p->arm_pid, SIGTERM))
88 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
89 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
90 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
92 "ARM process %u stopped\n", p->arm_pid);
94 GNUNET_CONFIGURATION_destroy (p->cfg);
99 abort_ksk_search_task (void *cls,
100 const struct GNUNET_SCHEDULER_TaskContext *tc)
102 if (ksk_search != NULL)
104 GNUNET_FS_search_stop (ksk_search);
106 if (sks_search == NULL)
115 abort_sks_search_task (void *cls,
116 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,
126 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
127 if (ksk_search == NULL)
135 progress_cb (void *cls,
136 const struct GNUNET_FS_ProgressInfo *event)
138 switch (event->status)
140 case GNUNET_FS_STATUS_SEARCH_RESULT:
141 if (sks_search == event->value.search.sc)
143 if (! GNUNET_FS_uri_test_equal (sks_expect_uri,
144 event->value.search.specifics.result.uri))
147 "Wrong result for sks search!\n");
150 /* give system 1ms to initiate update search! */
151 GNUNET_SCHEDULER_add_delayed (sched,
152 GNUNET_TIME_UNIT_MILLISECONDS,
153 &abort_sks_search_task,
156 else if (ksk_search == event->value.search.sc)
158 if (! GNUNET_FS_uri_test_equal (ksk_expect_uri,
159 event->value.search.specifics.result.uri))
162 "Wrong result for ksk search!\n");
165 GNUNET_SCHEDULER_add_continuation (sched,
166 &abort_ksk_search_task,
168 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
173 "Unexpected search result received!\n");
177 case GNUNET_FS_STATUS_SEARCH_ERROR:
179 "Error searching file: %s\n",
180 event->value.search.specifics.error.message);
181 if (sks_search == event->value.search.sc)
182 GNUNET_SCHEDULER_add_continuation (sched,
183 &abort_sks_search_task,
185 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
186 else if (ksk_search == event->value.search.sc)
187 GNUNET_SCHEDULER_add_continuation (sched,
188 &abort_ksk_search_task,
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:
211 "Unexpected event: %d\n",
215 return event->value.search.cctx;
220 publish_cont (void *cls,
221 const struct GNUNET_FS_Uri *ksk_uri,
225 struct GNUNET_FS_Uri *sks_uri;
227 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
231 fprintf (stderr, "Error publishing: %s\n", emsg);
236 GNUNET_CRYPTO_hash_to_enc (&nsid,
238 GNUNET_snprintf (sbuf,
240 "gnunet://fs/sks/%s/this",
242 sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
245 fprintf (stderr, "failed to parse URI `%s': %s\n",
253 ksk_search = GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "ksk_search");
254 sks_search = GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "sks_search");
255 GNUNET_FS_uri_destroy (sks_uri);
261 const struct GNUNET_FS_Uri *uri,
264 struct GNUNET_CONTAINER_MetaData *meta;
265 struct GNUNET_TIME_Absolute expiration;
266 struct GNUNET_FS_Uri *ksk_uri;
269 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
270 meta = GNUNET_CONTAINER_meta_data_create ();
272 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
273 GNUNET_assert (NULL == msg);
274 ksk_expect_uri = GNUNET_FS_uri_dup (uri);
275 GNUNET_FS_publish_ksk (fs,
281 GNUNET_FS_PUBLISH_OPTION_NONE,
284 GNUNET_FS_uri_destroy (ksk_uri);
285 GNUNET_CONTAINER_meta_data_destroy (meta);
291 const struct GNUNET_FS_Uri *uri,
294 struct GNUNET_CONTAINER_MetaData *meta;
295 struct GNUNET_FS_Namespace *ns;
296 struct GNUNET_TIME_Absolute expiration;
300 fprintf (stderr, "Error publishing: %s\n", emsg);
305 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
306 ns = GNUNET_FS_namespace_create (fs,
308 meta = GNUNET_CONTAINER_meta_data_create ();
309 GNUNET_assert (NULL == emsg);
310 sks_expect_uri = GNUNET_FS_uri_dup (uri);
311 GNUNET_FS_publish_sks (fs,
316 uri, /* FIXME: this is non-sense (use CHK URI!?) */
319 GNUNET_FS_PUBLISH_OPTION_NONE,
322 GNUNET_CONTAINER_meta_data_destroy (meta);
323 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
328 ns_iterator (void *cls,
330 const GNUNET_HashCode *id)
334 if (0 != strcmp (name,
345 struct GNUNET_FS_Namespace *ns;
346 struct GNUNET_TIME_Absolute expiration;
347 struct GNUNET_CONTAINER_MetaData *meta;
348 struct GNUNET_FS_Uri *ksk_uri;
351 ns = GNUNET_FS_namespace_create (fs,
354 GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
357 fprintf (stderr, "namespace_list failed to find namespace!\n");
358 GNUNET_FS_namespace_delete (ns, GNUNET_YES);
363 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
364 meta = GNUNET_CONTAINER_meta_data_create ();
365 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
366 GNUNET_FS_namespace_advertise (fs,
374 GNUNET_FS_uri_destroy (ksk_uri);
375 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
376 GNUNET_CONTAINER_meta_data_destroy (meta);
382 struct GNUNET_SCHEDULER_Handle *s,
385 const struct GNUNET_CONFIGURATION_Handle *cfg)
388 setup_peer (&p1, "test_fs_namespace_data.conf");
389 fs = GNUNET_FS_start (sched,
394 GNUNET_FS_FLAGS_NONE,
395 GNUNET_FS_OPTIONS_END);
401 main (int argc, char *argv[])
403 char *const argvx[] = {
406 "test_fs_namespace_data.conf",
412 struct GNUNET_GETOPT_CommandLineOption options[] = {
413 GNUNET_GETOPT_OPTION_END
416 GNUNET_log_setup ("test_fs_namespace",
423 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
424 argvx, "test-fs-namespace",
425 "nohelp", options, &run, NULL);
427 if (GNUNET_YES != update_started)
430 "Update search never started!\n");
433 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
438 /* end of test_fs_namespace.c */