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_NO
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 GNUNET_CONFIGURATION_destroy (p->cfg);
98 abort_ksk_search_task (void *cls,
99 const struct GNUNET_SCHEDULER_TaskContext *tc)
101 if (ksk_search != NULL)
103 GNUNET_FS_search_stop (ksk_search);
105 if (sks_search == NULL)
108 if (GNUNET_SCHEDULER_NO_TASK != kill_task)
109 GNUNET_SCHEDULER_cancel (kill_task);
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)
132 if (GNUNET_SCHEDULER_NO_TASK != kill_task)
133 GNUNET_SCHEDULER_cancel (kill_task);
139 do_timeout (void *cls,
140 const struct GNUNET_SCHEDULER_TaskContext *tc)
143 "Operation timed out\n");
144 kill_task = GNUNET_SCHEDULER_NO_TASK;
145 abort_sks_search_task (NULL, tc);
146 abort_ksk_search_task (NULL, tc);
152 progress_cb (void *cls,
153 const struct GNUNET_FS_ProgressInfo *event)
155 switch (event->status)
157 case GNUNET_FS_STATUS_SEARCH_RESULT:
158 if (sks_search == event->value.search.sc)
160 if (! GNUNET_FS_uri_test_equal (sks_expect_uri,
161 event->value.search.specifics.result.uri))
164 "Wrong result for sks search!\n");
167 /* give system 1ms to initiate update search! */
168 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
169 &abort_sks_search_task,
172 else if (ksk_search == event->value.search.sc)
174 if (! GNUNET_FS_uri_test_equal (ksk_expect_uri,
175 event->value.search.specifics.result.uri))
178 "Wrong result for ksk search!\n");
181 GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task,
183 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
188 "Unexpected search result received!\n");
192 case GNUNET_FS_STATUS_SEARCH_ERROR:
194 "Error searching file: %s\n",
195 event->value.search.specifics.error.message);
196 if (sks_search == event->value.search.sc)
197 GNUNET_SCHEDULER_add_continuation (&abort_sks_search_task,
199 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
200 else if (ksk_search == event->value.search.sc)
201 GNUNET_SCHEDULER_add_continuation (&abort_ksk_search_task,
203 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
207 case GNUNET_FS_STATUS_SEARCH_START:
208 GNUNET_assert ( (NULL == event->value.search.cctx) ||
209 (0 == strcmp ("sks_search", event->value.search.cctx)) ||
210 (0 == strcmp ("ksk_search", event->value.search.cctx)));
211 if (NULL == event->value.search.cctx)
213 GNUNET_assert (0 == strcmp ("sks_search", event->value.search.pctx));
214 update_started = GNUNET_YES;
216 GNUNET_assert (1 == event->value.search.anonymity);
218 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
220 case GNUNET_FS_STATUS_SEARCH_STOPPED:
224 "Unexpected event: %d\n",
228 return event->value.search.cctx;
233 publish_cont (void *cls,
234 const struct GNUNET_FS_Uri *ksk_uri,
238 struct GNUNET_FS_Uri *sks_uri;
240 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
244 fprintf (stderr, "Error publishing: %s\n", emsg);
249 GNUNET_CRYPTO_hash_to_enc (&nsid,
251 GNUNET_snprintf (sbuf,
253 "gnunet://fs/sks/%s/this",
255 sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
258 fprintf (stderr, "failed to parse URI `%s': %s\n",
266 ksk_search = GNUNET_FS_search_start (fs, ksk_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "ksk_search");
267 sks_search = GNUNET_FS_search_start (fs, sks_uri, 1, GNUNET_FS_SEARCH_OPTION_NONE, "sks_search");
268 GNUNET_FS_uri_destroy (sks_uri);
274 const struct GNUNET_FS_Uri *uri,
277 struct GNUNET_CONTAINER_MetaData *meta;
278 struct GNUNET_FS_Uri *ksk_uri;
280 struct GNUNET_FS_BlockOptions bo;
282 meta = GNUNET_CONTAINER_meta_data_create ();
284 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
285 GNUNET_assert (NULL == msg);
286 ksk_expect_uri = GNUNET_FS_uri_dup (uri);
287 bo.content_priority = 1;
288 bo.anonymity_level = 1;
289 bo.replication_level = 0;
290 bo.expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
291 GNUNET_FS_publish_ksk (fs,
296 GNUNET_FS_PUBLISH_OPTION_NONE,
299 GNUNET_FS_uri_destroy (ksk_uri);
300 GNUNET_CONTAINER_meta_data_destroy (meta);
306 const struct GNUNET_FS_Uri *uri,
309 struct GNUNET_CONTAINER_MetaData *meta;
310 struct GNUNET_FS_Namespace *ns;
311 struct GNUNET_FS_BlockOptions bo;
315 fprintf (stderr, "Error publishing: %s\n", emsg);
320 ns = GNUNET_FS_namespace_create (fs,
322 GNUNET_assert (NULL != ns);
323 meta = GNUNET_CONTAINER_meta_data_create ();
324 GNUNET_assert (NULL == emsg);
325 sks_expect_uri = GNUNET_FS_uri_dup (uri);
326 bo.content_priority = 1;
327 bo.anonymity_level = 1;
328 bo.replication_level = 0;
329 bo.expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
330 GNUNET_FS_publish_sks (fs,
335 uri, /* FIXME: this is non-sense (use CHK URI!?) */
337 GNUNET_FS_PUBLISH_OPTION_NONE,
340 GNUNET_CONTAINER_meta_data_destroy (meta);
341 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
346 ns_iterator (void *cls,
348 const GNUNET_HashCode *id)
352 if (0 != strcmp (name,
363 struct GNUNET_FS_Namespace *ns;
364 struct GNUNET_FS_BlockOptions bo;
365 struct GNUNET_CONTAINER_MetaData *meta;
366 struct GNUNET_FS_Uri *ksk_uri;
369 ns = GNUNET_FS_namespace_create (fs,
371 GNUNET_assert (NULL != ns);
373 GNUNET_FS_namespace_list (fs, &ns_iterator, &ok);
376 fprintf (stderr, "namespace_list failed to find namespace!\n");
377 GNUNET_FS_namespace_delete (ns, GNUNET_YES);
382 meta = GNUNET_CONTAINER_meta_data_create ();
383 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/testnsa", NULL);
384 bo.content_priority = 1;
385 bo.anonymity_level = 1;
386 bo.replication_level = 0;
387 bo.expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
388 GNUNET_FS_namespace_advertise (fs,
395 kill_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
398 GNUNET_FS_uri_destroy (ksk_uri);
399 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
400 GNUNET_CONTAINER_meta_data_destroy (meta);
408 const struct GNUNET_CONFIGURATION_Handle *cfg)
410 setup_peer (&p1, "test_fs_namespace_data.conf");
411 fs = GNUNET_FS_start (cfg,
415 GNUNET_FS_FLAGS_NONE,
416 GNUNET_FS_OPTIONS_END);
422 main (int argc, char *argv[])
424 char *const argvx[] = {
427 "test_fs_namespace_data.conf",
433 struct GNUNET_GETOPT_CommandLineOption options[] = {
434 GNUNET_GETOPT_OPTION_END
437 GNUNET_log_setup ("test_fs_namespace",
444 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
445 argvx, "test-fs-namespace",
446 "nohelp", options, &run, NULL);
448 if (GNUNET_YES != update_started)
451 "Update search never started!\n");
454 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
459 /* end of test_fs_namespace.c */