20e65aeace78ebb40062f13cbc42d1ead8cc6419
[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 (NULL, NULL, "gnunet-service-arm",
192                                         "gnunet-service-arm",
193 #if VERBOSE
194                                         "-L", "DEBUG",
195 #endif
196                                         "-c", cfgname, NULL);
197 #endif
198   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
199 }
200
201
202 static void
203 stop_arm (struct PeerContext *p)
204 {
205 #if START_ARM
206   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
207     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
208   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
209     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
210   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211               "ARM process %u stopped\n", p->arm_pid);
212 #endif
213   GNUNET_CONFIGURATION_destroy (p->cfg);
214 }
215
216
217 static void
218 run (void *cls,
219      struct GNUNET_SCHEDULER_Handle *s,
220      char *const *args,
221      const char *cfgfile,
222      const struct GNUNET_CONFIGURATION_Handle *cfg)
223 {
224   const char *keywords[] = {
225     "down_foo",
226     "down_bar",
227   };
228   char *buf;
229   struct GNUNET_CONTAINER_MetaData *meta;
230   struct GNUNET_FS_Uri *kuri;
231   struct GNUNET_FS_FileInformation *fi1;
232   struct GNUNET_FS_FileInformation *fi2;
233   struct GNUNET_FS_FileInformation *fidir;
234   size_t i;
235
236   sched = s;
237   setup_peer (&p1, "test_fs_publish_data.conf");
238   fs = GNUNET_FS_start (sched,
239                         cfg,
240                         "test-fs-publish",
241                         &progress_cb,
242                         NULL,
243                         GNUNET_FS_FLAGS_NONE,
244                         GNUNET_FS_OPTIONS_END);
245   GNUNET_assert (NULL != fs); 
246   fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
247   buf = GNUNET_malloc (FILESIZE);
248   for (i = 0; i < FILESIZE; i++)
249     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
250   GNUNET_assert (FILESIZE ==
251                  GNUNET_DISK_fn_write (fn1,
252                                        buf,
253                                        FILESIZE,
254                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
255   GNUNET_free (buf);
256
257   fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
258   buf = GNUNET_malloc (FILESIZE);
259   for (i = 0; i < FILESIZE; i++)
260     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
261   GNUNET_assert (FILESIZE ==
262                  GNUNET_DISK_fn_write (fn2,
263                                        buf,
264                                        FILESIZE,
265                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
266   GNUNET_free (buf);
267
268   meta = GNUNET_CONTAINER_meta_data_create ();
269   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
270   fi1 = GNUNET_FS_file_information_create_from_file (fs,
271                                                      "publish-context1",
272                                                      fn1,
273                                                      kuri,
274                                                      meta,
275                                                      GNUNET_YES,
276                                                      1,
277                                                      42,
278                                                      GNUNET_TIME_relative_to_absolute (LIFETIME)); 
279   fi2 = GNUNET_FS_file_information_create_from_file (fs,
280                                                      "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 (fs,
289                                                              "publish-context-dir",
290                                                              kuri,
291                                                              meta,
292                                                              3,
293                                                              42,
294                                                              GNUNET_TIME_relative_to_absolute (LIFETIME)); 
295   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
296   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
297   GNUNET_FS_uri_destroy (kuri);
298   GNUNET_CONTAINER_meta_data_destroy (meta);
299   GNUNET_assert (NULL != fidir);
300   start = GNUNET_TIME_absolute_get ();
301   publish = GNUNET_FS_publish_start (fs,
302                                      fidir,
303                                      NULL, NULL, NULL,
304                                      GNUNET_FS_PUBLISH_OPTION_NONE);
305   GNUNET_assert (publish != NULL);
306 }
307
308
309 int
310 main (int argc, char *argv[])
311 {
312   char *const argvx[] = { 
313     "test-fs-publish",
314     "-c",
315     "test_fs_publish_data.conf",
316 #if VERBOSE
317     "-L", "DEBUG",
318 #endif
319     NULL
320   };
321   struct GNUNET_GETOPT_CommandLineOption options[] = {
322     GNUNET_GETOPT_OPTION_END
323   };
324
325   GNUNET_log_setup ("test_fs_publish", 
326 #if VERBOSE
327                     "DEBUG",
328 #else
329                     "WARNING",
330 #endif
331                     NULL);
332   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
333                       argvx, "test-fs-publish",
334                       "nohelp", options, &run, NULL);
335   stop_arm (&p1);
336   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
337   GNUNET_DISK_directory_remove (fn1);
338   GNUNET_free_non_null (fn1);
339   GNUNET_DISK_directory_remove (fn2);
340   GNUNET_free_non_null (fn2);
341   return err;
342 }
343
344 /* end of test_fs_publish.c */