Renamed GNUNET_OS_process_close to GNUNET_OS_process_destroy
[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
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_fs_service.h"
31 #include <gauger.h>
32
33 #define VERBOSE GNUNET_NO
34
35 #define START_ARM GNUNET_YES
36
37 /**
38  * File-size we use for testing.
39  */
40 #define FILESIZE (1024 * 1024 * 2)
41
42 /**
43  * How long until we give up on transmitting the message?
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
46
47 /**
48  * How long should our test-content live?
49  */
50 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
51
52 struct PeerContext
53 {
54   struct GNUNET_CONFIGURATION_Handle *cfg;
55 #if START_ARM
56   struct GNUNET_OS_Process *arm_proc;
57 #endif
58 };
59
60 static struct PeerContext p1;
61
62 static struct GNUNET_TIME_Absolute start;
63
64 static struct GNUNET_FS_Handle *fs;
65
66 static struct GNUNET_FS_DownloadContext *download;
67
68 static struct GNUNET_FS_PublishContext *publish;
69
70 static GNUNET_SCHEDULER_TaskIdentifier timeout_kill;
71
72 static char *fn;
73
74 static char *fn1;
75
76 static int err;
77
78
79 static void
80 timeout_kill_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 {
82   fprintf (stderr, "Download not fast enough, timeout!\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, const struct GNUNET_SCHEDULER_TaskContext *tc)
99 {
100   if (publish != NULL)
101   {
102     GNUNET_FS_publish_stop (publish);
103     publish = NULL;
104   }
105 }
106
107
108 static void
109 stop_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 {
111   GNUNET_FS_stop (fs);
112   fs = NULL;
113 }
114
115
116 static void
117 abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
118 {
119   uint64_t size;
120
121   if (download != NULL)
122   {
123     GNUNET_FS_download_stop (download, GNUNET_YES);
124     download = NULL;
125   }
126   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
127   GNUNET_assert (size == FILESIZE);
128   GNUNET_DISK_directory_remove (fn);
129   GNUNET_free (fn);
130   fn = NULL;
131   GNUNET_SCHEDULER_cancel (timeout_kill);
132   timeout_kill = GNUNET_SCHEDULER_NO_TASK;
133 }
134
135
136 static void *
137 progress_cb (void *cls, 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.
149             progress.offset);
150 #endif
151     break;
152   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
153     printf ("Publishing complete, %llu kbps.\n",
154             (unsigned long long) (FILESIZE * 1000LL /
155                                   (1 +
156                                    GNUNET_TIME_absolute_get_duration
157                                    (start).rel_value) / 1024LL));
158     GAUGER ("FS", "Publishing speed (indexing)",
159             (unsigned long long) (FILESIZE * 1000LL /
160                                   (1 +
161                                    GNUNET_TIME_absolute_get_duration
162                                    (start).rel_value) / 1024LL), "kb/s");
163     fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
164     start = GNUNET_TIME_absolute_get ();
165     download =
166         GNUNET_FS_download_start (fs,
167                                   event->value.publish.specifics.
168                                   completed.chk_uri, NULL, fn, NULL, 0,
169                                   FILESIZE, 1, GNUNET_FS_DOWNLOAD_OPTION_NONE,
170                                   "download", NULL);
171     GNUNET_assert (download != NULL);
172     break;
173   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
174     printf ("Download complete,  %llu kbps.\n",
175             (unsigned long long) (FILESIZE * 1000LL /
176                                   (1 +
177                                    GNUNET_TIME_absolute_get_duration
178                                    (start).rel_value) / 1024LL));
179     GAUGER ("FS", "Local download speed (indexed)",
180             (unsigned long long) (FILESIZE * 1000LL /
181                                   (1 +
182                                    GNUNET_TIME_absolute_get_duration
183                                    (start).rel_value) / 1024LL), "kb/s");
184     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
185     break;
186   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
187     GNUNET_assert (download == event->value.download.dc);
188 #if VERBOSE
189     printf ("Download is progressing (%llu/%llu at level %u off %llu)...\n",
190             (unsigned long long) event->value.download.completed,
191             (unsigned long long) event->value.download.size,
192             event->value.download.specifics.progress.depth,
193             (unsigned long long) event->value.download.specifics.
194             progress.offset);
195 #endif
196     break;
197   case GNUNET_FS_STATUS_PUBLISH_ERROR:
198     FPRINTF (stderr, "Error publishing file: %s\n",
199              event->value.publish.specifics.error.message);
200     GNUNET_break (0);
201     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
202                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
203     break;
204   case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
205     FPRINTF (stderr, "Error downloading file: %s\n",
206              event->value.download.specifics.error.message);
207     GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
208     break;
209   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
210   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
211     break;
212   case GNUNET_FS_STATUS_PUBLISH_START:
213     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
214     GNUNET_assert (NULL == event->value.publish.pctx);
215     GNUNET_assert (FILESIZE == event->value.publish.size);
216     GNUNET_assert (0 == event->value.publish.completed);
217     GNUNET_assert (1 == event->value.publish.anonymity);
218     break;
219   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
220     GNUNET_assert (publish == event->value.publish.pc);
221     GNUNET_assert (FILESIZE == event->value.publish.size);
222     GNUNET_assert (1 == event->value.publish.anonymity);
223     GNUNET_SCHEDULER_add_now (&stop_fs_task, NULL);
224     break;
225   case GNUNET_FS_STATUS_DOWNLOAD_START:
226     GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
227     GNUNET_assert (NULL == event->value.download.pctx);
228     GNUNET_assert (NULL != event->value.download.uri);
229     GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
230     GNUNET_assert (FILESIZE == event->value.download.size);
231     GNUNET_assert (0 == event->value.download.completed);
232     GNUNET_assert (1 == event->value.download.anonymity);
233     break;
234   case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
235     GNUNET_assert (download == event->value.download.dc);
236     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
237                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
238     break;
239   default:
240     printf ("Unexpected event: %d\n", 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_proc =
253     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
254                                "gnunet-service-arm",
255                                "-c", cfgname, NULL);
256 #endif
257   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
258 }
259
260
261 static void
262 stop_arm (struct PeerContext *p)
263 {
264 #if START_ARM
265   if (NULL != p->arm_proc)
266   {
267     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
268       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
269     if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
270       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
271     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
272                 GNUNET_OS_process_get_pid (p->arm_proc));
273     GNUNET_OS_process_destroy (p->arm_proc);
274     p->arm_proc = NULL;
275   }
276 #endif
277   GNUNET_CONFIGURATION_destroy (p->cfg);
278 }
279
280
281 static void
282 run (void *cls, char *const *args, 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   struct GNUNET_FS_BlockOptions bo;
294   size_t i;
295
296   setup_peer (&p1, "test_fs_download_data.conf");
297   fs = GNUNET_FS_start (cfg, "test-fs-download-indexed", &progress_cb, NULL,
298                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
299   GNUNET_assert (NULL != fs);
300
301   fn1 = GNUNET_DISK_mktemp ("gnunet-download-indexed-test");
302   buf = GNUNET_malloc (FILESIZE);
303   for (i = 0; i < FILESIZE; i++)
304     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
305   GNUNET_assert (FILESIZE ==
306                  GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
307                                        GNUNET_DISK_PERM_USER_READ |
308                                        GNUNET_DISK_PERM_USER_WRITE));
309   GNUNET_free (buf);
310   meta = GNUNET_CONTAINER_meta_data_create ();
311   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
312   bo.content_priority = 42;
313   bo.anonymity_level = 1;
314   bo.replication_level = 0;
315   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
316   fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn1,
317                                                     kuri, meta, GNUNET_YES,
318                                                     &bo);
319   GNUNET_FS_uri_destroy (kuri);
320   GNUNET_CONTAINER_meta_data_destroy (meta);
321   GNUNET_assert (NULL != fi);
322   timeout_kill =
323       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
324   start = GNUNET_TIME_absolute_get ();
325   publish =
326       GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
327                                GNUNET_FS_PUBLISH_OPTION_NONE);
328   GNUNET_assert (publish != NULL);
329 }
330
331
332 int
333 main (int argc, char *argv[])
334 {
335   char *const argvx[] = {
336     "test-fs-download-indexed",
337     "-c",
338     "test_fs_download_data.conf",
339     NULL
340   };
341   struct GNUNET_GETOPT_CommandLineOption options[] = {
342     GNUNET_GETOPT_OPTION_END
343   };
344
345   GNUNET_log_setup ("test_fs_download_indexed",
346                     "WARNING",
347                     NULL);
348   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
349                       "test-fs-download-indexed", "nohelp", options, &run,
350                       NULL);
351   stop_arm (&p1);
352   if (fn1 != NULL)
353   {
354     GNUNET_DISK_directory_remove (fn1);
355     GNUNET_free (fn1);
356   }
357   if (fn != NULL)
358   {
359     GNUNET_DISK_directory_remove (fn);
360     GNUNET_free (fn);
361   }
362   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-download/");
363   return err;
364 }
365
366 /* end of test_fs_download_indexed.c */