-remove trailing whitespace
[oweals/gnunet.git] / src / fs / test_fs_download_persistence.c
1 /*
2      This file is part of GNUnet.
3      (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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier 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 = GNUNET_SCHEDULER_NO_TASK;
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 = GNUNET_SCHEDULER_NO_TASK;
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_COMPLETED:
166     printf ("Publishing complete, %llu kbps.\n",
167             (unsigned long long) (FILESIZE * 1000000LL /
168                                   (1 +
169                                    GNUNET_TIME_absolute_get_duration
170                                    (start).rel_value_us) / 1024LL));
171     fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
172     start = GNUNET_TIME_absolute_get ();
173     GNUNET_assert (download == NULL);
174     GNUNET_FS_download_start (fs,
175                               event->value.publish.specifics.completed.chk_uri,
176                               NULL, fn, NULL, 0, FILESIZE, 1,
177                               GNUNET_FS_DOWNLOAD_OPTION_NONE, "download", NULL);
178     break;
179   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
180     consider_restart (event->status);
181     printf ("Download complete,  %llu kbps.\n",
182             (unsigned long long) (FILESIZE * 1000000LL /
183                                   (1 +
184                                    GNUNET_TIME_absolute_get_duration
185                                    (start).rel_value_us) / 1024LL));
186     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
187     break;
188   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
189     consider_restart (event->status);
190     GNUNET_assert (download == event->value.download.dc);
191     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192                 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
193                 (unsigned long long) event->value.download.completed,
194                 (unsigned long long) event->value.download.size,
195                 event->value.download.specifics.progress.depth,
196                 (unsigned long long) event->value.download.specifics.
197                 progress.offset);
198     break;
199   case GNUNET_FS_STATUS_PUBLISH_ERROR:
200     FPRINTF (stderr, "Error publishing file: %s\n",
201              event->value.publish.specifics.error.message);
202     GNUNET_break (0);
203     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
204                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
205     break;
206   case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
207     FPRINTF (stderr, "Error downloading file: %s\n",
208              event->value.download.specifics.error.message);
209     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
210     break;
211   case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
212     GNUNET_assert (event->value.publish.pc == publish);
213     publish = NULL;
214     break;
215   case GNUNET_FS_STATUS_PUBLISH_RESUME:
216     GNUNET_assert (NULL == publish);
217     publish = event->value.publish.pc;
218     break;
219   case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
220     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download suspended.\n");
221     GNUNET_assert (event->value.download.dc == download);
222     download = NULL;
223     break;
224   case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
225     GNUNET_assert (NULL == download);
226     download = event->value.download.dc;
227     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download resumed.\n");
228     break;
229   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
230     consider_restart (event->status);
231     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download active.\n");
232     break;
233   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
234     consider_restart (event->status);
235     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download inactive.\n");
236     break;
237   case GNUNET_FS_STATUS_PUBLISH_START:
238     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
239     GNUNET_assert (NULL == event->value.publish.pctx);
240     GNUNET_assert (FILESIZE == event->value.publish.size);
241     GNUNET_assert (0 == event->value.publish.completed);
242     GNUNET_assert (1 == event->value.publish.anonymity);
243     break;
244   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
245     GNUNET_assert (publish == event->value.publish.pc);
246     GNUNET_assert (FILESIZE == event->value.publish.size);
247     GNUNET_assert (1 == event->value.publish.anonymity);
248     GNUNET_FS_stop (fs);
249     fs = NULL;
250     break;
251   case GNUNET_FS_STATUS_DOWNLOAD_START:
252     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download started.\n");
253     consider_restart (event->status);
254     GNUNET_assert (download == NULL);
255     download = event->value.download.dc;
256     GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
257     GNUNET_assert (NULL == event->value.download.pctx);
258     GNUNET_assert (NULL != event->value.download.uri);
259     GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
260     GNUNET_assert (FILESIZE == event->value.download.size);
261     GNUNET_assert (0 == event->value.download.completed);
262     GNUNET_assert (1 == event->value.download.anonymity);
263     break;
264   case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
265     GNUNET_assert (download == event->value.download.dc);
266     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
267                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
268     download = NULL;
269     break;
270   default:
271     printf ("Unexpected event: %d\n", event->status);
272     break;
273   }
274   return NULL;
275 }
276
277
278 static void
279 run (void *cls,
280      const struct GNUNET_CONFIGURATION_Handle *c,
281      struct GNUNET_TESTING_Peer *peer)
282 {
283   const char *keywords[] = {
284     "down_foo",
285     "down_bar",
286   };
287   char *buf;
288   struct GNUNET_CONTAINER_MetaData *meta;
289   struct GNUNET_FS_Uri *kuri;
290   struct GNUNET_FS_FileInformation *fi;
291   size_t i;
292   struct GNUNET_FS_BlockOptions bo;
293
294   cfg = c;
295   fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
296                         GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
297   GNUNET_assert (NULL != fs);
298   buf = GNUNET_malloc (FILESIZE);
299   for (i = 0; i < FILESIZE; i++)
300     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
301   meta = GNUNET_CONTAINER_meta_data_create ();
302   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
303   bo.content_priority = 42;
304   bo.anonymity_level = 1;
305   bo.replication_level = 0;
306   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
307   fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
308                                                     FILESIZE, buf, kuri, meta,
309                                                     GNUNET_NO, &bo);
310   GNUNET_FS_uri_destroy (kuri);
311   GNUNET_CONTAINER_meta_data_destroy (meta);
312   GNUNET_assert (NULL != fi);
313   timeout_kill =
314       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
315   start = GNUNET_TIME_absolute_get ();
316   publish =
317       GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
318                                GNUNET_FS_PUBLISH_OPTION_NONE);
319   GNUNET_assert (publish != NULL);
320 }
321
322
323 int
324 main (int argc, char *argv[])
325 {
326   if (0 != GNUNET_TESTING_peer_run ("test-fs-download-persistence",
327                                     "test_fs_download_data.conf",
328                                     &run, NULL))
329     return 1;
330   return err;
331 }
332
333 /* end of test_fs_download_persistence.c */