2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file fs/test_fs_unindex_persistence.c
23 * @brief simple testcase for simple publish + unindex operation
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_fs_service.h"
32 * File-size we use for testing.
34 #define FILESIZE (1024 * 1024 * 2)
37 * How long until we give up on transmitting the message?
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
42 * How long should our test-content live?
44 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47 static struct GNUNET_TIME_Absolute start;
49 static struct GNUNET_FS_Handle *fs;
51 static struct GNUNET_FS_UnindexContext *unindex;
53 static struct GNUNET_FS_PublishContext *publish;
57 static const struct GNUNET_CONFIGURATION_Handle *cfg;
61 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
63 GNUNET_FS_publish_stop (publish);
69 abort_unindex_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
73 GNUNET_FS_unindex_stop (unindex);
78 GNUNET_DISK_directory_remove (fn);
86 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
90 restart_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 fs = GNUNET_FS_start (cfg, "test-fs-unindex-persistence", &progress_cb, NULL,
94 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
99 * Consider scheduling the restart-task.
100 * Only runs the restart task once per event
103 * @param ev type of the event to consider
106 consider_restart (int ev)
112 for (i = 0; i < off; i++)
116 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
117 &restart_fs_task, NULL);
122 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
124 switch (event->status)
126 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
129 (unsigned long long) event->value.publish.completed,
130 (unsigned long long) event->value.publish.size,
131 event->value.publish.specifics.progress.depth,
132 (unsigned long long) event->value.publish.specifics.
135 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
137 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
138 printf ("Publishing complete, %llu kbps.\n",
139 (unsigned long long) (FILESIZE * 1000000LL /
141 GNUNET_TIME_absolute_get_duration
142 (start).rel_value_us) / 1024));
143 start = GNUNET_TIME_absolute_get ();
144 unindex = GNUNET_FS_unindex_start (fs, fn, "unindex");
145 GNUNET_assert (unindex != NULL);
147 case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
148 printf ("Unindex complete, %llu kbps.\n",
149 (unsigned long long) (FILESIZE * 1000000LL /
151 GNUNET_TIME_absolute_get_duration
152 (start).rel_value_us) / 1024));
153 GNUNET_SCHEDULER_add_continuation (&abort_unindex_task, NULL,
154 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
156 case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
157 consider_restart (event->status);
158 GNUNET_assert (unindex == event->value.unindex.uc);
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "Unindex is progressing (%llu/%llu at level %u off %llu)...\n",
161 (unsigned long long) event->value.unindex.completed,
162 (unsigned long long) event->value.unindex.size,
163 event->value.unindex.specifics.progress.depth,
164 (unsigned long long) event->value.unindex.specifics.
167 case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
168 if (event->value.publish.pc == publish)
171 case GNUNET_FS_STATUS_PUBLISH_RESUME:
174 publish = event->value.publish.pc;
175 return "publish-context";
178 case GNUNET_FS_STATUS_UNINDEX_SUSPEND:
179 GNUNET_assert (event->value.unindex.uc == unindex);
182 case GNUNET_FS_STATUS_UNINDEX_RESUME:
183 GNUNET_assert (NULL == unindex);
184 unindex = event->value.unindex.uc;
186 case GNUNET_FS_STATUS_PUBLISH_ERROR:
187 FPRINTF (stderr, "Error publishing file: %s\n",
188 event->value.publish.specifics.error.message);
190 GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
191 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
193 case GNUNET_FS_STATUS_UNINDEX_ERROR:
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 "Error unindexing file: %s\n",
196 event->value.unindex.specifics.error.message);
197 GNUNET_SCHEDULER_add_continuation (&abort_unindex_task, NULL,
198 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
200 case GNUNET_FS_STATUS_PUBLISH_START:
201 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
202 GNUNET_assert (NULL == event->value.publish.pctx);
203 GNUNET_assert (FILESIZE == event->value.publish.size);
204 GNUNET_assert (0 == event->value.publish.completed);
205 GNUNET_assert (1 == event->value.publish.anonymity);
207 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
208 GNUNET_assert (publish == event->value.publish.pc);
209 GNUNET_assert (FILESIZE == event->value.publish.size);
210 GNUNET_assert (1 == event->value.publish.anonymity);
214 case GNUNET_FS_STATUS_UNINDEX_START:
215 consider_restart (event->status);
216 GNUNET_assert (unindex == NULL);
217 GNUNET_assert (0 == strcmp ("unindex", event->value.unindex.cctx));
218 GNUNET_assert (0 == strcmp (fn, event->value.unindex.filename));
219 GNUNET_assert (FILESIZE == event->value.unindex.size);
220 GNUNET_assert (0 == event->value.unindex.completed);
222 case GNUNET_FS_STATUS_UNINDEX_STOPPED:
223 GNUNET_assert (unindex == event->value.unindex.uc);
224 GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
225 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
228 printf ("Unexpected event: %d\n", event->status);
237 const struct GNUNET_CONFIGURATION_Handle *c,
238 struct GNUNET_TESTING_Peer *peer)
240 const char *keywords[] = {
245 struct GNUNET_CONTAINER_MetaData *meta;
246 struct GNUNET_FS_Uri *kuri;
247 struct GNUNET_FS_FileInformation *fi;
249 struct GNUNET_FS_BlockOptions bo;
252 fn = GNUNET_DISK_mktemp ("gnunet-unindex-test-dst");
253 fs = GNUNET_FS_start (cfg, "test-fs-unindex-persistence", &progress_cb, NULL,
254 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
255 GNUNET_assert (NULL != fs);
256 buf = GNUNET_malloc (FILESIZE);
257 for (i = 0; i < FILESIZE; i++)
258 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
259 GNUNET_assert (FILESIZE ==
260 GNUNET_DISK_fn_write (fn, buf, FILESIZE,
261 GNUNET_DISK_PERM_USER_READ |
262 GNUNET_DISK_PERM_USER_WRITE));
264 meta = GNUNET_CONTAINER_meta_data_create ();
265 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
266 bo.content_priority = 42;
267 bo.anonymity_level = 1;
268 bo.replication_level = 0;
269 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
270 fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn,
271 kuri, meta, GNUNET_YES,
273 GNUNET_FS_uri_destroy (kuri);
274 GNUNET_CONTAINER_meta_data_destroy (meta);
275 GNUNET_assert (NULL != fi);
276 start = GNUNET_TIME_absolute_get ();
278 GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
279 GNUNET_FS_PUBLISH_OPTION_NONE);
280 GNUNET_assert (publish != NULL);
285 main (int argc, char *argv[])
287 if (0 != GNUNET_TESTING_peer_run ("test-fs-unindex-persistence",
288 "test_fs_unindex_data.conf",
294 /* end of test_fs_unindex_persistence.c */