-cleaning up fs-download testcases
[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_arm_service.h"
29 #include "gnunet_fs_service.h"
30 #include "gnunet_testing_lib-new.h"
31 #include <gauger.h>
32
33
34 /**
35  * File-size we use for testing.
36  */
37 #define FILESIZE (1024 * 1024 * 2)
38
39 /**
40  * How long until we give up on transmitting the message?
41  */
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
43
44 /**
45  * How long should our test-content live?
46  */
47 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
48
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 GNUNET_SCHEDULER_TaskIdentifier 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, const struct GNUNET_SCHEDULER_TaskContext *tc)
69 {
70   fprintf (stderr, "Download not fast enough, timeout!\n");
71   if (download != NULL)
72   {
73     GNUNET_FS_download_stop (download, GNUNET_YES);
74     download = NULL;
75   }
76   else if (publish != NULL)
77   {
78     GNUNET_FS_publish_stop (publish);
79     publish = NULL;
80   }
81   timeout_kill = GNUNET_SCHEDULER_NO_TASK;
82   err = 1;
83 }
84
85
86 static void
87 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 {
89   if (publish != NULL)
90   {
91     GNUNET_FS_publish_stop (publish);
92     publish = NULL;
93   }
94 }
95
96
97 static void
98 stop_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
99 {
100   GNUNET_FS_stop (fs);
101   fs = NULL;
102 }
103
104
105 static void
106 abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
107 {
108   uint64_t size;
109
110   if (download != NULL)
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 = GNUNET_SCHEDULER_NO_TASK;
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_COMPLETED:
141     printf ("Publishing complete, %llu kbps.\n",
142             (unsigned long long) (FILESIZE * 1000LL /
143                                   (1 +
144                                    GNUNET_TIME_absolute_get_duration
145                                    (start).rel_value) / 1024LL));
146     GAUGER ("FS", "Publishing speed (indexing)",
147             (unsigned long long) (FILESIZE * 1000LL /
148                                   (1 +
149                                    GNUNET_TIME_absolute_get_duration
150                                    (start).rel_value) / 1024LL), "kb/s");
151     fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
152     start = GNUNET_TIME_absolute_get ();
153     download =
154         GNUNET_FS_download_start (fs,
155                                   event->value.publish.specifics.
156                                   completed.chk_uri, NULL, fn, NULL, 0,
157                                   FILESIZE, 1, GNUNET_FS_DOWNLOAD_OPTION_NONE,
158                                   "download", NULL);
159     GNUNET_assert (download != NULL);
160     break;
161   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
162     printf ("Download complete,  %llu kbps.\n",
163             (unsigned long long) (FILESIZE * 1000LL /
164                                   (1 +
165                                    GNUNET_TIME_absolute_get_duration
166                                    (start).rel_value) / 1024LL));
167     GAUGER ("FS", "Local download speed (indexed)",
168             (unsigned long long) (FILESIZE * 1000LL /
169                                   (1 +
170                                    GNUNET_TIME_absolute_get_duration
171                                    (start).rel_value) / 1024LL), "kb/s");
172     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
173     break;
174   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
175     GNUNET_assert (download == event->value.download.dc);
176     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177                 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
178                 (unsigned long long) event->value.download.completed,
179                 (unsigned long long) event->value.download.size,
180                 event->value.download.specifics.progress.depth,
181                 (unsigned long long) event->value.download.specifics.
182                 progress.offset);
183     break;
184   case GNUNET_FS_STATUS_PUBLISH_ERROR:
185     FPRINTF (stderr, "Error publishing file: %s\n",
186              event->value.publish.specifics.error.message);
187     GNUNET_break (0);
188     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
189                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
190     break;
191   case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
192     FPRINTF (stderr, "Error downloading file: %s\n",
193              event->value.download.specifics.error.message);
194     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
195     break;
196   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
197   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
198     break;
199   case GNUNET_FS_STATUS_PUBLISH_START:
200     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
201     GNUNET_assert (NULL == event->value.publish.pctx);
202     GNUNET_assert (FILESIZE == event->value.publish.size);
203     GNUNET_assert (0 == event->value.publish.completed);
204     GNUNET_assert (1 == event->value.publish.anonymity);
205     break;
206   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
207     GNUNET_assert (publish == event->value.publish.pc);
208     GNUNET_assert (FILESIZE == event->value.publish.size);
209     GNUNET_assert (1 == event->value.publish.anonymity);
210     GNUNET_SCHEDULER_add_now (&stop_fs_task, NULL);
211     break;
212   case GNUNET_FS_STATUS_DOWNLOAD_START:
213     GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
214     GNUNET_assert (NULL == event->value.download.pctx);
215     GNUNET_assert (NULL != event->value.download.uri);
216     GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
217     GNUNET_assert (FILESIZE == event->value.download.size);
218     GNUNET_assert (0 == event->value.download.completed);
219     GNUNET_assert (1 == event->value.download.anonymity);
220     break;
221   case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
222     GNUNET_assert (download == event->value.download.dc);
223     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
224                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
225     break;
226   default:
227     printf ("Unexpected event: %d\n", event->status);
228     break;
229   }
230   return NULL;
231 }
232
233
234 static void
235 run (void *cls,
236      const struct GNUNET_CONFIGURATION_Handle *cfg)
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 */