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/>.
20 * @file set/test_set_union_result_smmetric
21 * @brief testcase for symmetric result mode of the union set operation
22 * @author Florian Dold
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_set_service.h"
32 * Value to return from #main().
36 static struct GNUNET_PeerIdentity local_id;
38 static struct GNUNET_HashCode app_id;
40 static struct GNUNET_SET_Handle *set1;
42 static struct GNUNET_SET_Handle *set2;
44 static struct GNUNET_SET_ListenHandle *listen_handle;
46 static const struct GNUNET_CONFIGURATION_Handle *config;
48 static struct GNUNET_SET_OperationHandle *oh1;
50 static struct GNUNET_SET_OperationHandle *oh2;
52 static int iter_count;
55 * Are we testing correctness for the empty set union?
60 * Number of elements found in set 1
62 static unsigned int count_set1;
65 * Number of elements found in set 2
67 static unsigned int count_set2;
70 * Task that is run when the test times out.
72 static struct GNUNET_SCHEDULER_Task *timeout_task;
76 result_cb_set1 (void *cls,
77 const struct GNUNET_SET_Element *element,
78 uint64_t current_size,
79 enum GNUNET_SET_Status status)
83 case GNUNET_SET_STATUS_ADD_LOCAL:
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86 "set 1: got element\n");
88 case GNUNET_SET_STATUS_FAILURE:
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
93 if (NULL != timeout_task)
95 GNUNET_SCHEDULER_cancel (timeout_task);
98 GNUNET_SCHEDULER_shutdown ();
100 case GNUNET_SET_STATUS_DONE:
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
104 GNUNET_SET_destroy (set1);
108 if (NULL != timeout_task)
110 GNUNET_SCHEDULER_cancel (timeout_task);
113 GNUNET_SCHEDULER_shutdown ();
116 case GNUNET_SET_STATUS_ADD_REMOTE:
125 result_cb_set2 (void *cls,
126 const struct GNUNET_SET_Element *element,
127 uint64_t current_size,
128 enum GNUNET_SET_Status status)
132 case GNUNET_SET_STATUS_ADD_LOCAL:
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "set 2: got element\n");
137 case GNUNET_SET_STATUS_FAILURE:
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142 if (NULL != timeout_task)
144 GNUNET_SCHEDULER_cancel (timeout_task);
147 GNUNET_SCHEDULER_shutdown ();
149 case GNUNET_SET_STATUS_DONE:
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 GNUNET_SET_destroy (set2);
157 if (NULL != timeout_task)
159 GNUNET_SCHEDULER_cancel (timeout_task);
162 GNUNET_SCHEDULER_shutdown ();
165 case GNUNET_SET_STATUS_ADD_REMOTE:
174 listen_cb (void *cls,
175 const struct GNUNET_PeerIdentity *other_peer,
176 const struct GNUNET_MessageHeader *context_msg,
177 struct GNUNET_SET_Request *request)
179 GNUNET_assert (NULL != context_msg);
180 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
182 "listen cb called\n");
183 oh2 = GNUNET_SET_accept (request,
184 GNUNET_SET_RESULT_SYMMETRIC,
185 (struct GNUNET_SET_Option[]) { 0 },
188 GNUNET_SET_commit (oh2,
194 * Start the set operation.
196 * @param cls closure, unused
201 struct GNUNET_MessageHeader context_msg;
203 context_msg.size = htons (sizeof context_msg);
204 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
206 listen_handle = GNUNET_SET_listen (config,
207 GNUNET_SET_OPERATION_UNION,
210 oh1 = GNUNET_SET_prepare (&local_id,
213 GNUNET_SET_RESULT_SYMMETRIC,
214 (struct GNUNET_SET_Option[]) { 0 },
215 &result_cb_set1, NULL);
216 GNUNET_SET_commit (oh1, set1);
221 * Initialize the second set, continue
223 * @param cls closure, unused
226 init_set2 (void *cls)
228 struct GNUNET_SET_Element element;
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231 "initializing set 2\n");
237 element.element_type = 0;
238 element.data = "hello";
239 element.size = strlen(element.data);
240 GNUNET_SET_add_element (set2,
244 element.data = "quux";
245 element.size = strlen(element.data);
246 GNUNET_SET_add_element (set2,
250 element.data = "baz";
251 element.size = strlen(element.data);
252 GNUNET_SET_add_element (set2,
259 * Initialize the first set, continue.
264 struct GNUNET_SET_Element element;
271 element.element_type = 0;
272 element.data = "hello";
273 element.size = strlen(element.data);
274 GNUNET_SET_add_element (set1,
278 element.data = "bar";
279 element.size = strlen(element.data);
280 GNUNET_SET_add_element (set1,
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "initialized set 1\n");
291 const struct GNUNET_SET_Element *element)
295 GNUNET_assert (iter_count == 3);
296 GNUNET_SET_destroy (cls);
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "iter: got element\n");
309 struct GNUNET_SET_Element element;
310 struct GNUNET_SET_Handle *iter_set;
313 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
314 element.element_type = 0;
315 element.data = "hello";
316 element.size = strlen(element.data);
317 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
318 element.data = "bar";
319 element.size = strlen(element.data);
320 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
321 element.data = "quux";
322 element.size = strlen(element.data);
323 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
325 GNUNET_SET_iterate (iter_set,
332 * Signature of the main function of a task.
337 timeout_fail (void *cls)
340 GNUNET_SCHEDULER_shutdown ();
341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
348 * Function run on shutdown.
353 do_shutdown (void *cls)
355 if (NULL != timeout_task)
357 GNUNET_SCHEDULER_cancel (timeout_task);
362 GNUNET_SET_operation_cancel (oh1);
367 GNUNET_SET_operation_cancel (oh2);
372 GNUNET_SET_destroy (set1);
377 GNUNET_SET_destroy (set2);
380 if (NULL != listen_handle)
382 GNUNET_SET_listen_cancel (listen_handle);
383 listen_handle = NULL;
389 * Signature of the 'main' function for a (single-peer) testcase that
390 * is run using 'GNUNET_TESTING_peer_run'.
393 * @param cfg configuration of the peer that was started
394 * @param peer identity of the peer that was created
398 const struct GNUNET_CONFIGURATION_Handle *cfg,
399 struct GNUNET_TESTING_Peer *peer)
401 timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
404 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
407 GNUNET_TESTING_peer_get_identity (peer,
413 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
414 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
415 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
417 /* test the real set reconciliation */
423 main (int argc, char **argv)
426 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
432 GNUNET_assert (0 == count_set1);
433 GNUNET_assert (0 == count_set2);
435 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
441 GNUNET_break (2 == count_set1);
442 GNUNET_break (1 == count_set2);