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
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_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 enum GNUNET_SET_Status status)
63 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
64 "Processing result set 1 (%d)\n",
68 case GNUNET_SET_STATUS_OK:
71 case GNUNET_SET_STATUS_FAILURE:
75 case GNUNET_SET_STATUS_DONE:
77 GNUNET_assert (1 == count);
78 GNUNET_SET_destroy (set1);
81 GNUNET_SCHEDULER_shutdown ();
90 result_cb_set2 (void *cls,
91 const struct GNUNET_SET_Element *element,
92 enum GNUNET_SET_Status status)
96 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
97 "Processing result set 2 (%d)\n",
101 case GNUNET_SET_STATUS_OK:
104 case GNUNET_SET_STATUS_FAILURE:
108 case GNUNET_SET_STATUS_DONE:
110 GNUNET_assert (1 == count);
111 GNUNET_SET_destroy (set2);
114 GNUNET_SCHEDULER_shutdown ();
123 listen_cb (void *cls,
124 const struct GNUNET_PeerIdentity *other_peer,
125 const struct GNUNET_MessageHeader *context_msg,
126 struct GNUNET_SET_Request *request)
128 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
129 "starting intersection by accepting and committing\n");
130 GNUNET_assert (NULL != context_msg);
131 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
132 GNUNET_SET_listen_cancel (listen_handle);
133 listen_handle = NULL;
134 oh2 = GNUNET_SET_accept (request,
135 GNUNET_SET_RESULT_FULL,
138 GNUNET_SET_commit (oh2,
144 * Start the set operation.
146 * @param cls closure, unused
151 struct GNUNET_MessageHeader context_msg;
153 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
154 "starting listener\n");
155 context_msg.size = htons (sizeof context_msg);
156 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
157 listen_handle = GNUNET_SET_listen (config,
158 GNUNET_SET_OPERATION_INTERSECTION,
162 oh1 = GNUNET_SET_prepare (&local_id,
165 GNUNET_SET_RESULT_FULL,
168 GNUNET_SET_commit (oh1,
174 * Initialize the second set, continue
176 * @param cls closure, unused
179 init_set2 (void *cls)
181 struct GNUNET_SET_Element element;
183 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
184 "initializing set 2\n");
185 element.element_type = 0;
186 element.data = "hello";
187 element.size = strlen(element.data);
188 GNUNET_SET_add_element (set2,
192 element.data = "quux";
193 element.size = strlen(element.data);
194 GNUNET_SET_add_element (set2,
198 element.data = "baz";
199 element.size = strlen(element.data);
200 GNUNET_SET_add_element (set2,
208 * Initialize the first set, continue.
213 struct GNUNET_SET_Element element;
215 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
216 "initializing set 1\n");
217 element.element_type = 0;
218 element.data = "hello";
219 element.size = strlen(element.data);
220 GNUNET_SET_add_element (set1,
224 element.data = "bar";
225 element.size = strlen(element.data);
226 GNUNET_SET_add_element (set1,
235 const struct GNUNET_SET_Element *element)
239 GNUNET_assert (iter_count == 3);
240 GNUNET_SET_destroy (cls);
251 struct GNUNET_SET_Element element;
252 struct GNUNET_SET_Handle *iter_set;
254 iter_set = GNUNET_SET_create (config,
255 GNUNET_SET_OPERATION_INTERSECTION);
256 element.element_type = 0;
257 element.data = "hello";
258 element.size = strlen(element.data);
259 GNUNET_SET_add_element (iter_set,
263 element.data = "bar";
264 element.size = strlen(element.data);
265 GNUNET_SET_add_element (iter_set,
269 element.data = "quux";
270 element.size = strlen(element.data);
271 GNUNET_SET_add_element (iter_set,
275 GNUNET_SET_iterate (iter_set,
282 * Function run on shutdown.
287 do_shutdown (void *cls)
291 GNUNET_SCHEDULER_cancel (tt);
296 GNUNET_SET_operation_cancel (oh1);
301 GNUNET_SET_operation_cancel (oh2);
306 GNUNET_SET_destroy (set1);
311 GNUNET_SET_destroy (set2);
314 if (NULL != listen_handle)
316 GNUNET_SET_listen_cancel (listen_handle);
317 listen_handle = NULL;
323 * Function run on timeout.
328 timeout_fail (void *cls)
331 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
332 "Testcase failed with timeout\n");
333 GNUNET_SCHEDULER_shutdown ();
339 * Signature of the 'main' function for a (single-peer) testcase that
340 * is run using 'GNUNET_TESTING_peer_run'.
343 * @param cfg configuration of the peer that was started
344 * @param peer identity of the peer that was created
348 const struct GNUNET_CONFIGURATION_Handle *cfg,
349 struct GNUNET_TESTING_Peer *peer)
352 GNUNET_TESTING_peer_get_identity (peer,
357 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
360 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
363 set1 = GNUNET_SET_create (cfg,
364 GNUNET_SET_OPERATION_INTERSECTION);
365 set2 = GNUNET_SET_create (cfg,
366 GNUNET_SET_OPERATION_INTERSECTION);
367 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
370 /* test the real set reconciliation */
379 if (0 != GNUNET_TESTING_peer_run ("test_set_intersection_result_full",