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 DEBUG_VERBOSE 42
35 #define START_ARM GNUNET_YES
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, 15)
45 * How long should our test-content live?
47 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
51 struct GNUNET_CONFIGURATION_Handle *cfg;
52 struct GNUNET_PeerIdentity id;
58 static struct PeerContext p1;
61 static struct GNUNET_SCHEDULER_Handle *sched;
63 static struct GNUNET_FS_Handle *fs;
65 static struct GNUNET_FS_DownloadContext *download;
67 static struct GNUNET_FS_PublishContext *publish;
73 abort_download_task (void *cls,
74 const struct GNUNET_SCHEDULER_TaskContext *tc)
76 GNUNET_FS_download_stop (download, GNUNET_YES);
82 abort_publish_task (void *cls,
83 const struct GNUNET_SCHEDULER_TaskContext *tc)
85 GNUNET_FS_publish_stop (publish);
91 progress_cb (void *cls,
92 const struct GNUNET_FS_ProgressInfo *event)
95 switch (event->status)
97 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
99 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
100 (unsigned long long) event->value.publish.completed,
101 (unsigned long long) event->value.publish.size,
102 event->value.publish.specifics.progress.depth,
103 (unsigned long long) event->value.publish.specifics.progress.offset);
106 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
108 printf ("Publish complete.\n");
110 GNUNET_SCHEDULER_add_continuation (sched,
114 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
115 fn = GNUNET_DISK_mktemp ("gnunet-download-test-dstXXXXXX");
116 download = GNUNET_FS_download_start (fs,
117 event->value.publish.specifics.completed.chk_uri,
123 GNUNET_FS_DOWNLOAD_OPTION_NONE,
125 GNUNET_assert (download != NULL);
127 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
129 printf ("Download complete.\n");
131 GNUNET_SCHEDULER_add_continuation (sched,
133 &abort_download_task,
135 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
137 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
139 printf ("Download is progressing (%llu/%llu at level %u off %llu)...\n",
140 (unsigned long long) event->value.download.completed,
141 (unsigned long long) event->value.download.size,
142 event->value.download.specifics.progress.depth,
143 (unsigned long long) event->value.download.specifics.progress.offset);
146 case GNUNET_FS_STATUS_PUBLISH_ERROR:
148 "Error publishing file: %s\n",
149 event->value.publish.specifics.error.message);
151 GNUNET_SCHEDULER_add_continuation (sched,
155 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
157 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
159 "Error downloading file: %s\n",
160 event->value.download.specifics.error.message);
161 GNUNET_SCHEDULER_add_continuation (sched,
163 &abort_download_task,
165 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
167 case GNUNET_FS_STATUS_PUBLISH_START:
168 /* FIXME: add checks here... */
170 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
171 /* FIXME: add checks here... */
173 case GNUNET_FS_STATUS_DOWNLOAD_START:
174 /* FIXME: add checks here... */
176 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
177 /* FIXME: add checks here... */
182 printf ("Unexpected event: %d\n",
191 setup_peer (struct PeerContext *p, const char *cfgname)
193 p->cfg = GNUNET_CONFIGURATION_create ();
195 p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
196 "gnunet-service-arm",
200 "-c", cfgname, NULL);
201 sleep (1); /* allow ARM to start */
203 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
204 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
209 stop_arm (struct PeerContext *p)
212 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
213 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
214 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
215 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "ARM process %u stopped\n", p->arm_pid);
219 GNUNET_CONFIGURATION_destroy (p->cfg);
225 struct GNUNET_SCHEDULER_Handle *s,
228 const struct GNUNET_CONFIGURATION_Handle *cfg)
230 const char *keywords[] = {
235 struct GNUNET_CONTAINER_MetaData *meta;
236 struct GNUNET_FS_Uri *kuri;
237 struct GNUNET_FS_FileInformation *fi;
241 setup_peer (&p1, "test_fs_download_data.conf");
242 fs = GNUNET_FS_start (sched,
247 GNUNET_FS_FLAGS_NONE,
248 GNUNET_FS_OPTIONS_END);
249 GNUNET_assert (NULL != fs);
250 buf = GNUNET_malloc (FILESIZE);
251 for (i = 0; i < FILESIZE; i++)
252 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
253 meta = GNUNET_CONTAINER_meta_data_create ();
254 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
255 fi = GNUNET_FS_file_information_create_from_data ("file-to-publish",
263 GNUNET_TIME_relative_to_absolute (LIFETIME));
264 GNUNET_FS_uri_destroy (kuri);
265 GNUNET_CONTAINER_meta_data_destroy (meta);
266 GNUNET_assert (NULL != fi);
267 publish = GNUNET_FS_publish_start (fs,
271 GNUNET_FS_PUBLISH_OPTION_NONE);
272 GNUNET_assert (publish != NULL);
277 main (int argc, char *argv[])
279 char *const argvx[] = {
282 "test_fs_download_data.conf",
288 struct GNUNET_GETOPT_CommandLineOption options[] = {
289 GNUNET_GETOPT_OPTION_END
292 GNUNET_log_setup ("test_fs_download",
299 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
300 argvx, "test-fs-download",
301 "nohelp", options, &run, NULL);
306 /* end of test_fs_download.c */