2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2008, 2009, 2011, 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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 GNUNET_SCHEDULER_TaskIdentifier timeout_kill;
70 timeout_kill_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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");
83 timeout_kill = GNUNET_SCHEDULER_NO_TASK;
89 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 GNUNET_FS_publish_stop (publish);
100 stop_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108 abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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);
123 timeout_kill = GNUNET_SCHEDULER_NO_TASK;
128 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
131 switch (event->status)
133 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
136 (unsigned long long) event->value.publish.completed,
137 (unsigned long long) event->value.publish.size,
138 event->value.publish.specifics.progress.depth,
139 (unsigned long long) event->value.publish.specifics.
142 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
144 "Publishing complete, %llu kb/s.\n",
145 (unsigned long long) (FILESIZE * 1000LL /
147 GNUNET_TIME_absolute_get_duration
148 (start).rel_value) / 1024LL));
150 (GNUNET_YES == indexed)
151 ? "Publishing speed (indexing)"
152 : "Publishing speed (insertion)",
153 (unsigned long long) (FILESIZE * 1000LL /
155 GNUNET_TIME_absolute_get_duration
156 (start).rel_value) / 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 * 1000LL /
173 GNUNET_TIME_absolute_get_duration
174 (start).rel_value) / 1024LL));
176 (GNUNET_YES == indexed)
177 ? "Local download speed (indexed)"
178 : "Local download speed (inserted)",
179 (unsigned long long) (FILESIZE * 1000LL /
181 GNUNET_TIME_absolute_get_duration
182 (start).rel_value) / 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_continuation (&abort_publish_task, NULL,
200 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
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);
207 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
208 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
210 case GNUNET_FS_STATUS_PUBLISH_START:
211 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
212 GNUNET_assert (NULL == event->value.publish.pctx);
213 GNUNET_assert (FILESIZE == event->value.publish.size);
214 GNUNET_assert (0 == event->value.publish.completed);
215 GNUNET_assert (1 == event->value.publish.anonymity);
217 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
218 GNUNET_assert (publish == event->value.publish.pc);
219 GNUNET_assert (FILESIZE == event->value.publish.size);
220 GNUNET_assert (1 == event->value.publish.anonymity);
221 GNUNET_SCHEDULER_add_now (&stop_fs_task, NULL);
223 case GNUNET_FS_STATUS_DOWNLOAD_START:
224 GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
225 GNUNET_assert (NULL == event->value.download.pctx);
226 GNUNET_assert (NULL != event->value.download.uri);
227 GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
228 GNUNET_assert (FILESIZE == event->value.download.size);
229 GNUNET_assert (0 == event->value.download.completed);
230 GNUNET_assert (1 == event->value.download.anonymity);
232 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
233 GNUNET_assert (download == event->value.download.dc);
234 GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
235 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
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], "stream"))
334 binary_name = "test-fs-download-stream";
335 config_name = "test_fs_download_stream.conf";
337 if (0 != GNUNET_TESTING_peer_run (binary_name,
339 &run, (void *) binary_name))
349 /* end of test_fs_download.c */