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