2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 struct GNUNET_SCHEDULER_Task * timeout_kill;
65 timeout_kill_task (void *cls)
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);
84 abort_publish_task (void *cls)
88 GNUNET_FS_publish_stop (publish);
95 abort_download_task (void *cls)
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);
115 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
119 restart_fs_task (void *cls)
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_now (&abort_publish_task, NULL);
207 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
208 FPRINTF (stderr, "Error downloading file: %s\n",
209 event->value.download.specifics.error.message);
210 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
212 case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
213 GNUNET_assert (event->value.publish.pc == publish);
216 case GNUNET_FS_STATUS_PUBLISH_RESUME:
217 GNUNET_assert (NULL == publish);
218 publish = event->value.publish.pc;
220 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download suspended.\n");
222 GNUNET_assert (event->value.download.dc == download);
225 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
226 GNUNET_assert (NULL == download);
227 download = event->value.download.dc;
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download resumed.\n");
230 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
231 consider_restart (event->status);
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download active.\n");
234 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
235 consider_restart (event->status);
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download inactive.\n");
238 case GNUNET_FS_STATUS_PUBLISH_START:
239 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
240 GNUNET_assert (NULL == event->value.publish.pctx);
241 GNUNET_assert (FILESIZE == event->value.publish.size);
242 GNUNET_assert (0 == event->value.publish.completed);
243 GNUNET_assert (1 == event->value.publish.anonymity);
245 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
246 GNUNET_assert (publish == event->value.publish.pc);
247 GNUNET_assert (FILESIZE == event->value.publish.size);
248 GNUNET_assert (1 == event->value.publish.anonymity);
252 case GNUNET_FS_STATUS_DOWNLOAD_START:
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download started.\n");
254 consider_restart (event->status);
255 GNUNET_assert (download == NULL);
256 download = event->value.download.dc;
257 GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
258 GNUNET_assert (NULL == event->value.download.pctx);
259 GNUNET_assert (NULL != event->value.download.uri);
260 GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
261 GNUNET_assert (FILESIZE == event->value.download.size);
262 GNUNET_assert (0 == event->value.download.completed);
263 GNUNET_assert (1 == event->value.download.anonymity);
265 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
266 GNUNET_assert (download == event->value.download.dc);
267 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
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 */