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 uint64_t current_size,
60 enum GNUNET_SET_Status status)
64 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
65 "Processing result set 1 (%d)\n",
69 case GNUNET_SET_STATUS_OK:
72 case GNUNET_SET_STATUS_FAILURE:
76 case GNUNET_SET_STATUS_DONE:
78 GNUNET_assert (1 == count);
79 GNUNET_SET_destroy (set1);
82 GNUNET_SCHEDULER_shutdown ();
91 result_cb_set2 (void *cls,
92 const struct GNUNET_SET_Element *element,
93 uint64_t current_size,
94 enum GNUNET_SET_Status status)
98 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
99 "Processing result set 2 (%d)\n",
103 case GNUNET_SET_STATUS_OK:
106 case GNUNET_SET_STATUS_FAILURE:
110 case GNUNET_SET_STATUS_DONE:
112 GNUNET_assert (1 == count);
113 GNUNET_SET_destroy (set2);
116 GNUNET_SCHEDULER_shutdown ();
125 listen_cb (void *cls,
126 const struct GNUNET_PeerIdentity *other_peer,
127 const struct GNUNET_MessageHeader *context_msg,
128 struct GNUNET_SET_Request *request)
130 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
131 "starting intersection by accepting and committing\n");
132 GNUNET_assert (NULL != context_msg);
133 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
134 GNUNET_SET_listen_cancel (listen_handle);
135 listen_handle = NULL;
136 oh2 = GNUNET_SET_accept (request,
137 GNUNET_SET_RESULT_FULL,
138 (struct GNUNET_SET_Option[]) { 0 },
141 GNUNET_SET_commit (oh2,
147 * Start the set operation.
149 * @param cls closure, unused
154 struct GNUNET_MessageHeader context_msg;
156 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
157 "starting listener\n");
158 context_msg.size = htons (sizeof context_msg);
159 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
160 listen_handle = GNUNET_SET_listen (config,
161 GNUNET_SET_OPERATION_INTERSECTION,
165 oh1 = GNUNET_SET_prepare (&local_id,
168 GNUNET_SET_RESULT_FULL,
169 (struct GNUNET_SET_Option[]) { 0 },
172 GNUNET_SET_commit (oh1,
178 * Initialize the second set, continue
180 * @param cls closure, unused
183 init_set2 (void *cls)
185 struct GNUNET_SET_Element element;
187 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
188 "initializing set 2\n");
189 element.element_type = 0;
190 element.data = "hello";
191 element.size = strlen(element.data);
192 GNUNET_SET_add_element (set2,
196 element.data = "quux";
197 element.size = strlen(element.data);
198 GNUNET_SET_add_element (set2,
202 element.data = "baz";
203 element.size = strlen(element.data);
204 GNUNET_SET_add_element (set2,
212 * Initialize the first set, continue.
217 struct GNUNET_SET_Element element;
219 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
220 "initializing set 1\n");
221 element.element_type = 0;
222 element.data = "hello";
223 element.size = strlen(element.data);
224 GNUNET_SET_add_element (set1,
228 element.data = "bar";
229 element.size = strlen(element.data);
230 GNUNET_SET_add_element (set1,
239 const struct GNUNET_SET_Element *element)
243 GNUNET_assert (iter_count == 3);
244 GNUNET_SET_destroy (cls);
255 struct GNUNET_SET_Element element;
256 struct GNUNET_SET_Handle *iter_set;
258 iter_set = GNUNET_SET_create (config,
259 GNUNET_SET_OPERATION_INTERSECTION);
260 element.element_type = 0;
261 element.data = "hello";
262 element.size = strlen(element.data);
263 GNUNET_SET_add_element (iter_set,
267 element.data = "bar";
268 element.size = strlen(element.data);
269 GNUNET_SET_add_element (iter_set,
273 element.data = "quux";
274 element.size = strlen(element.data);
275 GNUNET_SET_add_element (iter_set,
279 GNUNET_SET_iterate (iter_set,
286 * Function run on shutdown.
291 do_shutdown (void *cls)
295 GNUNET_SCHEDULER_cancel (tt);
300 GNUNET_SET_operation_cancel (oh1);
305 GNUNET_SET_operation_cancel (oh2);
310 GNUNET_SET_destroy (set1);
315 GNUNET_SET_destroy (set2);
318 if (NULL != listen_handle)
320 GNUNET_SET_listen_cancel (listen_handle);
321 listen_handle = NULL;
327 * Function run on timeout.
332 timeout_fail (void *cls)
335 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
336 "Testcase failed with timeout\n");
337 GNUNET_SCHEDULER_shutdown ();
343 * Signature of the 'main' function for a (single-peer) testcase that
344 * is run using 'GNUNET_TESTING_peer_run'.
347 * @param cfg configuration of the peer that was started
348 * @param peer identity of the peer that was created
352 const struct GNUNET_CONFIGURATION_Handle *cfg,
353 struct GNUNET_TESTING_Peer *peer)
356 GNUNET_TESTING_peer_get_identity (peer,
361 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
364 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
367 set1 = GNUNET_SET_create (cfg,
368 GNUNET_SET_OPERATION_INTERSECTION);
369 set2 = GNUNET_SET_create (cfg,
370 GNUNET_SET_OPERATION_INTERSECTION);
371 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
374 /* test the real set reconciliation */
383 if (0 != GNUNET_TESTING_peer_run ("test_set_intersection_result_full",