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:
72 case GNUNET_SET_STATUS_FAILURE:
76 case GNUNET_SET_STATUS_DONE:
78 GNUNET_assert (1 == count);
79 GNUNET_SET_destroy (set1);
82 GNUNET_SCHEDULER_shutdown ();
91 result_cb_set2 (void *cls,
92 const struct GNUNET_SET_Element *element,
93 uint64_t current_size,
94 enum GNUNET_SET_Status status)
98 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
99 "Processing result set 2 (%d)\n",
103 case GNUNET_SET_STATUS_OK:
106 case GNUNET_SET_STATUS_FAILURE:
110 case GNUNET_SET_STATUS_DONE:
112 GNUNET_assert (1 == count);
113 GNUNET_SET_destroy (set2);
116 GNUNET_SCHEDULER_shutdown ();
125 listen_cb (void *cls,
126 const struct GNUNET_PeerIdentity *other_peer,
127 const struct GNUNET_MessageHeader *context_msg,
128 struct GNUNET_SET_Request *request)
130 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
131 "starting intersection by accepting and committing\n");
132 GNUNET_assert (NULL != context_msg);
133 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
134 oh2 = GNUNET_SET_accept (request,
135 GNUNET_SET_RESULT_FULL,
136 (struct GNUNET_SET_Option[]) { 0 },
139 GNUNET_SET_commit (oh2,
145 * Start the set operation.
147 * @param cls closure, unused
152 struct GNUNET_MessageHeader context_msg;
154 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
155 "starting listener\n");
156 context_msg.size = htons (sizeof context_msg);
157 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
158 listen_handle = GNUNET_SET_listen (config,
159 GNUNET_SET_OPERATION_INTERSECTION,
163 oh1 = GNUNET_SET_prepare (&local_id,
166 GNUNET_SET_RESULT_FULL,
167 (struct GNUNET_SET_Option[]) { 0 },
170 GNUNET_SET_commit (oh1,
176 * Initialize the second set, continue
178 * @param cls closure, unused
181 init_set2 (void *cls)
183 struct GNUNET_SET_Element element;
185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
186 "initializing set 2\n");
187 element.element_type = 0;
188 element.data = "hello";
189 element.size = strlen(element.data);
190 GNUNET_SET_add_element (set2,
194 element.data = "quux";
195 element.size = strlen(element.data);
196 GNUNET_SET_add_element (set2,
200 element.data = "baz";
201 element.size = strlen(element.data);
202 GNUNET_SET_add_element (set2,
210 * Initialize the first set, continue.
215 struct GNUNET_SET_Element element;
217 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
218 "initializing set 1\n");
219 element.element_type = 0;
220 element.data = "hello";
221 element.size = strlen(element.data);
222 GNUNET_SET_add_element (set1,
226 element.data = "bar";
227 element.size = strlen(element.data);
228 GNUNET_SET_add_element (set1,
237 const struct GNUNET_SET_Element *element)
241 GNUNET_assert (iter_count == 3);
242 GNUNET_SET_destroy (cls);
253 struct GNUNET_SET_Element element;
254 struct GNUNET_SET_Handle *iter_set;
256 iter_set = GNUNET_SET_create (config,
257 GNUNET_SET_OPERATION_INTERSECTION);
258 element.element_type = 0;
259 element.data = "hello";
260 element.size = strlen(element.data);
261 GNUNET_SET_add_element (iter_set,
265 element.data = "bar";
266 element.size = strlen(element.data);
267 GNUNET_SET_add_element (iter_set,
271 element.data = "quux";
272 element.size = strlen(element.data);
273 GNUNET_SET_add_element (iter_set,
277 GNUNET_SET_iterate (iter_set,
284 * Function run on shutdown.
289 do_shutdown (void *cls)
293 GNUNET_SCHEDULER_cancel (tt);
298 GNUNET_SET_operation_cancel (oh1);
303 GNUNET_SET_operation_cancel (oh2);
308 GNUNET_SET_destroy (set1);
313 GNUNET_SET_destroy (set2);
316 if (NULL != listen_handle)
318 GNUNET_SET_listen_cancel (listen_handle);
319 listen_handle = NULL;
325 * Function run on timeout.
330 timeout_fail (void *cls)
333 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
334 "Testcase failed with timeout\n");
335 GNUNET_SCHEDULER_shutdown ();
341 * Signature of the 'main' function for a (single-peer) testcase that
342 * is run using 'GNUNET_TESTING_peer_run'.
345 * @param cfg configuration of the peer that was started
346 * @param peer identity of the peer that was created
350 const struct GNUNET_CONFIGURATION_Handle *cfg,
351 struct GNUNET_TESTING_Peer *peer)
354 GNUNET_TESTING_peer_get_identity (peer,
359 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
362 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
365 set1 = GNUNET_SET_create (cfg,
366 GNUNET_SET_OPERATION_INTERSECTION);
367 set2 = GNUNET_SET_create (cfg,
368 GNUNET_SET_OPERATION_INTERSECTION);
369 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
372 /* test the real set reconciliation */
381 if (0 != GNUNET_TESTING_peer_run ("test_set_intersection_result_full",