2b53bc5a47927ba5949ca9414000f8a8b48e656c
[oweals/gnunet.git] / src / fs / test_fs_list_indexed.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_list_indexed.c
23  * @brief simple testcase for list_indexed operation (indexing, listing
24  *        indexed)
25  * @author Christian Grothoff
26  *
27  * TODO:
28  * - actually call list_indexed API!
29  */
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_testing_lib-new.h"
33 #include "gnunet_fs_service.h"
34
35 /**
36  * File-size we use for testing.
37  */
38 #define FILESIZE (1024 * 1024 * 2)
39
40 /**
41  * How long until we give up on transmitting the message?
42  */
43 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
44
45 /**
46  * How long should our test-content live?
47  */
48 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
49
50
51 static struct GNUNET_TIME_Absolute start;
52
53 static struct GNUNET_FS_Handle *fs;
54
55 static struct GNUNET_FS_PublishContext *publish;
56
57 static char *fn1;
58
59 static char *fn2;
60
61 static int err;
62
63
64 static void
65 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
66 {
67   GNUNET_FS_publish_stop (publish);
68   publish = NULL;
69   GNUNET_DISK_directory_remove (fn1);
70   GNUNET_free (fn1);
71   fn1 = NULL;
72   GNUNET_DISK_directory_remove (fn2);
73   GNUNET_free (fn2);
74   fn2 = NULL;
75 }
76
77
78 static void
79 list_indexed_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 {
81
82   GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
83                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
84 }
85
86
87 static void *
88 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
89 {
90   void *ret;
91
92   ret = NULL;
93   switch (event->status)
94   {
95   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
96     ret = event->value.publish.cctx;
97     printf ("Publish complete,  %llu kbps.\n",
98             (unsigned long long) (FILESIZE * 1000 /
99                                   (1 +
100                                    GNUNET_TIME_absolute_get_duration
101                                    (start).rel_value) / 1024));
102     if (0 == strcmp ("list_indexed-context-dir", event->value.publish.cctx))
103       GNUNET_SCHEDULER_add_continuation (&list_indexed_task, NULL,
104                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
105
106     break;
107   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
108     ret = event->value.publish.cctx;
109     GNUNET_assert (publish == event->value.publish.pc);
110     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111                 "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     break;
118   case GNUNET_FS_STATUS_PUBLISH_ERROR:
119     ret = event->value.publish.cctx;
120     FPRINTF (stderr, "Error publishing file: %s\n",
121              event->value.publish.specifics.error.message);
122     err = 1;
123     if (0 == strcmp ("list_indexed-context-dir", event->value.publish.cctx))
124       GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
125                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
126     break;
127   case GNUNET_FS_STATUS_PUBLISH_START:
128     ret = event->value.publish.cctx;
129     if (0 == strcmp ("list_indexed-context1", event->value.publish.cctx))
130     {
131       GNUNET_assert (0 ==
132                      strcmp ("list_indexed-context-dir",
133                              event->value.publish.pctx));
134       GNUNET_assert (FILESIZE == event->value.publish.size);
135       GNUNET_assert (0 == event->value.publish.completed);
136       GNUNET_assert (1 == event->value.publish.anonymity);
137     }
138     else if (0 == strcmp ("list_indexed-context2", event->value.publish.cctx))
139     {
140       GNUNET_assert (0 ==
141                      strcmp ("list_indexed-context-dir",
142                              event->value.publish.pctx));
143       GNUNET_assert (FILESIZE == event->value.publish.size);
144       GNUNET_assert (0 == event->value.publish.completed);
145       GNUNET_assert (2 == event->value.publish.anonymity);
146     }
147     else if (0 ==
148              strcmp ("list_indexed-context-dir", event->value.publish.cctx))
149     {
150       GNUNET_assert (0 == event->value.publish.completed);
151       GNUNET_assert (3 == event->value.publish.anonymity);
152     }
153     else
154       GNUNET_assert (0);
155     break;
156   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
157     if (0 == strcmp ("list_indexed-context-dir", event->value.publish.cctx))
158     {
159       GNUNET_assert (publish == event->value.publish.pc);
160       publish = NULL;
161     }
162     break;
163   default:
164     printf ("Unexpected event: %d\n", event->status);
165     break;
166   }
167   return ret;
168 }
169
170
171 static void
172 run (void *cls,
173      const struct GNUNET_CONFIGURATION_Handle *cfg)
174 {
175   const char *keywords[] = {
176     "down_foo",
177     "down_bar",
178   };
179   char *buf;
180   struct GNUNET_CONTAINER_MetaData *meta;
181   struct GNUNET_FS_Uri *kuri;
182   struct GNUNET_FS_FileInformation *fi1;
183   struct GNUNET_FS_FileInformation *fi2;
184   struct GNUNET_FS_FileInformation *fidir;
185   size_t i;
186   struct GNUNET_FS_BlockOptions bo;
187
188   fs = GNUNET_FS_start (cfg, "test-fs-list_indexed", &progress_cb, NULL,
189                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
190   GNUNET_assert (NULL != fs);
191   fn1 = GNUNET_DISK_mktemp ("gnunet-list_indexed-test-dst");
192   buf = GNUNET_malloc (FILESIZE);
193   for (i = 0; i < FILESIZE; i++)
194     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
195   GNUNET_assert (FILESIZE ==
196                  GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
197                                        GNUNET_DISK_PERM_USER_READ |
198                                        GNUNET_DISK_PERM_USER_WRITE));
199   GNUNET_free (buf);
200
201   fn2 = GNUNET_DISK_mktemp ("gnunet-list_indexed-test-dst");
202   buf = GNUNET_malloc (FILESIZE);
203   for (i = 0; i < FILESIZE; i++)
204     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
205   GNUNET_assert (FILESIZE ==
206                  GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
207                                        GNUNET_DISK_PERM_USER_READ |
208                                        GNUNET_DISK_PERM_USER_WRITE));
209   GNUNET_free (buf);
210
211   meta = GNUNET_CONTAINER_meta_data_create ();
212   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
213   bo.content_priority = 42;
214   bo.anonymity_level = 1;
215   bo.replication_level = 0;
216   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
217   fi1 =
218       GNUNET_FS_file_information_create_from_file (fs, "list_indexed-context1",
219                                                    fn1, kuri, meta, GNUNET_YES,
220                                                    &bo);
221   GNUNET_assert (NULL != fi1);
222   bo.anonymity_level = 2;
223   fi2 =
224       GNUNET_FS_file_information_create_from_file (fs, "list_indexed-context2",
225                                                    fn2, kuri, meta, GNUNET_YES,
226                                                    &bo);
227   GNUNET_assert (NULL != fi2);
228   bo.anonymity_level = 3;
229   fidir =
230       GNUNET_FS_file_information_create_empty_directory (fs,
231                                                          "list_indexed-context-dir",
232                                                          kuri, meta, &bo, NULL);
233   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
234   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
235   GNUNET_FS_uri_destroy (kuri);
236   GNUNET_CONTAINER_meta_data_destroy (meta);
237   GNUNET_assert (NULL != fidir);
238   start = GNUNET_TIME_absolute_get ();
239   publish =
240       GNUNET_FS_publish_start (fs, fidir, NULL, NULL, NULL,
241                                GNUNET_FS_PUBLISH_OPTION_NONE);
242   GNUNET_assert (publish != NULL);
243 }
244
245
246 int
247 main (int argc, char *argv[])
248 {
249   if (0 != GNUNET_TESTING_peer_run ("test-fs-list-indexed",
250                                     "test_fs_list_indexed_data.conf",
251                                     &run, NULL))
252     return 1;
253   return 0;
254 }
255
256 /* end of test_fs_list_indexed.c */