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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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,
106 GNUNET_assert (size == FILESIZE);
107 GNUNET_DISK_directory_remove (fn);
110 GNUNET_SCHEDULER_cancel (timeout_kill);
116 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
120 restart_fs_task (void *cls)
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Restarting FS.\n");
124 fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
125 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
130 * Consider scheduling the restart-task.
131 * Only runs the restart task once per event
134 * @param ev type of the event to consider
137 consider_restart (int ev)
143 for (i = 0; i < off; i++)
147 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
148 &restart_fs_task, NULL);
153 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
155 switch (event->status)
157 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "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.
167 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
170 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
171 printf ("Publishing complete, %llu kbps.\n",
172 (unsigned long long) (FILESIZE * 1000000LL
174 + GNUNET_TIME_absolute_get_duration
175 (start).rel_value_us) / 1024LL));
176 fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
177 start = GNUNET_TIME_absolute_get ();
178 GNUNET_assert (download == NULL);
179 GNUNET_FS_download_start (fs,
180 event->value.publish.specifics.completed.chk_uri,
181 NULL, fn, NULL, 0, FILESIZE, 1,
182 GNUNET_FS_DOWNLOAD_OPTION_NONE, "download", NULL);
185 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
186 printf ("Download complete, %llu kbps.\n",
187 (unsigned long long) (FILESIZE * 1000000LL
189 + GNUNET_TIME_absolute_get_duration
190 (start).rel_value_us) / 1024LL));
191 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
194 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
195 consider_restart (event->status);
196 GNUNET_assert (download == event->value.download.dc);
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
199 (unsigned long long) event->value.download.completed,
200 (unsigned long long) event->value.download.size,
201 event->value.download.specifics.progress.depth,
202 (unsigned long long) event->value.download.specifics.
206 case GNUNET_FS_STATUS_PUBLISH_ERROR:
207 fprintf (stderr, "Error publishing file: %s\n",
208 event->value.publish.specifics.error.message);
210 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
213 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
214 fprintf (stderr, "Error downloading file: %s\n",
215 event->value.download.specifics.error.message);
216 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
219 case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
220 GNUNET_assert (event->value.publish.pc == publish);
224 case GNUNET_FS_STATUS_PUBLISH_RESUME:
225 GNUNET_assert (NULL == publish);
226 publish = event->value.publish.pc;
229 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download suspended.\n");
231 GNUNET_assert (event->value.download.dc == download);
235 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
236 GNUNET_assert (NULL == download);
237 download = event->value.download.dc;
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download resumed.\n");
241 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
242 consider_restart (event->status);
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download active.\n");
246 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
247 consider_restart (event->status);
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download inactive.\n");
251 case GNUNET_FS_STATUS_PUBLISH_START:
252 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
253 GNUNET_assert (NULL == event->value.publish.pctx);
254 GNUNET_assert (FILESIZE == event->value.publish.size);
255 GNUNET_assert (0 == event->value.publish.completed);
256 GNUNET_assert (1 == event->value.publish.anonymity);
259 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
260 GNUNET_assert (publish == event->value.publish.pc);
261 GNUNET_assert (FILESIZE == event->value.publish.size);
262 GNUNET_assert (1 == event->value.publish.anonymity);
267 case GNUNET_FS_STATUS_DOWNLOAD_START:
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download started.\n");
269 consider_restart (event->status);
270 GNUNET_assert (download == NULL);
271 download = event->value.download.dc;
272 GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
273 GNUNET_assert (NULL == event->value.download.pctx);
274 GNUNET_assert (NULL != event->value.download.uri);
275 GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
276 GNUNET_assert (FILESIZE == event->value.download.size);
277 GNUNET_assert (0 == event->value.download.completed);
278 GNUNET_assert (1 == event->value.download.anonymity);
281 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
282 GNUNET_assert (download == event->value.download.dc);
283 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
288 printf ("Unexpected event: %d\n", event->status);
297 const struct GNUNET_CONFIGURATION_Handle *c,
298 struct GNUNET_TESTING_Peer *peer)
300 const char *keywords[] = {
305 struct GNUNET_CONTAINER_MetaData *meta;
306 struct GNUNET_FS_Uri *kuri;
307 struct GNUNET_FS_FileInformation *fi;
309 struct GNUNET_FS_BlockOptions bo;
312 fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
313 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
314 GNUNET_assert (NULL != fs);
315 buf = GNUNET_malloc (FILESIZE);
316 for (i = 0; i < FILESIZE; i++)
317 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
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 fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
325 FILESIZE, buf, kuri, meta,
327 GNUNET_FS_uri_destroy (kuri);
328 GNUNET_CONTAINER_meta_data_destroy (meta);
329 GNUNET_assert (NULL != fi);
331 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
332 start = GNUNET_TIME_absolute_get ();
334 GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
335 GNUNET_FS_PUBLISH_OPTION_NONE);
336 GNUNET_assert (publish != NULL);
341 main (int argc, char *argv[])
343 if (0 != GNUNET_TESTING_peer_run ("test-fs-download-persistence",
344 "test_fs_download_data.conf",
351 /* end of test_fs_download_persistence.c */