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;
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 (NULL != ns);
127 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
128 if (ksk_search == NULL)
136 progress_cb (void *cls,
137 const struct GNUNET_FS_ProgressInfo *event)
139 switch (event->status)
141 case GNUNET_FS_STATUS_SEARCH_RESULT:
142 if (sks_search == event->value.search.sc)
144 if (! GNUNET_FS_uri_test_equal (sks_expect_uri,
145 event->value.search.specifics.result.uri))
148 "Wrong result for sks search!\n");
151 /* give system 1ms to initiate update search! */
152 GNUNET_SCHEDULER_add_delayed (sched,
153 GNUNET_TIME_UNIT_MILLISECONDS,
154 &abort_sks_search_task,
157 else if (ksk_search == event->value.search.sc)
159 if (! GNUNET_FS_uri_test_equal (ksk_expect_uri,
160 event->value.search.specifics.result.uri))
163 "Wrong result for ksk search!\n");
166 GNUNET_SCHEDULER_add_continuation (sched,
167 &abort_ksk_search_task,
169 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
174 "Unexpected search result received!\n");
178 case GNUNET_FS_STATUS_SEARCH_ERROR:
180 "Error searching file: %s\n",
181 event->value.search.specifics.error.message);
182 if (sks_search == event->value.search.sc)
183 GNUNET_SCHEDULER_add_continuation (sched,
184 &abort_sks_search_task,
186 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
187 else if (ksk_search == event->value.search.sc)
188 GNUNET_SCHEDULER_add_continuation (sched,
189 &abort_ksk_search_task,
191 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
195 case GNUNET_FS_STATUS_SEARCH_START:
196 GNUNET_assert ( (NULL == event->value.search.cctx) ||
197 (0 == strcmp ("sks_search", event->value.search.cctx)) ||
198 (0 == strcmp ("ksk_search", event->value.search.cctx)));
199 if (NULL == event->value.search.cctx)
201 GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
202 update_started = GNUNET_YES;
204 GNUNET_assert (1 == event->value.search.anonymity);
206 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
208 case GNUNET_FS_STATUS_SEARCH_STOPPED:
212 "Unexpected event: %d\n",
216 return event->value.search.cctx;
221 publish_cont (void *cls,
222 const struct GNUNET_FS_Uri *ksk_uri,
226 struct GNUNET_FS_Uri *sks_uri;
228 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
232 fprintf (stderr, "Error publishing: %s\n", emsg);
237 GNUNET_CRYPTO_hash_to_enc (&nsid,
239 GNUNET_snprintf (sbuf,
241 "gnunet://fs/sks/%s/this",
243 sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
246 fprintf (stderr, "failed to parse URI `%s': %s\n",
254 ksk_search = GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "ksk_search");
255 sks_search = GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "sks_search");
256 GNUNET_FS_uri_destroy (sks_uri);
262 const struct GNUNET_FS_Uri *uri,
265 struct GNUNET_CONTAINER_MetaData *meta;
266 struct GNUNET_TIME_Absolute expiration;
267 struct GNUNET_FS_Uri *ksk_uri;
270 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
271 meta = GNUNET_CONTAINER_meta_data_create ();
273 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
274 GNUNET_assert (NULL == msg);
275 ksk_expect_uri = GNUNET_FS_uri_dup (uri);
276 GNUNET_FS_publish_ksk (fs,
282 GNUNET_FS_PUBLISH_OPTION_NONE,
285 GNUNET_FS_uri_destroy (ksk_uri);
286 GNUNET_CONTAINER_meta_data_destroy (meta);
292 const struct GNUNET_FS_Uri *uri,
295 struct GNUNET_CONTAINER_MetaData *meta;
296 struct GNUNET_FS_Namespace *ns;
297 struct GNUNET_TIME_Absolute expiration;
301 fprintf (stderr, "Error publishing: %s\n", emsg);
306 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
307 ns = GNUNET_FS_namespace_create (fs,
309 GNUNET_assert (NULL != ns);
310 meta = GNUNET_CONTAINER_meta_data_create ();
311 GNUNET_assert (NULL == emsg);
312 sks_expect_uri = GNUNET_FS_uri_dup (uri);
313 GNUNET_FS_publish_sks (fs,
318 uri, /* FIXME: this is non-sense (use CHK URI!?) */
321 GNUNET_FS_PUBLISH_OPTION_NONE,
324 GNUNET_CONTAINER_meta_data_destroy (meta);
325 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
330 ns_iterator (void *cls,
332 const GNUNET_HashCode *id)
336 if (0 != strcmp (name,
347 struct GNUNET_FS_Namespace *ns;
348 struct GNUNET_TIME_Absolute expiration;
349 struct GNUNET_CONTAINER_MetaData *meta;
350 struct GNUNET_FS_Uri *ksk_uri;
353 ns = GNUNET_FS_namespace_create (fs,
355 GNUNET_assert (NULL != ns);
357 GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
360 fprintf (stderr, "namespace_list failed to find namespace!\n");
361 GNUNET_FS_namespace_delete (ns, GNUNET_YES);
366 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
367 meta = GNUNET_CONTAINER_meta_data_create ();
368 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
369 GNUNET_FS_namespace_advertise (fs,
377 GNUNET_FS_uri_destroy (ksk_uri);
378 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
379 GNUNET_CONTAINER_meta_data_destroy (meta);
385 struct GNUNET_SCHEDULER_Handle *s,
388 const struct GNUNET_CONFIGURATION_Handle *cfg)
391 setup_peer (&p1, "test_fs_namespace_data.conf");
392 fs = GNUNET_FS_start (sched,
397 GNUNET_FS_FLAGS_NONE,
398 GNUNET_FS_OPTIONS_END);
404 main (int argc, char *argv[])
406 char *const argvx[] = {
409 "test_fs_namespace_data.conf",
415 struct GNUNET_GETOPT_CommandLineOption options[] = {
416 GNUNET_GETOPT_OPTION_END
419 GNUNET_log_setup ("test_fs_namespace",
426 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
427 argvx, "test-fs-namespace",
428 "nohelp", options, &run, NULL);
430 if (GNUNET_YES != update_started)
433 "Update search never started!\n");
436 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
441 /* end of test_fs_namespace.c */