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