2 This file is part of GNUnet.
3 (C) 2004, 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_publish.c
23 * @brief simple testcase for publish operation (indexing, listing
24 * indexed, directory structure)
25 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_fs_service.h"
33 #define VERBOSE GNUNET_NO
35 #define START_ARM GNUNET_YES
38 * File-size we use for testing.
40 #define FILESIZE (1024 * 1024 * 2)
43 * How long until we give up on transmitting the message?
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
48 * How long should our test-content live?
50 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
54 struct GNUNET_CONFIGURATION_Handle *cfg;
55 struct GNUNET_PeerIdentity id;
61 static struct PeerContext p1;
63 static struct GNUNET_TIME_Absolute start;
65 static struct GNUNET_SCHEDULER_Handle *sched;
67 static struct GNUNET_FS_Handle *fs;
69 static struct GNUNET_FS_PublishContext *publish;
71 static struct GNUNET_FS_PublishContext *publish;
79 abort_publish_task (void *cls,
80 const struct GNUNET_SCHEDULER_TaskContext *tc)
82 GNUNET_FS_publish_stop (publish);
84 GNUNET_DISK_directory_remove (fn1);
87 GNUNET_DISK_directory_remove (fn2);
94 progress_cb (void *cls,
95 const struct GNUNET_FS_ProgressInfo *event)
98 switch (event->status)
100 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
101 printf ("Publish complete, %llu kbps.\n",
102 (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
103 GNUNET_SCHEDULER_add_continuation (sched,
107 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
109 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
110 GNUNET_assert (publish == event->value.publish.sc);
112 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
113 (unsigned long long) event->value.publish.completed,
114 (unsigned long long) event->value.publish.size,
115 event->value.publish.specifics.progress.depth,
116 (unsigned long long) event->value.publish.specifics.progress.offset);
119 case GNUNET_FS_STATUS_PUBLISH_ERROR:
121 "Error publishing file: %s\n",
122 event->value.publish.specifics.error.message);
123 GNUNET_SCHEDULER_add_continuation (sched,
127 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
129 case GNUNET_FS_STATUS_PUBLISH_START:
130 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
131 GNUNET_assert (NULL == event->value.publish.pctx);
132 GNUNET_assert (FILESIZE == event->value.publish.size);
133 GNUNET_assert (0 == event->value.publish.completed);
134 GNUNET_assert (1 == event->value.publish.anonymity);
136 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
137 GNUNET_assert (publish == event->value.publish.sc);
138 GNUNET_SCHEDULER_add_continuation (sched,
142 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
145 printf ("Unexpected event: %d\n",
154 setup_peer (struct PeerContext *p, const char *cfgname)
156 p->cfg = GNUNET_CONFIGURATION_create ();
158 p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
159 "gnunet-service-arm",
163 "-c", cfgname, NULL);
164 sleep (1); /* allow ARM to start */
166 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
167 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
172 stop_arm (struct PeerContext *p)
175 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
176 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
177 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
178 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "ARM process %u stopped\n", p->arm_pid);
182 GNUNET_CONFIGURATION_destroy (p->cfg);
188 struct GNUNET_SCHEDULER_Handle *s,
191 const struct GNUNET_CONFIGURATION_Handle *cfg)
193 const char *keywords[] = {
198 struct GNUNET_CONTAINER_MetaData *meta;
199 struct GNUNET_FS_Uri *kuri;
200 struct GNUNET_FS_FileInformation *fi1;
201 struct GNUNET_FS_FileInformation *fi2;
202 struct GNUNET_FS_FileInformation *fidir;
206 setup_peer (&p1, "test_fs_publish_data.conf");
207 fs = GNUNET_FS_start (sched,
212 GNUNET_FS_FLAGS_NONE,
213 GNUNET_FS_OPTIONS_END);
214 GNUNET_assert (NULL != fs);
215 fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
216 buf = GNUNET_malloc (FILESIZE);
217 for (i = 0; i < FILESIZE; i++)
218 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
219 GNUNET_assert (FILESIZE ==
220 GNUNET_DISK_fn_write (fn1,
223 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
226 fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
227 buf = GNUNET_malloc (FILESIZE);
228 for (i = 0; i < FILESIZE; i++)
229 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
230 GNUNET_assert (FILESIZE ==
231 GNUNET_DISK_fn_write (fn2,
234 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
237 meta = GNUNET_CONTAINER_meta_data_create ();
238 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
239 fi1 = GNUNET_FS_file_information_create_from_file ("publish-context1",
246 GNUNET_TIME_relative_to_absolute (LIFETIME));
247 fi2 = GNUNET_FS_file_information_create_from_file ("publish-context2",
254 GNUNET_TIME_relative_to_absolute (LIFETIME));
255 fidir = GNUNET_FS_file_information_create_empty_directory ("publish-context-dir",
260 GNUNET_TIME_relative_to_absolute (LIFETIME));
261 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
262 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
263 GNUNET_FS_uri_destroy (kuri);
264 GNUNET_CONTAINER_meta_data_destroy (meta);
265 GNUNET_assert (NULL != fidir);
266 start = GNUNET_TIME_absolute_get ();
267 publish = GNUNET_FS_publish_start (fs,
270 GNUNET_FS_PUBLISH_OPTION_NONE);
271 GNUNET_assert (publish != NULL);
276 main (int argc, char *argv[])
278 char *const argvx[] = {
281 "test_fs_publish_data.conf",
287 struct GNUNET_GETOPT_CommandLineOption options[] = {
288 GNUNET_GETOPT_OPTION_END
291 GNUNET_log_setup ("test_fs_publish",
298 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
299 argvx, "test-fs-publish",
300 "nohelp", options, &run, NULL);
302 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
303 GNUNET_DISK_directory_remove (fn1);
304 GNUNET_free_non_null (fn1);
305 GNUNET_DISK_directory_remove (fn2);
306 GNUNET_free_non_null (fn2);
310 /* end of test_fs_publish.c */