5cfbc391ba5e1e52fcc5ba5a18a3c57dae873df9
[oweals/gnunet.git] / src / fs / test_fs_download.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2008, 2009 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 2, 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_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   pid_t arm_pid;
56 #endif
57 };
58
59 static struct PeerContext p1;
60
61 static struct GNUNET_TIME_Absolute start;
62
63 static struct GNUNET_SCHEDULER_Handle *sched;
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   if (download != NULL)
82     {
83       GNUNET_FS_download_stop (download, GNUNET_YES);
84       download = NULL;
85     }
86   else if (publish != NULL)
87     {
88       GNUNET_FS_publish_stop (publish);
89       publish = NULL;
90     }
91   timeout_kill = GNUNET_SCHEDULER_NO_TASK;
92   err = 1;
93 }
94
95 static void
96 abort_publish_task (void *cls,
97                      const struct GNUNET_SCHEDULER_TaskContext *tc)
98 {
99   if (publish != NULL)
100     {
101       GNUNET_FS_publish_stop (publish);
102       publish = NULL;
103     }
104 }
105
106 static void
107 stop_fs_task (void *cls,
108               const struct GNUNET_SCHEDULER_TaskContext *tc)
109 {
110   GNUNET_FS_stop (fs);
111   fs = NULL;
112 }
113
114 static void
115 abort_download_task (void *cls,
116                      const struct GNUNET_SCHEDULER_TaskContext *tc)
117 {
118   uint64_t size;
119   
120   if (download != NULL)
121     {
122       GNUNET_FS_download_stop (download, GNUNET_YES);
123       download = NULL;
124     }
125   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES));
126   GNUNET_assert (size == FILESIZE); 
127   GNUNET_DISK_directory_remove (fn);
128   GNUNET_free (fn);
129   fn = NULL;
130   GNUNET_SCHEDULER_cancel (sched, timeout_kill);
131   timeout_kill = GNUNET_SCHEDULER_NO_TASK;
132 }
133
134
135 static void *
136 progress_cb (void *cls, 
137              const struct GNUNET_FS_ProgressInfo *event)
138 {
139
140   switch (event->status)
141     {
142     case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
143 #if VERBOSE
144       printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
145               (unsigned long long) event->value.publish.completed,
146               (unsigned long long) event->value.publish.size,
147               event->value.publish.specifics.progress.depth,
148               (unsigned long long) event->value.publish.specifics.progress.offset);
149 #endif      
150       break;
151     case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
152       printf ("Publishing complete, %llu kbps.\n",
153               (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024LL));
154       fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
155       start = GNUNET_TIME_absolute_get ();
156       download = GNUNET_FS_download_start (fs,
157                                            event->value.publish.specifics.completed.chk_uri,
158                                            NULL,
159                                            fn, NULL,
160                                            0,
161                                            FILESIZE,
162                                            1,
163                                            GNUNET_FS_DOWNLOAD_OPTION_NONE,
164                                            "download",
165                                            NULL);
166       GNUNET_assert (download != NULL);
167       break;
168     case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
169       printf ("Download complete,  %llu kbps.\n",
170               (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024LL));
171       GNUNET_SCHEDULER_add_now (sched,
172                                 &abort_download_task,
173                                 NULL);
174       break;
175     case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
176       GNUNET_assert (download == event->value.download.dc);
177 #if VERBOSE
178       printf ("Download is progressing (%llu/%llu at level %u off %llu)...\n",
179               (unsigned long long) event->value.download.completed,
180               (unsigned long long) event->value.download.size,
181               event->value.download.specifics.progress.depth,
182               (unsigned long long) event->value.download.specifics.progress.offset);
183 #endif
184       break;
185     case GNUNET_FS_STATUS_PUBLISH_ERROR:
186       fprintf (stderr,
187                "Error publishing file: %s\n",
188                event->value.publish.specifics.error.message);
189       GNUNET_break (0);
190       GNUNET_SCHEDULER_add_continuation (sched,
191                                          &abort_publish_task,
192                                          NULL,
193                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
194       break;
195     case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
196       fprintf (stderr,
197                "Error downloading file: %s\n",
198                event->value.download.specifics.error.message);
199       GNUNET_SCHEDULER_add_now (sched,
200                                 &abort_download_task,
201                                 NULL);
202       break;
203     case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
204     case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
205       break;
206     case GNUNET_FS_STATUS_PUBLISH_START:
207       GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
208       GNUNET_assert (NULL == event->value.publish.pctx);
209       GNUNET_assert (FILESIZE == event->value.publish.size);
210       GNUNET_assert (0 == event->value.publish.completed);
211       GNUNET_assert (1 == event->value.publish.anonymity);
212       break;
213     case GNUNET_FS_STATUS_PUBLISH_STOPPED:
214       GNUNET_assert (publish == event->value.publish.pc);
215       GNUNET_assert (FILESIZE == event->value.publish.size);
216       GNUNET_assert (1 == event->value.publish.anonymity);
217       GNUNET_SCHEDULER_add_now (sched,
218                                 &stop_fs_task,
219                                 NULL);
220       break;
221     case GNUNET_FS_STATUS_DOWNLOAD_START:
222       GNUNET_assert (download == NULL);
223       GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
224       GNUNET_assert (NULL == event->value.download.pctx);
225       GNUNET_assert (NULL != event->value.download.uri);
226       GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
227       GNUNET_assert (FILESIZE == event->value.download.size);
228       GNUNET_assert (0 == event->value.download.completed);
229       GNUNET_assert (1 == event->value.download.anonymity);
230       break;
231     case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
232       GNUNET_assert (download == event->value.download.dc);
233       GNUNET_SCHEDULER_add_continuation (sched,
234                                          &abort_publish_task,
235                                          NULL,
236                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
237       break;
238     default:
239       printf ("Unexpected event: %d\n", 
240               event->status);
241       break;
242     }
243   return NULL;
244 }
245
246
247 static void
248 setup_peer (struct PeerContext *p, const char *cfgname)
249 {
250   p->cfg = GNUNET_CONFIGURATION_create ();
251 #if START_ARM
252   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
253                                         "gnunet-service-arm",
254 #if VERBOSE
255                                         "-L", "DEBUG",
256 #endif
257                                         "-c", cfgname, NULL);
258 #endif
259   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
260 }
261
262
263 static void
264 stop_arm (struct PeerContext *p)
265 {
266 #if START_ARM
267   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
268     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
269   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
270     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
271   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272               "ARM process %u stopped\n", p->arm_pid);
273 #endif
274   GNUNET_CONFIGURATION_destroy (p->cfg);
275 }
276
277
278 static void
279 run (void *cls,
280      struct GNUNET_SCHEDULER_Handle *s,
281      char *const *args,
282      const char *cfgfile,
283      const struct GNUNET_CONFIGURATION_Handle *cfg)
284 {
285   const char *keywords[] = {
286     "down_foo",
287     "down_bar",
288   };
289   char *buf;
290   struct GNUNET_CONTAINER_MetaData *meta;
291   struct GNUNET_FS_Uri *kuri;
292   struct GNUNET_FS_FileInformation *fi;
293   size_t i;
294
295   sched = s;
296   setup_peer (&p1, "test_fs_download_data.conf");
297   fs = GNUNET_FS_start (sched,
298                         cfg,
299                         "test-fs-download",
300                         &progress_cb,
301                         NULL,
302                         GNUNET_FS_FLAGS_NONE,
303                         GNUNET_FS_OPTIONS_END);
304   GNUNET_assert (NULL != fs); 
305   buf = GNUNET_malloc (FILESIZE);
306   for (i = 0; i < FILESIZE; i++)
307     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
308   meta = GNUNET_CONTAINER_meta_data_create ();
309   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
310   fi = GNUNET_FS_file_information_create_from_data (fs,
311                                                     "publish-context",
312                                                     FILESIZE,
313                                                     buf,
314                                                     kuri,
315                                                     meta,
316                                                     GNUNET_NO,
317                                                     1,
318                                                     42,
319                                                     GNUNET_TIME_relative_to_absolute (LIFETIME)); 
320   GNUNET_FS_uri_destroy (kuri);
321   GNUNET_CONTAINER_meta_data_destroy (meta);
322   GNUNET_assert (NULL != fi);
323   timeout_kill = GNUNET_SCHEDULER_add_delayed (sched,
324                                                TIMEOUT,
325                                                &timeout_kill_task,
326                                                NULL);
327   start = GNUNET_TIME_absolute_get ();
328   publish = GNUNET_FS_publish_start (fs,
329                                     fi,
330                                     NULL, NULL, NULL,
331                                     GNUNET_FS_PUBLISH_OPTION_NONE);
332   GNUNET_assert (publish != NULL);
333 }
334
335
336 int
337 main (int argc, char *argv[])
338 {
339   char *const argvx[] = { 
340     "test-fs-download",
341     "-c",
342     "test_fs_download_data.conf",
343 #if VERBOSE
344     "-L", "DEBUG",
345 #endif
346     NULL
347   };
348   struct GNUNET_GETOPT_CommandLineOption options[] = {
349     GNUNET_GETOPT_OPTION_END
350   };
351
352   GNUNET_log_setup ("test_fs_download", 
353 #if VERBOSE
354                     "DEBUG",
355 #else
356                     "WARNING",
357 #endif
358                     NULL);
359   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
360                       argvx, "test-fs-download",
361                       "nohelp", options, &run, NULL);
362   stop_arm (&p1);
363   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-download/");
364   return err;
365 }
366
367 /* end of test_fs_download.c */