2 This file is part of GNUnet.
3 Copyright (C) 2012, 2016 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_union_result_smmetric
23 * @brief testcase for symmetric result mode of the union set operation
24 * @author Florian Dold
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_set_service.h"
34 * Value to return from #main().
38 static struct GNUNET_PeerIdentity local_id;
40 static struct GNUNET_HashCode app_id;
42 static struct GNUNET_SET_Handle *set1;
44 static struct GNUNET_SET_Handle *set2;
46 static struct GNUNET_SET_ListenHandle *listen_handle;
48 static const struct GNUNET_CONFIGURATION_Handle *config;
50 static struct GNUNET_SET_OperationHandle *oh1;
52 static struct GNUNET_SET_OperationHandle *oh2;
54 static int iter_count;
57 * Are we testing correctness for the empty set union?
62 * Number of elements found in set 1
64 static unsigned int count_set1;
67 * Number of elements found in set 2
69 static unsigned int count_set2;
72 * Task that is run when the test times out.
74 static struct GNUNET_SCHEDULER_Task *timeout_task;
78 result_cb_set1(void *cls,
79 const struct GNUNET_SET_Element *element,
80 uint64_t current_size,
81 enum GNUNET_SET_Status status)
85 case GNUNET_SET_STATUS_ADD_LOCAL:
87 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
88 "set 1: got element\n");
91 case GNUNET_SET_STATUS_FAILURE:
92 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
96 if (NULL != timeout_task)
98 GNUNET_SCHEDULER_cancel(timeout_task);
101 GNUNET_SCHEDULER_shutdown();
104 case GNUNET_SET_STATUS_DONE:
106 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
108 GNUNET_SET_destroy(set1);
112 if (NULL != timeout_task)
114 GNUNET_SCHEDULER_cancel(timeout_task);
117 GNUNET_SCHEDULER_shutdown();
121 case GNUNET_SET_STATUS_ADD_REMOTE:
131 result_cb_set2(void *cls,
132 const struct GNUNET_SET_Element *element,
133 uint64_t current_size,
134 enum GNUNET_SET_Status status)
138 case GNUNET_SET_STATUS_ADD_LOCAL:
140 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
141 "set 2: got element\n");
144 case GNUNET_SET_STATUS_FAILURE:
145 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
149 if (NULL != timeout_task)
151 GNUNET_SCHEDULER_cancel(timeout_task);
154 GNUNET_SCHEDULER_shutdown();
157 case GNUNET_SET_STATUS_DONE:
158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
161 GNUNET_SET_destroy(set2);
165 if (NULL != timeout_task)
167 GNUNET_SCHEDULER_cancel(timeout_task);
170 GNUNET_SCHEDULER_shutdown();
174 case GNUNET_SET_STATUS_ADD_REMOTE:
185 const struct GNUNET_PeerIdentity *other_peer,
186 const struct GNUNET_MessageHeader *context_msg,
187 struct GNUNET_SET_Request *request)
189 GNUNET_assert(NULL != context_msg);
190 GNUNET_assert(ntohs(context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
191 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
192 "listen cb called\n");
193 oh2 = GNUNET_SET_accept(request,
194 GNUNET_SET_RESULT_SYMMETRIC,
195 (struct GNUNET_SET_Option[]) { 0 },
198 GNUNET_SET_commit(oh2,
204 * Start the set operation.
206 * @param cls closure, unused
211 struct GNUNET_MessageHeader context_msg;
213 context_msg.size = htons(sizeof context_msg);
214 context_msg.type = htons(GNUNET_MESSAGE_TYPE_DUMMY);
216 listen_handle = GNUNET_SET_listen(config,
217 GNUNET_SET_OPERATION_UNION,
220 oh1 = GNUNET_SET_prepare(&local_id,
223 GNUNET_SET_RESULT_SYMMETRIC,
224 (struct GNUNET_SET_Option[]) { 0 },
225 &result_cb_set1, NULL);
226 GNUNET_SET_commit(oh1, set1);
231 * Initialize the second set, continue
233 * @param cls closure, unused
238 struct GNUNET_SET_Element element;
240 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
241 "initializing set 2\n");
247 element.element_type = 0;
248 element.data = "hello";
249 element.size = strlen(element.data);
250 GNUNET_SET_add_element(set2,
254 element.data = "quux";
255 element.size = strlen(element.data);
256 GNUNET_SET_add_element(set2,
260 element.data = "baz";
261 element.size = strlen(element.data);
262 GNUNET_SET_add_element(set2,
269 * Initialize the first set, continue.
274 struct GNUNET_SET_Element element;
281 element.element_type = 0;
282 element.data = "hello";
283 element.size = strlen(element.data);
284 GNUNET_SET_add_element(set1,
288 element.data = "bar";
289 element.size = strlen(element.data);
290 GNUNET_SET_add_element(set1,
294 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
295 "initialized set 1\n");
301 const struct GNUNET_SET_Element *element)
305 GNUNET_assert(iter_count == 3);
306 GNUNET_SET_destroy(cls);
309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
310 "iter: got element\n");
319 struct GNUNET_SET_Element element;
320 struct GNUNET_SET_Handle *iter_set;
323 iter_set = GNUNET_SET_create(config, GNUNET_SET_OPERATION_UNION);
324 element.element_type = 0;
325 element.data = "hello";
326 element.size = strlen(element.data);
327 GNUNET_SET_add_element(iter_set, &element, NULL, NULL);
328 element.data = "bar";
329 element.size = strlen(element.data);
330 GNUNET_SET_add_element(iter_set, &element, NULL, NULL);
331 element.data = "quux";
332 element.size = strlen(element.data);
333 GNUNET_SET_add_element(iter_set, &element, NULL, NULL);
335 GNUNET_SET_iterate(iter_set,
342 * Signature of the main function of a task.
347 timeout_fail(void *cls)
350 GNUNET_SCHEDULER_shutdown();
351 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
358 * Function run on shutdown.
363 do_shutdown(void *cls)
365 if (NULL != timeout_task)
367 GNUNET_SCHEDULER_cancel(timeout_task);
372 GNUNET_SET_operation_cancel(oh1);
377 GNUNET_SET_operation_cancel(oh2);
382 GNUNET_SET_destroy(set1);
387 GNUNET_SET_destroy(set2);
390 if (NULL != listen_handle)
392 GNUNET_SET_listen_cancel(listen_handle);
393 listen_handle = NULL;
399 * Signature of the 'main' function for a (single-peer) testcase that
400 * is run using 'GNUNET_TESTING_peer_run'.
403 * @param cfg configuration of the peer that was started
404 * @param peer identity of the peer that was created
408 const struct GNUNET_CONFIGURATION_Handle *cfg,
409 struct GNUNET_TESTING_Peer *peer)
411 timeout_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
414 GNUNET_SCHEDULER_add_shutdown(&do_shutdown,
417 GNUNET_TESTING_peer_get_identity(peer,
423 set1 = GNUNET_SET_create(cfg, GNUNET_SET_OPERATION_UNION);
424 set2 = GNUNET_SET_create(cfg, GNUNET_SET_OPERATION_UNION);
425 GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
427 /* test the real set reconciliation */
433 main(int argc, char **argv)
436 if (0 != GNUNET_TESTING_peer_run("test_set_api",
442 GNUNET_assert(0 == count_set1);
443 GNUNET_assert(0 == count_set2);
445 if (0 != GNUNET_TESTING_peer_run("test_set_api",
451 GNUNET_break(2 == count_set1);
452 GNUNET_break(1 == count_set2);