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;
78 abort_publish_task (void *cls,
79 const struct GNUNET_SCHEDULER_TaskContext *tc)
81 GNUNET_FS_publish_stop (publish);
83 GNUNET_DISK_directory_remove (fn1);
86 GNUNET_DISK_directory_remove (fn2);
93 progress_cb (void *cls,
94 const struct GNUNET_FS_ProgressInfo *event);
98 restart_fs_task (void *cls,
99 const struct GNUNET_SCHEDULER_TaskContext *tc)
102 fs = GNUNET_FS_start (cfg,
103 "test-fs-publish-persistence",
106 GNUNET_FS_FLAGS_PERSISTENCE,
107 GNUNET_FS_OPTIONS_END);
112 * Consider scheduling the restart-task.
113 * Only runs the restart task once per event
116 * @param ev type of the event to consider
119 consider_restart (int ev)
128 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
135 progress_cb (void *cls,
136 const struct GNUNET_FS_ProgressInfo *event)
141 switch (event->status)
143 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
144 consider_restart (event->status);
145 ret = event->value.publish.cctx;
146 printf ("Publish complete, %llu kbps.\n",
147 (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).rel_value) / 1024));
148 if (0 == strcmp ("publish-context-dir",
149 event->value.publish.cctx))
150 GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
152 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
154 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
155 consider_restart (event->status);
156 ret = event->value.publish.cctx;
157 GNUNET_assert (publish == event->value.publish.pc);
159 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
160 (unsigned long long) event->value.publish.completed,
161 (unsigned long long) event->value.publish.size,
162 event->value.publish.specifics.progress.depth,
163 (unsigned long long) event->value.publish.specifics.progress.offset);
166 case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
167 if (event->value.publish.pc == publish)
170 case GNUNET_FS_STATUS_PUBLISH_RESUME:
173 publish = event->value.publish.pc;
174 return "publish-context-dir";
177 case GNUNET_FS_STATUS_PUBLISH_ERROR:
178 ret = event->value.publish.cctx;
180 "Error publishing file: %s\n",
181 event->value.publish.specifics.error.message);
183 if (0 == strcmp ("publish-context-dir",
184 event->value.publish.cctx))
185 GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
187 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
189 case GNUNET_FS_STATUS_PUBLISH_START:
190 consider_restart (event->status);
191 publish = event->value.publish.pc;
192 ret = event->value.publish.cctx;
193 if (0 == strcmp ("publish-context1",
194 event->value.publish.cctx))
196 GNUNET_assert (0 == strcmp ("publish-context-dir",
197 event->value.publish.pctx));
198 GNUNET_assert (FILESIZE == event->value.publish.size);
199 GNUNET_assert (0 == event->value.publish.completed);
200 GNUNET_assert (1 == event->value.publish.anonymity);
202 else if (0 == strcmp ("publish-context2",
203 event->value.publish.cctx))
205 GNUNET_assert (0 == strcmp ("publish-context-dir",
206 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",
212 event->value.publish.cctx))
214 GNUNET_assert (0 == event->value.publish.completed);
215 GNUNET_assert (3 == event->value.publish.anonymity);
220 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
221 consider_restart (event->status);
222 if (0 == strcmp ("publish-context-dir",
223 event->value.publish.cctx))
224 GNUNET_assert (publish == event->value.publish.pc);
227 printf ("Unexpected event: %d\n",
236 setup_peer (struct PeerContext *p, const char *cfgname)
238 p->cfg = GNUNET_CONFIGURATION_create ();
240 p->arm_proc = 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 != 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,
260 "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
261 GNUNET_OS_process_close (p->arm_proc);
264 GNUNET_CONFIGURATION_destroy (p->cfg);
272 const struct GNUNET_CONFIGURATION_Handle *c)
274 const char *keywords[] = {
279 struct GNUNET_CONTAINER_MetaData *meta;
280 struct GNUNET_FS_Uri *kuri;
281 struct GNUNET_FS_FileInformation *fi1;
282 struct GNUNET_FS_FileInformation *fi2;
283 struct GNUNET_FS_FileInformation *fidir;
285 struct GNUNET_FS_BlockOptions bo;
288 setup_peer (&p1, "test_fs_publish_data.conf");
289 fs = GNUNET_FS_start (cfg,
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 bo.content_priority = 42;
321 bo.anonymity_level = 1;
322 bo.replication_level = 0;
323 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
324 fi1 = GNUNET_FS_file_information_create_from_file (fs,
331 GNUNET_assert (NULL != fi1);
332 bo.anonymity_level = 2;
333 fi2 = GNUNET_FS_file_information_create_from_file (fs,
340 GNUNET_assert (NULL != fi2);
341 bo.anonymity_level = 3;
342 fidir = GNUNET_FS_file_information_create_empty_directory (fs,
343 "publish-context-dir",
347 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
348 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
349 GNUNET_FS_uri_destroy (kuri);
350 GNUNET_CONTAINER_meta_data_destroy (meta);
351 GNUNET_assert (NULL != fidir);
352 start = GNUNET_TIME_absolute_get ();
353 GNUNET_FS_publish_start (fs,
356 GNUNET_FS_PUBLISH_OPTION_NONE);
357 GNUNET_assert (publish != NULL);
362 main (int argc, char *argv[])
364 char *const argvx[] = {
365 "test-fs-publish-persistence",
367 "test_fs_publish_data.conf",
373 struct GNUNET_GETOPT_CommandLineOption options[] = {
374 GNUNET_GETOPT_OPTION_END
377 GNUNET_log_setup ("test_fs_publish_persistence",
384 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
385 argvx, "test-fs-publish",
386 "nohelp", options, &run, NULL);
388 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
391 GNUNET_DISK_directory_remove (fn1);
396 GNUNET_DISK_directory_remove (fn2);
402 /* end of test_fs_publish_persistence.c */