fix shutdown
[oweals/gnunet.git] / src / fs / test_fs_publish_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_publish_persistence.c
23  * @brief simple testcase for persistence of simple publish 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 struct GNUNET_FS_Handle *fs;
64
65 static const struct GNUNET_CONFIGURATION_Handle *cfg;
66
67 static struct GNUNET_FS_PublishContext *publish;
68
69 static struct GNUNET_FS_PublishContext *publish;
70
71 static char *fn1;
72
73 static char *fn2;
74
75 static int err;
76
77 static GNUNET_SCHEDULER_TaskIdentifier rtask;
78
79 static void
80 abort_publish_task (void *cls,
81                     const struct GNUNET_SCHEDULER_TaskContext *tc)
82 {
83   GNUNET_FS_publish_stop (publish);
84   publish = NULL;
85   GNUNET_DISK_directory_remove (fn1);
86   GNUNET_free (fn1);
87   fn1 = NULL;
88   GNUNET_DISK_directory_remove (fn2);
89   GNUNET_free (fn2);
90   fn2 = NULL;
91   GNUNET_FS_stop (fs);
92   fs = NULL;
93   if (GNUNET_SCHEDULER_NO_TASK != rtask)
94     {
95       GNUNET_SCHEDULER_cancel (rtask);
96       rtask = GNUNET_SCHEDULER_NO_TASK;
97     }
98 }
99
100
101 static void *
102 progress_cb (void *cls, 
103              const struct GNUNET_FS_ProgressInfo *event);
104
105
106 static void
107 restart_fs_task (void *cls,
108                  const struct GNUNET_SCHEDULER_TaskContext *tc)
109 {
110   rtask = GNUNET_SCHEDULER_NO_TASK;
111   GNUNET_FS_stop (fs);
112   fs = GNUNET_FS_start (cfg,
113                         "test-fs-publish-persistence",
114                         &progress_cb,
115                         NULL,
116                         GNUNET_FS_FLAGS_PERSISTENCE,
117                         GNUNET_FS_OPTIONS_END);
118 }
119
120
121 /**
122  * Consider scheduling the restart-task. 
123  * Only runs the restart task once per event 
124  * category.
125  *
126  * @param ev type of the event to consider
127  */
128 static void
129 consider_restart (int ev)
130 {
131   static int prev[32];
132   static int off;
133   int i;
134   for (i=0;i<off;i++)
135     if (prev[i] == ev)
136       return;
137   prev[off++] = ev;
138   rtask = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
139                                               &restart_fs_task,
140                                               NULL);
141 }
142
143
144 static void *
145 progress_cb (void *cls, 
146              const struct GNUNET_FS_ProgressInfo *event)
147 {
148   void *ret;
149
150   ret = NULL;
151   switch (event->status)
152     {
153     case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
154       consider_restart (event->status);
155       ret = event->value.publish.cctx;
156       printf ("Publish complete,  %llu kbps.\n",
157               (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).rel_value) / 1024));
158       if (0 == strcmp ("publish-context-dir", 
159                        event->value.publish.cctx))      
160         GNUNET_SCHEDULER_add_now (&abort_publish_task,
161                                   NULL);
162       break;
163     case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
164       consider_restart (event->status);
165       ret = event->value.publish.cctx;
166       GNUNET_assert (publish == event->value.publish.pc);
167 #if VERBOSE
168       printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
169               (unsigned long long) event->value.publish.completed,
170               (unsigned long long) event->value.publish.size,
171               event->value.publish.specifics.progress.depth,
172               (unsigned long long) event->value.publish.specifics.progress.offset);
173 #endif
174       break;
175     case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
176       if  (event->value.publish.pc == publish)
177         publish = NULL;
178       break;
179     case GNUNET_FS_STATUS_PUBLISH_RESUME:
180       if (NULL == publish)
181         {
182           GNUNET_assert (GNUNET_YES ==
183                          GNUNET_FS_file_information_is_directory (event->value.publish.fi));
184           publish = event->value.publish.pc;
185           return "publish-context-dir";
186         }
187       break;
188     case GNUNET_FS_STATUS_PUBLISH_ERROR:
189       ret = event->value.publish.cctx;
190       fprintf (stderr,
191                "Error publishing file: %s\n",
192                event->value.publish.specifics.error.message);
193       err = 1;
194       GNUNET_SCHEDULER_add_now (&abort_publish_task,
195                                 NULL);
196       break;
197     case GNUNET_FS_STATUS_PUBLISH_START:
198       consider_restart (event->status);
199       publish = event->value.publish.pc;
200       ret = event->value.publish.cctx;
201       if (0 == strcmp ("publish-context1", 
202                        event->value.publish.cctx))
203         {
204           GNUNET_assert (0 == strcmp ("publish-context-dir", 
205                                       event->value.publish.pctx));
206           GNUNET_assert (FILESIZE == event->value.publish.size);
207           GNUNET_assert (0 == event->value.publish.completed);
208           GNUNET_assert (1 == event->value.publish.anonymity);
209         }
210       else if (0 == strcmp ("publish-context2", 
211                             event->value.publish.cctx))
212         {
213           GNUNET_assert (0 == strcmp ("publish-context-dir", 
214                                       event->value.publish.pctx));
215           GNUNET_assert (FILESIZE == event->value.publish.size);
216           GNUNET_assert (0 == event->value.publish.completed);
217           GNUNET_assert (2 == event->value.publish.anonymity);
218         }
219       else if (0 == strcmp ("publish-context-dir", 
220                             event->value.publish.cctx))
221         {
222           GNUNET_assert (0 == event->value.publish.completed);
223           GNUNET_assert (3 == event->value.publish.anonymity);
224         }
225       else
226         GNUNET_assert (0);
227       break;
228     case GNUNET_FS_STATUS_PUBLISH_STOPPED:
229       consider_restart (event->status);
230       if (0 == strcmp ("publish-context-dir", 
231                        event->value.publish.cctx))      
232         GNUNET_assert (publish == event->value.publish.pc);
233       break;
234     default:
235       printf ("Unexpected event: %d\n", 
236               event->status);
237       break;
238     }
239   return ret;
240 }
241
242
243 static void
244 setup_peer (struct PeerContext *p, const char *cfgname)
245 {
246   p->cfg = GNUNET_CONFIGURATION_create ();
247 #if START_ARM
248   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
249                                         "gnunet-service-arm",
250 #if VERBOSE
251                                         "-L", "DEBUG",
252 #endif
253                                         "-c", cfgname, NULL);
254 #endif
255   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
256 }
257
258
259 static void
260 stop_arm (struct PeerContext *p)
261 {
262 #if START_ARM
263   if (NULL != p->arm_proc)
264     {
265       if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
266         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
267       if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
268         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
269       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270                   "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
271       GNUNET_OS_process_close (p->arm_proc);
272       p->arm_proc = NULL;
273     }
274 #endif
275   GNUNET_CONFIGURATION_destroy (p->cfg);
276 }
277
278
279 static void
280 run (void *cls,
281      char *const *args,
282      const char *cfgfile,
283      const struct GNUNET_CONFIGURATION_Handle *c)
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 *fi1;
293   struct GNUNET_FS_FileInformation *fi2;
294   struct GNUNET_FS_FileInformation *fidir;
295   size_t i;
296   struct GNUNET_FS_BlockOptions bo;
297
298   cfg = c;
299   setup_peer (&p1, "test_fs_publish_data.conf");
300   fs = GNUNET_FS_start (cfg,
301                         "test-fs-publish-persistence",
302                         &progress_cb,
303                         NULL,
304                         GNUNET_FS_FLAGS_PERSISTENCE,
305                         GNUNET_FS_OPTIONS_END);
306   GNUNET_assert (NULL != fs); 
307   fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
308   buf = GNUNET_malloc (FILESIZE);
309   for (i = 0; i < FILESIZE; i++)
310     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
311   GNUNET_assert (FILESIZE ==
312                  GNUNET_DISK_fn_write (fn1,
313                                        buf,
314                                        FILESIZE,
315                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
316   GNUNET_free (buf);
317
318   fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
319   buf = GNUNET_malloc (FILESIZE);
320   for (i = 0; i < FILESIZE; i++)
321     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
322   GNUNET_assert (FILESIZE ==
323                  GNUNET_DISK_fn_write (fn2,
324                                        buf,
325                                        FILESIZE,
326                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
327   GNUNET_free (buf);
328
329   meta = GNUNET_CONTAINER_meta_data_create ();
330   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
331   bo.content_priority = 42;
332   bo.anonymity_level = 1;
333   bo.replication_level = 0;
334   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME); 
335   fi1 = GNUNET_FS_file_information_create_from_file (fs,
336                                                      "publish-context1",
337                                                      fn1,
338                                                      kuri,
339                                                      meta,
340                                                      GNUNET_YES,
341                                                      &bo);
342   GNUNET_assert (NULL != fi1);
343   bo.anonymity_level = 2;
344   fi2 = GNUNET_FS_file_information_create_from_file (fs,
345                                                      "publish-context2",
346                                                      fn2,
347                                                      kuri,
348                                                      meta,
349                                                      GNUNET_YES,
350                                                      &bo);
351   GNUNET_assert (NULL != fi2);
352   bo.anonymity_level = 3;
353   fidir = GNUNET_FS_file_information_create_empty_directory (fs,
354                                                              "publish-context-dir",
355                                                              kuri,
356                                                              meta,
357                                                              &bo);
358   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
359   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
360   GNUNET_FS_uri_destroy (kuri);
361   GNUNET_CONTAINER_meta_data_destroy (meta);
362   GNUNET_assert (NULL != fidir);
363   start = GNUNET_TIME_absolute_get ();
364   GNUNET_FS_publish_start (fs,
365                            fidir,
366                            NULL, NULL, NULL,
367                            GNUNET_FS_PUBLISH_OPTION_NONE);
368   GNUNET_assert (publish != NULL);
369 }
370
371
372 int
373 main (int argc, char *argv[])
374 {
375   char *const argvx[] = { 
376     "test-fs-publish-persistence",
377     "-c",
378     "test_fs_publish_data.conf",
379 #if VERBOSE
380     "-L", "DEBUG",
381 #endif
382     NULL
383   };
384   struct GNUNET_GETOPT_CommandLineOption options[] = {
385     GNUNET_GETOPT_OPTION_END
386   };
387
388   GNUNET_log_setup ("test_fs_publish_persistence", 
389 #if VERBOSE
390                     "DEBUG",
391 #else
392                     "WARNING",
393 #endif
394                     NULL);
395   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
396                       argvx, "test-fs-publish",
397                       "nohelp", options, &run, NULL);
398   stop_arm (&p1);
399   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
400   if (fn1 != NULL)
401     {
402       GNUNET_DISK_directory_remove (fn1);
403       GNUNET_free (fn1);
404     }
405   if (fn2 != NULL)
406     {
407       GNUNET_DISK_directory_remove (fn2);
408       GNUNET_free (fn2);
409     }
410   return err;
411 }
412
413 /* end of test_fs_publish_persistence.c */