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_download_persistence.c
23 * @brief simple testcase for persistence of simple download operation
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #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 const struct GNUNET_CONFIGURATION_Handle *cfg;
51 static struct GNUNET_FS_Handle *fs;
53 static struct GNUNET_FS_DownloadContext *download;
55 static struct GNUNET_FS_PublishContext *publish;
57 static GNUNET_SCHEDULER_TaskIdentifier timeout_kill;
65 timeout_kill_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
67 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
70 GNUNET_FS_download_stop (download, GNUNET_YES);
73 else if (publish != NULL)
75 GNUNET_FS_publish_stop (publish);
78 timeout_kill = GNUNET_SCHEDULER_NO_TASK;
84 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 GNUNET_FS_publish_stop (publish);
95 abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 GNUNET_FS_download_stop (download, GNUNET_YES);
104 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
105 GNUNET_assert (size == FILESIZE);
106 GNUNET_DISK_directory_remove (fn);
109 GNUNET_SCHEDULER_cancel (timeout_kill);
110 timeout_kill = GNUNET_SCHEDULER_NO_TASK;
115 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
119 restart_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Restarting FS.\n");
123 fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
124 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
129 * Consider scheduling the restart-task.
130 * Only runs the restart task once per event
133 * @param ev type of the event to consider
136 consider_restart (int ev)
142 for (i = 0; i < off; i++)
146 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
147 &restart_fs_task, NULL);
152 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
154 switch (event->status)
156 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
159 (unsigned long long) event->value.publish.completed,
160 (unsigned long long) event->value.publish.size,
161 event->value.publish.specifics.progress.depth,
162 (unsigned long long) event->value.publish.specifics.
165 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
166 printf ("Publishing complete, %llu kbps.\n",
167 (unsigned long long) (FILESIZE * 1000LL /
169 GNUNET_TIME_absolute_get_duration
170 (start).rel_value) / 1024LL));
171 fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
172 start = GNUNET_TIME_absolute_get ();
173 GNUNET_assert (download == NULL);
174 GNUNET_FS_download_start (fs,
175 event->value.publish.specifics.completed.chk_uri,
176 NULL, fn, NULL, 0, FILESIZE, 1,
177 GNUNET_FS_DOWNLOAD_OPTION_NONE, "download", NULL);
179 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
180 consider_restart (event->status);
181 printf ("Download complete, %llu kbps.\n",
182 (unsigned long long) (FILESIZE * 1000LL /
184 GNUNET_TIME_absolute_get_duration
185 (start).rel_value) / 1024LL));
186 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
188 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
189 consider_restart (event->status);
190 GNUNET_assert (download == event->value.download.dc);
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
193 (unsigned long long) event->value.download.completed,
194 (unsigned long long) event->value.download.size,
195 event->value.download.specifics.progress.depth,
196 (unsigned long long) event->value.download.specifics.
199 case GNUNET_FS_STATUS_PUBLISH_ERROR:
200 FPRINTF (stderr, "Error publishing file: %s\n",
201 event->value.publish.specifics.error.message);
203 GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
204 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
206 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
207 FPRINTF (stderr, "Error downloading file: %s\n",
208 event->value.download.specifics.error.message);
209 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
211 case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
212 GNUNET_assert (event->value.publish.pc == publish);
215 case GNUNET_FS_STATUS_PUBLISH_RESUME:
216 GNUNET_assert (NULL == publish);
217 publish = event->value.publish.pc;
219 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download suspended.\n");
221 GNUNET_assert (event->value.download.dc == download);
224 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
225 GNUNET_assert (NULL == download);
226 download = event->value.download.dc;
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download resumed.\n");
229 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
230 consider_restart (event->status);
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download active.\n");
233 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
234 consider_restart (event->status);
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download inactive.\n");
237 case GNUNET_FS_STATUS_PUBLISH_START:
238 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
239 GNUNET_assert (NULL == event->value.publish.pctx);
240 GNUNET_assert (FILESIZE == event->value.publish.size);
241 GNUNET_assert (0 == event->value.publish.completed);
242 GNUNET_assert (1 == event->value.publish.anonymity);
244 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
245 GNUNET_assert (publish == event->value.publish.pc);
246 GNUNET_assert (FILESIZE == event->value.publish.size);
247 GNUNET_assert (1 == event->value.publish.anonymity);
251 case GNUNET_FS_STATUS_DOWNLOAD_START:
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download started.\n");
253 consider_restart (event->status);
254 GNUNET_assert (download == NULL);
255 download = event->value.download.dc;
256 GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
257 GNUNET_assert (NULL == event->value.download.pctx);
258 GNUNET_assert (NULL != event->value.download.uri);
259 GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
260 GNUNET_assert (FILESIZE == event->value.download.size);
261 GNUNET_assert (0 == event->value.download.completed);
262 GNUNET_assert (1 == event->value.download.anonymity);
264 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
265 GNUNET_assert (download == event->value.download.dc);
266 GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
267 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
271 printf ("Unexpected event: %d\n", event->status);
280 const struct GNUNET_CONFIGURATION_Handle *c,
281 struct GNUNET_TESTING_Peer *peer)
283 const char *keywords[] = {
288 struct GNUNET_CONTAINER_MetaData *meta;
289 struct GNUNET_FS_Uri *kuri;
290 struct GNUNET_FS_FileInformation *fi;
292 struct GNUNET_FS_BlockOptions bo;
295 fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
296 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
297 GNUNET_assert (NULL != fs);
298 buf = GNUNET_malloc (FILESIZE);
299 for (i = 0; i < FILESIZE; i++)
300 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
301 meta = GNUNET_CONTAINER_meta_data_create ();
302 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
303 bo.content_priority = 42;
304 bo.anonymity_level = 1;
305 bo.replication_level = 0;
306 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
307 fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
308 FILESIZE, buf, kuri, meta,
310 GNUNET_FS_uri_destroy (kuri);
311 GNUNET_CONTAINER_meta_data_destroy (meta);
312 GNUNET_assert (NULL != fi);
314 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
315 start = GNUNET_TIME_absolute_get ();
317 GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
318 GNUNET_FS_PUBLISH_OPTION_NONE);
319 GNUNET_assert (publish != NULL);
324 main (int argc, char *argv[])
326 if (0 != GNUNET_TESTING_peer_run ("test-fs-download-persistence",
327 "test_fs_download_data.conf",
333 /* end of test_fs_download_persistence.c */