2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2008, 2009 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 2, 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 download
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_fs_service.h"
32 #define VERBOSE GNUNET_NO
34 #define START_ARM GNUNET_YES
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, 15)
44 * How long should our test-content live?
46 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
50 struct GNUNET_CONFIGURATION_Handle *cfg;
51 struct GNUNET_PeerIdentity id;
57 static struct PeerContext p1;
60 static struct GNUNET_SCHEDULER_Handle *sched;
62 static struct GNUNET_FS_Handle *fs;
64 static struct GNUNET_FS_DownloadContext *download;
66 static struct GNUNET_FS_PublishContext *publish;
72 abort_download_task (void *cls,
73 const struct GNUNET_SCHEDULER_TaskContext *tc)
75 GNUNET_FS_download_stop (download, GNUNET_YES);
81 abort_publish_task (void *cls,
82 const struct GNUNET_SCHEDULER_TaskContext *tc)
84 GNUNET_FS_publish_stop (publish);
90 progress_cb (void *cls,
91 const struct GNUNET_FS_ProgressInfo *event)
94 switch (event->status)
96 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
98 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
99 (unsigned long long) event->value.publish.completed,
100 (unsigned long long) event->value.publish.size,
101 event->value.publish.specifics.progress.depth,
102 (unsigned long long) event->value.publish.specifics.progress.offset);
105 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
107 printf ("Publish complete.\n");
109 GNUNET_SCHEDULER_add_continuation (sched,
113 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
114 fn = GNUNET_DISK_mktemp ("gnunet-download-test-dstXXXXXX");
115 download = GNUNET_FS_download_start (fs,
116 event->value.publish.specifics.completed.chk_uri,
122 GNUNET_FS_DOWNLOAD_OPTION_NONE,
124 GNUNET_assert (download != NULL);
126 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
128 printf ("Download complete.\n");
130 GNUNET_SCHEDULER_add_continuation (sched,
132 &abort_download_task,
134 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
136 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
138 printf ("Download is progressing (%llu/%llu at level %u off %llu)...\n",
139 (unsigned long long) event->value.download.completed,
140 (unsigned long long) event->value.download.size,
141 event->value.download.specifics.progress.depth,
142 (unsigned long long) event->value.download.specifics.progress.offset);
145 case GNUNET_FS_STATUS_PUBLISH_ERROR:
147 "Error publishing file: %s\n",
148 event->value.publish.specifics.error.message);
150 GNUNET_SCHEDULER_add_continuation (sched,
154 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
156 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
158 "Error downloading file: %s\n",
159 event->value.download.specifics.error.message);
160 GNUNET_SCHEDULER_add_continuation (sched,
162 &abort_download_task,
164 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
166 case GNUNET_FS_STATUS_PUBLISH_START:
167 /* FIXME: add checks here... */
169 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
170 /* FIXME: add checks here... */
172 case GNUNET_FS_STATUS_DOWNLOAD_START:
173 /* FIXME: add checks here... */
175 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
176 /* FIXME: add checks here... */
181 printf ("Unexpected event: %d\n",
190 setup_peer (struct PeerContext *p, const char *cfgname)
192 p->cfg = GNUNET_CONFIGURATION_create ();
194 p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
195 "gnunet-service-arm",
199 "-c", cfgname, NULL);
200 sleep (1); /* allow ARM to start */
202 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
203 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
208 stop_arm (struct PeerContext *p)
211 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
212 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
213 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
214 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "ARM process %u stopped\n", p->arm_pid);
218 GNUNET_CONFIGURATION_destroy (p->cfg);
224 struct GNUNET_SCHEDULER_Handle *s,
227 const struct GNUNET_CONFIGURATION_Handle *cfg)
229 const char *keywords[] = {
234 struct GNUNET_CONTAINER_MetaData *meta;
235 struct GNUNET_FS_Uri *kuri;
236 struct GNUNET_FS_FileInformation *fi;
240 setup_peer (&p1, "test_fs_download_data.conf");
241 fs = GNUNET_FS_start (sched,
246 GNUNET_FS_FLAGS_NONE,
247 GNUNET_FS_OPTIONS_END);
248 GNUNET_assert (NULL != fs);
249 buf = GNUNET_malloc (FILESIZE);
250 for (i = 0; i < FILESIZE; i++)
251 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
252 meta = GNUNET_CONTAINER_meta_data_create ();
253 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
254 fi = GNUNET_FS_file_information_create_from_data ("file-to-publish",
262 GNUNET_TIME_relative_to_absolute (LIFETIME));
263 GNUNET_FS_uri_destroy (kuri);
264 GNUNET_CONTAINER_meta_data_destroy (meta);
265 GNUNET_assert (NULL != fi);
266 publish = GNUNET_FS_publish_start (fs,
270 GNUNET_FS_PUBLISH_OPTION_NONE);
271 GNUNET_assert (publish != NULL);
276 main (int argc, char *argv[])
278 char *const argvx[] = {
281 "test_fs_download_data.conf",
287 struct GNUNET_GETOPT_CommandLineOption options[] = {
288 GNUNET_GETOPT_OPTION_END
291 GNUNET_log_setup ("test_fs_download",
298 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
299 argvx, "test-fs-download",
300 "nohelp", options, &run, NULL);
305 /* end of test_fs_download.c */