2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2008, 2009, 2010 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_persistence.c
23 * @brief simple testcase for persistence of simple download operation
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
37 * File-size we use for testing.
39 #define FILESIZE (1024 * 1024 * 2)
42 * How long until we give up on transmitting the message?
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
47 * How long should our test-content live?
49 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
53 struct GNUNET_CONFIGURATION_Handle *cfg;
55 struct GNUNET_OS_Process *arm_proc;
59 static struct PeerContext p1;
61 static struct GNUNET_TIME_Absolute start;
63 static const struct GNUNET_CONFIGURATION_Handle *cfg;
65 static struct GNUNET_FS_Handle *fs;
67 static struct GNUNET_FS_DownloadContext *download;
69 static struct GNUNET_FS_PublishContext *publish;
71 static GNUNET_SCHEDULER_TaskIdentifier timeout_kill;
78 timeout_kill_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
83 GNUNET_FS_download_stop (download, GNUNET_YES);
86 else if (publish != NULL)
88 GNUNET_FS_publish_stop (publish);
91 timeout_kill = GNUNET_SCHEDULER_NO_TASK;
96 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
100 GNUNET_FS_publish_stop (publish);
107 abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 if (download != NULL)
113 GNUNET_FS_download_stop (download, GNUNET_YES);
116 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES));
117 GNUNET_assert (size == FILESIZE);
118 GNUNET_DISK_directory_remove (fn);
121 GNUNET_SCHEDULER_cancel (timeout_kill);
122 timeout_kill = GNUNET_SCHEDULER_NO_TASK;
127 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
131 restart_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Restarting FS.\n");
135 fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
136 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
141 * Consider scheduling the restart-task.
142 * Only runs the restart task once per event
145 * @param ev type of the event to consider
148 consider_restart (int ev)
154 for (i = 0; i < off; i++)
158 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
159 &restart_fs_task, NULL);
164 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
166 switch (event->status)
168 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
170 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
171 (unsigned long long) event->value.publish.completed,
172 (unsigned long long) event->value.publish.size,
173 event->value.publish.specifics.progress.depth,
174 (unsigned long long) event->value.publish.specifics.
178 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
179 printf ("Publishing complete, %llu kbps.\n",
180 (unsigned long long) (FILESIZE * 1000LL /
182 GNUNET_TIME_absolute_get_duration
183 (start).rel_value) / 1024LL));
184 fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
185 start = GNUNET_TIME_absolute_get ();
186 GNUNET_assert (download == NULL);
187 GNUNET_FS_download_start (fs,
188 event->value.publish.specifics.completed.chk_uri,
189 NULL, fn, NULL, 0, FILESIZE, 1,
190 GNUNET_FS_DOWNLOAD_OPTION_NONE, "download", NULL);
192 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
193 consider_restart (event->status);
194 printf ("Download complete, %llu kbps.\n",
195 (unsigned long long) (FILESIZE * 1000LL /
197 GNUNET_TIME_absolute_get_duration
198 (start).rel_value) / 1024LL));
199 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
201 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
202 consider_restart (event->status);
203 GNUNET_assert (download == event->value.download.dc);
205 printf ("Download is progressing (%llu/%llu at level %u off %llu)...\n",
206 (unsigned long long) event->value.download.completed,
207 (unsigned long long) event->value.download.size,
208 event->value.download.specifics.progress.depth,
209 (unsigned long long) event->value.download.specifics.
213 case GNUNET_FS_STATUS_PUBLISH_ERROR:
214 fprintf (stderr, "Error publishing file: %s\n",
215 event->value.publish.specifics.error.message);
217 GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
218 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
220 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
221 fprintf (stderr, "Error downloading file: %s\n",
222 event->value.download.specifics.error.message);
223 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
225 case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
226 GNUNET_assert (event->value.publish.pc == publish);
229 case GNUNET_FS_STATUS_PUBLISH_RESUME:
230 GNUNET_assert (NULL == publish);
231 publish = event->value.publish.pc;
233 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download suspended.\n");
235 GNUNET_assert (event->value.download.dc == download);
238 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
239 GNUNET_assert (NULL == download);
240 download = event->value.download.dc;
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download resumed.\n");
243 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
244 consider_restart (event->status);
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download active.\n");
247 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
248 consider_restart (event->status);
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download inactive.\n");
251 case GNUNET_FS_STATUS_PUBLISH_START:
252 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
253 GNUNET_assert (NULL == event->value.publish.pctx);
254 GNUNET_assert (FILESIZE == event->value.publish.size);
255 GNUNET_assert (0 == event->value.publish.completed);
256 GNUNET_assert (1 == event->value.publish.anonymity);
258 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
259 GNUNET_assert (publish == event->value.publish.pc);
260 GNUNET_assert (FILESIZE == event->value.publish.size);
261 GNUNET_assert (1 == event->value.publish.anonymity);
265 case GNUNET_FS_STATUS_DOWNLOAD_START:
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download started.\n");
267 consider_restart (event->status);
268 GNUNET_assert (download == NULL);
269 download = event->value.download.dc;
270 GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
271 GNUNET_assert (NULL == event->value.download.pctx);
272 GNUNET_assert (NULL != event->value.download.uri);
273 GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
274 GNUNET_assert (FILESIZE == event->value.download.size);
275 GNUNET_assert (0 == event->value.download.completed);
276 GNUNET_assert (1 == event->value.download.anonymity);
278 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
279 GNUNET_assert (download == event->value.download.dc);
280 GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
281 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
285 printf ("Unexpected event: %d\n", event->status);
293 setup_peer (struct PeerContext *p, const char *cfgname)
295 p->cfg = GNUNET_CONFIGURATION_create ();
298 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
299 "gnunet-service-arm",
303 "-c", cfgname, NULL);
305 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
310 stop_arm (struct PeerContext *p)
313 if (NULL != p->arm_proc)
315 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
316 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
317 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
318 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
320 GNUNET_OS_process_get_pid (p->arm_proc));
321 GNUNET_OS_process_close (p->arm_proc);
325 GNUNET_CONFIGURATION_destroy (p->cfg);
330 run (void *cls, char *const *args, const char *cfgfile,
331 const struct GNUNET_CONFIGURATION_Handle *c)
333 const char *keywords[] = {
338 struct GNUNET_CONTAINER_MetaData *meta;
339 struct GNUNET_FS_Uri *kuri;
340 struct GNUNET_FS_FileInformation *fi;
342 struct GNUNET_FS_BlockOptions bo;
345 setup_peer (&p1, "test_fs_download_data.conf");
346 fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
347 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
348 GNUNET_assert (NULL != fs);
349 buf = GNUNET_malloc (FILESIZE);
350 for (i = 0; i < FILESIZE; i++)
351 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
352 meta = GNUNET_CONTAINER_meta_data_create ();
353 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
354 bo.content_priority = 42;
355 bo.anonymity_level = 1;
356 bo.replication_level = 0;
357 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
358 fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
359 FILESIZE, buf, kuri, meta,
361 GNUNET_FS_uri_destroy (kuri);
362 GNUNET_CONTAINER_meta_data_destroy (meta);
363 GNUNET_assert (NULL != fi);
365 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
366 start = GNUNET_TIME_absolute_get ();
368 GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
369 GNUNET_FS_PUBLISH_OPTION_NONE);
370 GNUNET_assert (publish != NULL);
375 main (int argc, char *argv[])
377 char *const argvx[] = {
378 "test-fs-download-persistence",
380 "test_fs_download_data.conf",
386 struct GNUNET_GETOPT_CommandLineOption options[] = {
387 GNUNET_GETOPT_OPTION_END
389 GNUNET_log_setup ("test_fs_download_persistence",
396 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-download/");
397 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
398 "test-fs-download-persistence", "nohelp", options, &run,
401 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-download/");
405 /* end of test_fs_download_persistence.c */