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, 60)
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;
60 static struct PeerContext p1;
62 static struct GNUNET_TIME_Absolute start;
64 static struct GNUNET_SCHEDULER_Handle *sched;
66 static struct GNUNET_FS_Handle *fs;
68 static struct GNUNET_FS_PublishContext *publish;
70 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)
100 switch (event->status)
102 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
103 ret = event->value.publish.cctx;
104 printf ("Publish complete, %llu kbps.\n",
105 (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
106 if (0 == strcmp ("list_indexed-context-dir",
107 event->value.publish.cctx))
108 GNUNET_SCHEDULER_add_continuation (sched,
111 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
113 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
114 ret = event->value.publish.cctx;
115 GNUNET_assert (publish == event->value.publish.sc);
117 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
118 (unsigned long long) event->value.publish.completed,
119 (unsigned long long) event->value.publish.size,
120 event->value.publish.specifics.progress.depth,
121 (unsigned long long) event->value.publish.specifics.progress.offset);
124 case GNUNET_FS_STATUS_PUBLISH_ERROR:
125 ret = event->value.publish.cctx;
127 "Error publishing file: %s\n",
128 event->value.publish.specifics.error.message);
130 if (0 == strcmp ("list_indexed-context-dir",
131 event->value.publish.cctx))
132 GNUNET_SCHEDULER_add_continuation (sched,
135 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
137 case GNUNET_FS_STATUS_PUBLISH_START:
138 ret = event->value.publish.cctx;
139 if (0 == strcmp ("publish-context1",
140 event->value.publish.cctx))
142 GNUNET_assert (0 == strcmp ("publish-context-dir",
143 event->value.publish.pctx));
144 GNUNET_assert (FILESIZE == event->value.publish.size);
145 GNUNET_assert (0 == event->value.publish.completed);
146 GNUNET_assert (1 == event->value.publish.anonymity);
148 else if (0 == strcmp ("publish-context2",
149 event->value.publish.cctx))
151 GNUNET_assert (0 == strcmp ("publish-context-dir",
152 event->value.publish.pctx));
153 GNUNET_assert (FILESIZE == event->value.publish.size);
154 GNUNET_assert (0 == event->value.publish.completed);
155 GNUNET_assert (2 == event->value.publish.anonymity);
157 else if (0 == strcmp ("publish-context-dir",
158 event->value.publish.cctx))
160 GNUNET_assert (0 == event->value.publish.completed);
161 GNUNET_assert (3 == event->value.publish.anonymity);
166 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
167 if (0 == strcmp ("list_indexed-context-dir",
168 event->value.publish.cctx))
170 GNUNET_assert (publish == event->value.publish.sc);
171 GNUNET_SCHEDULER_add_continuation (sched,
174 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
178 printf ("Unexpected event: %d\n",
187 setup_peer (struct PeerContext *p, const char *cfgname)
189 p->cfg = GNUNET_CONFIGURATION_create ();
191 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
192 "gnunet-service-arm",
196 "-c", cfgname, NULL);
198 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
199 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
204 stop_arm (struct PeerContext *p)
207 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
208 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
209 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
210 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "ARM process %u stopped\n", p->arm_pid);
214 GNUNET_CONFIGURATION_destroy (p->cfg);
220 struct GNUNET_SCHEDULER_Handle *s,
223 const struct GNUNET_CONFIGURATION_Handle *cfg)
225 const char *keywords[] = {
230 struct GNUNET_CONTAINER_MetaData *meta;
231 struct GNUNET_FS_Uri *kuri;
232 struct GNUNET_FS_FileInformation *fi1;
233 struct GNUNET_FS_FileInformation *fi2;
234 struct GNUNET_FS_FileInformation *fidir;
238 setup_peer (&p1, "test_fs_publish_data.conf");
239 fs = GNUNET_FS_start (sched,
244 GNUNET_FS_FLAGS_NONE,
245 GNUNET_FS_OPTIONS_END);
246 GNUNET_assert (NULL != fs);
247 fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
248 buf = GNUNET_malloc (FILESIZE);
249 for (i = 0; i < FILESIZE; i++)
250 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
251 GNUNET_assert (FILESIZE ==
252 GNUNET_DISK_fn_write (fn1,
255 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
258 fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
259 buf = GNUNET_malloc (FILESIZE);
260 for (i = 0; i < FILESIZE; i++)
261 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
262 GNUNET_assert (FILESIZE ==
263 GNUNET_DISK_fn_write (fn2,
266 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
269 meta = GNUNET_CONTAINER_meta_data_create ();
270 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
271 fi1 = GNUNET_FS_file_information_create_from_file ("publish-context1",
278 GNUNET_TIME_relative_to_absolute (LIFETIME));
279 fi2 = GNUNET_FS_file_information_create_from_file ("publish-context2",
286 GNUNET_TIME_relative_to_absolute (LIFETIME));
287 fidir = GNUNET_FS_file_information_create_empty_directory ("publish-context-dir",
292 GNUNET_TIME_relative_to_absolute (LIFETIME));
293 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
294 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
295 GNUNET_FS_uri_destroy (kuri);
296 GNUNET_CONTAINER_meta_data_destroy (meta);
297 GNUNET_assert (NULL != fidir);
298 start = GNUNET_TIME_absolute_get ();
299 publish = GNUNET_FS_publish_start (fs,
302 GNUNET_FS_PUBLISH_OPTION_NONE);
303 GNUNET_assert (publish != NULL);
308 main (int argc, char *argv[])
310 char *const argvx[] = {
313 "test_fs_publish_data.conf",
319 struct GNUNET_GETOPT_CommandLineOption options[] = {
320 GNUNET_GETOPT_OPTION_END
323 GNUNET_log_setup ("test_fs_publish",
330 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
331 argvx, "test-fs-publish",
332 "nohelp", options, &run, NULL);
334 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
335 GNUNET_DISK_directory_remove (fn1);
336 GNUNET_free_non_null (fn1);
337 GNUNET_DISK_directory_remove (fn2);
338 GNUNET_free_non_null (fn2);
342 /* end of test_fs_publish.c */