-fixing #2578
[oweals/gnunet.git] / src / fs / test_fs_download_indexed.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2008, 2009, 2011 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_indexed.c
23  * @brief simple testcase for downloading of indexed file
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_fs_service.h"
29 #include "gnunet_testing_lib-new.h"
30 #include <gauger.h>
31
32
33 /**
34  * File-size we use for testing.
35  */
36 #define FILESIZE (1024 * 1024 * 2)
37
38 /**
39  * How long until we give up on transmitting the message?
40  */
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
42
43 /**
44  * How long should our test-content live?
45  */
46 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47
48
49 static struct GNUNET_TIME_Absolute start;
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 char *fn1;
62
63 static int err;
64
65
66 static void
67 timeout_kill_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
68 {
69   fprintf (stderr, "Download not fast enough, timeout!\n");
70   if (download != NULL)
71   {
72     GNUNET_FS_download_stop (download, GNUNET_YES);
73     download = NULL;
74   }
75   else if (publish != NULL)
76   {
77     GNUNET_FS_publish_stop (publish);
78     publish = NULL;
79   }
80   timeout_kill = GNUNET_SCHEDULER_NO_TASK;
81   err = 1;
82 }
83
84
85 static void
86 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
87 {
88   if (publish != NULL)
89   {
90     GNUNET_FS_publish_stop (publish);
91     publish = NULL;
92   }
93 }
94
95
96 static void
97 stop_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98 {
99   GNUNET_FS_stop (fs);
100   fs = NULL;
101 }
102
103
104 static void
105 abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
106 {
107   uint64_t size;
108
109   if (download != NULL)
110   {
111     GNUNET_FS_download_stop (download, GNUNET_YES);
112     download = NULL;
113   }
114   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
115   GNUNET_assert (size == FILESIZE);
116   GNUNET_DISK_directory_remove (fn);
117   GNUNET_free (fn);
118   fn = NULL;
119   GNUNET_SCHEDULER_cancel (timeout_kill);
120   timeout_kill = GNUNET_SCHEDULER_NO_TASK;
121 }
122
123
124 static void *
125 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
126 {
127
128   switch (event->status)
129   {
130   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
131     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132                 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
133                 (unsigned long long) event->value.publish.completed,
134                 (unsigned long long) event->value.publish.size,
135                 event->value.publish.specifics.progress.depth,
136                 (unsigned long long) event->value.publish.specifics.
137                 progress.offset);
138     break;
139   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
140     printf ("Publishing complete, %llu kbps.\n",
141             (unsigned long long) (FILESIZE * 1000LL /
142                                   (1 +
143                                    GNUNET_TIME_absolute_get_duration
144                                    (start).rel_value) / 1024LL));
145     GAUGER ("FS", "Publishing speed (indexing)",
146             (unsigned long long) (FILESIZE * 1000LL /
147                                   (1 +
148                                    GNUNET_TIME_absolute_get_duration
149                                    (start).rel_value) / 1024LL), "kb/s");
150     fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
151     start = GNUNET_TIME_absolute_get ();
152     download =
153         GNUNET_FS_download_start (fs,
154                                   event->value.publish.specifics.
155                                   completed.chk_uri, NULL, fn, NULL, 0,
156                                   FILESIZE, 1, GNUNET_FS_DOWNLOAD_OPTION_NONE,
157                                   "download", NULL);
158     GNUNET_assert (download != NULL);
159     break;
160   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
161     printf ("Download complete,  %llu kbps.\n",
162             (unsigned long long) (FILESIZE * 1000LL /
163                                   (1 +
164                                    GNUNET_TIME_absolute_get_duration
165                                    (start).rel_value) / 1024LL));
166     GAUGER ("FS", "Local download speed (indexed)",
167             (unsigned long long) (FILESIZE * 1000LL /
168                                   (1 +
169                                    GNUNET_TIME_absolute_get_duration
170                                    (start).rel_value) / 1024LL), "kb/s");
171     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
172     break;
173   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
174     GNUNET_assert (download == event->value.download.dc);
175     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176                 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
177                 (unsigned long long) event->value.download.completed,
178                 (unsigned long long) event->value.download.size,
179                 event->value.download.specifics.progress.depth,
180                 (unsigned long long) event->value.download.specifics.
181                 progress.offset);
182     break;
183   case GNUNET_FS_STATUS_PUBLISH_ERROR:
184     FPRINTF (stderr, "Error publishing file: %s\n",
185              event->value.publish.specifics.error.message);
186     GNUNET_break (0);
187     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
188                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
189     break;
190   case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
191     FPRINTF (stderr, "Error downloading file: %s\n",
192              event->value.download.specifics.error.message);
193     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
194     break;
195   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
196   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
197     break;
198   case GNUNET_FS_STATUS_PUBLISH_START:
199     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
200     GNUNET_assert (NULL == event->value.publish.pctx);
201     GNUNET_assert (FILESIZE == event->value.publish.size);
202     GNUNET_assert (0 == event->value.publish.completed);
203     GNUNET_assert (1 == event->value.publish.anonymity);
204     break;
205   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
206     GNUNET_assert (publish == event->value.publish.pc);
207     GNUNET_assert (FILESIZE == event->value.publish.size);
208     GNUNET_assert (1 == event->value.publish.anonymity);
209     GNUNET_SCHEDULER_add_now (&stop_fs_task, NULL);
210     break;
211   case GNUNET_FS_STATUS_DOWNLOAD_START:
212     GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
213     GNUNET_assert (NULL == event->value.download.pctx);
214     GNUNET_assert (NULL != event->value.download.uri);
215     GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
216     GNUNET_assert (FILESIZE == event->value.download.size);
217     GNUNET_assert (0 == event->value.download.completed);
218     GNUNET_assert (1 == event->value.download.anonymity);
219     break;
220   case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
221     GNUNET_assert (download == event->value.download.dc);
222     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
223                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
224     break;
225   default:
226     printf ("Unexpected event: %d\n", event->status);
227     break;
228   }
229   return NULL;
230 }
231
232
233 static void
234 run (void *cls,
235      const struct GNUNET_CONFIGURATION_Handle *cfg,
236      struct GNUNET_TESTING_Peer *peer)
237 {
238   const char *keywords[] = {
239     "down_foo",
240     "down_bar",
241   };
242   char *buf;
243   struct GNUNET_CONTAINER_MetaData *meta;
244   struct GNUNET_FS_Uri *kuri;
245   struct GNUNET_FS_FileInformation *fi;
246   struct GNUNET_FS_BlockOptions bo;
247   size_t i;
248
249   fs = GNUNET_FS_start (cfg, "test-fs-download-indexed", &progress_cb, NULL,
250                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
251   GNUNET_assert (NULL != fs);
252
253   fn1 = GNUNET_DISK_mktemp ("gnunet-download-indexed-test");
254   buf = GNUNET_malloc (FILESIZE);
255   for (i = 0; i < FILESIZE; i++)
256     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
257   GNUNET_assert (FILESIZE ==
258                  GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
259                                        GNUNET_DISK_PERM_USER_READ |
260                                        GNUNET_DISK_PERM_USER_WRITE));
261   GNUNET_free (buf);
262   meta = GNUNET_CONTAINER_meta_data_create ();
263   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
264   bo.content_priority = 42;
265   bo.anonymity_level = 1;
266   bo.replication_level = 0;
267   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
268   fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn1,
269                                                     kuri, meta, GNUNET_YES,
270                                                     &bo);
271   GNUNET_FS_uri_destroy (kuri);
272   GNUNET_CONTAINER_meta_data_destroy (meta);
273   GNUNET_assert (NULL != fi);
274   timeout_kill =
275       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
276   start = GNUNET_TIME_absolute_get ();
277   publish =
278       GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
279                                GNUNET_FS_PUBLISH_OPTION_NONE);
280   GNUNET_assert (publish != NULL);
281 }
282
283
284 int
285 main (int argc, char *argv[])
286 {
287   if (0 != GNUNET_TESTING_peer_run ("test-fs-download-indexed",
288                                     "test_fs_download_data.conf",
289                                     &run, NULL))
290     return 1;
291   return err;
292 }
293
294 /* end of test_fs_download_indexed.c */