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 PeerContext p1;
40 static GNUNET_HashCode nsid;
42 static struct GNUNET_FS_Uri *sks_expect_uri;
44 static struct GNUNET_FS_Uri *ksk_expect_uri;
46 static struct GNUNET_FS_Handle *fs;
48 static struct GNUNET_FS_SearchContext *sks_search;
50 static struct GNUNET_FS_SearchContext *ksk_search;
52 static int update_started;
58 struct GNUNET_CONFIGURATION_Handle *cfg;
60 struct GNUNET_OS_Process *arm_proc;
66 setup_peer (struct PeerContext *p, const char *cfgname)
68 p->cfg = GNUNET_CONFIGURATION_create ();
70 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
77 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
82 stop_arm (struct PeerContext *p)
85 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
86 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
87 if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
88 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90 "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
91 GNUNET_OS_process_close (p->arm_proc);
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 (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 (&abort_ksk_search_task,
167 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
172 "Unexpected search result received!\n");
176 case GNUNET_FS_STATUS_SEARCH_ERROR:
178 "Error searching file: %s\n",
179 event->value.search.specifics.error.message);
180 if (sks_search == event->value.search.sc)
181 GNUNET_SCHEDULER_add_continuation (&abort_sks_search_task,
183 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
184 else if (ksk_search == event->value.search.sc)
185 GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task,
187 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
191 case GNUNET_FS_STATUS_SEARCH_START:
192 GNUNET_assert ( (NULL == event->value.search.cctx) ||
193 (0 == strcmp ("sks_search", event->value.search.cctx)) ||
194 (0 == strcmp ("ksk_search", event->value.search.cctx)));
195 if (NULL == event->value.search.cctx)
197 GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
198 update_started = GNUNET_YES;
200 GNUNET_assert (1 == event->value.search.anonymity);
202 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
204 case GNUNET_FS_STATUS_SEARCH_STOPPED:
208 "Unexpected event: %d\n",
212 return event->value.search.cctx;
217 publish_cont (void *cls,
218 const struct GNUNET_FS_Uri *ksk_uri,
222 struct GNUNET_FS_Uri *sks_uri;
224 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
228 fprintf (stderr, "Error publishing: %s\n", emsg);
233 GNUNET_CRYPTO_hash_to_enc (&nsid,
235 GNUNET_snprintf (sbuf,
237 "gnunet://fs/sks/%s/this",
239 sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
242 fprintf (stderr, "failed to parse URI `%s': %s\n",
250 ksk_search = GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "ksk_search");
251 sks_search = GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "sks_search");
252 GNUNET_FS_uri_destroy (sks_uri);
258 const struct GNUNET_FS_Uri *uri,
261 struct GNUNET_CONTAINER_MetaData *meta;
262 struct GNUNET_TIME_Absolute expiration;
263 struct GNUNET_FS_Uri *ksk_uri;
266 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
267 meta = GNUNET_CONTAINER_meta_data_create ();
269 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
270 GNUNET_assert (NULL == msg);
271 ksk_expect_uri = GNUNET_FS_uri_dup (uri);
272 GNUNET_FS_publish_ksk (fs,
278 GNUNET_FS_PUBLISH_OPTION_NONE,
281 GNUNET_FS_uri_destroy (ksk_uri);
282 GNUNET_CONTAINER_meta_data_destroy (meta);
288 const struct GNUNET_FS_Uri *uri,
291 struct GNUNET_CONTAINER_MetaData *meta;
292 struct GNUNET_FS_Namespace *ns;
293 struct GNUNET_TIME_Absolute expiration;
297 fprintf (stderr, "Error publishing: %s\n", emsg);
302 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
303 ns = GNUNET_FS_namespace_create (fs,
305 GNUNET_assert (NULL != ns);
306 meta = GNUNET_CONTAINER_meta_data_create ();
307 GNUNET_assert (NULL == emsg);
308 sks_expect_uri = GNUNET_FS_uri_dup (uri);
309 GNUNET_FS_publish_sks (fs,
314 uri, /* FIXME: this is non-sense (use CHK URI!?) */
317 GNUNET_FS_PUBLISH_OPTION_NONE,
320 GNUNET_CONTAINER_meta_data_destroy (meta);
321 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
326 ns_iterator (void *cls,
328 const GNUNET_HashCode *id)
332 if (0 != strcmp (name,
343 struct GNUNET_FS_Namespace *ns;
344 struct GNUNET_TIME_Absolute expiration;
345 struct GNUNET_CONTAINER_MetaData *meta;
346 struct GNUNET_FS_Uri *ksk_uri;
349 ns = GNUNET_FS_namespace_create (fs,
351 GNUNET_assert (NULL != ns);
353 GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
356 fprintf (stderr, "namespace_list failed to find namespace!\n");
357 GNUNET_FS_namespace_delete (ns, GNUNET_YES);
362 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
363 meta = GNUNET_CONTAINER_meta_data_create ();
364 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
365 GNUNET_FS_namespace_advertise (fs,
373 GNUNET_FS_uri_destroy (ksk_uri);
374 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
375 GNUNET_CONTAINER_meta_data_destroy (meta);
383 const struct GNUNET_CONFIGURATION_Handle *cfg)
385 setup_peer (&p1, "test_fs_namespace_data.conf");
386 fs = GNUNET_FS_start (cfg,
390 GNUNET_FS_FLAGS_NONE,
391 GNUNET_FS_OPTIONS_END);
397 main (int argc, char *argv[])
399 char *const argvx[] = {
402 "test_fs_namespace_data.conf",
408 struct GNUNET_GETOPT_CommandLineOption options[] = {
409 GNUNET_GETOPT_OPTION_END
412 GNUNET_log_setup ("test_fs_namespace",
419 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
420 argvx, "test-fs-namespace",
421 "nohelp", options, &run, NULL);
423 if (GNUNET_YES != update_started)
426 "Update search never started!\n");
429 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
434 /* end of test_fs_namespace.c */