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 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.
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.
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/>.
19 * @file fs/test_fs_publish_persistence.c
20 * @brief simple testcase for persistence of simple publish operation
21 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_fs_service.h"
30 * File-size we use for testing.
32 #define FILESIZE (1024 * 1024 * 2)
35 * How long until we give up on transmitting the message?
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
40 * How long should our test-content live?
42 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
45 static struct GNUNET_TIME_Absolute start;
47 static struct GNUNET_FS_Handle *fs;
49 static const struct GNUNET_CONFIGURATION_Handle *cfg;
51 static struct GNUNET_FS_PublishContext *publish;
53 static struct GNUNET_FS_PublishContext *publish;
61 static struct GNUNET_SCHEDULER_Task *rtask;
65 abort_publish_task (void *cls)
67 GNUNET_FS_publish_stop (publish);
69 GNUNET_DISK_directory_remove (fn1);
72 GNUNET_DISK_directory_remove (fn2);
79 GNUNET_SCHEDULER_cancel (rtask);
86 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
90 restart_fs_task (void *cls)
94 fs = GNUNET_FS_start (cfg, "test-fs-publish-persistence",
96 GNUNET_FS_FLAGS_PERSISTENCE,
97 GNUNET_FS_OPTIONS_END);
102 * Consider scheduling the restart-task.
103 * Only runs the restart task once per event
106 * @param ev type of the event to consider
109 consider_restart (int ev)
115 for (i = 0; i < off; i++)
120 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
121 &restart_fs_task, NULL);
126 progress_cb (void *cls,
127 const struct GNUNET_FS_ProgressInfo *event)
132 switch (event->status)
134 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
135 ret = event->value.publish.cctx;
136 printf ("Publish complete, %llu kbps.\n",
137 (unsigned long long) (FILESIZE * 1000000LL /
139 GNUNET_TIME_absolute_get_duration
140 (start).rel_value_us) / 1024));
141 if ( (NULL != event->value.publish.cctx) &&
142 (0 == strcmp ("publish-context-dir", event->value.publish.cctx)) )
143 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
145 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
146 ret = event->value.publish.cctx;
148 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
149 consider_restart (event->status);
150 ret = event->value.publish.cctx;
151 GNUNET_assert (publish == event->value.publish.pc);
153 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
154 (unsigned long long) event->value.publish.completed,
155 (unsigned long long) event->value.publish.size,
156 event->value.publish.specifics.progress.depth,
157 (unsigned long long) event->value.publish.specifics.
161 case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
162 if (event->value.publish.pc == publish)
165 case GNUNET_FS_STATUS_PUBLISH_RESUME:
168 GNUNET_assert (GNUNET_YES ==
169 GNUNET_FS_file_information_is_directory (event->
172 publish = event->value.publish.pc;
173 return "publish-context-dir";
176 case GNUNET_FS_STATUS_PUBLISH_ERROR:
177 ret = event->value.publish.cctx;
178 FPRINTF (stderr, "Error publishing file: %s\n",
179 event->value.publish.specifics.error.message);
181 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
183 case GNUNET_FS_STATUS_PUBLISH_START:
184 consider_restart (event->status);
185 publish = event->value.publish.pc;
186 ret = event->value.publish.cctx;
187 if (0 == strcmp ("publish-context1", event->value.publish.cctx))
190 strcmp ("publish-context-dir", event->value.publish.pctx));
191 GNUNET_assert (FILESIZE == event->value.publish.size);
192 GNUNET_assert (0 == event->value.publish.completed);
193 GNUNET_assert (1 == event->value.publish.anonymity);
195 else if (0 == strcmp ("publish-context2", event->value.publish.cctx))
198 strcmp ("publish-context-dir", event->value.publish.pctx));
199 GNUNET_assert (FILESIZE == event->value.publish.size);
200 GNUNET_assert (0 == event->value.publish.completed);
201 GNUNET_assert (2 == event->value.publish.anonymity);
203 else if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
205 GNUNET_assert (0 == event->value.publish.completed);
206 GNUNET_assert (3 == event->value.publish.anonymity);
211 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
212 consider_restart (event->status);
213 if ( (NULL != event->value.publish.cctx) &&
214 (0 == strcmp ("publish-context-dir", event->value.publish.cctx)) )
215 GNUNET_assert (publish == event->value.publish.pc);
218 printf ("Unexpected event: %d\n", event->status);
227 const struct GNUNET_CONFIGURATION_Handle *c,
228 struct GNUNET_TESTING_Peer *peer)
230 const char *keywords[] = {
235 struct GNUNET_CONTAINER_MetaData *meta;
236 struct GNUNET_FS_Uri *kuri;
237 struct GNUNET_FS_FileInformation *fi1;
238 struct GNUNET_FS_FileInformation *fi2;
239 struct GNUNET_FS_FileInformation *fidir;
241 struct GNUNET_FS_BlockOptions bo;
244 fs = GNUNET_FS_start (cfg, "test-fs-publish-persistence", &progress_cb, NULL,
245 GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
246 GNUNET_assert (NULL != fs);
247 fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
248 buf = GNUNET_malloc (FILESIZE);
249 for (i = 0; i < FILESIZE; i++)
250 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
251 GNUNET_assert (FILESIZE ==
252 GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
253 GNUNET_DISK_PERM_USER_READ |
254 GNUNET_DISK_PERM_USER_WRITE));
257 fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
258 buf = GNUNET_malloc (FILESIZE);
259 for (i = 0; i < FILESIZE; i++)
260 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
261 GNUNET_assert (FILESIZE ==
262 GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
263 GNUNET_DISK_PERM_USER_READ |
264 GNUNET_DISK_PERM_USER_WRITE));
267 meta = GNUNET_CONTAINER_meta_data_create ();
268 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
269 bo.content_priority = 42;
270 bo.anonymity_level = 1;
271 bo.replication_level = 0;
272 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
274 GNUNET_FS_file_information_create_from_file (fs, "publish-context1", fn1,
275 kuri, meta, GNUNET_YES, &bo);
276 GNUNET_assert (NULL != fi1);
277 bo.anonymity_level = 2;
279 GNUNET_FS_file_information_create_from_file (fs, "publish-context2", fn2,
280 kuri, meta, GNUNET_YES, &bo);
281 GNUNET_assert (NULL != fi2);
282 bo.anonymity_level = 3;
284 GNUNET_FS_file_information_create_empty_directory (fs,
285 "publish-context-dir",
286 kuri, meta, &bo, NULL);
287 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
288 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
289 GNUNET_FS_uri_destroy (kuri);
290 GNUNET_CONTAINER_meta_data_destroy (meta);
291 GNUNET_assert (NULL != fidir);
292 start = GNUNET_TIME_absolute_get ();
293 GNUNET_FS_publish_start (fs, fidir, NULL, NULL, NULL,
294 GNUNET_FS_PUBLISH_OPTION_NONE);
295 GNUNET_assert (publish != NULL);
300 main (int argc, char *argv[])
302 if (0 != GNUNET_TESTING_peer_run ("test-fs-publish-persistence",
303 "test_fs_publish_data.conf",
309 /* end of test_fs_publish_persistence.c */