arg
[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_continuation (&abort_publish_task,
151                                            NULL,
152                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
153       break;
154     case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
155       consider_restart (event->status);
156       ret = event->value.publish.cctx;
157       GNUNET_assert (publish == event->value.publish.pc);
158 #if VERBOSE
159       printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
160               (unsigned long long) event->value.publish.completed,
161               (unsigned long long) event->value.publish.size,
162               event->value.publish.specifics.progress.depth,
163               (unsigned long long) event->value.publish.specifics.progress.offset);
164 #endif
165       break;
166     case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
167       if  (event->value.publish.pc == publish)
168         publish = NULL;
169       break;
170     case GNUNET_FS_STATUS_PUBLISH_RESUME:
171       if (NULL == publish)
172         {
173           publish = event->value.publish.pc;
174           return "publish-context-dir";
175         }
176       break;
177     case GNUNET_FS_STATUS_PUBLISH_ERROR:
178       ret = event->value.publish.cctx;
179       fprintf (stderr,
180                "Error publishing file: %s\n",
181                event->value.publish.specifics.error.message);
182       err = 1;
183       if (0 == strcmp ("publish-context-dir", 
184                        event->value.publish.cctx))              
185         GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
186                                            NULL,
187                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
188       break;
189     case GNUNET_FS_STATUS_PUBLISH_START:
190       consider_restart (event->status);
191       publish = event->value.publish.pc;
192       ret = event->value.publish.cctx;
193       if (0 == strcmp ("publish-context1", 
194                        event->value.publish.cctx))
195         {
196           GNUNET_assert (0 == strcmp ("publish-context-dir", 
197                                       event->value.publish.pctx));
198           GNUNET_assert (FILESIZE == event->value.publish.size);
199           GNUNET_assert (0 == event->value.publish.completed);
200           GNUNET_assert (1 == event->value.publish.anonymity);
201         }
202       else if (0 == strcmp ("publish-context2", 
203                             event->value.publish.cctx))
204         {
205           GNUNET_assert (0 == strcmp ("publish-context-dir", 
206                                       event->value.publish.pctx));
207           GNUNET_assert (FILESIZE == event->value.publish.size);
208           GNUNET_assert (0 == event->value.publish.completed);
209           GNUNET_assert (2 == event->value.publish.anonymity);
210         }
211       else if (0 == strcmp ("publish-context-dir", 
212                             event->value.publish.cctx))
213         {
214           GNUNET_assert (0 == event->value.publish.completed);
215           GNUNET_assert (3 == event->value.publish.anonymity);
216         }
217       else
218         GNUNET_assert (0);
219       break;
220     case GNUNET_FS_STATUS_PUBLISH_STOPPED:
221       consider_restart (event->status);
222       if (0 == strcmp ("publish-context-dir", 
223                        event->value.publish.cctx))      
224         GNUNET_assert (publish == event->value.publish.pc);
225       break;
226     default:
227       printf ("Unexpected event: %d\n", 
228               event->status);
229       break;
230     }
231   return ret;
232 }
233
234
235 static void
236 setup_peer (struct PeerContext *p, const char *cfgname)
237 {
238   p->cfg = GNUNET_CONFIGURATION_create ();
239 #if START_ARM
240   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
241                                         "gnunet-service-arm",
242 #if VERBOSE
243                                         "-L", "DEBUG",
244 #endif
245                                         "-c", cfgname, NULL);
246 #endif
247   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
248 }
249
250
251 static void
252 stop_arm (struct PeerContext *p)
253 {
254 #if START_ARM
255   if (NULL != p->arm_proc)
256     {
257       if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
258         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
259       if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
260         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
261       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262                   "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
263       GNUNET_OS_process_close (p->arm_proc);
264       p->arm_proc = NULL;
265     }
266 #endif
267   GNUNET_CONFIGURATION_destroy (p->cfg);
268 }
269
270
271 static void
272 run (void *cls,
273      char *const *args,
274      const char *cfgfile,
275      const struct GNUNET_CONFIGURATION_Handle *c)
276 {
277   const char *keywords[] = {
278     "down_foo",
279     "down_bar",
280   };
281   char *buf;
282   struct GNUNET_CONTAINER_MetaData *meta;
283   struct GNUNET_FS_Uri *kuri;
284   struct GNUNET_FS_FileInformation *fi1;
285   struct GNUNET_FS_FileInformation *fi2;
286   struct GNUNET_FS_FileInformation *fidir;
287   size_t i;
288   struct GNUNET_FS_BlockOptions bo;
289
290   cfg = c;
291   setup_peer (&p1, "test_fs_publish_data.conf");
292   fs = GNUNET_FS_start (cfg,
293                         "test-fs-publish-persistence",
294                         &progress_cb,
295                         NULL,
296                         GNUNET_FS_FLAGS_PERSISTENCE,
297                         GNUNET_FS_OPTIONS_END);
298   GNUNET_assert (NULL != fs); 
299   fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
300   buf = GNUNET_malloc (FILESIZE);
301   for (i = 0; i < FILESIZE; i++)
302     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
303   GNUNET_assert (FILESIZE ==
304                  GNUNET_DISK_fn_write (fn1,
305                                        buf,
306                                        FILESIZE,
307                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
308   GNUNET_free (buf);
309
310   fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
311   buf = GNUNET_malloc (FILESIZE);
312   for (i = 0; i < FILESIZE; i++)
313     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
314   GNUNET_assert (FILESIZE ==
315                  GNUNET_DISK_fn_write (fn2,
316                                        buf,
317                                        FILESIZE,
318                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
319   GNUNET_free (buf);
320
321   meta = GNUNET_CONTAINER_meta_data_create ();
322   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
323   bo.content_priority = 42;
324   bo.anonymity_level = 1;
325   bo.replication_level = 0;
326   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME); 
327   fi1 = GNUNET_FS_file_information_create_from_file (fs,
328                                                      "publish-context1",
329                                                      fn1,
330                                                      kuri,
331                                                      meta,
332                                                      GNUNET_YES,
333                                                      &bo);
334   GNUNET_assert (NULL != fi1);
335   bo.anonymity_level = 2;
336   fi2 = GNUNET_FS_file_information_create_from_file (fs,
337                                                      "publish-context2",
338                                                      fn2,
339                                                      kuri,
340                                                      meta,
341                                                      GNUNET_YES,
342                                                      &bo);
343   GNUNET_assert (NULL != fi2);
344   bo.anonymity_level = 3;
345   fidir = GNUNET_FS_file_information_create_empty_directory (fs,
346                                                              "publish-context-dir",
347                                                              kuri,
348                                                              meta,
349                                                              &bo);
350   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
351   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
352   GNUNET_FS_uri_destroy (kuri);
353   GNUNET_CONTAINER_meta_data_destroy (meta);
354   GNUNET_assert (NULL != fidir);
355   start = GNUNET_TIME_absolute_get ();
356   GNUNET_FS_publish_start (fs,
357                            fidir,
358                            NULL, NULL, NULL,
359                            GNUNET_FS_PUBLISH_OPTION_NONE);
360   GNUNET_assert (publish != NULL);
361 }
362
363
364 int
365 main (int argc, char *argv[])
366 {
367   char *const argvx[] = { 
368     "test-fs-publish-persistence",
369     "-c",
370     "test_fs_publish_data.conf",
371 #if VERBOSE
372     "-L", "DEBUG",
373 #endif
374     NULL
375   };
376   struct GNUNET_GETOPT_CommandLineOption options[] = {
377     GNUNET_GETOPT_OPTION_END
378   };
379
380   GNUNET_log_setup ("test_fs_publish_persistence", 
381 #if VERBOSE
382                     "DEBUG",
383 #else
384                     "WARNING",
385 #endif
386                     NULL);
387   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
388                       argvx, "test-fs-publish",
389                       "nohelp", options, &run, NULL);
390   stop_arm (&p1);
391   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
392   if (fn1 != NULL)
393     {
394       GNUNET_DISK_directory_remove (fn1);
395       GNUNET_free (fn1);
396     }
397   if (fn2 != NULL)
398     {
399       GNUNET_DISK_directory_remove (fn2);
400       GNUNET_free (fn2);
401     }
402   return err;
403 }
404
405 /* end of test_fs_publish_persistence.c */