2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2008, 2009, 2011, 2012 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.c
23 * @brief simple testcase for simple publish + download operation
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_fs_service.h"
30 #include "gnunet_testing_lib.h"
34 * File-size we use for testing.
36 #define FILESIZE (1024 * 1024 * 2)
39 * How long until we give up on transmitting the message?
41 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120)
44 * How long should our test-content live?
46 #define LIFETIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15)
48 static unsigned int anonymity_level;
52 static struct GNUNET_TIME_Absolute start;
54 static struct GNUNET_FS_Handle *fs;
56 static struct GNUNET_FS_DownloadContext *download;
58 static struct GNUNET_FS_PublishContext *publish;
60 static struct GNUNET_SCHEDULER_Task * timeout_kill;
70 timeout_kill_task(void *cls)
74 GNUNET_FS_download_stop(download, GNUNET_YES);
77 else if (NULL != publish)
79 GNUNET_FS_publish_stop(publish);
82 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
89 abort_publish_task(void *cls)
93 GNUNET_FS_publish_stop(publish);
100 stop_fs_task(void *cls)
108 abort_download_task(void *cls)
112 if (NULL != download)
114 GNUNET_FS_download_stop(download, GNUNET_YES);
117 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_size(fn, &size, GNUNET_YES, GNUNET_NO));
118 GNUNET_assert(size == FILESIZE);
119 GNUNET_DISK_directory_remove(fn);
122 GNUNET_SCHEDULER_cancel(timeout_kill);
128 progress_cb(void *cls, const struct GNUNET_FS_ProgressInfo *event)
130 switch (event->status)
132 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
133 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
134 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
135 (unsigned long long)event->value.publish.completed,
136 (unsigned long long)event->value.publish.size,
137 event->value.publish.specifics.progress.depth,
138 (unsigned long long)event->value.publish.specifics.
142 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
145 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
147 "Publishing complete, %llu kb/s.\n",
148 (unsigned long long)(FILESIZE * 1000000LL /
150 GNUNET_TIME_absolute_get_duration
151 (start).rel_value_us) / 1024LL));
153 (GNUNET_YES == indexed)
154 ? "Publishing speed (indexing)"
155 : "Publishing speed (insertion)",
156 (unsigned long long)(FILESIZE * 1000000LL /
158 GNUNET_TIME_absolute_get_duration
159 (start).rel_value_us) / 1024LL), "kb/s");
160 fn = GNUNET_DISK_mktemp("gnunet-download-test-dst");
161 start = GNUNET_TIME_absolute_get();
163 GNUNET_FS_download_start(fs,
164 event->value.publish.specifics.
165 completed.chk_uri, NULL, fn, NULL, 0,
166 FILESIZE, anonymity_level,
167 GNUNET_FS_DOWNLOAD_OPTION_NONE,
169 GNUNET_assert(download != NULL);
172 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
174 "Download complete, %llu kb/s.\n",
175 (unsigned long long)(FILESIZE * 1000000LL /
177 GNUNET_TIME_absolute_get_duration
178 (start).rel_value_us) / 1024LL));
180 (GNUNET_YES == indexed)
181 ? "Local download speed (indexed)"
182 : "Local download speed (inserted)",
183 (unsigned long long)(FILESIZE * 1000000LL /
185 GNUNET_TIME_absolute_get_duration
186 (start).rel_value_us) / 1024LL), "kb/s");
187 GNUNET_SCHEDULER_add_now(&abort_download_task, NULL);
190 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
191 GNUNET_assert(download == event->value.download.dc);
192 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
193 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
194 (unsigned long long)event->value.download.completed,
195 (unsigned long long)event->value.download.size,
196 event->value.download.specifics.progress.depth,
197 (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);
206 GNUNET_SCHEDULER_shutdown();
209 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
210 fprintf(stderr, "Error downloading file: %s\n",
211 event->value.download.specifics.error.message);
212 GNUNET_SCHEDULER_add_now(&abort_download_task, NULL);
213 GNUNET_SCHEDULER_shutdown();
216 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
217 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
220 case GNUNET_FS_STATUS_PUBLISH_START:
221 GNUNET_assert(0 == strcmp("publish-context", event->value.publish.cctx));
222 GNUNET_assert(NULL == event->value.publish.pctx);
223 GNUNET_assert(FILESIZE == event->value.publish.size);
224 GNUNET_assert(0 == event->value.publish.completed);
225 GNUNET_assert(1 == event->value.publish.anonymity);
228 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
229 GNUNET_assert(publish == event->value.publish.pc);
230 GNUNET_assert(FILESIZE == event->value.publish.size);
231 GNUNET_assert(1 == event->value.publish.anonymity);
232 GNUNET_SCHEDULER_add_now(&stop_fs_task, NULL);
235 case GNUNET_FS_STATUS_DOWNLOAD_START:
236 GNUNET_assert(0 == strcmp("download", event->value.download.cctx));
237 GNUNET_assert(NULL == event->value.download.pctx);
238 GNUNET_assert(NULL != event->value.download.uri);
239 GNUNET_assert(0 == strcmp(fn, event->value.download.filename));
240 GNUNET_assert(FILESIZE == event->value.download.size);
241 GNUNET_assert(0 == event->value.download.completed);
242 GNUNET_assert(1 == event->value.download.anonymity);
245 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
246 GNUNET_assert(download == event->value.download.dc);
247 GNUNET_SCHEDULER_add_now(&abort_publish_task, NULL);
251 printf("Unexpected event: %d\n", event->status);
260 const struct GNUNET_CONFIGURATION_Handle *cfg,
261 struct GNUNET_TESTING_Peer *peer)
263 const char *binary_name = cls;
264 const char *keywords[] = {
269 struct GNUNET_CONTAINER_MetaData *meta;
270 struct GNUNET_FS_Uri *kuri;
271 struct GNUNET_FS_FileInformation *fi;
273 struct GNUNET_FS_BlockOptions bo;
276 GNUNET_CONFIGURATION_get_value_yesno(cfg,
282 fs = GNUNET_FS_start(cfg, binary_name, &progress_cb, NULL,
283 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
284 GNUNET_assert(NULL != fs);
285 buf = GNUNET_malloc(FILESIZE);
286 for (i = 0; i < FILESIZE; i++)
287 buf[i] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 256);
288 meta = GNUNET_CONTAINER_meta_data_create();
289 kuri = GNUNET_FS_uri_ksk_create_from_args(2, keywords);
290 bo.content_priority = 42;
291 bo.anonymity_level = anonymity_level;
292 bo.replication_level = 0;
293 bo.expiration_time = GNUNET_TIME_relative_to_absolute(LIFETIME);
296 GNUNET_CONFIGURATION_get_value_yesno(cfg,
300 fn1 = GNUNET_DISK_mktemp("gnunet-download-indexed-test");
301 GNUNET_assert(FILESIZE ==
302 GNUNET_DISK_fn_write(fn1, buf, FILESIZE,
303 GNUNET_DISK_PERM_USER_READ |
304 GNUNET_DISK_PERM_USER_WRITE));
306 fi = GNUNET_FS_file_information_create_from_file(fs, "publish-context", fn1,
307 kuri, meta, GNUNET_YES,
309 indexed = GNUNET_YES;
313 fi = GNUNET_FS_file_information_create_from_data(fs, "publish-context",
314 FILESIZE, buf, kuri, meta,
316 /* note: buf will be free'd as part of 'fi' now */
319 GNUNET_FS_uri_destroy(kuri);
320 GNUNET_CONTAINER_meta_data_destroy(meta);
321 GNUNET_assert(NULL != fi);
323 GNUNET_SCHEDULER_add_delayed(TIMEOUT, &timeout_kill_task, NULL);
324 start = GNUNET_TIME_absolute_get();
326 GNUNET_FS_publish_start(fs, fi, NULL, NULL, NULL,
327 GNUNET_FS_PUBLISH_OPTION_NONE);
328 GNUNET_assert(publish != NULL);
333 main(int argc, char *argv[])
335 const char *binary_name;
336 const char *config_name;
338 binary_name = "test-fs-download";
339 config_name = "test_fs_download_data.conf";
340 if (NULL != strstr(argv[0], "indexed"))
342 binary_name = "test-fs-download-indexed";
343 config_name = "test_fs_download_indexed.conf";
345 if (NULL != strstr(argv[0], "cadet"))
347 binary_name = "test-fs-download-cadet";
348 config_name = "test_fs_download_cadet.conf";
350 if (0 != GNUNET_TESTING_peer_run(binary_name,
352 &run, (void *)binary_name))
362 /* end of test_fs_download.c */