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