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_PROGRESS_DIRECTORY:
167 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
168 printf ("Publishing complete, %llu kbps.\n",
169 (unsigned long long) (FILESIZE * 1000000LL /
171 GNUNET_TIME_absolute_get_duration
172 (start).rel_value_us) / 1024LL));
173 fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
174 start = GNUNET_TIME_absolute_get ();
175 GNUNET_assert (download == NULL);
176 GNUNET_FS_download_start (fs,
177 event->value.publish.specifics.completed.chk_uri,
178 NULL, fn, NULL, 0, FILESIZE, 1,
179 GNUNET_FS_DOWNLOAD_OPTION_NONE, "download", NULL);
181 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
182 consider_restart (event->status);
183 printf ("Download complete, %llu kbps.\n",
184 (unsigned long long) (FILESIZE * 1000000LL /
186 GNUNET_TIME_absolute_get_duration
187 (start).rel_value_us) / 1024LL));
188 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
190 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
191 consider_restart (event->status);
192 GNUNET_assert (download == event->value.download.dc);
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
195 (unsigned long long) event->value.download.completed,
196 (unsigned long long) event->value.download.size,
197 event->value.download.specifics.progress.depth,
198 (unsigned long long) event->value.download.specifics.
201 case GNUNET_FS_STATUS_PUBLISH_ERROR:
202 FPRINTF (stderr, "Error publishing file: %s\n",
203 event->value.publish.specifics.error.message);
205 GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
206 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
208 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
209 FPRINTF (stderr, "Error downloading file: %s\n",
210 event->value.download.specifics.error.message);
211 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
213 case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
214 GNUNET_assert (event->value.publish.pc == publish);
217 case GNUNET_FS_STATUS_PUBLISH_RESUME:
218 GNUNET_assert (NULL == publish);
219 publish = event->value.publish.pc;
221 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download suspended.\n");
223 GNUNET_assert (event->value.download.dc == download);
226 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
227 GNUNET_assert (NULL == download);
228 download = event->value.download.dc;
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download resumed.\n");
231 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
232 consider_restart (event->status);
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download active.\n");
235 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
236 consider_restart (event->status);
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download inactive.\n");
239 case GNUNET_FS_STATUS_PUBLISH_START:
240 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
241 GNUNET_assert (NULL == event->value.publish.pctx);
242 GNUNET_assert (FILESIZE == event->value.publish.size);
243 GNUNET_assert (0 == event->value.publish.completed);
244 GNUNET_assert (1 == event->value.publish.anonymity);
246 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
247 GNUNET_assert (publish == event->value.publish.pc);
248 GNUNET_assert (FILESIZE == event->value.publish.size);
249 GNUNET_assert (1 == event->value.publish.anonymity);
253 case GNUNET_FS_STATUS_DOWNLOAD_START:
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download started.\n");
255 consider_restart (event->status);
256 GNUNET_assert (download == NULL);
257 download = event->value.download.dc;
258 GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
259 GNUNET_assert (NULL == event->value.download.pctx);
260 GNUNET_assert (NULL != event->value.download.uri);
261 GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
262 GNUNET_assert (FILESIZE == event->value.download.size);
263 GNUNET_assert (0 == event->value.download.completed);
264 GNUNET_assert (1 == event->value.download.anonymity);
266 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
267 GNUNET_assert (download == event->value.download.dc);
268 GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
269 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
273 printf ("Unexpected event: %d\n", event->status);
282 const struct GNUNET_CONFIGURATION_Handle *c,
283 struct GNUNET_TESTING_Peer *peer)
285 const char *keywords[] = {
290 struct GNUNET_CONTAINER_MetaData *meta;
291 struct GNUNET_FS_Uri *kuri;
292 struct GNUNET_FS_FileInformation *fi;
294 struct GNUNET_FS_BlockOptions bo;
297 fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
298 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
299 GNUNET_assert (NULL != fs);
300 buf = GNUNET_malloc (FILESIZE);
301 for (i = 0; i < FILESIZE; i++)
302 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
303 meta = GNUNET_CONTAINER_meta_data_create ();
304 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
305 bo.content_priority = 42;
306 bo.anonymity_level = 1;
307 bo.replication_level = 0;
308 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
309 fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
310 FILESIZE, buf, kuri, meta,
312 GNUNET_FS_uri_destroy (kuri);
313 GNUNET_CONTAINER_meta_data_destroy (meta);
314 GNUNET_assert (NULL != fi);
316 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
317 start = GNUNET_TIME_absolute_get ();
319 GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
320 GNUNET_FS_PUBLISH_OPTION_NONE);
321 GNUNET_assert (publish != NULL);
326 main (int argc, char *argv[])
328 if (0 != GNUNET_TESTING_peer_run ("test-fs-download-persistence",
329 "test_fs_download_data.conf",
335 /* end of test_fs_download_persistence.c */