aboutsummaryrefslogtreecommitdiff
path: root/src/fs/test_fs_download.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/test_fs_download.c')
-rw-r--r--src/fs/test_fs_download.c368
1 files changed, 0 insertions, 368 deletions
diff --git a/src/fs/test_fs_download.c b/src/fs/test_fs_download.c
deleted file mode 100644
index 2fe0da77c..000000000
--- a/src/fs/test_fs_download.c
+++ /dev/null
@@ -1,368 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2008, 2009, 2011, 2012 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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file fs/test_fs_download.c
23 * @brief simple testcase for simple publish + download operation
24 * @author Christian Grothoff
25 */
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_fs_service.h"
30#include "gnunet_testing_lib.h"
31#include <gauger.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, 120)
42
43/**
44 * How long should our test-content live?
45 */
46#define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47
48static unsigned int anonymity_level;
49
50static int indexed;
51
52static struct GNUNET_TIME_Absolute start;
53
54static struct GNUNET_FS_Handle *fs;
55
56static struct GNUNET_FS_DownloadContext *download;
57
58static struct GNUNET_FS_PublishContext *publish;
59
60static struct GNUNET_SCHEDULER_Task *timeout_kill;
61
62static char *fn;
63
64static char *fn1;
65
66static int err;
67
68
69static void
70timeout_kill_task (void *cls)
71{
72 if (NULL != download)
73 {
74 GNUNET_FS_download_stop (download, GNUNET_YES);
75 download = NULL;
76 }
77 else if (NULL != publish)
78 {
79 GNUNET_FS_publish_stop (publish);
80 publish = NULL;
81 }
82 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
83 timeout_kill = NULL;
84 err = 1;
85}
86
87
88static void
89abort_publish_task (void *cls)
90{
91 if (NULL != publish)
92 {
93 GNUNET_FS_publish_stop (publish);
94 publish = NULL;
95 }
96}
97
98
99static void
100stop_fs_task (void *cls)
101{
102 GNUNET_FS_stop (fs);
103 fs = NULL;
104}
105
106
107static void
108abort_download_task (void *cls)
109{
110 uint64_t size;
111
112 if (NULL != download)
113 {
114 GNUNET_FS_download_stop (download, GNUNET_YES);
115 download = NULL;
116 }
117 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES,
118 GNUNET_NO));
119 GNUNET_assert (size == FILESIZE);
120 GNUNET_DISK_directory_remove (fn);
121 GNUNET_free (fn);
122 fn = NULL;
123 GNUNET_SCHEDULER_cancel (timeout_kill);
124 timeout_kill = NULL;
125}
126
127
128static void *
129progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
130{
131 switch (event->status)
132 {
133 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
136 (unsigned long long) event->value.publish.completed,
137 (unsigned long long) event->value.publish.size,
138 event->value.publish.specifics.progress.depth,
139 (unsigned long long) event->value.publish.specifics.
140 progress.offset);
141 break;
142
143 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
144 break;
145
146 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
147 fprintf (stdout,
148 "Publishing complete, %llu kb/s.\n",
149 (unsigned long long) (FILESIZE * 1000000LL
150 / (1
151 + GNUNET_TIME_absolute_get_duration
152 (start).rel_value_us) / 1024LL));
153 GAUGER ("FS",
154 (GNUNET_YES == indexed)
155 ? "Publishing speed (indexing)"
156 : "Publishing speed (insertion)",
157 (unsigned long long) (FILESIZE * 1000000LL
158 / (1
159 + GNUNET_TIME_absolute_get_duration
160 (start).rel_value_us) / 1024LL), "kb/s");
161 fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
162 start = GNUNET_TIME_absolute_get ();
163 download =
164 GNUNET_FS_download_start (fs,
165 event->value.publish.specifics.
166 completed.chk_uri, NULL, fn, NULL, 0,
167 FILESIZE, anonymity_level,
168 GNUNET_FS_DOWNLOAD_OPTION_NONE,
169 "download", NULL);
170 GNUNET_assert (download != NULL);
171 break;
172
173 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
174 fprintf (stdout,
175 "Download complete, %llu kb/s.\n",
176 (unsigned long long) (FILESIZE * 1000000LL
177 / (1
178 + GNUNET_TIME_absolute_get_duration
179 (start).rel_value_us) / 1024LL));
180 GAUGER ("FS",
181 (GNUNET_YES == indexed)
182 ? "Local download speed (indexed)"
183 : "Local download speed (inserted)",
184 (unsigned long long) (FILESIZE * 1000000LL
185 / (1
186 + GNUNET_TIME_absolute_get_duration
187 (start).rel_value_us) / 1024LL), "kb/s");
188 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
189 break;
190
191 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
192 GNUNET_assert (download == event->value.download.dc);
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "Download is progressing (%llu/%llu at level %u off %llu)...\n",
195 (unsigned long long) event->value.download.completed,
196 (unsigned long long) event->value.download.size,
197 event->value.download.specifics.progress.depth,
198 (unsigned long long) event->value.download.specifics.
199 progress.offset);
200 break;
201
202 case GNUNET_FS_STATUS_PUBLISH_ERROR:
203 fprintf (stderr, "Error publishing file: %s\n",
204 event->value.publish.specifics.error.message);
205 GNUNET_break (0);
206 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
207 GNUNET_SCHEDULER_shutdown ();
208 break;
209
210 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
211 fprintf (stderr, "Error downloading file: %s\n",
212 event->value.download.specifics.error.message);
213 GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
214 GNUNET_SCHEDULER_shutdown ();
215 break;
216
217 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
218 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
219 break;
220
221 case GNUNET_FS_STATUS_PUBLISH_START:
222 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
223 GNUNET_assert (NULL == event->value.publish.pctx);
224 GNUNET_assert (FILESIZE == event->value.publish.size);
225 GNUNET_assert (0 == event->value.publish.completed);
226 GNUNET_assert (1 == event->value.publish.anonymity);
227 break;
228
229 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
230 GNUNET_assert (publish == event->value.publish.pc);
231 GNUNET_assert (FILESIZE == event->value.publish.size);
232 GNUNET_assert (1 == event->value.publish.anonymity);
233 GNUNET_SCHEDULER_add_now (&stop_fs_task, NULL);
234 break;
235
236 case GNUNET_FS_STATUS_DOWNLOAD_START:
237 GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
238 GNUNET_assert (NULL == event->value.download.pctx);
239 GNUNET_assert (NULL != event->value.download.uri);
240 GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
241 GNUNET_assert (FILESIZE == event->value.download.size);
242 GNUNET_assert (0 == event->value.download.completed);
243 GNUNET_assert (1 == event->value.download.anonymity);
244 break;
245
246 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
247 GNUNET_assert (download == event->value.download.dc);
248 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
249 break;
250
251 default:
252 printf ("Unexpected event: %d\n", event->status);
253 break;
254 }
255 return NULL;
256}
257
258
259static void
260run (void *cls,
261 const struct GNUNET_CONFIGURATION_Handle *cfg,
262 struct GNUNET_TESTING_Peer *peer)
263{
264 const char *binary_name = cls;
265 const char *keywords[] = {
266 "down_foo",
267 "down_bar",
268 };
269 char *buf;
270 struct GNUNET_CONTAINER_MetaData *meta;
271 struct GNUNET_FS_Uri *kuri;
272 struct GNUNET_FS_FileInformation *fi;
273 size_t i;
274 struct GNUNET_FS_BlockOptions bo;
275
276 if (GNUNET_YES ==
277 GNUNET_CONFIGURATION_get_value_yesno (cfg,
278 "download-test",
279 "USE_STREAM"))
280 anonymity_level = 0;
281 else
282 anonymity_level = 1;
283 fs = GNUNET_FS_start (cfg, binary_name, &progress_cb, NULL,
284 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
285 GNUNET_assert (NULL != fs);
286 buf = GNUNET_malloc (FILESIZE);
287 for (i = 0; i < FILESIZE; i++)
288 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
289 meta = GNUNET_CONTAINER_meta_data_create ();
290 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
291 bo.content_priority = 42;
292 bo.anonymity_level = anonymity_level;
293 bo.replication_level = 0;
294 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
295
296 if (GNUNET_YES ==
297 GNUNET_CONFIGURATION_get_value_yesno (cfg,
298 "download-test",
299 "USE_INDEX"))
300 {
301 fn1 = GNUNET_DISK_mktemp ("gnunet-download-indexed-test");
302 (void) GNUNET_DISK_directory_remove (fn1);
303 GNUNET_assert (GNUNET_OK ==
304 GNUNET_DISK_fn_write (fn1,
305 buf,
306 FILESIZE,
307 GNUNET_DISK_PERM_USER_READ
308 | GNUNET_DISK_PERM_USER_WRITE));
309 GNUNET_free (buf);
310 fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context",
311 fn1,
312 kuri, meta, GNUNET_YES,
313 &bo);
314 indexed = GNUNET_YES;
315 }
316 else
317 {
318 fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
319 FILESIZE, buf, kuri, meta,
320 GNUNET_NO, &bo);
321 /* note: buf will be free'd as part of 'fi' now */
322 indexed = GNUNET_NO;
323 }
324 GNUNET_FS_uri_destroy (kuri);
325 GNUNET_CONTAINER_meta_data_destroy (meta);
326 GNUNET_assert (NULL != fi);
327 timeout_kill =
328 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
329 start = GNUNET_TIME_absolute_get ();
330 publish =
331 GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
332 GNUNET_FS_PUBLISH_OPTION_NONE);
333 GNUNET_assert (publish != NULL);
334}
335
336
337int
338main (int argc, char *argv[])
339{
340 const char *binary_name;
341 const char *config_name;
342
343 binary_name = "test-fs-download";
344 config_name = "test_fs_download_data.conf";
345 if (NULL != strstr (argv[0], "indexed"))
346 {
347 binary_name = "test-fs-download-indexed";
348 config_name = "test_fs_download_indexed.conf";
349 }
350 if (NULL != strstr (argv[0], "cadet"))
351 {
352 binary_name = "test-fs-download-cadet";
353 config_name = "test_fs_download_cadet.conf";
354 }
355 if (0 != GNUNET_TESTING_peer_run (binary_name,
356 config_name,
357 &run, (void *) binary_name))
358 return 1;
359 if (NULL != fn1)
360 {
361 unlink (fn1);
362 GNUNET_free (fn1);
363 }
364 return err;
365}
366
367
368/* end of test_fs_download.c */