46a2dc3da7725ae7efc86133b7dd1f95f008a252
[oweals/gnunet.git] / src / fs / test_fs_publish.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2008, 2009 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 2, 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.c
23  * @brief simple testcase for publish operation (indexing, listing
24  *        indexed, directory structure)
25  * @author Christian Grothoff
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_fs_service.h"
32
33 #define VERBOSE GNUNET_NO
34
35 #define START_ARM GNUNET_YES
36
37 /**
38  * File-size we use for testing.
39  */
40 #define FILESIZE (1024 * 1024 * 2)
41
42 /**
43  * How long until we give up on transmitting the message?
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
46
47 /**
48  * How long should our test-content live?
49  */ 
50 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
51
52 struct PeerContext
53 {
54   struct GNUNET_CONFIGURATION_Handle *cfg;
55 #if START_ARM
56   pid_t arm_pid;
57 #endif
58 };
59
60 static struct PeerContext p1;
61
62 static struct GNUNET_TIME_Absolute start;
63
64 static struct GNUNET_SCHEDULER_Handle *sched;
65
66 static struct GNUNET_FS_Handle *fs;
67
68 static struct GNUNET_FS_PublishContext *publish;
69
70 static struct GNUNET_FS_PublishContext *publish;
71
72 static char *fn1;
73
74 static char *fn2;
75
76 static int err;
77
78 static void
79 abort_publish_task (void *cls,
80                     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 }
91
92
93 static void *
94 progress_cb (void *cls, 
95              const struct GNUNET_FS_ProgressInfo *event)
96 {
97   void *ret;
98
99   ret = NULL;
100   switch (event->status)
101     {
102     case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
103       ret = event->value.publish.cctx;
104       printf ("Publish complete,  %llu kbps.\n",
105               (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
106       if (0 == strcmp ("list_indexed-context-dir", 
107                        event->value.publish.cctx))      
108         GNUNET_SCHEDULER_add_continuation (sched,
109                                            &abort_publish_task,
110                                            NULL,
111                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
112       break;
113     case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
114       ret = event->value.publish.cctx;
115       GNUNET_assert (publish == event->value.publish.sc);
116 #if VERBOSE
117       printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
118               (unsigned long long) event->value.publish.completed,
119               (unsigned long long) event->value.publish.size,
120               event->value.publish.specifics.progress.depth,
121               (unsigned long long) event->value.publish.specifics.progress.offset);
122 #endif
123       break;
124     case GNUNET_FS_STATUS_PUBLISH_ERROR:
125       ret = event->value.publish.cctx;
126       fprintf (stderr,
127                "Error publishing file: %s\n",
128                event->value.publish.specifics.error.message);
129       err = 1;
130       if (0 == strcmp ("list_indexed-context-dir", 
131                        event->value.publish.cctx))              
132         GNUNET_SCHEDULER_add_continuation (sched,
133                                            &abort_publish_task,
134                                            NULL,
135                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
136       break;
137     case GNUNET_FS_STATUS_PUBLISH_START:
138       ret = event->value.publish.cctx;
139       if (0 == strcmp ("publish-context1", 
140                        event->value.publish.cctx))
141         {
142           GNUNET_assert (0 == strcmp ("publish-context-dir", 
143                                       event->value.publish.pctx));
144           GNUNET_assert (FILESIZE == event->value.publish.size);
145           GNUNET_assert (0 == event->value.publish.completed);
146           GNUNET_assert (1 == event->value.publish.anonymity);
147         }
148       else if (0 == strcmp ("publish-context2", 
149                             event->value.publish.cctx))
150         {
151           GNUNET_assert (0 == strcmp ("publish-context-dir", 
152                                       event->value.publish.pctx));
153           GNUNET_assert (FILESIZE == event->value.publish.size);
154           GNUNET_assert (0 == event->value.publish.completed);
155           GNUNET_assert (2 == event->value.publish.anonymity);
156         }
157       else if (0 == strcmp ("publish-context-dir", 
158                             event->value.publish.cctx))
159         {
160           GNUNET_assert (0 == event->value.publish.completed);
161           GNUNET_assert (3 == event->value.publish.anonymity);
162         }
163       else
164         GNUNET_assert (0);
165       break;
166     case GNUNET_FS_STATUS_PUBLISH_STOPPED:
167       if (0 == strcmp ("list_indexed-context-dir", 
168                        event->value.publish.cctx))      
169         {
170           GNUNET_assert (publish == event->value.publish.sc);
171           GNUNET_SCHEDULER_add_continuation (sched,
172                                              &abort_publish_task,
173                                              NULL,
174                                              GNUNET_SCHEDULER_REASON_PREREQ_DONE);
175         }
176       break;
177     default:
178       printf ("Unexpected event: %d\n", 
179               event->status);
180       break;
181     }
182   return ret;
183 }
184
185
186 static void
187 setup_peer (struct PeerContext *p, const char *cfgname)
188 {
189   p->cfg = GNUNET_CONFIGURATION_create ();
190 #if START_ARM
191   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
192                                         "gnunet-service-arm",
193 #if VERBOSE
194                                         "-L", "DEBUG",
195 #endif
196                                         "-c", cfgname, NULL);
197   sleep (1);                    /* allow ARM to start */
198 #endif
199   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
200   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
201 }
202
203
204 static void
205 stop_arm (struct PeerContext *p)
206 {
207 #if START_ARM
208   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
209     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
210   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
211     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
212   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213               "ARM process %u stopped\n", p->arm_pid);
214 #endif
215   GNUNET_CONFIGURATION_destroy (p->cfg);
216 }
217
218
219 static void
220 run (void *cls,
221      struct GNUNET_SCHEDULER_Handle *s,
222      char *const *args,
223      const char *cfgfile,
224      const struct GNUNET_CONFIGURATION_Handle *cfg)
225 {
226   const char *keywords[] = {
227     "down_foo",
228     "down_bar",
229   };
230   char *buf;
231   struct GNUNET_CONTAINER_MetaData *meta;
232   struct GNUNET_FS_Uri *kuri;
233   struct GNUNET_FS_FileInformation *fi1;
234   struct GNUNET_FS_FileInformation *fi2;
235   struct GNUNET_FS_FileInformation *fidir;
236   size_t i;
237
238   sched = s;
239   setup_peer (&p1, "test_fs_publish_data.conf");
240   fs = GNUNET_FS_start (sched,
241                         cfg,
242                         "test-fs-publish",
243                         &progress_cb,
244                         NULL,
245                         GNUNET_FS_FLAGS_NONE,
246                         GNUNET_FS_OPTIONS_END);
247   GNUNET_assert (NULL != fs); 
248   fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
249   buf = GNUNET_malloc (FILESIZE);
250   for (i = 0; i < FILESIZE; i++)
251     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
252   GNUNET_assert (FILESIZE ==
253                  GNUNET_DISK_fn_write (fn1,
254                                        buf,
255                                        FILESIZE,
256                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
257   GNUNET_free (buf);
258
259   fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
260   buf = GNUNET_malloc (FILESIZE);
261   for (i = 0; i < FILESIZE; i++)
262     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
263   GNUNET_assert (FILESIZE ==
264                  GNUNET_DISK_fn_write (fn2,
265                                        buf,
266                                        FILESIZE,
267                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
268   GNUNET_free (buf);
269
270   meta = GNUNET_CONTAINER_meta_data_create ();
271   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
272   fi1 = GNUNET_FS_file_information_create_from_file ("publish-context1",
273                                                      fn1,
274                                                      kuri,
275                                                      meta,
276                                                      GNUNET_YES,
277                                                      1,
278                                                      42,
279                                                      GNUNET_TIME_relative_to_absolute (LIFETIME)); 
280   fi2 = GNUNET_FS_file_information_create_from_file ("publish-context2",
281                                                      fn2,
282                                                      kuri,
283                                                      meta,
284                                                      GNUNET_YES,
285                                                      2,
286                                                      42,
287                                                      GNUNET_TIME_relative_to_absolute (LIFETIME)); 
288   fidir = GNUNET_FS_file_information_create_empty_directory ("publish-context-dir",
289                                                              kuri,
290                                                              meta,
291                                                              3,
292                                                              42,
293                                                              GNUNET_TIME_relative_to_absolute (LIFETIME)); 
294   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
295   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
296   GNUNET_FS_uri_destroy (kuri);
297   GNUNET_CONTAINER_meta_data_destroy (meta);
298   GNUNET_assert (NULL != fidir);
299   start = GNUNET_TIME_absolute_get ();
300   publish = GNUNET_FS_publish_start (fs,
301                                      fidir,
302                                      NULL, NULL, NULL,
303                                      GNUNET_FS_PUBLISH_OPTION_NONE);
304   GNUNET_assert (publish != NULL);
305 }
306
307
308 int
309 main (int argc, char *argv[])
310 {
311   char *const argvx[] = { 
312     "test-fs-publish",
313     "-c",
314     "test_fs_publish_data.conf",
315 #if VERBOSE
316     "-L", "DEBUG",
317 #endif
318     NULL
319   };
320   struct GNUNET_GETOPT_CommandLineOption options[] = {
321     GNUNET_GETOPT_OPTION_END
322   };
323
324   GNUNET_log_setup ("test_fs_publish", 
325 #if VERBOSE
326                     "DEBUG",
327 #else
328                     "WARNING",
329 #endif
330                     NULL);
331   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
332                       argvx, "test-fs-publish",
333                       "nohelp", options, &run, NULL);
334   stop_arm (&p1);
335   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
336   GNUNET_DISK_directory_remove (fn1);
337   GNUNET_free_non_null (fn1);
338   GNUNET_DISK_directory_remove (fn2);
339   GNUNET_free_non_null (fn2);
340   return err;
341 }
342
343 /* end of test_fs_publish.c */