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