2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2008, 2009, 2011 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_arm_service.h"
30 #include "gnunet_fs_service.h"
31 #include "gnunet_testing_lib-new.h"
35 * File-size we use for testing.
37 #define FILESIZE (1024 * 1024 * 2)
40 * How long until we give up on transmitting the message?
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
45 * How long should our test-content live?
47 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
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 GNUNET_SCHEDULER_TaskIdentifier timeout_kill;
66 timeout_kill_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
70 GNUNET_FS_download_stop (download, GNUNET_YES);
73 else if (NULL != publish)
75 GNUNET_FS_publish_stop (publish);
78 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
79 timeout_kill = GNUNET_SCHEDULER_NO_TASK;
85 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
89 GNUNET_FS_publish_stop (publish);
96 stop_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108 if (NULL != download)
110 GNUNET_FS_download_stop (download, GNUNET_YES);
113 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
114 GNUNET_assert (size == FILESIZE);
115 GNUNET_DISK_directory_remove (fn);
118 GNUNET_SCHEDULER_cancel (timeout_kill);
119 timeout_kill = GNUNET_SCHEDULER_NO_TASK;
124 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
127 switch (event->status)
129 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
132 (unsigned long long) event->value.publish.completed,
133 (unsigned long long) event->value.publish.size,
134 event->value.publish.specifics.progress.depth,
135 (unsigned long long) event->value.publish.specifics.
138 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
140 "Publishing complete, %llu kb/s.\n",
141 (unsigned long long) (FILESIZE * 1000LL /
143 GNUNET_TIME_absolute_get_duration
144 (start).rel_value) / 1024LL));
145 GAUGER ("FS", "Publishing speed (insertion)",
146 (unsigned long long) (FILESIZE * 1000LL /
148 GNUNET_TIME_absolute_get_duration
149 (start).rel_value) / 1024LL), "kb/s");
150 fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
151 start = GNUNET_TIME_absolute_get ();
153 GNUNET_FS_download_start (fs,
154 event->value.publish.specifics.
155 completed.chk_uri, NULL, fn, NULL, 0,
156 FILESIZE, 1, GNUNET_FS_DOWNLOAD_OPTION_NONE,
158 GNUNET_assert (download != NULL);
160 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
162 "Download complete, %llu kb/s.\n",
163 (unsigned long long) (FILESIZE * 1000LL /
165 GNUNET_TIME_absolute_get_duration
166 (start).rel_value) / 1024LL));
167 GAUGER ("FS", "Local download speed (inserted)",
168 (unsigned long long) (FILESIZE * 1000LL /
170 GNUNET_TIME_absolute_get_duration
171 (start).rel_value) / 1024LL), "kb/s");
172 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
174 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
175 GNUNET_assert (download == event->value.download.dc);
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
178 (unsigned long long) event->value.download.completed,
179 (unsigned long long) event->value.download.size,
180 event->value.download.specifics.progress.depth,
181 (unsigned long long) event->value.download.specifics.
184 case GNUNET_FS_STATUS_PUBLISH_ERROR:
185 FPRINTF (stderr, "Error publishing file: %s\n",
186 event->value.publish.specifics.error.message);
188 GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
189 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
191 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
192 FPRINTF (stderr, "Error downloading file: %s\n",
193 event->value.download.specifics.error.message);
194 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
196 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
197 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
199 case GNUNET_FS_STATUS_PUBLISH_START:
200 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
201 GNUNET_assert (NULL == event->value.publish.pctx);
202 GNUNET_assert (FILESIZE == event->value.publish.size);
203 GNUNET_assert (0 == event->value.publish.completed);
204 GNUNET_assert (1 == event->value.publish.anonymity);
206 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
207 GNUNET_assert (publish == event->value.publish.pc);
208 GNUNET_assert (FILESIZE == event->value.publish.size);
209 GNUNET_assert (1 == event->value.publish.anonymity);
210 GNUNET_SCHEDULER_add_now (&stop_fs_task, NULL);
212 case GNUNET_FS_STATUS_DOWNLOAD_START:
213 GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
214 GNUNET_assert (NULL == event->value.download.pctx);
215 GNUNET_assert (NULL != event->value.download.uri);
216 GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
217 GNUNET_assert (FILESIZE == event->value.download.size);
218 GNUNET_assert (0 == event->value.download.completed);
219 GNUNET_assert (1 == event->value.download.anonymity);
221 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
222 GNUNET_assert (download == event->value.download.dc);
223 GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
224 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
227 printf ("Unexpected event: %d\n", event->status);
236 const struct GNUNET_CONFIGURATION_Handle *cfg)
238 const char *keywords[] = {
243 struct GNUNET_CONTAINER_MetaData *meta;
244 struct GNUNET_FS_Uri *kuri;
245 struct GNUNET_FS_FileInformation *fi;
247 struct GNUNET_FS_BlockOptions bo;
249 fs = GNUNET_FS_start (cfg, "test-fs-download", &progress_cb, NULL,
250 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
251 GNUNET_assert (NULL != fs);
252 buf = GNUNET_malloc (FILESIZE);
253 for (i = 0; i < FILESIZE; i++)
254 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
255 meta = GNUNET_CONTAINER_meta_data_create ();
256 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
257 bo.content_priority = 42;
258 bo.anonymity_level = 1;
259 bo.replication_level = 0;
260 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
261 fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
262 FILESIZE, buf, kuri, meta,
264 GNUNET_FS_uri_destroy (kuri);
265 GNUNET_CONTAINER_meta_data_destroy (meta);
266 GNUNET_assert (NULL != fi);
268 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
269 start = GNUNET_TIME_absolute_get ();
271 GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
272 GNUNET_FS_PUBLISH_OPTION_NONE);
273 GNUNET_assert (publish != NULL);
278 main (int argc, char *argv[])
280 if (0 != GNUNET_TESTING_peer_run ("test-fs-download",
281 "test_fs_download_data.conf",
287 /* end of test_fs_download.c */