2 This file is part of GNUnet.
3 Copyright (C) 2012 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_search_probes.c
18 * @brief simple testcase for publish + search operation with probes
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_testing_lib.h"
24 #include "gnunet_fs_service.h"
28 * File-size we use for testing.
33 * How long until we give up on transmitting the message?
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
38 * How long should our test-content live?
40 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
43 static struct GNUNET_TIME_Absolute start;
45 static struct GNUNET_FS_Handle *fs;
47 static struct GNUNET_FS_SearchContext *search;
49 static struct GNUNET_FS_PublishContext *publish;
51 static struct GNUNET_SCHEDULER_Task * timeout_task;
57 abort_error (void *cls)
64 GNUNET_FS_search_stop (search);
69 GNUNET_FS_publish_stop (publish);
77 abort_publish_task (void *cls)
81 GNUNET_FS_publish_stop (publish);
84 if (NULL != timeout_task)
86 GNUNET_SCHEDULER_cancel (timeout_task);
93 abort_search_task (void *cls)
96 GNUNET_FS_search_stop (search);
102 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
104 const char *keywords[] = {
107 struct GNUNET_FS_Uri *kuri;
109 switch (event->status)
111 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
114 (unsigned long long) event->value.publish.completed,
115 (unsigned long long) event->value.publish.size,
116 event->value.publish.specifics.progress.depth,
117 (unsigned long long) event->value.publish.specifics.
120 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
122 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
123 kuri = GNUNET_FS_uri_ksk_create_from_args (1, keywords);
124 start = GNUNET_TIME_absolute_get ();
126 GNUNET_FS_search_start (fs, kuri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
128 GNUNET_FS_uri_destroy (kuri);
129 GNUNET_assert (search != NULL);
131 case GNUNET_FS_STATUS_SEARCH_RESULT:
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Search complete.\n");
134 case GNUNET_FS_STATUS_PUBLISH_ERROR:
135 FPRINTF (stderr, "Error publishing file: %s\n",
136 event->value.publish.specifics.error.message);
138 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
140 case GNUNET_FS_STATUS_SEARCH_ERROR:
141 FPRINTF (stderr, "Error searching file: %s\n",
142 event->value.search.specifics.error.message);
143 GNUNET_SCHEDULER_add_now (&abort_search_task, NULL);
145 case GNUNET_FS_STATUS_PUBLISH_START:
146 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
147 GNUNET_assert (NULL == event->value.publish.pctx);
148 GNUNET_assert (FILESIZE == event->value.publish.size);
149 GNUNET_assert (0 == event->value.publish.completed);
150 GNUNET_assert (1 == event->value.publish.anonymity);
152 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
153 GNUNET_assert (publish == event->value.publish.pc);
154 GNUNET_assert (FILESIZE == event->value.publish.size);
155 GNUNET_assert (1 == event->value.publish.anonymity);
159 case GNUNET_FS_STATUS_SEARCH_UPDATE:
160 if ( (0 < event->value.search.specifics.update.availability_rank) &&
161 (0 < event->value.search.specifics.update.availability_certainty) )
162 GNUNET_SCHEDULER_add_now (&abort_search_task, NULL);
164 case GNUNET_FS_STATUS_SEARCH_START:
165 GNUNET_assert (search == NULL);
166 GNUNET_assert (0 == strcmp ("search", event->value.search.cctx));
167 GNUNET_assert (1 == event->value.search.anonymity);
169 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
171 case GNUNET_FS_STATUS_SEARCH_STOPPED:
172 GNUNET_assert (search == event->value.search.sc);
173 GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
177 "Unexpected event: %d\n",
187 const struct GNUNET_CONFIGURATION_Handle *cfg,
188 struct GNUNET_TESTING_Peer *peer)
190 const char *keywords[] = {
195 struct GNUNET_CONTAINER_MetaData *meta;
196 struct GNUNET_FS_Uri *kuri;
197 struct GNUNET_FS_BlockOptions bo;
198 struct GNUNET_FS_FileInformation *fi;
201 fs = GNUNET_FS_start (cfg, "test-fs-search", &progress_cb, NULL,
202 GNUNET_FS_FLAGS_DO_PROBES,
203 GNUNET_FS_OPTIONS_END);
204 GNUNET_assert (NULL != fs);
205 buf = GNUNET_malloc (FILESIZE);
206 for (i = 0; i < FILESIZE; i++)
207 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
208 meta = GNUNET_CONTAINER_meta_data_create ();
209 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
210 bo.content_priority = 42;
211 bo.anonymity_level = 1;
212 bo.replication_level = 0;
213 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
214 fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
215 FILESIZE, buf, kuri, meta,
217 GNUNET_FS_uri_destroy (kuri);
218 GNUNET_CONTAINER_meta_data_destroy (meta);
219 GNUNET_assert (NULL != fi);
220 start = GNUNET_TIME_absolute_get ();
222 GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
223 GNUNET_FS_PUBLISH_OPTION_NONE);
224 GNUNET_assert (publish != NULL);
225 timeout_task = GNUNET_SCHEDULER_add_delayed (LIFETIME,
231 main (int argc, char *argv[])
233 if (0 != GNUNET_TESTING_peer_run ("test-fs-search-probes",
234 "test_fs_search_data.conf",
240 /* end of test_fs_search_probes.c */