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 3, 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;
55 struct GNUNET_OS_Process *arm_proc;
59 static struct PeerContext p1;
61 static struct GNUNET_TIME_Absolute start;
63 static struct GNUNET_FS_Handle *fs;
65 static const struct GNUNET_CONFIGURATION_Handle *cfg;
67 static struct GNUNET_FS_PublishContext *publish;
69 static struct GNUNET_FS_PublishContext *publish;
77 static GNUNET_SCHEDULER_TaskIdentifier rtask;
80 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 GNUNET_FS_publish_stop (publish);
84 GNUNET_DISK_directory_remove (fn1);
87 GNUNET_DISK_directory_remove (fn2);
92 if (GNUNET_SCHEDULER_NO_TASK != rtask)
94 GNUNET_SCHEDULER_cancel (rtask);
95 rtask = GNUNET_SCHEDULER_NO_TASK;
100 static void *progress_cb (void *cls,
101 const struct GNUNET_FS_ProgressInfo *event);
105 restart_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
107 rtask = GNUNET_SCHEDULER_NO_TASK;
109 fs = GNUNET_FS_start (cfg, "test-fs-publish-persistence", &progress_cb, NULL,
110 GNUNET_FS_FLAGS_PERSISTENCE, 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)
128 for (i = 0; i < off; i++)
133 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
134 &restart_fs_task, NULL);
139 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
144 switch (event->status)
146 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
147 consider_restart (event->status);
148 ret = event->value.publish.cctx;
149 printf ("Publish complete, %llu kbps.\n",
150 (unsigned long long) (FILESIZE * 1000LL /
152 GNUNET_TIME_absolute_get_duration (start).
154 if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
155 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
157 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
158 consider_restart (event->status);
159 ret = event->value.publish.cctx;
160 GNUNET_assert (publish == event->value.publish.pc);
162 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
163 (unsigned long long) event->value.publish.completed,
164 (unsigned long long) event->value.publish.size,
165 event->value.publish.specifics.progress.depth,
166 (unsigned long long) event->value.publish.specifics.progress.
170 case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
171 if (event->value.publish.pc == publish)
174 case GNUNET_FS_STATUS_PUBLISH_RESUME:
177 GNUNET_assert (GNUNET_YES ==
178 GNUNET_FS_file_information_is_directory (event->value.
180 publish = event->value.publish.pc;
181 return "publish-context-dir";
184 case GNUNET_FS_STATUS_PUBLISH_ERROR:
185 ret = event->value.publish.cctx;
186 fprintf (stderr, "Error publishing file: %s\n",
187 event->value.publish.specifics.error.message);
189 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
191 case GNUNET_FS_STATUS_PUBLISH_START:
192 consider_restart (event->status);
193 publish = event->value.publish.pc;
194 ret = event->value.publish.cctx;
195 if (0 == strcmp ("publish-context1", event->value.publish.cctx))
198 strcmp ("publish-context-dir", event->value.publish.pctx));
199 GNUNET_assert (FILESIZE == event->value.publish.size);
200 GNUNET_assert (0 == event->value.publish.completed);
201 GNUNET_assert (1 == event->value.publish.anonymity);
203 else if (0 == strcmp ("publish-context2", event->value.publish.cctx))
206 strcmp ("publish-context-dir", event->value.publish.pctx));
207 GNUNET_assert (FILESIZE == event->value.publish.size);
208 GNUNET_assert (0 == event->value.publish.completed);
209 GNUNET_assert (2 == event->value.publish.anonymity);
211 else if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
213 GNUNET_assert (0 == event->value.publish.completed);
214 GNUNET_assert (3 == event->value.publish.anonymity);
219 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
220 consider_restart (event->status);
221 if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
222 GNUNET_assert (publish == event->value.publish.pc);
225 printf ("Unexpected event: %d\n", event->status);
233 setup_peer (struct PeerContext *p, const char *cfgname)
235 p->cfg = GNUNET_CONFIGURATION_create ();
238 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
239 "gnunet-service-arm",
243 "-c", cfgname, NULL);
245 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
250 stop_arm (struct PeerContext *p)
253 if (NULL != p->arm_proc)
255 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
256 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
257 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
258 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
260 GNUNET_OS_process_get_pid (p->arm_proc));
261 GNUNET_OS_process_close (p->arm_proc);
265 GNUNET_CONFIGURATION_destroy (p->cfg);
270 run (void *cls, char *const *args, const char *cfgfile,
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;
284 struct GNUNET_FS_BlockOptions bo;
287 setup_peer (&p1, "test_fs_publish_data.conf");
288 fs = GNUNET_FS_start (cfg, "test-fs-publish-persistence", &progress_cb, NULL,
289 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
290 GNUNET_assert (NULL != fs);
291 fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
292 buf = GNUNET_malloc (FILESIZE);
293 for (i = 0; i < FILESIZE; i++)
294 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
295 GNUNET_assert (FILESIZE ==
296 GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
297 GNUNET_DISK_PERM_USER_READ |
298 GNUNET_DISK_PERM_USER_WRITE));
301 fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
302 buf = GNUNET_malloc (FILESIZE);
303 for (i = 0; i < FILESIZE; i++)
304 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
305 GNUNET_assert (FILESIZE ==
306 GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
307 GNUNET_DISK_PERM_USER_READ |
308 GNUNET_DISK_PERM_USER_WRITE));
311 meta = GNUNET_CONTAINER_meta_data_create ();
312 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
313 bo.content_priority = 42;
314 bo.anonymity_level = 1;
315 bo.replication_level = 0;
316 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
318 GNUNET_FS_file_information_create_from_file (fs, "publish-context1", fn1,
319 kuri, meta, GNUNET_YES, &bo);
320 GNUNET_assert (NULL != fi1);
321 bo.anonymity_level = 2;
323 GNUNET_FS_file_information_create_from_file (fs, "publish-context2", fn2,
324 kuri, meta, GNUNET_YES, &bo);
325 GNUNET_assert (NULL != fi2);
326 bo.anonymity_level = 3;
328 GNUNET_FS_file_information_create_empty_directory (fs,
329 "publish-context-dir",
331 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
332 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
333 GNUNET_FS_uri_destroy (kuri);
334 GNUNET_CONTAINER_meta_data_destroy (meta);
335 GNUNET_assert (NULL != fidir);
336 start = GNUNET_TIME_absolute_get ();
337 GNUNET_FS_publish_start (fs, fidir, NULL, NULL, NULL,
338 GNUNET_FS_PUBLISH_OPTION_NONE);
339 GNUNET_assert (publish != NULL);
344 main (int argc, char *argv[])
346 char *const argvx[] = {
347 "test-fs-publish-persistence",
349 "test_fs_publish_data.conf",
355 struct GNUNET_GETOPT_CommandLineOption options[] = {
356 GNUNET_GETOPT_OPTION_END
359 GNUNET_log_setup ("test_fs_publish_persistence",
366 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
367 "test-fs-publish", "nohelp", options, &run, NULL);
369 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
372 GNUNET_DISK_directory_remove (fn1);
377 GNUNET_DISK_directory_remove (fn2);
383 /* end of test_fs_publish_persistence.c */