Fixed reconnect
[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, NULL,
105                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
106     break;
107   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
108     ret = event->value.publish.cctx;
109     GNUNET_assert (publish == event->value.publish.pc);
110 #if VERBOSE
111     printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
112             (unsigned long long) event->value.publish.completed,
113             (unsigned long long) event->value.publish.size,
114             event->value.publish.specifics.progress.depth,
115             (unsigned long long) event->value.publish.specifics.
116             progress.offset);
117 #endif
118     break;
119   case GNUNET_FS_STATUS_PUBLISH_ERROR:
120     ret = event->value.publish.cctx;
121     fprintf (stderr, "Error publishing file: %s\n",
122              event->value.publish.specifics.error.message);
123     err = 1;
124     if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
125     {
126       fprintf (stderr, "Scheduling abort task for error on `%s'\n",
127                (const char *) event->value.publish.cctx);
128       GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
129                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
130     }
131     break;
132   case GNUNET_FS_STATUS_PUBLISH_START:
133     ret = event->value.publish.cctx;
134     if (0 == strcmp ("publish-context1", event->value.publish.cctx))
135     {
136       GNUNET_assert (0 ==
137                      strcmp ("publish-context-dir", event->value.publish.pctx));
138       GNUNET_assert (FILESIZE == event->value.publish.size);
139       GNUNET_assert (0 == event->value.publish.completed);
140       GNUNET_assert (1 == event->value.publish.anonymity);
141     }
142     else if (0 == strcmp ("publish-context2", event->value.publish.cctx))
143     {
144       GNUNET_assert (0 ==
145                      strcmp ("publish-context-dir", event->value.publish.pctx));
146       GNUNET_assert (FILESIZE == event->value.publish.size);
147       GNUNET_assert (0 == event->value.publish.completed);
148       GNUNET_assert (2 == event->value.publish.anonymity);
149     }
150     else if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
151     {
152       GNUNET_assert (0 == event->value.publish.completed);
153       GNUNET_assert (3 == event->value.publish.anonymity);
154     }
155     else
156       GNUNET_assert (0);
157     break;
158   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
159     if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
160       GNUNET_assert (publish == event->value.publish.pc);
161     break;
162   default:
163     printf ("Unexpected event: %d\n", event->status);
164     break;
165   }
166   return ret;
167 }
168
169
170 static void
171 setup_peer (struct PeerContext *p, const char *cfgname)
172 {
173   p->cfg = GNUNET_CONFIGURATION_create ();
174 #if START_ARM
175   p->arm_proc =
176       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
177                                "gnunet-service-arm",
178 #if VERBOSE
179                                "-L", "DEBUG",
180 #endif
181                                "-c", cfgname, NULL);
182 #endif
183   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
184 }
185
186
187 static void
188 stop_arm (struct PeerContext *p)
189 {
190 #if START_ARM
191   if (NULL != p->arm_proc)
192   {
193     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
194       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
195     if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
196       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
197     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
198                 GNUNET_OS_process_get_pid (p->arm_proc));
199     GNUNET_OS_process_close (p->arm_proc);
200     p->arm_proc = NULL;
201   }
202 #endif
203   GNUNET_CONFIGURATION_destroy (p->cfg);
204 }
205
206
207 static void
208 run (void *cls, char *const *args, const char *cfgfile,
209      const struct GNUNET_CONFIGURATION_Handle *cfg)
210 {
211   const char *keywords[] = {
212     "down_foo",
213     "down_bar",
214   };
215   char *buf;
216   struct GNUNET_CONTAINER_MetaData *meta;
217   struct GNUNET_FS_Uri *kuri;
218   struct GNUNET_FS_FileInformation *fi1;
219   struct GNUNET_FS_FileInformation *fi2;
220   struct GNUNET_FS_FileInformation *fidir;
221   size_t i;
222   struct GNUNET_FS_BlockOptions bo;
223
224   setup_peer (&p1, "test_fs_publish_data.conf");
225   fs = GNUNET_FS_start (cfg, "test-fs-publish", &progress_cb, NULL,
226                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
227   GNUNET_assert (NULL != fs);
228   fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
229   buf = GNUNET_malloc (FILESIZE);
230   for (i = 0; i < FILESIZE; i++)
231     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
232   GNUNET_assert (FILESIZE ==
233                  GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
234                                        GNUNET_DISK_PERM_USER_READ |
235                                        GNUNET_DISK_PERM_USER_WRITE));
236   GNUNET_free (buf);
237
238   fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
239   buf = GNUNET_malloc (FILESIZE);
240   for (i = 0; i < FILESIZE; i++)
241     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
242   GNUNET_assert (FILESIZE ==
243                  GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
244                                        GNUNET_DISK_PERM_USER_READ |
245                                        GNUNET_DISK_PERM_USER_WRITE));
246   GNUNET_free (buf);
247
248   meta = GNUNET_CONTAINER_meta_data_create ();
249   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
250   bo.content_priority = 42;
251   bo.anonymity_level = 1;
252   bo.replication_level = 0;
253   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
254
255   fi1 =
256       GNUNET_FS_file_information_create_from_file (fs, "publish-context1", fn1,
257                                                    kuri, meta, GNUNET_YES, &bo);
258
259   GNUNET_assert (NULL != fi1);
260   bo.anonymity_level = 2;
261   fi2 =
262       GNUNET_FS_file_information_create_from_file (fs, "publish-context2", fn2,
263                                                    kuri, meta, GNUNET_YES, &bo);
264   GNUNET_assert (NULL != fi2);
265   bo.anonymity_level = 3;
266   fidir =
267       GNUNET_FS_file_information_create_empty_directory (fs,
268                                                          "publish-context-dir",
269                                                          kuri, meta, &bo);
270   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
271   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
272   GNUNET_FS_uri_destroy (kuri);
273   GNUNET_CONTAINER_meta_data_destroy (meta);
274   GNUNET_assert (NULL != fidir);
275   start = GNUNET_TIME_absolute_get ();
276   publish =
277       GNUNET_FS_publish_start (fs, fidir, NULL, NULL, NULL,
278                                GNUNET_FS_PUBLISH_OPTION_NONE);
279   GNUNET_assert (publish != NULL);
280 }
281
282
283 int
284 main (int argc, char *argv[])
285 {
286   char *const argvx[] = {
287     "test-fs-publish",
288     "-c",
289     "test_fs_publish_data.conf",
290 #if VERBOSE
291     "-L", "DEBUG",
292 #endif
293     NULL
294   };
295   struct GNUNET_GETOPT_CommandLineOption options[] = {
296     GNUNET_GETOPT_OPTION_END
297   };
298
299   GNUNET_log_setup ("test_fs_publish",
300 #if VERBOSE
301                     "DEBUG",
302 #else
303                     "WARNING",
304 #endif
305                     NULL);
306   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
307                       "test-fs-publish", "nohelp", options, &run, NULL);
308   stop_arm (&p1);
309   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
310   if (fn1 != NULL)
311   {
312     GNUNET_DISK_directory_remove (fn1);
313     GNUNET_free (fn1);
314   }
315   if (fn2 != NULL)
316   {
317     GNUNET_DISK_directory_remove (fn2);
318     GNUNET_free (fn2);
319   }
320   return err;
321 }
322
323 /* end of test_fs_publish.c */