additional fs tests or fs test harnesses
[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, 15)
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   struct GNUNET_PeerIdentity id;   
56 #if START_ARM
57   pid_t arm_pid;
58 #endif
59 };
60
61 static struct PeerContext p1;
62
63 static struct GNUNET_TIME_Absolute start;
64
65 static struct GNUNET_SCHEDULER_Handle *sched;
66
67 static struct GNUNET_FS_Handle *fs;
68
69 static struct GNUNET_FS_PublishContext *publish;
70
71 static struct GNUNET_FS_PublishContext *publish;
72
73 static char *fn1;
74
75 static char *fn2;
76
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
98   switch (event->status)
99     {
100     case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
101       printf ("Publish complete,  %llu kbps.\n",
102               (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
103       GNUNET_SCHEDULER_add_continuation (sched,
104                                          GNUNET_NO,
105                                          &abort_publish_task,
106                                          NULL,
107                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
108       break;
109     case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
110       GNUNET_assert (publish == event->value.publish.sc);
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       fprintf (stderr,
121                "Error publishing file: %s\n",
122                event->value.publish.specifics.error.message);
123       GNUNET_SCHEDULER_add_continuation (sched,
124                                          GNUNET_NO,
125                                          &abort_publish_task,
126                                          NULL,
127                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
128       break;
129     case GNUNET_FS_STATUS_PUBLISH_START:
130       GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
131       GNUNET_assert (NULL == event->value.publish.pctx);
132       GNUNET_assert (FILESIZE == event->value.publish.size);
133       GNUNET_assert (0 == event->value.publish.completed);
134       GNUNET_assert (1 == event->value.publish.anonymity);
135       break;
136     case GNUNET_FS_STATUS_PUBLISH_STOPPED:
137       GNUNET_assert (publish == event->value.publish.sc);
138       GNUNET_SCHEDULER_add_continuation (sched,
139                                          GNUNET_NO,
140                                          &abort_publish_task,
141                                          NULL,
142                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
143       break;
144     default:
145       printf ("Unexpected event: %d\n", 
146               event->status);
147       break;
148     }
149   return NULL;
150 }
151
152
153 static void
154 setup_peer (struct PeerContext *p, const char *cfgname)
155 {
156   p->cfg = GNUNET_CONFIGURATION_create ();
157 #if START_ARM
158   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
159                                         "gnunet-service-arm",
160 #if VERBOSE
161                                         "-L", "DEBUG",
162 #endif
163                                         "-c", cfgname, NULL);
164   sleep (1);                    /* allow ARM to start */
165 #endif
166   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
167   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
168 }
169
170
171 static void
172 stop_arm (struct PeerContext *p)
173 {
174 #if START_ARM
175   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
176     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
177   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
178     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
179   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180               "ARM process %u stopped\n", p->arm_pid);
181 #endif
182   GNUNET_CONFIGURATION_destroy (p->cfg);
183 }
184
185
186 static void
187 run (void *cls,
188      struct GNUNET_SCHEDULER_Handle *s,
189      char *const *args,
190      const char *cfgfile,
191      const struct GNUNET_CONFIGURATION_Handle *cfg)
192 {
193   const char *keywords[] = {
194     "down_foo",
195     "down_bar",
196   };
197   char *buf;
198   struct GNUNET_CONTAINER_MetaData *meta;
199   struct GNUNET_FS_Uri *kuri;
200   struct GNUNET_FS_FileInformation *fi1;
201   struct GNUNET_FS_FileInformation *fi2;
202   struct GNUNET_FS_FileInformation *fidir;
203   size_t i;
204
205   sched = s;
206   setup_peer (&p1, "test_fs_publish_data.conf");
207   fs = GNUNET_FS_start (sched,
208                         cfg,
209                         "test-fs-publish",
210                         &progress_cb,
211                         NULL,
212                         GNUNET_FS_FLAGS_NONE,
213                         GNUNET_FS_OPTIONS_END);
214   GNUNET_assert (NULL != fs); 
215   fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
216   buf = GNUNET_malloc (FILESIZE);
217   for (i = 0; i < FILESIZE; i++)
218     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
219   GNUNET_assert (FILESIZE ==
220                  GNUNET_DISK_fn_write (fn1,
221                                        buf,
222                                        FILESIZE,
223                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
224   GNUNET_free (buf);
225
226   fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
227   buf = GNUNET_malloc (FILESIZE);
228   for (i = 0; i < FILESIZE; i++)
229     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
230   GNUNET_assert (FILESIZE ==
231                  GNUNET_DISK_fn_write (fn2,
232                                        buf,
233                                        FILESIZE,
234                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
235   GNUNET_free (buf);
236
237   meta = GNUNET_CONTAINER_meta_data_create ();
238   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
239   fi1 = GNUNET_FS_file_information_create_from_file ("publish-context1",
240                                                      fn1,
241                                                      kuri,
242                                                      meta,
243                                                      GNUNET_YES,
244                                                      1,
245                                                      42,
246                                                      GNUNET_TIME_relative_to_absolute (LIFETIME)); 
247   fi2 = GNUNET_FS_file_information_create_from_file ("publish-context2",
248                                                      fn2,
249                                                      kuri,
250                                                      meta,
251                                                      GNUNET_YES,
252                                                      1,
253                                                      42,
254                                                      GNUNET_TIME_relative_to_absolute (LIFETIME)); 
255   fidir = GNUNET_FS_file_information_create_empty_directory ("publish-context-dir",
256                                                              kuri,
257                                                              meta,
258                                                              1,
259                                                              42,
260                                                              GNUNET_TIME_relative_to_absolute (LIFETIME)); 
261   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
262   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
263   GNUNET_FS_uri_destroy (kuri);
264   GNUNET_CONTAINER_meta_data_destroy (meta);
265   GNUNET_assert (NULL != fidir);
266   start = GNUNET_TIME_absolute_get ();
267   publish = GNUNET_FS_publish_start (fs,
268                                      fidir,
269                                      NULL, NULL, NULL,
270                                      GNUNET_FS_PUBLISH_OPTION_NONE);
271   GNUNET_assert (publish != NULL);
272 }
273
274
275 int
276 main (int argc, char *argv[])
277 {
278   char *const argvx[] = { 
279     "test-fs-publish",
280     "-c",
281     "test_fs_publish_data.conf",
282 #if VERBOSE
283     "-L", "DEBUG",
284 #endif
285     NULL
286   };
287   struct GNUNET_GETOPT_CommandLineOption options[] = {
288     GNUNET_GETOPT_OPTION_END
289   };
290
291   GNUNET_log_setup ("test_fs_publish", 
292 #if VERBOSE
293                     "DEBUG",
294 #else
295                     "WARNING",
296 #endif
297                     NULL);
298   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
299                       argvx, "test-fs-publish",
300                       "nohelp", options, &run, NULL);
301   stop_arm (&p1);
302   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
303   GNUNET_DISK_directory_remove (fn1);
304   GNUNET_free_non_null (fn1);
305   GNUNET_DISK_directory_remove (fn2);
306   GNUNET_free_non_null (fn2);
307   return 0;
308 }
309
310 /* end of test_fs_publish.c */