Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / fs / test_fs_download.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2005, 2006, 2008, 2009, 2011, 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file fs/test_fs_download.c
21  * @brief simple testcase for simple publish + download operation
22  * @author Christian Grothoff
23  */
24
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_fs_service.h"
28 #include "gnunet_testing_lib.h"
29 #include <gauger.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, 120)
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 static unsigned int anonymity_level;
47
48 static int indexed;
49
50 static struct GNUNET_TIME_Absolute start;
51
52 static struct GNUNET_FS_Handle *fs;
53
54 static struct GNUNET_FS_DownloadContext *download;
55
56 static struct GNUNET_FS_PublishContext *publish;
57
58 static struct GNUNET_SCHEDULER_Task * timeout_kill;
59
60 static char *fn;
61
62 static char *fn1;
63
64 static int err;
65
66
67 static void
68 timeout_kill_task (void *cls)
69 {
70   if (NULL != download)
71   {
72     GNUNET_FS_download_stop (download, GNUNET_YES);
73     download = NULL;
74   }
75   else if (NULL != publish)
76   {
77     GNUNET_FS_publish_stop (publish);
78     publish = NULL;
79   }
80   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
81   timeout_kill = NULL;
82   err = 1;
83 }
84
85
86 static void
87 abort_publish_task (void *cls)
88 {
89   if (NULL != publish)
90   {
91     GNUNET_FS_publish_stop (publish);
92     publish = NULL;
93   }
94 }
95
96
97 static void
98 stop_fs_task (void *cls)
99 {
100   GNUNET_FS_stop (fs);
101   fs = NULL;
102 }
103
104
105 static void
106 abort_download_task (void *cls)
107 {
108   uint64_t size;
109
110   if (NULL != download)
111   {
112     GNUNET_FS_download_stop (download, GNUNET_YES);
113     download = NULL;
114   }
115   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
116   GNUNET_assert (size == FILESIZE);
117   GNUNET_DISK_directory_remove (fn);
118   GNUNET_free (fn);
119   fn = NULL;
120   GNUNET_SCHEDULER_cancel (timeout_kill);
121   timeout_kill = NULL;
122 }
123
124
125 static void *
126 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
127 {
128
129   switch (event->status)
130   {
131   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
132     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133                 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
134                 (unsigned long long) event->value.publish.completed,
135                 (unsigned long long) event->value.publish.size,
136                 event->value.publish.specifics.progress.depth,
137                 (unsigned long long) event->value.publish.specifics.
138                 progress.offset);
139     break;
140   case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
141     break;
142   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
143     fprintf (stdout,
144              "Publishing complete, %llu kb/s.\n",
145              (unsigned long long) (FILESIZE * 1000000LL /
146                                    (1 +
147                                     GNUNET_TIME_absolute_get_duration
148                                     (start).rel_value_us) / 1024LL));
149     GAUGER ("FS",
150             (GNUNET_YES == indexed)
151             ? "Publishing speed (indexing)"
152              : "Publishing speed (insertion)",
153             (unsigned long long) (FILESIZE * 1000000LL /
154                                   (1 +
155                                    GNUNET_TIME_absolute_get_duration
156                                    (start).rel_value_us) / 1024LL), "kb/s");
157     fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
158     start = GNUNET_TIME_absolute_get ();
159     download =
160         GNUNET_FS_download_start (fs,
161                                   event->value.publish.specifics.
162                                   completed.chk_uri, NULL, fn, NULL, 0,
163                                   FILESIZE, anonymity_level,
164                                   GNUNET_FS_DOWNLOAD_OPTION_NONE,
165                                   "download", NULL);
166     GNUNET_assert (download != NULL);
167     break;
168   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
169     fprintf (stdout,
170              "Download complete,  %llu kb/s.\n",
171              (unsigned long long) (FILESIZE * 1000000LL /
172                                    (1 +
173                                     GNUNET_TIME_absolute_get_duration
174                                     (start).rel_value_us) / 1024LL));
175     GAUGER ("FS",
176             (GNUNET_YES == indexed)
177             ? "Local download speed (indexed)"
178             : "Local download speed (inserted)",
179             (unsigned long long) (FILESIZE * 1000000LL /
180                                   (1 +
181                                    GNUNET_TIME_absolute_get_duration
182                                    (start).rel_value_us) / 1024LL), "kb/s");
183     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
184     break;
185   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
186     GNUNET_assert (download == event->value.download.dc);
187     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188                 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
189                 (unsigned long long) event->value.download.completed,
190                 (unsigned long long) event->value.download.size,
191                 event->value.download.specifics.progress.depth,
192                 (unsigned long long) event->value.download.specifics.
193                 progress.offset);
194     break;
195   case GNUNET_FS_STATUS_PUBLISH_ERROR:
196     FPRINTF (stderr, "Error publishing file: %s\n",
197              event->value.publish.specifics.error.message);
198     GNUNET_break (0);
199     GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
200     GNUNET_SCHEDULER_shutdown ();
201     break;
202   case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
203     FPRINTF (stderr, "Error downloading file: %s\n",
204              event->value.download.specifics.error.message);
205     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
206     GNUNET_SCHEDULER_shutdown ();
207     break;
208   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
209   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
210     break;
211   case GNUNET_FS_STATUS_PUBLISH_START:
212     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
213     GNUNET_assert (NULL == event->value.publish.pctx);
214     GNUNET_assert (FILESIZE == event->value.publish.size);
215     GNUNET_assert (0 == event->value.publish.completed);
216     GNUNET_assert (1 == event->value.publish.anonymity);
217     break;
218   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
219     GNUNET_assert (publish == event->value.publish.pc);
220     GNUNET_assert (FILESIZE == event->value.publish.size);
221     GNUNET_assert (1 == event->value.publish.anonymity);
222     GNUNET_SCHEDULER_add_now (&stop_fs_task, NULL);
223     break;
224   case GNUNET_FS_STATUS_DOWNLOAD_START:
225     GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
226     GNUNET_assert (NULL == event->value.download.pctx);
227     GNUNET_assert (NULL != event->value.download.uri);
228     GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
229     GNUNET_assert (FILESIZE == event->value.download.size);
230     GNUNET_assert (0 == event->value.download.completed);
231     GNUNET_assert (1 == event->value.download.anonymity);
232     break;
233   case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
234     GNUNET_assert (download == event->value.download.dc);
235     GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
236     break;
237   default:
238     printf ("Unexpected event: %d\n", event->status);
239     break;
240   }
241   return NULL;
242 }
243
244
245 static void
246 run (void *cls,
247      const struct GNUNET_CONFIGURATION_Handle *cfg,
248      struct GNUNET_TESTING_Peer *peer)
249 {
250   const char *binary_name = cls;
251   const char *keywords[] = {
252     "down_foo",
253     "down_bar",
254   };
255   char *buf;
256   struct GNUNET_CONTAINER_MetaData *meta;
257   struct GNUNET_FS_Uri *kuri;
258   struct GNUNET_FS_FileInformation *fi;
259   size_t i;
260   struct GNUNET_FS_BlockOptions bo;
261
262   if (GNUNET_YES ==
263       GNUNET_CONFIGURATION_get_value_yesno (cfg,
264                                             "download-test",
265                                             "USE_STREAM"))
266     anonymity_level = 0;
267   else
268     anonymity_level = 1;
269   fs = GNUNET_FS_start (cfg, binary_name, &progress_cb, NULL,
270                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
271   GNUNET_assert (NULL != fs);
272   buf = GNUNET_malloc (FILESIZE);
273   for (i = 0; i < FILESIZE; i++)
274     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
275   meta = GNUNET_CONTAINER_meta_data_create ();
276   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
277   bo.content_priority = 42;
278   bo.anonymity_level = anonymity_level;
279   bo.replication_level = 0;
280   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
281
282   if (GNUNET_YES ==
283       GNUNET_CONFIGURATION_get_value_yesno (cfg,
284                                             "download-test",
285                                             "USE_INDEX"))
286   {
287     fn1 = GNUNET_DISK_mktemp ("gnunet-download-indexed-test");
288     GNUNET_assert (FILESIZE ==
289                    GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
290                                          GNUNET_DISK_PERM_USER_READ |
291                                          GNUNET_DISK_PERM_USER_WRITE));
292     GNUNET_free (buf);
293     fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn1,
294                                                       kuri, meta, GNUNET_YES,
295                                                       &bo);
296     indexed = GNUNET_YES;
297   }
298   else
299   {
300     fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
301                                                       FILESIZE, buf, kuri, meta,
302                                                       GNUNET_NO, &bo);
303     /* note: buf will be free'd as part of 'fi' now */
304     indexed = GNUNET_NO;
305   }
306   GNUNET_FS_uri_destroy (kuri);
307   GNUNET_CONTAINER_meta_data_destroy (meta);
308   GNUNET_assert (NULL != fi);
309   timeout_kill =
310       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
311   start = GNUNET_TIME_absolute_get ();
312   publish =
313       GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
314                                GNUNET_FS_PUBLISH_OPTION_NONE);
315   GNUNET_assert (publish != NULL);
316 }
317
318
319 int
320 main (int argc, char *argv[])
321 {
322   const char *binary_name;
323   const char *config_name;
324
325   binary_name = "test-fs-download";
326   config_name = "test_fs_download_data.conf";
327   if (NULL != strstr (argv[0], "indexed"))
328   {
329     binary_name = "test-fs-download-indexed";
330     config_name = "test_fs_download_indexed.conf";
331   }
332   if (NULL != strstr (argv[0], "cadet"))
333   {
334     binary_name = "test-fs-download-cadet";
335     config_name = "test_fs_download_cadet.conf";
336   }
337   if (0 != GNUNET_TESTING_peer_run (binary_name,
338                                     config_name,
339                                     &run, (void *) binary_name))
340     return 1;
341   if (NULL != fn1)
342   {
343     UNLINK (fn1);
344     GNUNET_free (fn1);
345   }
346   return err;
347 }
348
349 /* end of test_fs_download.c */