-fixing 2352
[oweals/gnunet.git] / src / fs / test_fs_unindex.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_unindex.c
23  * @brief simple testcase for simple publish + unindex operation
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_fs_service.h"
31
32 #define VERBOSE GNUNET_NO
33
34 #define START_ARM GNUNET_YES
35
36 /**
37  * File-size we use for testing.
38  */
39 #define FILESIZE (1024 * 1024 * 2)
40
41 /**
42  * How long until we give up on transmitting the message?
43  */
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
45
46 /**
47  * How long should our test-content live?
48  */
49 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
50
51 struct PeerContext
52 {
53   struct GNUNET_CONFIGURATION_Handle *cfg;
54 #if START_ARM
55   struct GNUNET_OS_Process *arm_proc;
56 #endif
57 };
58
59 static struct PeerContext p1;
60
61 static struct GNUNET_TIME_Absolute start;
62
63 static struct GNUNET_FS_Handle *fs;
64
65 static struct GNUNET_FS_UnindexContext *unindex;
66
67 static struct GNUNET_FS_PublishContext *publish;
68
69 static char *fn;
70
71
72 static void
73 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
74 {
75   GNUNET_FS_publish_stop (publish);
76   publish = NULL;
77 }
78
79
80 static void
81 abort_unindex_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 {
83   GNUNET_FS_unindex_stop (unindex);
84   unindex = NULL;
85   GNUNET_DISK_directory_remove (fn);
86   GNUNET_free (fn);
87   fn = NULL;
88 }
89
90
91 static void *
92 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
93 {
94
95   switch (event->status)
96   {
97   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
98 #if VERBOSE
99     printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
100             (unsigned long long) event->value.publish.completed,
101             (unsigned long long) event->value.publish.size,
102             event->value.publish.specifics.progress.depth,
103             (unsigned long long) event->value.publish.specifics.
104             progress.offset);
105 #endif
106     break;
107   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
108     printf ("Publishing complete, %llu kbps.\n",
109             (unsigned long long) (FILESIZE * 1000 /
110                                   (1 +
111                                    GNUNET_TIME_absolute_get_duration
112                                    (start).rel_value) / 1024));
113     start = GNUNET_TIME_absolute_get ();
114     unindex = GNUNET_FS_unindex_start (fs, fn, "unindex");
115     GNUNET_assert (unindex != NULL);
116     break;
117   case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
118     printf ("Unindex complete,  %llu kbps.\n",
119             (unsigned long long) (FILESIZE * 1000 /
120                                   (1 +
121                                    GNUNET_TIME_absolute_get_duration
122                                    (start).rel_value) / 1024));
123     GNUNET_SCHEDULER_add_continuation (&abort_unindex_task, NULL,
124                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
125     break;
126   case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
127     GNUNET_assert (unindex == event->value.unindex.uc);
128 #if VERBOSE
129     printf ("Unindex is progressing (%llu/%llu at level %u off %llu)...\n",
130             (unsigned long long) event->value.unindex.completed,
131             (unsigned long long) event->value.unindex.size,
132             event->value.unindex.specifics.progress.depth,
133             (unsigned long long) event->value.unindex.specifics.
134             progress.offset);
135 #endif
136     break;
137   case GNUNET_FS_STATUS_PUBLISH_ERROR:
138     FPRINTF (stderr, "Error publishing file: %s\n",
139              event->value.publish.specifics.error.message);
140     GNUNET_break (0);
141     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
142                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
143     break;
144   case GNUNET_FS_STATUS_UNINDEX_ERROR:
145     FPRINTF (stderr, "Error unindexing file: %s\n",
146              event->value.unindex.specifics.error.message);
147     GNUNET_SCHEDULER_add_continuation (&abort_unindex_task, NULL,
148                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
149     break;
150   case GNUNET_FS_STATUS_PUBLISH_START:
151     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
152     GNUNET_assert (NULL == event->value.publish.pctx);
153     GNUNET_assert (FILESIZE == event->value.publish.size);
154     GNUNET_assert (0 == event->value.publish.completed);
155     GNUNET_assert (1 == event->value.publish.anonymity);
156     break;
157   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
158     GNUNET_assert (publish == event->value.publish.pc);
159     GNUNET_assert (FILESIZE == event->value.publish.size);
160     GNUNET_assert (1 == event->value.publish.anonymity);
161     GNUNET_FS_stop (fs);
162     fs = NULL;
163     break;
164   case GNUNET_FS_STATUS_UNINDEX_START:
165     GNUNET_assert (unindex == NULL);
166     GNUNET_assert (0 == strcmp ("unindex", event->value.unindex.cctx));
167     GNUNET_assert (0 == strcmp (fn, event->value.unindex.filename));
168     GNUNET_assert (FILESIZE == event->value.unindex.size);
169     GNUNET_assert (0 == event->value.unindex.completed);
170     break;
171   case GNUNET_FS_STATUS_UNINDEX_STOPPED:
172     GNUNET_assert (unindex == event->value.unindex.uc);
173     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
174                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
175     break;
176   default:
177     printf ("Unexpected event: %d\n", event->status);
178     break;
179   }
180   return NULL;
181 }
182
183
184 static void
185 setup_peer (struct PeerContext *p, const char *cfgname)
186 {
187   p->cfg = GNUNET_CONFIGURATION_create ();
188 #if START_ARM
189   p->arm_proc =
190       GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
191                                "gnunet-service-arm",
192 #if VERBOSE
193                                "-L", "DEBUG",
194 #endif
195                                "-c", cfgname, NULL);
196 #endif
197   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
198 }
199
200
201 static void
202 stop_arm (struct PeerContext *p)
203 {
204 #if START_ARM
205   if (NULL != p->arm_proc)
206   {
207     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
208       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
209     if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
210       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
211     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
212                 GNUNET_OS_process_get_pid (p->arm_proc));
213     GNUNET_OS_process_destroy (p->arm_proc);
214     p->arm_proc = NULL;
215   }
216 #endif
217   GNUNET_CONFIGURATION_destroy (p->cfg);
218 }
219
220
221 static void
222 run (void *cls, char *const *args, const char *cfgfile,
223      const struct GNUNET_CONFIGURATION_Handle *cfg)
224 {
225   const char *keywords[] = {
226     "down_foo",
227     "down_bar",
228   };
229   char *buf;
230   struct GNUNET_CONTAINER_MetaData *meta;
231   struct GNUNET_FS_Uri *kuri;
232   struct GNUNET_FS_FileInformation *fi;
233   size_t i;
234   struct GNUNET_FS_BlockOptions bo;
235
236   setup_peer (&p1, "test_fs_unindex_data.conf");
237   fn = GNUNET_DISK_mktemp ("gnunet-unindex-test-dst");
238   fs = GNUNET_FS_start (cfg, "test-fs-unindex", &progress_cb, NULL,
239                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
240   GNUNET_assert (NULL != fs);
241   buf = GNUNET_malloc (FILESIZE);
242   for (i = 0; i < FILESIZE; i++)
243     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
244   GNUNET_assert (FILESIZE ==
245                  GNUNET_DISK_fn_write (fn, buf, FILESIZE,
246                                        GNUNET_DISK_PERM_USER_READ |
247                                        GNUNET_DISK_PERM_USER_WRITE));
248   GNUNET_free (buf);
249   meta = GNUNET_CONTAINER_meta_data_create ();
250   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
251   bo.content_priority = 42;
252   bo.anonymity_level = 1;
253   bo.replication_level = 0;
254   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
255   fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn,
256                                                     kuri, meta, GNUNET_YES,
257                                                     &bo);
258   GNUNET_FS_uri_destroy (kuri);
259   GNUNET_CONTAINER_meta_data_destroy (meta);
260   GNUNET_assert (NULL != fi);
261   start = GNUNET_TIME_absolute_get ();
262   publish =
263       GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
264                                GNUNET_FS_PUBLISH_OPTION_NONE);
265   GNUNET_assert (publish != NULL);
266 }
267
268
269 int
270 main (int argc, char *argv[])
271 {
272   char *const argvx[] = {
273     "test-fs-unindex",
274     "-c",
275     "test_fs_unindex_data.conf",
276 #if VERBOSE
277     "-L", "DEBUG",
278 #endif
279     NULL
280   };
281   struct GNUNET_GETOPT_CommandLineOption options[] = {
282     GNUNET_GETOPT_OPTION_END
283   };
284
285   GNUNET_log_setup ("test_fs_unindex",
286 #if VERBOSE
287                     "DEBUG",
288 #else
289                     "WARNING",
290 #endif
291                     NULL);
292   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
293                       "test-fs-unindex", "nohelp", options, &run, NULL);
294   stop_arm (&p1);
295   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-unindex/");
296   if (NULL != fn)
297   {
298     GNUNET_DISK_directory_remove (fn);
299     GNUNET_free (fn);
300   }
301   return 0;
302 }
303
304 /* end of test_fs_unindex.c */