2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2008, 2009, 2010 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_persistence.c
23 * @brief simple testcase for persistence of simple publish operation
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_fs_service.h"
32 #define VERBOSE GNUNET_NO
34 #define START_ARM GNUNET_YES
37 * File-size we use for testing.
39 #define FILESIZE (1024 * 1024 * 2)
42 * How long until we give up on transmitting the message?
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
47 * How long should our test-content live?
49 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
53 struct GNUNET_CONFIGURATION_Handle *cfg;
59 static struct PeerContext p1;
61 static struct GNUNET_TIME_Absolute start;
63 static struct GNUNET_SCHEDULER_Handle *sched;
65 static struct GNUNET_FS_Handle *fs;
67 static const struct GNUNET_CONFIGURATION_Handle *cfg;
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);
100 restart_fs_task (void *cls,
101 const struct GNUNET_SCHEDULER_TaskContext *tc)
104 fs = GNUNET_FS_start (sched,
106 "test-fs-publish-persistence",
109 GNUNET_FS_FLAGS_PERSISTENCE,
110 GNUNET_FS_OPTIONS_END);
115 * Consider scheduling the restart-task.
116 * Only runs the restart task once per event
119 * @param ev type of the event to consider
122 consider_restart (int ev)
131 GNUNET_SCHEDULER_add_with_priority (sched,
132 GNUNET_SCHEDULER_PRIORITY_URGENT,
139 progress_cb (void *cls,
140 const struct GNUNET_FS_ProgressInfo *event)
145 switch (event->status)
147 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
148 consider_restart (event->status);
149 ret = event->value.publish.cctx;
150 printf ("Publish complete, %llu kbps.\n",
151 (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
152 if (0 == strcmp ("list_indexed-context-dir",
153 event->value.publish.cctx))
154 GNUNET_SCHEDULER_add_continuation (sched,
157 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
159 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
160 consider_restart (event->status);
161 ret = event->value.publish.cctx;
162 GNUNET_assert (publish == event->value.publish.sc);
164 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
165 (unsigned long long) event->value.publish.completed,
166 (unsigned long long) event->value.publish.size,
167 event->value.publish.specifics.progress.depth,
168 (unsigned long long) event->value.publish.specifics.progress.offset);
171 case GNUNET_FS_STATUS_PUBLISH_ERROR:
172 ret = event->value.publish.cctx;
174 "Error publishing file: %s\n",
175 event->value.publish.specifics.error.message);
177 if (0 == strcmp ("list_indexed-context-dir",
178 event->value.publish.cctx))
179 GNUNET_SCHEDULER_add_continuation (sched,
182 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
184 case GNUNET_FS_STATUS_PUBLISH_START:
185 consider_restart (event->status);
186 ret = event->value.publish.cctx;
187 if (0 == strcmp ("publish-context1",
188 event->value.publish.cctx))
190 GNUNET_assert (0 == strcmp ("publish-context-dir",
191 event->value.publish.pctx));
192 GNUNET_assert (FILESIZE == event->value.publish.size);
193 GNUNET_assert (0 == event->value.publish.completed);
194 GNUNET_assert (1 == event->value.publish.anonymity);
196 else if (0 == strcmp ("publish-context2",
197 event->value.publish.cctx))
199 GNUNET_assert (0 == strcmp ("publish-context-dir",
200 event->value.publish.pctx));
201 GNUNET_assert (FILESIZE == event->value.publish.size);
202 GNUNET_assert (0 == event->value.publish.completed);
203 GNUNET_assert (2 == event->value.publish.anonymity);
205 else if (0 == strcmp ("publish-context-dir",
206 event->value.publish.cctx))
208 GNUNET_assert (0 == event->value.publish.completed);
209 GNUNET_assert (3 == event->value.publish.anonymity);
214 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
215 consider_restart (event->status);
216 if (0 == strcmp ("list_indexed-context-dir",
217 event->value.publish.cctx))
219 GNUNET_assert (publish == event->value.publish.sc);
220 GNUNET_SCHEDULER_add_continuation (sched,
223 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
227 printf ("Unexpected event: %d\n",
236 setup_peer (struct PeerContext *p, const char *cfgname)
238 p->cfg = GNUNET_CONFIGURATION_create ();
240 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
241 "gnunet-service-arm",
245 "-c", cfgname, NULL);
247 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
252 stop_arm (struct PeerContext *p)
255 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
256 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
257 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
258 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "ARM process %u stopped\n", p->arm_pid);
262 GNUNET_CONFIGURATION_destroy (p->cfg);
268 struct GNUNET_SCHEDULER_Handle *s,
271 const struct GNUNET_CONFIGURATION_Handle *c)
273 const char *keywords[] = {
278 struct GNUNET_CONTAINER_MetaData *meta;
279 struct GNUNET_FS_Uri *kuri;
280 struct GNUNET_FS_FileInformation *fi1;
281 struct GNUNET_FS_FileInformation *fi2;
282 struct GNUNET_FS_FileInformation *fidir;
287 setup_peer (&p1, "test_fs_publish_data.conf");
288 fs = GNUNET_FS_start (sched,
290 "test-fs-publish-persistence",
293 GNUNET_FS_FLAGS_PERSISTENCE,
294 GNUNET_FS_OPTIONS_END);
295 GNUNET_assert (NULL != fs);
296 fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
297 buf = GNUNET_malloc (FILESIZE);
298 for (i = 0; i < FILESIZE; i++)
299 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
300 GNUNET_assert (FILESIZE ==
301 GNUNET_DISK_fn_write (fn1,
304 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
307 fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
308 buf = GNUNET_malloc (FILESIZE);
309 for (i = 0; i < FILESIZE; i++)
310 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
311 GNUNET_assert (FILESIZE ==
312 GNUNET_DISK_fn_write (fn2,
315 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
318 meta = GNUNET_CONTAINER_meta_data_create ();
319 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
320 fi1 = GNUNET_FS_file_information_create_from_file (fs,
328 GNUNET_TIME_relative_to_absolute (LIFETIME));
329 fi2 = GNUNET_FS_file_information_create_from_file (fs,
337 GNUNET_TIME_relative_to_absolute (LIFETIME));
338 fidir = GNUNET_FS_file_information_create_empty_directory (fs,
339 "publish-context-dir",
344 GNUNET_TIME_relative_to_absolute (LIFETIME));
345 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
346 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
347 GNUNET_FS_uri_destroy (kuri);
348 GNUNET_CONTAINER_meta_data_destroy (meta);
349 GNUNET_assert (NULL != fidir);
350 start = GNUNET_TIME_absolute_get ();
351 publish = GNUNET_FS_publish_start (fs,
354 GNUNET_FS_PUBLISH_OPTION_NONE);
355 GNUNET_assert (publish != NULL);
360 main (int argc, char *argv[])
362 char *const argvx[] = {
363 "test-fs-publish-persistence",
365 "test_fs_publish_data.conf",
371 struct GNUNET_GETOPT_CommandLineOption options[] = {
372 GNUNET_GETOPT_OPTION_END
375 GNUNET_log_setup ("test_fs_publish_persistence",
382 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
383 argvx, "test-fs-publish",
384 "nohelp", options, &run, NULL);
386 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
387 GNUNET_DISK_directory_remove (fn1);
388 GNUNET_free_non_null (fn1);
389 GNUNET_DISK_directory_remove (fn2);
390 GNUNET_free_non_null (fn2);
394 /* end of test_fs_publish_persistence.c */