arg
[oweals/gnunet.git] / src / fs / test_fs_download_persistence.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2008, 2009, 2010 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_persistence.c
23  * @brief simple testcase for persistence of simple download operation
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_fs_service.h"
31
32 #define VERBOSE GNUNET_NO
33
34 #define START_ARM GNUNET_YES
35
36 /**
37  * File-size we use for testing.
38  */
39 #define FILESIZE (1024 * 1024 * 2)
40
41 /**
42  * How long until we give up on transmitting the message?
43  */
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
45
46 /**
47  * How long should our test-content live?
48  */ 
49 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
50
51 struct PeerContext
52 {
53   struct GNUNET_CONFIGURATION_Handle *cfg;
54 #if START_ARM
55   struct GNUNET_OS_Process *arm_proc;
56 #endif
57 };
58
59 static struct PeerContext p1;
60
61 static struct GNUNET_TIME_Absolute start;
62
63 static const struct GNUNET_CONFIGURATION_Handle *cfg;
64
65 static struct GNUNET_FS_Handle *fs;
66
67 static struct GNUNET_FS_DownloadContext *download;
68
69 static struct GNUNET_FS_PublishContext *publish;
70
71 static GNUNET_SCHEDULER_TaskIdentifier timeout_kill;
72
73 static char *fn;
74
75 static int err;
76
77 static void
78 timeout_kill_task (void *cls,
79                    const struct GNUNET_SCHEDULER_TaskContext *tc)
80 {
81   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
82               "Timeout downloading file\n");
83   if (download != NULL)
84     {
85       GNUNET_FS_download_stop (download, GNUNET_YES);
86       download = NULL;
87     }
88   else if (publish != NULL)
89     {
90       GNUNET_FS_publish_stop (publish);
91       publish = NULL;
92     }
93   timeout_kill = GNUNET_SCHEDULER_NO_TASK;
94   err = 1;
95 }
96
97 static void
98 abort_publish_task (void *cls,
99                      const struct GNUNET_SCHEDULER_TaskContext *tc)
100 {
101   if (publish != NULL)
102     {
103       GNUNET_FS_publish_stop (publish);
104       publish = NULL;
105     }
106 }
107
108
109 static void
110 abort_download_task (void *cls,
111                      const struct GNUNET_SCHEDULER_TaskContext *tc)
112 {
113   uint64_t size;
114   
115   if (download != NULL)
116     {
117       GNUNET_FS_download_stop (download, GNUNET_YES);
118       download = NULL;
119     }
120   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES));
121   GNUNET_assert (size == FILESIZE); 
122   GNUNET_DISK_directory_remove (fn);
123   GNUNET_free (fn);
124   fn = NULL;
125   GNUNET_SCHEDULER_cancel (timeout_kill);
126   timeout_kill = GNUNET_SCHEDULER_NO_TASK;
127 }
128
129
130 static void *
131 progress_cb (void *cls, 
132              const struct GNUNET_FS_ProgressInfo *event);
133
134
135 static void
136 restart_fs_task (void *cls,
137                  const struct GNUNET_SCHEDULER_TaskContext *tc)
138 {
139   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
140               "Restarting FS.\n");
141   GNUNET_FS_stop (fs);
142   fs = GNUNET_FS_start (cfg,
143                         "test-fs-download-persistence",
144                         &progress_cb,
145                         NULL,
146                         GNUNET_FS_FLAGS_PERSISTENCE,
147                         GNUNET_FS_OPTIONS_END);
148 }
149
150
151 /**
152  * Consider scheduling the restart-task. 
153  * Only runs the restart task once per event 
154  * category.
155  *
156  * @param ev type of the event to consider
157  */
158 static void
159 consider_restart (int ev)
160 {
161   static int prev[32];
162   static int off;
163   int i;
164   for (i=0;i<off;i++)
165     if (prev[i] == ev)
166       return;
167   prev[off++] = ev;
168   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
169                                       &restart_fs_task,
170                                       NULL);
171 }
172
173
174 static void *
175 progress_cb (void *cls, 
176              const struct GNUNET_FS_ProgressInfo *event)
177 {
178   switch (event->status)
179     {
180     case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
181 #if VERBOSE
182       printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
183               (unsigned long long) event->value.publish.completed,
184               (unsigned long long) event->value.publish.size,
185               event->value.publish.specifics.progress.depth,
186               (unsigned long long) event->value.publish.specifics.progress.offset);
187 #endif      
188       break;
189     case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
190       printf ("Publishing complete, %llu kbps.\n",
191               (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).rel_value) / 1024LL));
192       fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
193       start = GNUNET_TIME_absolute_get ();
194       GNUNET_assert (download == NULL);
195       GNUNET_FS_download_start (fs,
196                                 event->value.publish.specifics.completed.chk_uri,
197                                 NULL,
198                                 fn, NULL,
199                                 0,
200                                 FILESIZE,
201                                 1,
202                                 GNUNET_FS_DOWNLOAD_OPTION_NONE,
203                                 "download",
204                                 NULL);
205       break;
206     case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
207       consider_restart (event->status);
208       printf ("Download complete,  %llu kbps.\n",
209               (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).rel_value) / 1024LL));
210       GNUNET_SCHEDULER_add_now (&abort_download_task,
211                                 NULL);
212       break;
213     case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
214       consider_restart (event->status);
215       GNUNET_assert (download == event->value.download.dc);
216 #if VERBOSE
217       printf ("Download is progressing (%llu/%llu at level %u off %llu)...\n",
218               (unsigned long long) event->value.download.completed,
219               (unsigned long long) event->value.download.size,
220               event->value.download.specifics.progress.depth,
221               (unsigned long long) event->value.download.specifics.progress.offset);
222 #endif
223       break;
224     case GNUNET_FS_STATUS_PUBLISH_ERROR:
225       fprintf (stderr,
226                "Error publishing file: %s\n",
227                event->value.publish.specifics.error.message);
228       GNUNET_break (0);
229       GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
230                                          NULL,
231                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
232       break;
233     case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
234       fprintf (stderr,
235                "Error downloading file: %s\n",
236                event->value.download.specifics.error.message);
237       GNUNET_SCHEDULER_add_now (&abort_download_task,
238                                 NULL);
239       break;
240     case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
241       GNUNET_assert (event->value.publish.pc == publish);
242       publish = NULL;
243       break;
244     case GNUNET_FS_STATUS_PUBLISH_RESUME:
245       GNUNET_assert (NULL == publish);
246       publish = event->value.publish.pc;
247       break;
248     case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
249       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250                   "Download suspended.\n");
251       GNUNET_assert (event->value.download.dc == download);
252       download = NULL;
253       break;
254     case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
255       GNUNET_assert (NULL == download);
256       download = event->value.download.dc;
257       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258                   "Download resumed.\n");
259       break;
260     case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
261       consider_restart (event->status);
262       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263                   "Download active.\n");
264       break;
265     case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
266       consider_restart (event->status);
267       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268                   "Download inactive.\n");
269       break;
270     case GNUNET_FS_STATUS_PUBLISH_START:
271       GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
272       GNUNET_assert (NULL == event->value.publish.pctx);
273       GNUNET_assert (FILESIZE == event->value.publish.size);
274       GNUNET_assert (0 == event->value.publish.completed);
275       GNUNET_assert (1 == event->value.publish.anonymity);
276       break;
277     case GNUNET_FS_STATUS_PUBLISH_STOPPED:
278       GNUNET_assert (publish == event->value.publish.pc);
279       GNUNET_assert (FILESIZE == event->value.publish.size);
280       GNUNET_assert (1 == event->value.publish.anonymity);
281       GNUNET_FS_stop (fs);
282       fs = NULL;
283       break;
284     case GNUNET_FS_STATUS_DOWNLOAD_START:
285       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
286                   "Download started.\n");
287       consider_restart (event->status);
288       GNUNET_assert (download == NULL);
289       download = event->value.download.dc;
290       GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
291       GNUNET_assert (NULL == event->value.download.pctx);
292       GNUNET_assert (NULL != event->value.download.uri);
293       GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
294       GNUNET_assert (FILESIZE == event->value.download.size);
295       GNUNET_assert (0 == event->value.download.completed);
296       GNUNET_assert (1 == event->value.download.anonymity);
297       break;
298     case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
299       GNUNET_assert (download == event->value.download.dc);
300       GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
301                                          NULL,
302                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
303       download = NULL;
304       break;
305     default:
306       printf ("Unexpected event: %d\n", 
307               event->status);
308       break;
309     }
310   return NULL;
311 }
312
313
314 static void
315 setup_peer (struct PeerContext *p, const char *cfgname)
316 {
317   p->cfg = GNUNET_CONFIGURATION_create ();
318 #if START_ARM
319   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
320                                         "gnunet-service-arm",
321 #if VERBOSE
322                                         "-L", "DEBUG",
323 #endif
324                                         "-c", cfgname, NULL);
325 #endif
326   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
327 }
328
329
330 static void
331 stop_arm (struct PeerContext *p)
332 {
333 #if START_ARM
334   if (NULL != p->arm_proc)
335     {
336       if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
337         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
338       if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
339         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
340       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341                   "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
342       GNUNET_OS_process_close (p->arm_proc);
343       p->arm_proc = NULL;
344     }
345 #endif
346   GNUNET_CONFIGURATION_destroy (p->cfg);
347 }
348
349
350 static void
351 run (void *cls,
352      char *const *args,
353      const char *cfgfile,
354      const struct GNUNET_CONFIGURATION_Handle *c)
355 {
356   const char *keywords[] = {
357     "down_foo",
358     "down_bar",
359   };
360   char *buf;
361   struct GNUNET_CONTAINER_MetaData *meta;
362   struct GNUNET_FS_Uri *kuri;
363   struct GNUNET_FS_FileInformation *fi;
364   size_t i;
365   struct GNUNET_FS_BlockOptions bo;
366
367   cfg = c;
368   setup_peer (&p1, "test_fs_download_data.conf");
369   fs = GNUNET_FS_start (cfg,
370                         "test-fs-download-persistence",
371                         &progress_cb,
372                         NULL,
373                         GNUNET_FS_FLAGS_PERSISTENCE,
374                         GNUNET_FS_OPTIONS_END);
375   GNUNET_assert (NULL != fs); 
376   buf = GNUNET_malloc (FILESIZE);
377   for (i = 0; i < FILESIZE; i++)
378     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
379   meta = GNUNET_CONTAINER_meta_data_create ();
380   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
381   bo.content_priority = 42;
382   bo.anonymity_level = 1;
383   bo.replication_level = 0;
384   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME); 
385   fi = GNUNET_FS_file_information_create_from_data (fs,
386                                                     "publish-context",
387                                                     FILESIZE,
388                                                     buf,
389                                                     kuri,
390                                                     meta,
391                                                     GNUNET_NO,
392                                                     &bo);
393   GNUNET_FS_uri_destroy (kuri);
394   GNUNET_CONTAINER_meta_data_destroy (meta);
395   GNUNET_assert (NULL != fi);
396   timeout_kill = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
397                                                &timeout_kill_task,
398                                                NULL);
399   start = GNUNET_TIME_absolute_get ();
400   publish = GNUNET_FS_publish_start (fs,
401                                     fi,
402                                     NULL, NULL, NULL,
403                                     GNUNET_FS_PUBLISH_OPTION_NONE);
404   GNUNET_assert (publish != NULL);
405 }
406
407
408 int
409 main (int argc, char *argv[])
410 {
411   char *const argvx[] = { 
412     "test-fs-download-persistence",
413     "-c",
414     "test_fs_download_data.conf",
415 #if VERBOSE
416     "-L", "DEBUG",
417 #endif
418     NULL
419   };
420   struct GNUNET_GETOPT_CommandLineOption options[] = {
421     GNUNET_GETOPT_OPTION_END
422   };
423   GNUNET_log_setup ("test_fs_download_persistence", 
424 #if VERBOSE
425                     "DEBUG",
426 #else
427                     "WARNING",
428 #endif
429                     NULL);
430   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-download/");
431   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
432                       argvx, "test-fs-download-persistence",
433                       "nohelp", options, &run, NULL);
434   stop_arm (&p1);
435   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-download/");
436   return err;
437 }
438
439 /* end of test_fs_download_persistence.c */