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 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.
17 * @file fs/test_fs_download.c
18 * @brief simple testcase for simple publish + download operation
19 * @author Christian Grothoff
23 #include "gnunet_util_lib.h"
24 #include "gnunet_fs_service.h"
25 #include "gnunet_testing_lib.h"
29 * File-size we use for testing.
31 #define FILESIZE (1024 * 1024 * 2)
34 * How long until we give up on transmitting the message?
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
39 * How long should our test-content live?
41 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
43 static unsigned int anonymity_level;
47 static struct GNUNET_TIME_Absolute start;
49 static struct GNUNET_FS_Handle *fs;
51 static struct GNUNET_FS_DownloadContext *download;
53 static struct GNUNET_FS_PublishContext *publish;
55 static struct GNUNET_SCHEDULER_Task * timeout_kill;
65 timeout_kill_task (void *cls)
69 GNUNET_FS_download_stop (download, GNUNET_YES);
72 else if (NULL != publish)
74 GNUNET_FS_publish_stop (publish);
77 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
84 abort_publish_task (void *cls)
88 GNUNET_FS_publish_stop (publish);
95 stop_fs_task (void *cls)
103 abort_download_task (void *cls)
107 if (NULL != download)
109 GNUNET_FS_download_stop (download, GNUNET_YES);
112 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
113 GNUNET_assert (size == FILESIZE);
114 GNUNET_DISK_directory_remove (fn);
117 GNUNET_SCHEDULER_cancel (timeout_kill);
123 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
126 switch (event->status)
128 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
131 (unsigned long long) event->value.publish.completed,
132 (unsigned long long) event->value.publish.size,
133 event->value.publish.specifics.progress.depth,
134 (unsigned long long) event->value.publish.specifics.
137 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
139 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
141 "Publishing complete, %llu kb/s.\n",
142 (unsigned long long) (FILESIZE * 1000000LL /
144 GNUNET_TIME_absolute_get_duration
145 (start).rel_value_us) / 1024LL));
147 (GNUNET_YES == indexed)
148 ? "Publishing speed (indexing)"
149 : "Publishing speed (insertion)",
150 (unsigned long long) (FILESIZE * 1000000LL /
152 GNUNET_TIME_absolute_get_duration
153 (start).rel_value_us) / 1024LL), "kb/s");
154 fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
155 start = GNUNET_TIME_absolute_get ();
157 GNUNET_FS_download_start (fs,
158 event->value.publish.specifics.
159 completed.chk_uri, NULL, fn, NULL, 0,
160 FILESIZE, anonymity_level,
161 GNUNET_FS_DOWNLOAD_OPTION_NONE,
163 GNUNET_assert (download != NULL);
165 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
167 "Download complete, %llu kb/s.\n",
168 (unsigned long long) (FILESIZE * 1000000LL /
170 GNUNET_TIME_absolute_get_duration
171 (start).rel_value_us) / 1024LL));
173 (GNUNET_YES == indexed)
174 ? "Local download speed (indexed)"
175 : "Local download speed (inserted)",
176 (unsigned long long) (FILESIZE * 1000000LL /
178 GNUNET_TIME_absolute_get_duration
179 (start).rel_value_us) / 1024LL), "kb/s");
180 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
182 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
183 GNUNET_assert (download == event->value.download.dc);
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
186 (unsigned long long) event->value.download.completed,
187 (unsigned long long) event->value.download.size,
188 event->value.download.specifics.progress.depth,
189 (unsigned long long) event->value.download.specifics.
192 case GNUNET_FS_STATUS_PUBLISH_ERROR:
193 FPRINTF (stderr, "Error publishing file: %s\n",
194 event->value.publish.specifics.error.message);
196 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
197 GNUNET_SCHEDULER_shutdown ();
199 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
200 FPRINTF (stderr, "Error downloading file: %s\n",
201 event->value.download.specifics.error.message);
202 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
203 GNUNET_SCHEDULER_shutdown ();
205 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
206 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
208 case GNUNET_FS_STATUS_PUBLISH_START:
209 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
210 GNUNET_assert (NULL == event->value.publish.pctx);
211 GNUNET_assert (FILESIZE == event->value.publish.size);
212 GNUNET_assert (0 == event->value.publish.completed);
213 GNUNET_assert (1 == event->value.publish.anonymity);
215 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
216 GNUNET_assert (publish == event->value.publish.pc);
217 GNUNET_assert (FILESIZE == event->value.publish.size);
218 GNUNET_assert (1 == event->value.publish.anonymity);
219 GNUNET_SCHEDULER_add_now (&stop_fs_task, NULL);
221 case GNUNET_FS_STATUS_DOWNLOAD_START:
222 GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
223 GNUNET_assert (NULL == event->value.download.pctx);
224 GNUNET_assert (NULL != event->value.download.uri);
225 GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
226 GNUNET_assert (FILESIZE == event->value.download.size);
227 GNUNET_assert (0 == event->value.download.completed);
228 GNUNET_assert (1 == event->value.download.anonymity);
230 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
231 GNUNET_assert (download == event->value.download.dc);
232 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
235 printf ("Unexpected event: %d\n", event->status);
244 const struct GNUNET_CONFIGURATION_Handle *cfg,
245 struct GNUNET_TESTING_Peer *peer)
247 const char *binary_name = cls;
248 const char *keywords[] = {
253 struct GNUNET_CONTAINER_MetaData *meta;
254 struct GNUNET_FS_Uri *kuri;
255 struct GNUNET_FS_FileInformation *fi;
257 struct GNUNET_FS_BlockOptions bo;
260 GNUNET_CONFIGURATION_get_value_yesno (cfg,
266 fs = GNUNET_FS_start (cfg, binary_name, &progress_cb, NULL,
267 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
268 GNUNET_assert (NULL != fs);
269 buf = GNUNET_malloc (FILESIZE);
270 for (i = 0; i < FILESIZE; i++)
271 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
272 meta = GNUNET_CONTAINER_meta_data_create ();
273 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
274 bo.content_priority = 42;
275 bo.anonymity_level = anonymity_level;
276 bo.replication_level = 0;
277 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
280 GNUNET_CONFIGURATION_get_value_yesno (cfg,
284 fn1 = GNUNET_DISK_mktemp ("gnunet-download-indexed-test");
285 GNUNET_assert (FILESIZE ==
286 GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
287 GNUNET_DISK_PERM_USER_READ |
288 GNUNET_DISK_PERM_USER_WRITE));
290 fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn1,
291 kuri, meta, GNUNET_YES,
293 indexed = GNUNET_YES;
297 fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
298 FILESIZE, buf, kuri, meta,
300 /* note: buf will be free'd as part of 'fi' now */
303 GNUNET_FS_uri_destroy (kuri);
304 GNUNET_CONTAINER_meta_data_destroy (meta);
305 GNUNET_assert (NULL != fi);
307 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
308 start = GNUNET_TIME_absolute_get ();
310 GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
311 GNUNET_FS_PUBLISH_OPTION_NONE);
312 GNUNET_assert (publish != NULL);
317 main (int argc, char *argv[])
319 const char *binary_name;
320 const char *config_name;
322 binary_name = "test-fs-download";
323 config_name = "test_fs_download_data.conf";
324 if (NULL != strstr (argv[0], "indexed"))
326 binary_name = "test-fs-download-indexed";
327 config_name = "test_fs_download_indexed.conf";
329 if (NULL != strstr (argv[0], "cadet"))
331 binary_name = "test-fs-download-cadet";
332 config_name = "test_fs_download_cadet.conf";
334 if (0 != GNUNET_TESTING_peer_run (binary_name,
336 &run, (void *) binary_name))
346 /* end of test_fs_download.c */