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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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");
90 case GNUNET_SET_STATUS_FAILURE:
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95 if (NULL != timeout_task)
97 GNUNET_SCHEDULER_cancel (timeout_task);
100 GNUNET_SCHEDULER_shutdown ();
102 case GNUNET_SET_STATUS_DONE:
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
106 GNUNET_SET_destroy (set1);
110 if (NULL != timeout_task)
112 GNUNET_SCHEDULER_cancel (timeout_task);
115 GNUNET_SCHEDULER_shutdown ();
118 case GNUNET_SET_STATUS_ADD_REMOTE:
127 result_cb_set2 (void *cls,
128 const struct GNUNET_SET_Element *element,
129 enum GNUNET_SET_Status status)
133 case GNUNET_SET_STATUS_ADD_LOCAL:
135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136 "set 2: got element\n");
138 case GNUNET_SET_STATUS_FAILURE:
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143 if (NULL != timeout_task)
145 GNUNET_SCHEDULER_cancel (timeout_task);
148 GNUNET_SCHEDULER_shutdown ();
150 case GNUNET_SET_STATUS_DONE:
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154 GNUNET_SET_destroy (set2);
158 if (NULL != timeout_task)
160 GNUNET_SCHEDULER_cancel (timeout_task);
163 GNUNET_SCHEDULER_shutdown ();
166 case GNUNET_SET_STATUS_ADD_REMOTE:
175 listen_cb (void *cls,
176 const struct GNUNET_PeerIdentity *other_peer,
177 const struct GNUNET_MessageHeader *context_msg,
178 struct GNUNET_SET_Request *request)
180 GNUNET_assert (NULL != context_msg);
181 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "listen cb called\n");
184 GNUNET_SET_listen_cancel (listen_handle);
185 listen_handle = NULL;
186 oh2 = GNUNET_SET_accept (request,
187 GNUNET_SET_RESULT_SYMMETRIC,
188 (struct GNUNET_SET_Option[]) { 0 },
191 GNUNET_SET_commit (oh2,
197 * Start the set operation.
199 * @param cls closure, unused
204 struct GNUNET_MessageHeader context_msg;
206 context_msg.size = htons (sizeof context_msg);
207 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
209 listen_handle = GNUNET_SET_listen (config,
210 GNUNET_SET_OPERATION_UNION,
213 oh1 = GNUNET_SET_prepare (&local_id,
216 GNUNET_SET_RESULT_SYMMETRIC,
217 (struct GNUNET_SET_Option[]) { 0 },
218 &result_cb_set1, NULL);
219 GNUNET_SET_commit (oh1, set1);
224 * Initialize the second set, continue
226 * @param cls closure, unused
229 init_set2 (void *cls)
231 struct GNUNET_SET_Element element;
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "initializing set 2\n");
240 element.element_type = 0;
241 element.data = "hello";
242 element.size = strlen(element.data);
243 GNUNET_SET_add_element (set2,
247 element.data = "quux";
248 element.size = strlen(element.data);
249 GNUNET_SET_add_element (set2,
253 element.data = "baz";
254 element.size = strlen(element.data);
255 GNUNET_SET_add_element (set2,
262 * Initialize the first set, continue.
267 struct GNUNET_SET_Element element;
274 element.element_type = 0;
275 element.data = "hello";
276 element.size = strlen(element.data);
277 GNUNET_SET_add_element (set1,
281 element.data = "bar";
282 element.size = strlen(element.data);
283 GNUNET_SET_add_element (set1,
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "initialized set 1\n");
294 const struct GNUNET_SET_Element *element)
298 GNUNET_assert (iter_count == 3);
299 GNUNET_SET_destroy (cls);
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303 "iter: got element\n");
312 struct GNUNET_SET_Element element;
313 struct GNUNET_SET_Handle *iter_set;
316 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
317 element.element_type = 0;
318 element.data = "hello";
319 element.size = strlen(element.data);
320 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
321 element.data = "bar";
322 element.size = strlen(element.data);
323 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
324 element.data = "quux";
325 element.size = strlen(element.data);
326 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
328 GNUNET_SET_iterate (iter_set,
335 * Signature of the main function of a task.
340 timeout_fail (void *cls)
343 GNUNET_SCHEDULER_shutdown ();
344 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
351 * Function run on shutdown.
356 do_shutdown (void *cls)
358 if (NULL != timeout_task)
360 GNUNET_SCHEDULER_cancel (timeout_task);
365 GNUNET_SET_operation_cancel (oh1);
370 GNUNET_SET_operation_cancel (oh2);
375 GNUNET_SET_destroy (set1);
380 GNUNET_SET_destroy (set2);
383 if (NULL != listen_handle)
385 GNUNET_SET_listen_cancel (listen_handle);
386 listen_handle = NULL;
392 * Signature of the 'main' function for a (single-peer) testcase that
393 * is run using 'GNUNET_TESTING_peer_run'.
396 * @param cfg configuration of the peer that was started
397 * @param peer identity of the peer that was created
401 const struct GNUNET_CONFIGURATION_Handle *cfg,
402 struct GNUNET_TESTING_Peer *peer)
404 timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
407 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
410 GNUNET_TESTING_peer_get_identity (peer,
416 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
417 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
418 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
420 /* test the real set reconciliation */
426 main (int argc, char **argv)
429 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
435 GNUNET_assert (0 == count_set1);
436 GNUNET_assert (0 == count_set2);
438 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
444 GNUNET_break (2 == count_set1);
445 GNUNET_break (1 == count_set2);