-bringing copyright tags up to FSF standard
[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 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 struct GNUNET_SCHEDULER_Task * 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 = NULL;
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 = NULL;
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_PROGRESS_DIRECTORY:
143     break;
144   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
145     fprintf (stdout,
146              "Publishing complete, %llu kb/s.\n",
147              (unsigned long long) (FILESIZE * 1000000LL /
148                                    (1 +
149                                     GNUNET_TIME_absolute_get_duration
150                                     (start).rel_value_us) / 1024LL));
151     GAUGER ("FS",
152             (GNUNET_YES == indexed)
153             ? "Publishing speed (indexing)"
154              : "Publishing speed (insertion)",
155             (unsigned long long) (FILESIZE * 1000000LL /
156                                   (1 +
157                                    GNUNET_TIME_absolute_get_duration
158                                    (start).rel_value_us) / 1024LL), "kb/s");
159     fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
160     start = GNUNET_TIME_absolute_get ();
161     download =
162         GNUNET_FS_download_start (fs,
163                                   event->value.publish.specifics.
164                                   completed.chk_uri, NULL, fn, NULL, 0,
165                                   FILESIZE, anonymity_level,
166                                   GNUNET_FS_DOWNLOAD_OPTION_NONE,
167                                   "download", NULL);
168     GNUNET_assert (download != NULL);
169     break;
170   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
171     fprintf (stdout,
172              "Download complete,  %llu kb/s.\n",
173              (unsigned long long) (FILESIZE * 1000000LL /
174                                    (1 +
175                                     GNUNET_TIME_absolute_get_duration
176                                     (start).rel_value_us) / 1024LL));
177     GAUGER ("FS",
178             (GNUNET_YES == indexed)
179             ? "Local download speed (indexed)"
180             : "Local download speed (inserted)",
181             (unsigned long long) (FILESIZE * 1000000LL /
182                                   (1 +
183                                    GNUNET_TIME_absolute_get_duration
184                                    (start).rel_value_us) / 1024LL), "kb/s");
185     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
186     break;
187   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
188     GNUNET_assert (download == event->value.download.dc);
189     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190                 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
191                 (unsigned long long) event->value.download.completed,
192                 (unsigned long long) event->value.download.size,
193                 event->value.download.specifics.progress.depth,
194                 (unsigned long long) event->value.download.specifics.
195                 progress.offset);
196     break;
197   case GNUNET_FS_STATUS_PUBLISH_ERROR:
198     FPRINTF (stderr, "Error publishing file: %s\n",
199              event->value.publish.specifics.error.message);
200     GNUNET_break (0);
201     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
202                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
203     GNUNET_SCHEDULER_shutdown ();
204     break;
205   case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
206     FPRINTF (stderr, "Error downloading file: %s\n",
207              event->value.download.specifics.error.message);
208     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
209     GNUNET_SCHEDULER_shutdown ();
210     break;
211   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
212   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
213     break;
214   case GNUNET_FS_STATUS_PUBLISH_START:
215     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
216     GNUNET_assert (NULL == event->value.publish.pctx);
217     GNUNET_assert (FILESIZE == event->value.publish.size);
218     GNUNET_assert (0 == event->value.publish.completed);
219     GNUNET_assert (1 == event->value.publish.anonymity);
220     break;
221   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
222     GNUNET_assert (publish == event->value.publish.pc);
223     GNUNET_assert (FILESIZE == event->value.publish.size);
224     GNUNET_assert (1 == event->value.publish.anonymity);
225     GNUNET_SCHEDULER_add_now (&stop_fs_task, NULL);
226     break;
227   case GNUNET_FS_STATUS_DOWNLOAD_START:
228     GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
229     GNUNET_assert (NULL == event->value.download.pctx);
230     GNUNET_assert (NULL != event->value.download.uri);
231     GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
232     GNUNET_assert (FILESIZE == event->value.download.size);
233     GNUNET_assert (0 == event->value.download.completed);
234     GNUNET_assert (1 == event->value.download.anonymity);
235     break;
236   case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
237     GNUNET_assert (download == event->value.download.dc);
238     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
239                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
240     break;
241   default:
242     printf ("Unexpected event: %d\n", event->status);
243     break;
244   }
245   return NULL;
246 }
247
248
249 static void
250 run (void *cls,
251      const struct GNUNET_CONFIGURATION_Handle *cfg,
252      struct GNUNET_TESTING_Peer *peer)
253 {
254   const char *binary_name = cls;
255   const char *keywords[] = {
256     "down_foo",
257     "down_bar",
258   };
259   char *buf;
260   struct GNUNET_CONTAINER_MetaData *meta;
261   struct GNUNET_FS_Uri *kuri;
262   struct GNUNET_FS_FileInformation *fi;
263   size_t i;
264   struct GNUNET_FS_BlockOptions bo;
265
266   if (GNUNET_YES ==
267       GNUNET_CONFIGURATION_get_value_yesno (cfg,
268                                             "download-test",
269                                             "USE_STREAM"))
270     anonymity_level = 0;
271   else
272     anonymity_level = 1;
273   fs = GNUNET_FS_start (cfg, binary_name, &progress_cb, NULL,
274                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
275   GNUNET_assert (NULL != fs);
276   buf = GNUNET_malloc (FILESIZE);
277   for (i = 0; i < FILESIZE; i++)
278     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
279   meta = GNUNET_CONTAINER_meta_data_create ();
280   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
281   bo.content_priority = 42;
282   bo.anonymity_level = anonymity_level;
283   bo.replication_level = 0;
284   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
285
286   if (GNUNET_YES ==
287       GNUNET_CONFIGURATION_get_value_yesno (cfg,
288                                             "download-test",
289                                             "USE_INDEX"))
290   {
291     fn1 = GNUNET_DISK_mktemp ("gnunet-download-indexed-test");
292     GNUNET_assert (FILESIZE ==
293                    GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
294                                          GNUNET_DISK_PERM_USER_READ |
295                                          GNUNET_DISK_PERM_USER_WRITE));
296     GNUNET_free (buf);
297     fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn1,
298                                                       kuri, meta, GNUNET_YES,
299                                                       &bo);
300     indexed = GNUNET_YES;
301   }
302   else
303   {
304     fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
305                                                       FILESIZE, buf, kuri, meta,
306                                                       GNUNET_NO, &bo);
307     /* note: buf will be free'd as part of 'fi' now */
308     indexed = GNUNET_NO;
309   }
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   const char *binary_name;
327   const char *config_name;
328
329   binary_name = "test-fs-download";
330   config_name = "test_fs_download_data.conf";
331   if (NULL != strstr (argv[0], "indexed"))
332   {
333     binary_name = "test-fs-download-indexed";
334     config_name = "test_fs_download_indexed.conf";
335   }
336   if (NULL != strstr (argv[0], "cadet"))
337   {
338     binary_name = "test-fs-download-cadet";
339     config_name = "test_fs_download_cadet.conf";
340   }
341   if (0 != GNUNET_TESTING_peer_run (binary_name,
342                                     config_name,
343                                     &run, (void *) binary_name))
344     return 1;
345   if (NULL != fn1)
346   {
347     UNLINK (fn1);
348     GNUNET_free (fn1);
349   }
350   return err;
351 }
352
353 /* end of test_fs_download.c */