2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2008, 2009 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.
16 * @file fs/test_fs_publish.c
17 * @brief simple testcase for publish operation (indexing, listing
18 * indexed, directory structure)
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_PublishContext *publish;
56 abort_publish_task (void *cls)
58 GNUNET_FS_publish_stop (publish);
60 GNUNET_DISK_directory_remove (fn1);
63 GNUNET_DISK_directory_remove (fn2);
70 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
75 switch (event->status)
77 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
78 ret = event->value.publish.cctx;
79 printf ("Publish complete, %llu kbps.\n",
80 (unsigned long long) (FILESIZE * 1000000LL /
82 GNUNET_TIME_absolute_get_duration
83 (start).rel_value_us) / 1024));
84 if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
85 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
87 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
88 ret = event->value.publish.cctx;
89 GNUNET_assert (publish == event->value.publish.pc);
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
92 (unsigned long long) event->value.publish.completed,
93 (unsigned long long) event->value.publish.size,
94 event->value.publish.specifics.progress.depth,
95 (unsigned long long) event->value.publish.specifics.
98 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
99 ret = event->value.publish.cctx;
101 case GNUNET_FS_STATUS_PUBLISH_ERROR:
102 ret = event->value.publish.cctx;
103 FPRINTF (stderr, "Error publishing file: %s\n",
104 event->value.publish.specifics.error.message);
106 if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
108 FPRINTF (stderr, "Scheduling abort task for error on `%s'\n",
109 (const char *) event->value.publish.cctx);
110 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
113 case GNUNET_FS_STATUS_PUBLISH_START:
114 ret = event->value.publish.cctx;
115 if (0 == strcmp ("publish-context1", event->value.publish.cctx))
118 strcmp ("publish-context-dir", event->value.publish.pctx));
119 GNUNET_assert (FILESIZE == event->value.publish.size);
120 GNUNET_assert (0 == event->value.publish.completed);
121 GNUNET_assert (1 == event->value.publish.anonymity);
123 else if (0 == strcmp ("publish-context2", event->value.publish.cctx))
126 strcmp ("publish-context-dir", event->value.publish.pctx));
127 GNUNET_assert (FILESIZE == event->value.publish.size);
128 GNUNET_assert (0 == event->value.publish.completed);
129 GNUNET_assert (2 == event->value.publish.anonymity);
131 else if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
133 GNUNET_assert (0 == event->value.publish.completed);
134 GNUNET_assert (3 == event->value.publish.anonymity);
139 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
140 if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
141 GNUNET_assert (publish == event->value.publish.pc);
144 printf ("Unexpected event: %d\n", event->status);
153 const struct GNUNET_CONFIGURATION_Handle *cfg,
154 struct GNUNET_TESTING_Peer *peer)
156 const char *keywords[] = {
161 struct GNUNET_CONTAINER_MetaData *meta;
162 struct GNUNET_FS_Uri *kuri;
163 struct GNUNET_FS_FileInformation *fi1;
164 struct GNUNET_FS_FileInformation *fi2;
165 struct GNUNET_FS_FileInformation *fidir;
167 struct GNUNET_FS_BlockOptions bo;
169 fs = GNUNET_FS_start (cfg, "test-fs-publish", &progress_cb, NULL,
170 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
171 GNUNET_assert (NULL != fs);
172 fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
173 buf = GNUNET_malloc (FILESIZE);
174 for (i = 0; i < FILESIZE; i++)
175 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
176 GNUNET_assert (FILESIZE ==
177 GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
178 GNUNET_DISK_PERM_USER_READ |
179 GNUNET_DISK_PERM_USER_WRITE));
182 fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
183 buf = GNUNET_malloc (FILESIZE);
184 for (i = 0; i < FILESIZE; i++)
185 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
186 GNUNET_assert (FILESIZE ==
187 GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
188 GNUNET_DISK_PERM_USER_READ |
189 GNUNET_DISK_PERM_USER_WRITE));
192 meta = GNUNET_CONTAINER_meta_data_create ();
193 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
194 bo.content_priority = 42;
195 bo.anonymity_level = 1;
196 bo.replication_level = 0;
197 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
200 GNUNET_FS_file_information_create_from_file (fs, "publish-context1", fn1,
201 kuri, meta, GNUNET_YES, &bo);
203 GNUNET_assert (NULL != fi1);
204 bo.anonymity_level = 2;
206 GNUNET_FS_file_information_create_from_file (fs, "publish-context2", fn2,
207 kuri, meta, GNUNET_YES, &bo);
208 GNUNET_assert (NULL != fi2);
209 bo.anonymity_level = 3;
211 GNUNET_FS_file_information_create_empty_directory (fs,
212 "publish-context-dir",
213 kuri, meta, &bo, NULL);
214 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
215 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
216 GNUNET_FS_uri_destroy (kuri);
217 GNUNET_CONTAINER_meta_data_destroy (meta);
218 GNUNET_assert (NULL != fidir);
219 start = GNUNET_TIME_absolute_get ();
221 GNUNET_FS_publish_start (fs, fidir, NULL, NULL, NULL,
222 GNUNET_FS_PUBLISH_OPTION_NONE);
223 GNUNET_assert (publish != NULL);
228 main (int argc, char *argv[])
230 if (0 != GNUNET_TESTING_peer_run ("test-fs-publish",
231 "test_fs_publish_data.conf",
237 /* end of test_fs_publish.c */