2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU 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.
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.
17 * @file fs/test_fs_unindex_persistence.c
18 * @brief simple testcase for simple publish + unindex operation
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_testing_lib.h"
24 #include "gnunet_fs_service.h"
27 * File-size we use for testing.
29 #define FILESIZE (1024 * 1024 * 2)
32 * How long until we give up on transmitting the message?
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
37 * How long should our test-content live?
39 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
42 static struct GNUNET_TIME_Absolute start;
44 static struct GNUNET_FS_Handle *fs;
46 static struct GNUNET_FS_UnindexContext *unindex;
48 static struct GNUNET_FS_PublishContext *publish;
52 static const struct GNUNET_CONFIGURATION_Handle *cfg;
56 abort_publish_task (void *cls)
58 GNUNET_FS_publish_stop (publish);
64 abort_unindex_task (void *cls)
68 GNUNET_FS_unindex_stop (unindex);
73 GNUNET_DISK_directory_remove (fn);
81 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
85 restart_fs_task (void *cls)
88 fs = GNUNET_FS_start (cfg, "test-fs-unindex-persistence", &progress_cb, NULL,
89 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
94 * Consider scheduling the restart-task.
95 * Only runs the restart task once per event
98 * @param ev type of the event to consider
101 consider_restart (int ev)
107 for (i = 0; i < off; i++)
111 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
112 &restart_fs_task, NULL);
117 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
119 switch (event->status)
121 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
124 (unsigned long long) event->value.publish.completed,
125 (unsigned long long) event->value.publish.size,
126 event->value.publish.specifics.progress.depth,
127 (unsigned long long) event->value.publish.specifics.
130 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
132 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
133 printf ("Publishing complete, %llu kbps.\n",
134 (unsigned long long) (FILESIZE * 1000000LL /
136 GNUNET_TIME_absolute_get_duration
137 (start).rel_value_us) / 1024));
138 start = GNUNET_TIME_absolute_get ();
139 unindex = GNUNET_FS_unindex_start (fs, fn, "unindex");
140 GNUNET_assert (unindex != NULL);
142 case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
143 printf ("Unindex complete, %llu kbps.\n",
144 (unsigned long long) (FILESIZE * 1000000LL /
146 GNUNET_TIME_absolute_get_duration
147 (start).rel_value_us) / 1024));
148 GNUNET_SCHEDULER_add_now (&abort_unindex_task, NULL);
150 case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
151 consider_restart (event->status);
152 GNUNET_assert (unindex == event->value.unindex.uc);
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154 "Unindex is progressing (%llu/%llu at level %u off %llu)...\n",
155 (unsigned long long) event->value.unindex.completed,
156 (unsigned long long) event->value.unindex.size,
157 event->value.unindex.specifics.progress.depth,
158 (unsigned long long) event->value.unindex.specifics.
161 case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
162 if (event->value.publish.pc == publish)
165 case GNUNET_FS_STATUS_PUBLISH_RESUME:
168 publish = event->value.publish.pc;
169 return "publish-context";
172 case GNUNET_FS_STATUS_UNINDEX_SUSPEND:
173 GNUNET_assert (event->value.unindex.uc == unindex);
176 case GNUNET_FS_STATUS_UNINDEX_RESUME:
177 GNUNET_assert (NULL == unindex);
178 unindex = event->value.unindex.uc;
180 case GNUNET_FS_STATUS_PUBLISH_ERROR:
181 FPRINTF (stderr, "Error publishing file: %s\n",
182 event->value.publish.specifics.error.message);
184 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
186 case GNUNET_FS_STATUS_UNINDEX_ERROR:
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 "Error unindexing file: %s\n",
189 event->value.unindex.specifics.error.message);
190 GNUNET_SCHEDULER_add_now (&abort_unindex_task, NULL);
192 case GNUNET_FS_STATUS_PUBLISH_START:
193 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
194 GNUNET_assert (NULL == event->value.publish.pctx);
195 GNUNET_assert (FILESIZE == event->value.publish.size);
196 GNUNET_assert (0 == event->value.publish.completed);
197 GNUNET_assert (1 == event->value.publish.anonymity);
199 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
200 GNUNET_assert (publish == event->value.publish.pc);
201 GNUNET_assert (FILESIZE == event->value.publish.size);
202 GNUNET_assert (1 == event->value.publish.anonymity);
206 case GNUNET_FS_STATUS_UNINDEX_START:
207 consider_restart (event->status);
208 GNUNET_assert (unindex == NULL);
209 GNUNET_assert (0 == strcmp ("unindex", event->value.unindex.cctx));
210 GNUNET_assert (0 == strcmp (fn, event->value.unindex.filename));
211 GNUNET_assert (FILESIZE == event->value.unindex.size);
212 GNUNET_assert (0 == event->value.unindex.completed);
214 case GNUNET_FS_STATUS_UNINDEX_STOPPED:
215 GNUNET_assert (unindex == event->value.unindex.uc);
216 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
219 printf ("Unexpected event: %d\n", event->status);
228 const struct GNUNET_CONFIGURATION_Handle *c,
229 struct GNUNET_TESTING_Peer *peer)
231 const char *keywords[] = {
236 struct GNUNET_CONTAINER_MetaData *meta;
237 struct GNUNET_FS_Uri *kuri;
238 struct GNUNET_FS_FileInformation *fi;
240 struct GNUNET_FS_BlockOptions bo;
243 fn = GNUNET_DISK_mktemp ("gnunet-unindex-test-dst");
244 fs = GNUNET_FS_start (cfg, "test-fs-unindex-persistence", &progress_cb, NULL,
245 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
246 GNUNET_assert (NULL != fs);
247 buf = GNUNET_malloc (FILESIZE);
248 for (i = 0; i < FILESIZE; i++)
249 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
250 GNUNET_assert (FILESIZE ==
251 GNUNET_DISK_fn_write (fn, buf, FILESIZE,
252 GNUNET_DISK_PERM_USER_READ |
253 GNUNET_DISK_PERM_USER_WRITE));
255 meta = GNUNET_CONTAINER_meta_data_create ();
256 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
257 bo.content_priority = 42;
258 bo.anonymity_level = 1;
259 bo.replication_level = 0;
260 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
261 fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn,
262 kuri, meta, GNUNET_YES,
264 GNUNET_FS_uri_destroy (kuri);
265 GNUNET_CONTAINER_meta_data_destroy (meta);
266 GNUNET_assert (NULL != fi);
267 start = GNUNET_TIME_absolute_get ();
269 GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
270 GNUNET_FS_PUBLISH_OPTION_NONE);
271 GNUNET_assert (publish != NULL);
276 main (int argc, char *argv[])
278 if (0 != GNUNET_TESTING_peer_run ("test-fs-unindex-persistence",
279 "test_fs_unindex_data.conf",
285 /* end of test_fs_unindex_persistence.c */