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