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