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.
21 * @file fs/test_fs_publish_persistence.c
22 * @brief simple testcase for persistence of simple publish operation
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_fs_service.h"
32 * File-size we use for testing.
34 #define FILESIZE (1024 * 1024 * 2)
37 * How long until we give up on transmitting the message?
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
42 * How long should our test-content live?
44 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47 static struct GNUNET_TIME_Absolute start;
49 static struct GNUNET_FS_Handle *fs;
51 static const struct GNUNET_CONFIGURATION_Handle *cfg;
53 static struct GNUNET_FS_PublishContext *publish;
55 static struct GNUNET_FS_PublishContext *publish;
63 static GNUNET_SCHEDULER_TaskIdentifier rtask;
67 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
69 GNUNET_FS_publish_stop (publish);
71 GNUNET_DISK_directory_remove (fn1);
74 GNUNET_DISK_directory_remove (fn2);
79 if (GNUNET_SCHEDULER_NO_TASK != rtask)
81 GNUNET_SCHEDULER_cancel (rtask);
82 rtask = GNUNET_SCHEDULER_NO_TASK;
88 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
92 restart_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
94 rtask = GNUNET_SCHEDULER_NO_TASK;
96 fs = GNUNET_FS_start (cfg, "test-fs-publish-persistence",
98 GNUNET_FS_FLAGS_PERSISTENCE,
99 GNUNET_FS_OPTIONS_END);
104 * Consider scheduling the restart-task.
105 * Only runs the restart task once per event
108 * @param ev type of the event to consider
111 consider_restart (int ev)
117 for (i = 0; i < off; i++)
122 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
123 &restart_fs_task, NULL);
128 progress_cb (void *cls,
129 const struct GNUNET_FS_ProgressInfo *event)
134 switch (event->status)
136 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
137 consider_restart (event->status);
138 ret = event->value.publish.cctx;
139 printf ("Publish complete, %llu kbps.\n",
140 (unsigned long long) (FILESIZE * 1000000LL /
142 GNUNET_TIME_absolute_get_duration
143 (start).rel_value_us) / 1024));
144 if ( (NULL != event->value.publish.cctx) &&
145 (0 == strcmp ("publish-context-dir", event->value.publish.cctx)) )
146 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
148 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
149 ret = event->value.publish.cctx;
151 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
152 consider_restart (event->status);
153 ret = event->value.publish.cctx;
154 GNUNET_assert (publish == event->value.publish.pc);
156 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
157 (unsigned long long) event->value.publish.completed,
158 (unsigned long long) event->value.publish.size,
159 event->value.publish.specifics.progress.depth,
160 (unsigned long long) event->value.publish.specifics.
164 case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
165 if (event->value.publish.pc == publish)
168 case GNUNET_FS_STATUS_PUBLISH_RESUME:
171 GNUNET_assert (GNUNET_YES ==
172 GNUNET_FS_file_information_is_directory (event->
175 publish = event->value.publish.pc;
176 return "publish-context-dir";
179 case GNUNET_FS_STATUS_PUBLISH_ERROR:
180 ret = event->value.publish.cctx;
181 FPRINTF (stderr, "Error publishing file: %s\n",
182 event->value.publish.specifics.error.message);
184 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
186 case GNUNET_FS_STATUS_PUBLISH_START:
187 consider_restart (event->status);
188 publish = event->value.publish.pc;
189 ret = event->value.publish.cctx;
190 if (0 == strcmp ("publish-context1", event->value.publish.cctx))
193 strcmp ("publish-context-dir", event->value.publish.pctx));
194 GNUNET_assert (FILESIZE == event->value.publish.size);
195 GNUNET_assert (0 == event->value.publish.completed);
196 GNUNET_assert (1 == event->value.publish.anonymity);
198 else if (0 == strcmp ("publish-context2", event->value.publish.cctx))
201 strcmp ("publish-context-dir", event->value.publish.pctx));
202 GNUNET_assert (FILESIZE == event->value.publish.size);
203 GNUNET_assert (0 == event->value.publish.completed);
204 GNUNET_assert (2 == event->value.publish.anonymity);
206 else if (0 == strcmp ("publish-context-dir", 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 ( (NULL != event->value.publish.cctx) &&
217 (0 == strcmp ("publish-context-dir", event->value.publish.cctx)) )
218 GNUNET_assert (publish == event->value.publish.pc);
221 printf ("Unexpected event: %d\n", event->status);
230 const struct GNUNET_CONFIGURATION_Handle *c,
231 struct GNUNET_TESTING_Peer *peer)
233 const char *keywords[] = {
238 struct GNUNET_CONTAINER_MetaData *meta;
239 struct GNUNET_FS_Uri *kuri;
240 struct GNUNET_FS_FileInformation *fi1;
241 struct GNUNET_FS_FileInformation *fi2;
242 struct GNUNET_FS_FileInformation *fidir;
244 struct GNUNET_FS_BlockOptions bo;
247 fs = GNUNET_FS_start (cfg, "test-fs-publish-persistence", &progress_cb, NULL,
248 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
249 GNUNET_assert (NULL != fs);
250 fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
251 buf = GNUNET_malloc (FILESIZE);
252 for (i = 0; i < FILESIZE; i++)
253 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
254 GNUNET_assert (FILESIZE ==
255 GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
256 GNUNET_DISK_PERM_USER_READ |
257 GNUNET_DISK_PERM_USER_WRITE));
260 fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
261 buf = GNUNET_malloc (FILESIZE);
262 for (i = 0; i < FILESIZE; i++)
263 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
264 GNUNET_assert (FILESIZE ==
265 GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
266 GNUNET_DISK_PERM_USER_READ |
267 GNUNET_DISK_PERM_USER_WRITE));
270 meta = GNUNET_CONTAINER_meta_data_create ();
271 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
272 bo.content_priority = 42;
273 bo.anonymity_level = 1;
274 bo.replication_level = 0;
275 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
277 GNUNET_FS_file_information_create_from_file (fs, "publish-context1", fn1,
278 kuri, meta, GNUNET_YES, &bo);
279 GNUNET_assert (NULL != fi1);
280 bo.anonymity_level = 2;
282 GNUNET_FS_file_information_create_from_file (fs, "publish-context2", fn2,
283 kuri, meta, GNUNET_YES, &bo);
284 GNUNET_assert (NULL != fi2);
285 bo.anonymity_level = 3;
287 GNUNET_FS_file_information_create_empty_directory (fs,
288 "publish-context-dir",
289 kuri, meta, &bo, NULL);
290 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
291 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
292 GNUNET_FS_uri_destroy (kuri);
293 GNUNET_CONTAINER_meta_data_destroy (meta);
294 GNUNET_assert (NULL != fidir);
295 start = GNUNET_TIME_absolute_get ();
296 GNUNET_FS_publish_start (fs, fidir, NULL, NULL, NULL,
297 GNUNET_FS_PUBLISH_OPTION_NONE);
298 GNUNET_assert (publish != NULL);
303 main (int argc, char *argv[])
305 if (0 != GNUNET_TESTING_peer_run ("test-fs-publish-persistence",
306 "test_fs_publish_data.conf",
312 /* end of test_fs_publish_persistence.c */