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