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/>.
20 * @file fs/test_fs_download.c
21 * @brief simple testcase for simple publish + download operation
22 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_fs_service.h"
28 #include "gnunet_testing_lib.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, 120)
42 * How long should our test-content live?
44 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
46 static unsigned int anonymity_level;
50 static struct GNUNET_TIME_Absolute start;
52 static struct GNUNET_FS_Handle *fs;
54 static struct GNUNET_FS_DownloadContext *download;
56 static struct GNUNET_FS_PublishContext *publish;
58 static struct GNUNET_SCHEDULER_Task * timeout_kill;
68 timeout_kill_task (void *cls)
72 GNUNET_FS_download_stop (download, GNUNET_YES);
75 else if (NULL != publish)
77 GNUNET_FS_publish_stop (publish);
80 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
87 abort_publish_task (void *cls)
91 GNUNET_FS_publish_stop (publish);
98 stop_fs_task (void *cls)
106 abort_download_task (void *cls)
110 if (NULL != download)
112 GNUNET_FS_download_stop (download, GNUNET_YES);
115 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
116 GNUNET_assert (size == FILESIZE);
117 GNUNET_DISK_directory_remove (fn);
120 GNUNET_SCHEDULER_cancel (timeout_kill);
126 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
129 switch (event->status)
131 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
134 (unsigned long long) event->value.publish.completed,
135 (unsigned long long) event->value.publish.size,
136 event->value.publish.specifics.progress.depth,
137 (unsigned long long) event->value.publish.specifics.
140 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
142 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
144 "Publishing complete, %llu kb/s.\n",
145 (unsigned long long) (FILESIZE * 1000000LL /
147 GNUNET_TIME_absolute_get_duration
148 (start).rel_value_us) / 1024LL));
150 (GNUNET_YES == indexed)
151 ? "Publishing speed (indexing)"
152 : "Publishing speed (insertion)",
153 (unsigned long long) (FILESIZE * 1000000LL /
155 GNUNET_TIME_absolute_get_duration
156 (start).rel_value_us) / 1024LL), "kb/s");
157 fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
158 start = GNUNET_TIME_absolute_get ();
160 GNUNET_FS_download_start (fs,
161 event->value.publish.specifics.
162 completed.chk_uri, NULL, fn, NULL, 0,
163 FILESIZE, anonymity_level,
164 GNUNET_FS_DOWNLOAD_OPTION_NONE,
166 GNUNET_assert (download != NULL);
168 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
170 "Download complete, %llu kb/s.\n",
171 (unsigned long long) (FILESIZE * 1000000LL /
173 GNUNET_TIME_absolute_get_duration
174 (start).rel_value_us) / 1024LL));
176 (GNUNET_YES == indexed)
177 ? "Local download speed (indexed)"
178 : "Local download speed (inserted)",
179 (unsigned long long) (FILESIZE * 1000000LL /
181 GNUNET_TIME_absolute_get_duration
182 (start).rel_value_us) / 1024LL), "kb/s");
183 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
185 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
186 GNUNET_assert (download == event->value.download.dc);
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
189 (unsigned long long) event->value.download.completed,
190 (unsigned long long) event->value.download.size,
191 event->value.download.specifics.progress.depth,
192 (unsigned long long) event->value.download.specifics.
195 case GNUNET_FS_STATUS_PUBLISH_ERROR:
196 FPRINTF (stderr, "Error publishing file: %s\n",
197 event->value.publish.specifics.error.message);
199 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
200 GNUNET_SCHEDULER_shutdown ();
202 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
203 FPRINTF (stderr, "Error downloading file: %s\n",
204 event->value.download.specifics.error.message);
205 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
206 GNUNET_SCHEDULER_shutdown ();
208 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
209 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
211 case GNUNET_FS_STATUS_PUBLISH_START:
212 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
213 GNUNET_assert (NULL == event->value.publish.pctx);
214 GNUNET_assert (FILESIZE == event->value.publish.size);
215 GNUNET_assert (0 == event->value.publish.completed);
216 GNUNET_assert (1 == event->value.publish.anonymity);
218 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
219 GNUNET_assert (publish == event->value.publish.pc);
220 GNUNET_assert (FILESIZE == event->value.publish.size);
221 GNUNET_assert (1 == event->value.publish.anonymity);
222 GNUNET_SCHEDULER_add_now (&stop_fs_task, NULL);
224 case GNUNET_FS_STATUS_DOWNLOAD_START:
225 GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
226 GNUNET_assert (NULL == event->value.download.pctx);
227 GNUNET_assert (NULL != event->value.download.uri);
228 GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
229 GNUNET_assert (FILESIZE == event->value.download.size);
230 GNUNET_assert (0 == event->value.download.completed);
231 GNUNET_assert (1 == event->value.download.anonymity);
233 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
234 GNUNET_assert (download == event->value.download.dc);
235 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
238 printf ("Unexpected event: %d\n", event->status);
247 const struct GNUNET_CONFIGURATION_Handle *cfg,
248 struct GNUNET_TESTING_Peer *peer)
250 const char *binary_name = cls;
251 const char *keywords[] = {
256 struct GNUNET_CONTAINER_MetaData *meta;
257 struct GNUNET_FS_Uri *kuri;
258 struct GNUNET_FS_FileInformation *fi;
260 struct GNUNET_FS_BlockOptions bo;
263 GNUNET_CONFIGURATION_get_value_yesno (cfg,
269 fs = GNUNET_FS_start (cfg, binary_name, &progress_cb, NULL,
270 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
271 GNUNET_assert (NULL != fs);
272 buf = GNUNET_malloc (FILESIZE);
273 for (i = 0; i < FILESIZE; i++)
274 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
275 meta = GNUNET_CONTAINER_meta_data_create ();
276 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
277 bo.content_priority = 42;
278 bo.anonymity_level = anonymity_level;
279 bo.replication_level = 0;
280 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
283 GNUNET_CONFIGURATION_get_value_yesno (cfg,
287 fn1 = GNUNET_DISK_mktemp ("gnunet-download-indexed-test");
288 GNUNET_assert (FILESIZE ==
289 GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
290 GNUNET_DISK_PERM_USER_READ |
291 GNUNET_DISK_PERM_USER_WRITE));
293 fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn1,
294 kuri, meta, GNUNET_YES,
296 indexed = GNUNET_YES;
300 fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
301 FILESIZE, buf, kuri, meta,
303 /* note: buf will be free'd as part of 'fi' now */
306 GNUNET_FS_uri_destroy (kuri);
307 GNUNET_CONTAINER_meta_data_destroy (meta);
308 GNUNET_assert (NULL != fi);
310 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
311 start = GNUNET_TIME_absolute_get ();
313 GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
314 GNUNET_FS_PUBLISH_OPTION_NONE);
315 GNUNET_assert (publish != NULL);
320 main (int argc, char *argv[])
322 const char *binary_name;
323 const char *config_name;
325 binary_name = "test-fs-download";
326 config_name = "test_fs_download_data.conf";
327 if (NULL != strstr (argv[0], "indexed"))
329 binary_name = "test-fs-download-indexed";
330 config_name = "test_fs_download_indexed.conf";
332 if (NULL != strstr (argv[0], "cadet"))
334 binary_name = "test-fs-download-cadet";
335 config_name = "test_fs_download_cadet.conf";
337 if (0 != GNUNET_TESTING_peer_run (binary_name,
339 &run, (void *) binary_name))
349 /* end of test_fs_download.c */