c069875cfb082322fc8f761630ca92af7383657f
[oweals/gnunet.git] / src / fs / test_fs_download.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2008, 2009, 2011, 2012 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.c
23  * @brief simple testcase for simple publish + download operation
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_fs_service.h"
30 #include "gnunet_testing_lib.h"
31 #include <gauger.h>
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 static unsigned int anonymity_level;
49
50 static int indexed;
51
52 static struct GNUNET_TIME_Absolute start;
53
54 static struct GNUNET_FS_Handle *fs;
55
56 static struct GNUNET_FS_DownloadContext *download;
57
58 static struct GNUNET_FS_PublishContext *publish;
59
60 static GNUNET_SCHEDULER_TaskIdentifier timeout_kill;
61
62 static char *fn;
63
64 static char *fn1;
65
66 static int err;
67
68
69 static void
70 timeout_kill_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 {
72   if (NULL != download)
73   {
74     GNUNET_FS_download_stop (download, GNUNET_YES);
75     download = NULL;
76   }
77   else if (NULL != publish)
78   {
79     GNUNET_FS_publish_stop (publish);
80     publish = NULL;
81   }
82   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
83   timeout_kill = GNUNET_SCHEDULER_NO_TASK;
84   err = 1;
85 }
86
87
88 static void
89 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
90 {
91   if (NULL != publish)
92   {
93     GNUNET_FS_publish_stop (publish);
94     publish = NULL;
95   }
96 }
97
98
99 static void
100 stop_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 {
102   GNUNET_FS_stop (fs);
103   fs = NULL;
104 }
105
106
107 static void
108 abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 {
110   uint64_t size;
111
112   if (NULL != download)
113   {
114     GNUNET_FS_download_stop (download, GNUNET_YES);
115     download = NULL;
116   }
117   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
118   GNUNET_assert (size == FILESIZE);
119   GNUNET_DISK_directory_remove (fn);
120   GNUNET_free (fn);
121   fn = NULL;
122   GNUNET_SCHEDULER_cancel (timeout_kill);
123   timeout_kill = GNUNET_SCHEDULER_NO_TASK;
124 }
125
126
127 static void *
128 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
129 {
130
131   switch (event->status)
132   {
133   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
134     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135                 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
136                 (unsigned long long) event->value.publish.completed,
137                 (unsigned long long) event->value.publish.size,
138                 event->value.publish.specifics.progress.depth,
139                 (unsigned long long) event->value.publish.specifics.
140                 progress.offset);
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_continuation (&abort_publish_task, NULL,
200                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
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     break;
207   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
208   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
209     break;
210   case GNUNET_FS_STATUS_PUBLISH_START:
211     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
212     GNUNET_assert (NULL == event->value.publish.pctx);
213     GNUNET_assert (FILESIZE == event->value.publish.size);
214     GNUNET_assert (0 == event->value.publish.completed);
215     GNUNET_assert (1 == event->value.publish.anonymity);
216     break;
217   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
218     GNUNET_assert (publish == event->value.publish.pc);
219     GNUNET_assert (FILESIZE == event->value.publish.size);
220     GNUNET_assert (1 == event->value.publish.anonymity);
221     GNUNET_SCHEDULER_add_now (&stop_fs_task, NULL);
222     break;
223   case GNUNET_FS_STATUS_DOWNLOAD_START:
224     GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
225     GNUNET_assert (NULL == event->value.download.pctx);
226     GNUNET_assert (NULL != event->value.download.uri);
227     GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
228     GNUNET_assert (FILESIZE == event->value.download.size);
229     GNUNET_assert (0 == event->value.download.completed);
230     GNUNET_assert (1 == event->value.download.anonymity);
231     break;
232   case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
233     GNUNET_assert (download == event->value.download.dc);
234     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
235                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
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], "mesh"))
333   {
334     binary_name = "test-fs-download-mesh";
335     config_name = "test_fs_download_mesh.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 */