2 This file is part of GNUnet.
3 Copyright (C) 2012-2014 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_intersection_result_full.c
23 * @brief testcase for full result mode of the intersection set operation
24 * @author Christian Fuchs
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_set_service.h"
35 static struct GNUNET_PeerIdentity local_id;
37 static struct GNUNET_HashCode app_id;
39 static struct GNUNET_SET_Handle *set1;
41 static struct GNUNET_SET_Handle *set2;
43 static struct GNUNET_SET_ListenHandle *listen_handle;
45 static const struct GNUNET_CONFIGURATION_Handle *config;
47 static int iter_count;
49 static struct GNUNET_SCHEDULER_Task *tt;
51 static struct GNUNET_SET_OperationHandle *oh1;
53 static struct GNUNET_SET_OperationHandle *oh2;
57 result_cb_set1 (void *cls,
58 const struct GNUNET_SET_Element *element,
59 uint64_t current_size,
60 enum GNUNET_SET_Status status)
64 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
65 "Processing result set 1 (%d)\n",
69 case GNUNET_SET_STATUS_OK:
73 case GNUNET_SET_STATUS_FAILURE:
78 case GNUNET_SET_STATUS_DONE:
80 GNUNET_assert (1 == count);
81 GNUNET_SET_destroy (set1);
84 GNUNET_SCHEDULER_shutdown ();
94 result_cb_set2 (void *cls,
95 const struct GNUNET_SET_Element *element,
96 uint64_t current_size,
97 enum GNUNET_SET_Status status)
101 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
102 "Processing result set 2 (%d)\n",
106 case GNUNET_SET_STATUS_OK:
110 case GNUNET_SET_STATUS_FAILURE:
115 case GNUNET_SET_STATUS_DONE:
117 GNUNET_assert (1 == count);
118 GNUNET_SET_destroy (set2);
121 GNUNET_SCHEDULER_shutdown ();
131 listen_cb (void *cls,
132 const struct GNUNET_PeerIdentity *other_peer,
133 const struct GNUNET_MessageHeader *context_msg,
134 struct GNUNET_SET_Request *request)
136 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
137 "starting intersection by accepting and committing\n");
138 GNUNET_assert (NULL != context_msg);
139 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
140 oh2 = GNUNET_SET_accept (request,
141 GNUNET_SET_RESULT_FULL,
142 (struct GNUNET_SET_Option[]) { 0 },
145 GNUNET_SET_commit (oh2,
151 * Start the set operation.
153 * @param cls closure, unused
158 struct GNUNET_MessageHeader context_msg;
160 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
161 "starting listener\n");
162 context_msg.size = htons (sizeof context_msg);
163 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
164 listen_handle = GNUNET_SET_listen (config,
165 GNUNET_SET_OPERATION_INTERSECTION,
169 oh1 = GNUNET_SET_prepare (&local_id,
172 GNUNET_SET_RESULT_FULL,
173 (struct GNUNET_SET_Option[]) { 0 },
176 GNUNET_SET_commit (oh1,
182 * Initialize the second set, continue
184 * @param cls closure, unused
187 init_set2 (void *cls)
189 struct GNUNET_SET_Element element;
191 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
192 "initializing set 2\n");
193 element.element_type = 0;
194 element.data = "hello";
195 element.size = strlen (element.data);
196 GNUNET_SET_add_element (set2,
200 element.data = "quux";
201 element.size = strlen (element.data);
202 GNUNET_SET_add_element (set2,
206 element.data = "baz";
207 element.size = strlen (element.data);
208 GNUNET_SET_add_element (set2,
216 * Initialize the first set, continue.
221 struct GNUNET_SET_Element element;
223 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
224 "initializing set 1\n");
225 element.element_type = 0;
226 element.data = "hello";
227 element.size = strlen (element.data);
228 GNUNET_SET_add_element (set1,
232 element.data = "bar";
233 element.size = strlen (element.data);
234 GNUNET_SET_add_element (set1,
243 const struct GNUNET_SET_Element *element)
247 GNUNET_assert (iter_count == 3);
248 GNUNET_SET_destroy (cls);
259 struct GNUNET_SET_Element element;
260 struct GNUNET_SET_Handle *iter_set;
262 iter_set = GNUNET_SET_create (config,
263 GNUNET_SET_OPERATION_INTERSECTION);
264 element.element_type = 0;
265 element.data = "hello";
266 element.size = strlen (element.data);
267 GNUNET_SET_add_element (iter_set,
271 element.data = "bar";
272 element.size = strlen (element.data);
273 GNUNET_SET_add_element (iter_set,
277 element.data = "quux";
278 element.size = strlen (element.data);
279 GNUNET_SET_add_element (iter_set,
283 GNUNET_SET_iterate (iter_set,
290 * Function run on shutdown.
295 do_shutdown (void *cls)
299 GNUNET_SCHEDULER_cancel (tt);
304 GNUNET_SET_operation_cancel (oh1);
309 GNUNET_SET_operation_cancel (oh2);
314 GNUNET_SET_destroy (set1);
319 GNUNET_SET_destroy (set2);
322 if (NULL != listen_handle)
324 GNUNET_SET_listen_cancel (listen_handle);
325 listen_handle = NULL;
331 * Function run on timeout.
336 timeout_fail (void *cls)
339 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
340 "Testcase failed with timeout\n");
341 GNUNET_SCHEDULER_shutdown ();
347 * Signature of the 'main' function for a (single-peer) testcase that
348 * is run using 'GNUNET_TESTING_peer_run'.
351 * @param cfg configuration of the peer that was started
352 * @param peer identity of the peer that was created
356 const struct GNUNET_CONFIGURATION_Handle *cfg,
357 struct GNUNET_TESTING_Peer *peer)
360 GNUNET_TESTING_peer_get_identity (peer,
365 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
366 GNUNET_TIME_UNIT_SECONDS, 5),
369 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
372 set1 = GNUNET_SET_create (cfg,
373 GNUNET_SET_OPERATION_INTERSECTION);
374 set2 = GNUNET_SET_create (cfg,
375 GNUNET_SET_OPERATION_INTERSECTION);
376 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
379 /* test the real set reconciliation */
388 if (0 != GNUNET_TESTING_peer_run ("test_set_intersection_result_full",