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