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 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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file set/test_set_api.c
23 * @brief testcase for set_api.c
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_set_service.h"
32 static struct GNUNET_PeerIdentity local_id;
34 static struct GNUNET_HashCode app_id;
36 static struct GNUNET_SET_Handle *set1;
38 static struct GNUNET_SET_Handle *set2;
40 static struct GNUNET_SET_ListenHandle *listen_handle;
42 static struct GNUNET_SET_OperationHandle *oh1;
44 static struct GNUNET_SET_OperationHandle *oh2;
46 static const struct GNUNET_CONFIGURATION_Handle *config;
48 static unsigned int iter_count;
52 static struct GNUNET_SCHEDULER_Task *tt;
56 result_cb_set1 (void *cls,
57 const struct GNUNET_SET_Element *element,
59 enum GNUNET_SET_Status status)
63 case GNUNET_SET_STATUS_OK:
64 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 1: got element\n");
67 case GNUNET_SET_STATUS_FAILURE:
70 fprintf (stderr, "set 1: received failure status!\n");
74 GNUNET_SCHEDULER_cancel (tt);
77 GNUNET_SCHEDULER_shutdown ();
80 case GNUNET_SET_STATUS_DONE:
81 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 1: done\n");
85 GNUNET_SET_destroy (set1);
90 GNUNET_SCHEDULER_cancel (tt);
92 GNUNET_SCHEDULER_shutdown ();
103 result_cb_set2 (void *cls,
104 const struct GNUNET_SET_Element *element,
106 enum GNUNET_SET_Status status)
110 case GNUNET_SET_STATUS_OK:
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 2: got element\n");
114 case GNUNET_SET_STATUS_FAILURE:
117 fprintf (stderr, "set 2: received failure status\n");
118 GNUNET_SCHEDULER_shutdown ();
122 case GNUNET_SET_STATUS_DONE:
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 2: done\n");
125 GNUNET_SET_destroy (set2);
129 GNUNET_SCHEDULER_cancel (tt);
131 GNUNET_SCHEDULER_shutdown ();
142 listen_cb (void *cls,
143 const struct GNUNET_PeerIdentity *other_peer,
144 const struct GNUNET_MessageHeader *context_msg,
145 struct GNUNET_SET_Request *request)
147 GNUNET_assert (NULL != context_msg);
148 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "listen cb called\n");
150 oh2 = GNUNET_SET_accept (request,
151 GNUNET_SET_RESULT_ADDED,
152 (struct GNUNET_SET_Option[]){ 0 },
155 GNUNET_SET_commit (oh2, set2);
160 * Start the set operation.
162 * @param cls closure, unused
167 struct GNUNET_MessageHeader context_msg;
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting reconciliation\n");
170 context_msg.size = htons (sizeof context_msg);
171 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
172 listen_handle = GNUNET_SET_listen (config,
173 GNUNET_SET_OPERATION_UNION,
177 oh1 = GNUNET_SET_prepare (&local_id,
180 GNUNET_SET_RESULT_ADDED,
181 (struct GNUNET_SET_Option[]){ 0 },
184 GNUNET_SET_commit (oh1, set1);
189 * Initialize the second set, continue
191 * @param cls closure, unused
194 init_set2 (void *cls)
196 struct GNUNET_SET_Element element;
198 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
200 element.element_type = 0;
201 element.data = "hello";
202 element.size = strlen (element.data);
203 GNUNET_SET_add_element (set2, &element, NULL, NULL);
204 element.data = "quux";
205 element.size = strlen (element.data);
206 GNUNET_SET_add_element (set2, &element, NULL, NULL);
207 element.data = "baz";
208 element.size = strlen (element.data);
209 GNUNET_SET_add_element (set2, &element, &start, NULL);
214 * Initialize the first set, continue.
219 struct GNUNET_SET_Element element;
221 element.element_type = 0;
222 element.data = "hello";
223 element.size = strlen (element.data);
224 GNUNET_SET_add_element (set1, &element, NULL, NULL);
225 element.data = "bar";
226 element.size = strlen (element.data);
227 GNUNET_SET_add_element (set1, &element, &init_set2, NULL);
228 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized set 1\n");
233 iter_cb (void *cls, const struct GNUNET_SET_Element *element)
235 struct GNUNET_SET_Handle *set = cls;
239 GNUNET_assert (3 == iter_count);
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "Iteration finished, destroying set %p\n",
243 GNUNET_SET_destroy (set);
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "iter: got element %u\n", iter_count);
255 struct GNUNET_SET_Element element;
256 struct GNUNET_SET_Handle *iter_set;
258 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Testing iteration over 3 elements on set %p\n",
262 element.element_type = 0;
264 element.data = "hello";
265 element.size = strlen (element.data);
266 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
267 element.data = "bar";
268 element.size = strlen (element.data);
269 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
270 element.data = "quux";
271 element.size = strlen (element.data);
272 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
273 GNUNET_SET_iterate (iter_set, &iter_cb, iter_set);
278 * Function run on timeout.
283 timeout_fail (void *cls)
286 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Testcase failed with timeout\n");
287 GNUNET_SCHEDULER_shutdown ();
293 * Function run on shutdown.
298 do_shutdown (void *cls)
302 GNUNET_SCHEDULER_cancel (tt);
307 GNUNET_SET_operation_cancel (oh1);
312 GNUNET_SET_operation_cancel (oh2);
317 GNUNET_SET_destroy (set1);
322 GNUNET_SET_destroy (set2);
325 if (NULL != listen_handle)
327 GNUNET_SET_listen_cancel (listen_handle);
328 listen_handle = NULL;
334 * Signature of the 'main' function for a (single-peer) testcase that
335 * is run using 'GNUNET_TESTING_peer_run'.
338 * @param cfg configuration of the peer that was started
339 * @param peer identity of the peer that was created
343 const struct GNUNET_CONFIGURATION_Handle *cfg,
344 struct GNUNET_TESTING_Peer *peer)
346 struct GNUNET_SET_OperationHandle *my_oh;
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running preparatory tests\n");
349 tt = GNUNET_SCHEDULER_add_delayed (
350 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
353 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
356 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_get_peer_identity (cfg, &local_id));
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "my id (from CRYPTO): %s\n",
359 GNUNET_i2s (&local_id));
360 GNUNET_TESTING_peer_get_identity (peer, &local_id);
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "my id (from TESTING): %s\n",
363 GNUNET_i2s (&local_id));
366 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
367 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Created sets %p and %p for union operation\n",
372 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
374 /* test if canceling an uncommited request works! */
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Launching and instantly stopping set operation\n");
377 my_oh = GNUNET_SET_prepare (&local_id,
380 GNUNET_SET_RESULT_ADDED,
381 (struct GNUNET_SET_Option[]){ 0 },
384 GNUNET_SET_operation_cancel (my_oh);
386 /* test the real set reconciliation */
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running real set-reconciliation\n");
393 main (int argc, char **argv)
395 GNUNET_log_setup ("test_set_api", "WARNING", NULL);
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Launching peer\n");
398 GNUNET_TESTING_peer_run ("test_set_api", "test_set.conf", &run, NULL))