2 This file is part of GNUnet.
3 Copyright (C) 2012 Christian Grothoff (and other contributing authors)
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
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;
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;
50 * Are we testing correctness for the empty set union?
55 * Number of elements found in set 1
57 static unsigned int count_set1;
60 * Number of elements found in set 2
62 static unsigned int count_set2;
66 result_cb_set1 (void *cls,
67 const struct GNUNET_SET_Element *element,
68 enum GNUNET_SET_Status status)
72 case GNUNET_SET_STATUS_ADD_LOCAL:
74 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75 "set 1: got element\n");
77 case GNUNET_SET_STATUS_FAILURE:
78 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81 GNUNET_SCHEDULER_shutdown ();
83 case GNUNET_SET_STATUS_DONE:
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86 GNUNET_SET_destroy (set1);
89 GNUNET_SCHEDULER_shutdown ();
91 case GNUNET_SET_STATUS_ADD_REMOTE:
100 result_cb_set2 (void *cls,
101 const struct GNUNET_SET_Element *element,
102 enum GNUNET_SET_Status status)
106 case GNUNET_SET_STATUS_ADD_LOCAL:
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109 "set 2: got element\n");
111 case GNUNET_SET_STATUS_FAILURE:
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
115 GNUNET_SCHEDULER_shutdown ();
117 case GNUNET_SET_STATUS_DONE:
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
120 GNUNET_SET_destroy (set2);
123 GNUNET_SCHEDULER_shutdown ();
125 case GNUNET_SET_STATUS_ADD_REMOTE:
134 listen_cb (void *cls,
135 const struct GNUNET_PeerIdentity *other_peer,
136 const struct GNUNET_MessageHeader *context_msg,
137 struct GNUNET_SET_Request *request)
139 struct GNUNET_SET_OperationHandle *oh;
141 GNUNET_assert (NULL != context_msg);
142 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_TEST);
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
144 "listen cb called\n");
145 GNUNET_SET_listen_cancel (listen_handle);
146 oh = GNUNET_SET_accept (request,
147 GNUNET_SET_RESULT_SYMMETRIC,
150 GNUNET_SET_commit (oh, set2);
155 * Start the set operation.
157 * @param cls closure, unused
162 struct GNUNET_SET_OperationHandle *oh;
163 struct GNUNET_MessageHeader context_msg;
165 context_msg.size = htons (sizeof context_msg);
166 context_msg.type = htons (GNUNET_MESSAGE_TYPE_TEST);
168 listen_handle = GNUNET_SET_listen (config,
169 GNUNET_SET_OPERATION_UNION,
172 oh = GNUNET_SET_prepare (&local_id,
175 GNUNET_SET_RESULT_SYMMETRIC,
176 &result_cb_set1, NULL);
177 GNUNET_SET_commit (oh, set1);
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_DEBUG,
192 "initializing set 2\n");
198 element.element_type = 0;
199 element.data = "hello";
200 element.size = strlen(element.data);
201 GNUNET_SET_add_element (set2,
205 element.data = "quux";
206 element.size = strlen(element.data);
207 GNUNET_SET_add_element (set2,
211 element.data = "baz";
212 element.size = strlen(element.data);
213 GNUNET_SET_add_element (set2,
220 * Initialize the first set, continue.
225 struct GNUNET_SET_Element element;
232 element.element_type = 0;
233 element.data = "hello";
234 element.size = strlen(element.data);
235 GNUNET_SET_add_element (set1,
239 element.data = "bar";
240 element.size = strlen(element.data);
241 GNUNET_SET_add_element (set1,
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246 "initialized set 1\n");
252 const struct GNUNET_SET_Element *element)
256 GNUNET_assert (iter_count == 3);
257 GNUNET_SET_destroy (cls);
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "iter: got element\n");
270 struct GNUNET_SET_Element element;
271 struct GNUNET_SET_Handle *iter_set;
274 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
275 element.element_type = 0;
276 element.data = "hello";
277 element.size = strlen(element.data);
278 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
279 element.data = "bar";
280 element.size = strlen(element.data);
281 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
282 element.data = "quux";
283 element.size = strlen(element.data);
284 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
286 GNUNET_SET_iterate (iter_set,
293 * Signature of the main function of a task.
296 * @param tc context information (why was this task triggered now)
299 timeout_fail (void *cls,
300 const struct GNUNET_SCHEDULER_TaskContext *tc)
302 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
304 GNUNET_SCHEDULER_shutdown ();
310 * Signature of the 'main' function for a (single-peer) testcase that
311 * is run using 'GNUNET_TESTING_peer_run'.
314 * @param cfg configuration of the peer that was started
315 * @param peer identity of the peer that was created
319 const struct GNUNET_CONFIGURATION_Handle *cfg,
320 struct GNUNET_TESTING_Peer *peer)
322 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
327 GNUNET_TESTING_peer_get_identity (peer,
332 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
333 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
334 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
336 /* test the real set reconciliation */
342 main (int argc, char **argv)
345 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
351 GNUNET_assert (0 == count_set1);
352 GNUNET_assert (0 == count_set2);
354 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
360 GNUNET_assert (2 == count_set1);
361 GNUNET_assert (1 == count_set2);