fix #3869: outdated FSF address
[oweals/gnunet.git] / src / fs / test_fs_download_persistence.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file fs/test_fs_download_persistence.c
23  * @brief simple testcase for persistence of simple download operation
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_fs_service.h"
30
31 /**
32  * File-size we use for testing.
33  */
34 #define FILESIZE (1024 * 1024 * 2)
35
36 /**
37  * How long until we give up on transmitting the message?
38  */
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
40
41 /**
42  * How long should our test-content live?
43  */
44 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
45
46
47 static struct GNUNET_TIME_Absolute start;
48
49 static const struct GNUNET_CONFIGURATION_Handle *cfg;
50
51 static struct GNUNET_FS_Handle *fs;
52
53 static struct GNUNET_FS_DownloadContext *download;
54
55 static struct GNUNET_FS_PublishContext *publish;
56
57 static struct GNUNET_SCHEDULER_Task * timeout_kill;
58
59 static char *fn;
60
61 static int err;
62
63
64 static void
65 timeout_kill_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
66 {
67   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
68   if (download != NULL)
69   {
70     GNUNET_FS_download_stop (download, GNUNET_YES);
71     download = NULL;
72   }
73   else if (publish != NULL)
74   {
75     GNUNET_FS_publish_stop (publish);
76     publish = NULL;
77   }
78   timeout_kill = NULL;
79   err = 1;
80 }
81
82
83 static void
84 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 {
86   if (publish != NULL)
87   {
88     GNUNET_FS_publish_stop (publish);
89     publish = NULL;
90   }
91 }
92
93
94 static void
95 abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 {
97   uint64_t size;
98
99   if (download != NULL)
100   {
101     GNUNET_FS_download_stop (download, GNUNET_YES);
102     download = NULL;
103   }
104   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
105   GNUNET_assert (size == FILESIZE);
106   GNUNET_DISK_directory_remove (fn);
107   GNUNET_free (fn);
108   fn = NULL;
109   GNUNET_SCHEDULER_cancel (timeout_kill);
110   timeout_kill = NULL;
111 }
112
113
114 static void *
115 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
116
117
118 static void
119 restart_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 {
121   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Restarting FS.\n");
122   GNUNET_FS_stop (fs);
123   fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
124                         GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
125 }
126
127
128 /**
129  * Consider scheduling the restart-task.
130  * Only runs the restart task once per event
131  * category.
132  *
133  * @param ev type of the event to consider
134  */
135 static void
136 consider_restart (int ev)
137 {
138   static int prev[32];
139   static int off;
140   int i;
141
142   for (i = 0; i < off; i++)
143     if (prev[i] == ev)
144       return;
145   prev[off++] = ev;
146   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
147                                       &restart_fs_task, NULL);
148 }
149
150
151 static void *
152 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
153 {
154   switch (event->status)
155   {
156   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
157     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158                 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
159                 (unsigned long long) event->value.publish.completed,
160                 (unsigned long long) event->value.publish.size,
161                 event->value.publish.specifics.progress.depth,
162                 (unsigned long long) event->value.publish.specifics.
163                 progress.offset);
164     break;
165   case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
166     break;
167   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
168     printf ("Publishing complete, %llu kbps.\n",
169             (unsigned long long) (FILESIZE * 1000000LL /
170                                   (1 +
171                                    GNUNET_TIME_absolute_get_duration
172                                    (start).rel_value_us) / 1024LL));
173     fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
174     start = GNUNET_TIME_absolute_get ();
175     GNUNET_assert (download == NULL);
176     GNUNET_FS_download_start (fs,
177                               event->value.publish.specifics.completed.chk_uri,
178                               NULL, fn, NULL, 0, FILESIZE, 1,
179                               GNUNET_FS_DOWNLOAD_OPTION_NONE, "download", NULL);
180     break;
181   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
182     consider_restart (event->status);
183     printf ("Download complete,  %llu kbps.\n",
184             (unsigned long long) (FILESIZE * 1000000LL /
185                                   (1 +
186                                    GNUNET_TIME_absolute_get_duration
187                                    (start).rel_value_us) / 1024LL));
188     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
189     break;
190   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
191     consider_restart (event->status);
192     GNUNET_assert (download == event->value.download.dc);
193     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194                 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
195                 (unsigned long long) event->value.download.completed,
196                 (unsigned long long) event->value.download.size,
197                 event->value.download.specifics.progress.depth,
198                 (unsigned long long) event->value.download.specifics.
199                 progress.offset);
200     break;
201   case GNUNET_FS_STATUS_PUBLISH_ERROR:
202     FPRINTF (stderr, "Error publishing file: %s\n",
203              event->value.publish.specifics.error.message);
204     GNUNET_break (0);
205     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
206                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
207     break;
208   case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
209     FPRINTF (stderr, "Error downloading file: %s\n",
210              event->value.download.specifics.error.message);
211     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
212     break;
213   case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
214     GNUNET_assert (event->value.publish.pc == publish);
215     publish = NULL;
216     break;
217   case GNUNET_FS_STATUS_PUBLISH_RESUME:
218     GNUNET_assert (NULL == publish);
219     publish = event->value.publish.pc;
220     break;
221   case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
222     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download suspended.\n");
223     GNUNET_assert (event->value.download.dc == download);
224     download = NULL;
225     break;
226   case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
227     GNUNET_assert (NULL == download);
228     download = event->value.download.dc;
229     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download resumed.\n");
230     break;
231   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
232     consider_restart (event->status);
233     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download active.\n");
234     break;
235   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
236     consider_restart (event->status);
237     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download inactive.\n");
238     break;
239   case GNUNET_FS_STATUS_PUBLISH_START:
240     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
241     GNUNET_assert (NULL == event->value.publish.pctx);
242     GNUNET_assert (FILESIZE == event->value.publish.size);
243     GNUNET_assert (0 == event->value.publish.completed);
244     GNUNET_assert (1 == event->value.publish.anonymity);
245     break;
246   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
247     GNUNET_assert (publish == event->value.publish.pc);
248     GNUNET_assert (FILESIZE == event->value.publish.size);
249     GNUNET_assert (1 == event->value.publish.anonymity);
250     GNUNET_FS_stop (fs);
251     fs = NULL;
252     break;
253   case GNUNET_FS_STATUS_DOWNLOAD_START:
254     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download started.\n");
255     consider_restart (event->status);
256     GNUNET_assert (download == NULL);
257     download = event->value.download.dc;
258     GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
259     GNUNET_assert (NULL == event->value.download.pctx);
260     GNUNET_assert (NULL != event->value.download.uri);
261     GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
262     GNUNET_assert (FILESIZE == event->value.download.size);
263     GNUNET_assert (0 == event->value.download.completed);
264     GNUNET_assert (1 == event->value.download.anonymity);
265     break;
266   case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
267     GNUNET_assert (download == event->value.download.dc);
268     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
269                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
270     download = NULL;
271     break;
272   default:
273     printf ("Unexpected event: %d\n", event->status);
274     break;
275   }
276   return NULL;
277 }
278
279
280 static void
281 run (void *cls,
282      const struct GNUNET_CONFIGURATION_Handle *c,
283      struct GNUNET_TESTING_Peer *peer)
284 {
285   const char *keywords[] = {
286     "down_foo",
287     "down_bar",
288   };
289   char *buf;
290   struct GNUNET_CONTAINER_MetaData *meta;
291   struct GNUNET_FS_Uri *kuri;
292   struct GNUNET_FS_FileInformation *fi;
293   size_t i;
294   struct GNUNET_FS_BlockOptions bo;
295
296   cfg = c;
297   fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
298                         GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
299   GNUNET_assert (NULL != fs);
300   buf = GNUNET_malloc (FILESIZE);
301   for (i = 0; i < FILESIZE; i++)
302     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
303   meta = GNUNET_CONTAINER_meta_data_create ();
304   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
305   bo.content_priority = 42;
306   bo.anonymity_level = 1;
307   bo.replication_level = 0;
308   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
309   fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
310                                                     FILESIZE, buf, kuri, meta,
311                                                     GNUNET_NO, &bo);
312   GNUNET_FS_uri_destroy (kuri);
313   GNUNET_CONTAINER_meta_data_destroy (meta);
314   GNUNET_assert (NULL != fi);
315   timeout_kill =
316       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
317   start = GNUNET_TIME_absolute_get ();
318   publish =
319       GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
320                                GNUNET_FS_PUBLISH_OPTION_NONE);
321   GNUNET_assert (publish != NULL);
322 }
323
324
325 int
326 main (int argc, char *argv[])
327 {
328   if (0 != GNUNET_TESTING_peer_run ("test-fs-download-persistence",
329                                     "test_fs_download_data.conf",
330                                     &run, NULL))
331     return 1;
332   return err;
333 }
334
335 /* end of test_fs_download_persistence.c */