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;
80 abort_publish_task (void *cls,
81 const struct GNUNET_SCHEDULER_TaskContext *tc)
83 GNUNET_FS_publish_stop (publish);
85 GNUNET_DISK_directory_remove (fn1);
88 GNUNET_DISK_directory_remove (fn2);
95 progress_cb (void *cls,
96 const struct GNUNET_FS_ProgressInfo *event)
101 switch (event->status)
103 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
104 ret = event->value.publish.cctx;
105 printf ("Publish complete, %llu kbps.\n",
106 (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
107 if (0 == strcmp ("list_indexed-context-dir",
108 event->value.publish.cctx))
109 GNUNET_SCHEDULER_add_continuation (sched,
113 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
115 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
116 ret = event->value.publish.cctx;
117 GNUNET_assert (publish == event->value.publish.sc);
119 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
120 (unsigned long long) event->value.publish.completed,
121 (unsigned long long) event->value.publish.size,
122 event->value.publish.specifics.progress.depth,
123 (unsigned long long) event->value.publish.specifics.progress.offset);
126 case GNUNET_FS_STATUS_PUBLISH_ERROR:
127 ret = event->value.publish.cctx;
129 "Error publishing file: %s\n",
130 event->value.publish.specifics.error.message);
132 if (0 == strcmp ("list_indexed-context-dir",
133 event->value.publish.cctx))
134 GNUNET_SCHEDULER_add_continuation (sched,
138 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
140 case GNUNET_FS_STATUS_PUBLISH_START:
141 ret = event->value.publish.cctx;
142 if (0 == strcmp ("publish-context1",
143 event->value.publish.cctx))
145 GNUNET_assert (0 == strcmp ("publish-context-dir",
146 event->value.publish.pctx));
147 GNUNET_assert (FILESIZE == event->value.publish.size);
148 GNUNET_assert (0 == event->value.publish.completed);
149 GNUNET_assert (1 == event->value.publish.anonymity);
151 else if (0 == strcmp ("publish-context2",
152 event->value.publish.cctx))
154 GNUNET_assert (0 == strcmp ("publish-context-dir",
155 event->value.publish.pctx));
156 GNUNET_assert (FILESIZE == event->value.publish.size);
157 GNUNET_assert (0 == event->value.publish.completed);
158 GNUNET_assert (2 == event->value.publish.anonymity);
160 else if (0 == strcmp ("publish-context-dir",
161 event->value.publish.cctx))
163 GNUNET_assert (0 == event->value.publish.completed);
164 GNUNET_assert (3 == event->value.publish.anonymity);
169 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
170 if (0 == strcmp ("list_indexed-context-dir",
171 event->value.publish.cctx))
173 GNUNET_assert (publish == event->value.publish.sc);
174 GNUNET_SCHEDULER_add_continuation (sched,
178 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
182 printf ("Unexpected event: %d\n",
191 setup_peer (struct PeerContext *p, const char *cfgname)
193 p->cfg = GNUNET_CONFIGURATION_create ();
195 p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
196 "gnunet-service-arm",
200 "-c", cfgname, NULL);
201 sleep (1); /* allow ARM to start */
203 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
204 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
209 stop_arm (struct PeerContext *p)
212 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
213 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
214 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
215 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "ARM process %u stopped\n", p->arm_pid);
219 GNUNET_CONFIGURATION_destroy (p->cfg);
225 struct GNUNET_SCHEDULER_Handle *s,
228 const struct GNUNET_CONFIGURATION_Handle *cfg)
230 const char *keywords[] = {
235 struct GNUNET_CONTAINER_MetaData *meta;
236 struct GNUNET_FS_Uri *kuri;
237 struct GNUNET_FS_FileInformation *fi1;
238 struct GNUNET_FS_FileInformation *fi2;
239 struct GNUNET_FS_FileInformation *fidir;
243 setup_peer (&p1, "test_fs_publish_data.conf");
244 fs = GNUNET_FS_start (sched,
249 GNUNET_FS_FLAGS_NONE,
250 GNUNET_FS_OPTIONS_END);
251 GNUNET_assert (NULL != fs);
252 fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
253 buf = GNUNET_malloc (FILESIZE);
254 for (i = 0; i < FILESIZE; i++)
255 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
256 GNUNET_assert (FILESIZE ==
257 GNUNET_DISK_fn_write (fn1,
260 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
263 fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
264 buf = GNUNET_malloc (FILESIZE);
265 for (i = 0; i < FILESIZE; i++)
266 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
267 GNUNET_assert (FILESIZE ==
268 GNUNET_DISK_fn_write (fn2,
271 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
274 meta = GNUNET_CONTAINER_meta_data_create ();
275 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
276 fi1 = GNUNET_FS_file_information_create_from_file ("publish-context1",
283 GNUNET_TIME_relative_to_absolute (LIFETIME));
284 fi2 = GNUNET_FS_file_information_create_from_file ("publish-context2",
291 GNUNET_TIME_relative_to_absolute (LIFETIME));
292 fidir = GNUNET_FS_file_information_create_empty_directory ("publish-context-dir",
297 GNUNET_TIME_relative_to_absolute (LIFETIME));
298 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
299 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
300 GNUNET_FS_uri_destroy (kuri);
301 GNUNET_CONTAINER_meta_data_destroy (meta);
302 GNUNET_assert (NULL != fidir);
303 start = GNUNET_TIME_absolute_get ();
304 publish = GNUNET_FS_publish_start (fs,
307 GNUNET_FS_PUBLISH_OPTION_NONE);
308 GNUNET_assert (publish != NULL);
313 main (int argc, char *argv[])
315 char *const argvx[] = {
318 "test_fs_publish_data.conf",
324 struct GNUNET_GETOPT_CommandLineOption options[] = {
325 GNUNET_GETOPT_OPTION_END
328 GNUNET_log_setup ("test_fs_publish",
335 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
336 argvx, "test-fs-publish",
337 "nohelp", options, &run, NULL);
339 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
340 GNUNET_DISK_directory_remove (fn1);
341 GNUNET_free_non_null (fn1);
342 GNUNET_DISK_directory_remove (fn2);
343 GNUNET_free_non_null (fn2);
347 /* end of test_fs_publish.c */