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