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