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
28 * - child search of "sks" search (the "next" identifier) is not stopped => no termination!
33 #include "gnunet_util_lib.h"
34 #include "gnunet_arm_service.h"
35 #include "gnunet_fs_service.h"
37 #define VERBOSE GNUNET_YES
39 #define START_ARM GNUNET_YES
41 static struct GNUNET_SCHEDULER_Handle *sched;
43 static struct PeerContext p1;
45 static GNUNET_HashCode nsid;
47 static struct GNUNET_FS_Uri *sks_expect_uri;
49 static struct GNUNET_FS_Uri *ksk_expect_uri;
51 static struct GNUNET_FS_Handle *fs;
53 static struct GNUNET_FS_SearchContext *sks_search;
55 static struct GNUNET_FS_SearchContext *ksk_search;
57 static int update_started;
63 struct GNUNET_CONFIGURATION_Handle *cfg;
71 setup_peer (struct PeerContext *p, const char *cfgname)
73 p->cfg = GNUNET_CONFIGURATION_create ();
75 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
82 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
83 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
88 stop_arm (struct PeerContext *p)
91 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
92 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
93 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
94 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 "ARM process %u stopped\n", p->arm_pid);
98 GNUNET_CONFIGURATION_destroy (p->cfg);
103 abort_ksk_search_task (void *cls,
104 const struct GNUNET_SCHEDULER_TaskContext *tc)
106 if (ksk_search != NULL)
108 GNUNET_FS_search_stop (ksk_search);
110 if (sks_search == NULL)
112 fprintf (stderr, "initiating shutdown\n");
120 abort_sks_search_task (void *cls,
121 const struct GNUNET_SCHEDULER_TaskContext *tc)
123 struct GNUNET_FS_Namespace *ns;
125 if (sks_search == NULL)
127 GNUNET_FS_search_stop (sks_search);
129 ns = GNUNET_FS_namespace_create (fs,
131 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_YES));
132 if (ksk_search == NULL)
134 fprintf (stderr, "initiating shutdown\n");
141 progress_cb (void *cls,
142 const struct GNUNET_FS_ProgressInfo *event)
144 switch (event->status)
146 case GNUNET_FS_STATUS_SEARCH_RESULT:
147 if (sks_search == event->value.search.sc)
150 "Search result for sks received\n");
151 if (! GNUNET_FS_uri_test_equal (sks_expect_uri,
152 event->value.search.specifics.result.uri))
155 "Wrong result for sks search!\n");
158 /* give system 1ms to initiate update search! */
159 GNUNET_SCHEDULER_add_delayed (sched,
160 GNUNET_TIME_UNIT_MILLISECONDS,
161 &abort_sks_search_task,
164 else if (ksk_search == event->value.search.sc)
167 "Search result for ksk received\n");
168 if (! GNUNET_FS_uri_test_equal (ksk_expect_uri,
169 event->value.search.specifics.result.uri))
172 "Wrong result for ksk search!\n");
175 GNUNET_SCHEDULER_add_continuation (sched,
176 &abort_ksk_search_task,
178 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
183 "Search result received!?\n");
187 case GNUNET_FS_STATUS_SEARCH_ERROR:
189 "Error searching file: %s\n",
190 event->value.search.specifics.error.message);
191 if (sks_search == event->value.search.sc)
192 GNUNET_SCHEDULER_add_continuation (sched,
193 &abort_sks_search_task,
195 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
196 else if (ksk_search == event->value.search.sc)
197 GNUNET_SCHEDULER_add_continuation (sched,
198 &abort_ksk_search_task,
200 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
204 case GNUNET_FS_STATUS_SEARCH_START:
206 "Search start event for `%s' received\n",
207 (const char*) event->value.search.cctx);
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:
222 "Search stop event received\n");
223 GNUNET_assert ( (ksk_search == event->value.search.sc) ||
224 (sks_search == event->value.search.sc));
228 "Unexpected event: %d\n",
232 return event->value.search.cctx;
237 publish_cont (void *cls,
238 const struct GNUNET_FS_Uri *ksk_uri,
242 struct GNUNET_FS_Uri *sks_uri;
244 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
248 fprintf (stderr, "Error publishing: %s\n", emsg);
253 GNUNET_CRYPTO_hash_to_enc (&nsid,
255 GNUNET_snprintf (sbuf,
257 "gnunet://fs/sks/%s/this",
259 sks_uri = GNUNET_FS_uri_parse (sbuf, &msg);
262 fprintf (stderr, "failed to parse URI `%s': %s\n",
269 fprintf (stderr, "Starting keyword search...\n");
270 ksk_search = GNUNET_FS_search_start (fs, ksk_uri, 1, "ksk_search");
271 fprintf (stderr, "Starting namespace search...\n") ;
272 sks_search = GNUNET_FS_search_start (fs, sks_uri, 1, "sks_search");
273 GNUNET_FS_uri_destroy (sks_uri);
279 const struct GNUNET_FS_Uri *uri,
282 struct GNUNET_CONTAINER_MetaData *meta;
283 struct GNUNET_TIME_Absolute expiration;
284 struct GNUNET_FS_Uri *ksk_uri;
287 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
288 meta = GNUNET_CONTAINER_meta_data_create ();
290 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
291 GNUNET_assert (NULL == msg);
292 fprintf (stderr, "Advertising update 'this' namespace entry under keyword...\n");
293 ksk_expect_uri = GNUNET_FS_uri_dup (uri);
294 GNUNET_FS_publish_ksk (fs,
300 GNUNET_FS_PUBLISH_OPTION_NONE,
303 GNUNET_FS_uri_destroy (ksk_uri);
304 GNUNET_CONTAINER_meta_data_destroy (meta);
310 const struct GNUNET_FS_Uri *uri,
313 struct GNUNET_CONTAINER_MetaData *meta;
314 struct GNUNET_FS_Namespace *ns;
315 struct GNUNET_TIME_Absolute expiration;
319 fprintf (stderr, "Error publishing: %s\n", emsg);
324 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
325 ns = GNUNET_FS_namespace_create (fs,
327 meta = GNUNET_CONTAINER_meta_data_create ();
328 GNUNET_assert (NULL == emsg);
329 fprintf (stderr, "Advertising update 'this->next' namespace update...\n");
330 sks_expect_uri = GNUNET_FS_uri_dup (uri);
331 GNUNET_FS_publish_sks (fs,
336 uri, /* FIXME: this is non-sense (use CHK URI!?) */
339 GNUNET_FS_PUBLISH_OPTION_NONE,
342 GNUNET_CONTAINER_meta_data_destroy (meta);
343 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
348 ns_iterator (void *cls,
350 const GNUNET_HashCode *id)
354 if (0 != strcmp (name,
365 struct GNUNET_FS_Namespace *ns;
366 struct GNUNET_TIME_Absolute expiration;
367 struct GNUNET_CONTAINER_MetaData *meta;
370 ns = GNUNET_FS_namespace_create (fs,
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 fprintf (stderr, "Advertising namespace root...\n");
383 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
384 meta = GNUNET_CONTAINER_meta_data_create ();
385 GNUNET_FS_namespace_advertise (fs,
392 GNUNET_FS_namespace_delete (ns, GNUNET_NO);
393 GNUNET_CONTAINER_meta_data_destroy (meta);
399 struct GNUNET_SCHEDULER_Handle *s,
402 const struct GNUNET_CONFIGURATION_Handle *cfg)
405 setup_peer (&p1, "test_fs_namespace_data.conf");
406 fs = GNUNET_FS_start (sched,
411 GNUNET_FS_FLAGS_NONE,
412 GNUNET_FS_OPTIONS_END);
418 main (int argc, char *argv[])
420 char *const argvx[] = {
423 "test_fs_namespace_data.conf",
429 struct GNUNET_GETOPT_CommandLineOption options[] = {
430 GNUNET_GETOPT_OPTION_END
433 GNUNET_log_setup ("test_fs_namespace",
440 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
441 argvx, "test-fs-namespace",
442 "nohelp", options, &run, NULL);
444 if (GNUNET_YES != update_started)
447 "Update search never started!\n");
450 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
455 /* end of test_fs_namespace.c */