2 This file is part of GNUnet.
3 Copyright (C) 2012 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_api.c
23 * @brief testcase for set_api.c
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_set_service.h"
32 static struct GNUNET_PeerIdentity local_id;
34 static struct GNUNET_HashCode app_id;
36 static struct GNUNET_SET_Handle *set1;
38 static struct GNUNET_SET_Handle *set2;
40 static struct GNUNET_SET_ListenHandle *listen_handle;
42 static struct GNUNET_SET_OperationHandle *oh1;
44 static struct GNUNET_SET_OperationHandle *oh2;
46 static const struct GNUNET_CONFIGURATION_Handle *config;
48 static unsigned int iter_count;
52 static struct GNUNET_SCHEDULER_Task *tt;
56 result_cb_set1 (void *cls,
57 const struct GNUNET_SET_Element *element,
58 enum GNUNET_SET_Status status)
62 case GNUNET_SET_STATUS_OK:
63 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
64 "set 1: got element\n");
66 case GNUNET_SET_STATUS_FAILURE:
70 "set 1: received failure status!\n");
74 GNUNET_SCHEDULER_cancel (tt);
77 GNUNET_SCHEDULER_shutdown ();
79 case GNUNET_SET_STATUS_DONE:
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 GNUNET_SET_destroy (set1);
90 GNUNET_SCHEDULER_cancel (tt);
92 GNUNET_SCHEDULER_shutdown ();
102 result_cb_set2 (void *cls,
103 const struct GNUNET_SET_Element *element,
104 enum GNUNET_SET_Status status)
108 case GNUNET_SET_STATUS_OK:
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110 "set 2: got element\n");
112 case GNUNET_SET_STATUS_FAILURE:
116 "set 2: received failure status\n");
119 case GNUNET_SET_STATUS_DONE:
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123 GNUNET_SET_destroy (set2);
127 GNUNET_SCHEDULER_cancel (tt);
129 GNUNET_SCHEDULER_shutdown ();
139 listen_cb (void *cls,
140 const struct GNUNET_PeerIdentity *other_peer,
141 const struct GNUNET_MessageHeader *context_msg,
142 struct GNUNET_SET_Request *request)
144 GNUNET_assert (NULL != context_msg);
145 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147 "listen cb called\n");
148 GNUNET_SET_listen_cancel (listen_handle);
149 listen_handle = NULL;
150 oh2 = GNUNET_SET_accept (request,
151 GNUNET_SET_RESULT_ADDED,
152 (struct GNUNET_SET_Option[]) { 0 },
155 GNUNET_SET_commit (oh2,
161 * Start the set operation.
163 * @param cls closure, unused
168 struct GNUNET_MessageHeader context_msg;
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "Starting reconciliation\n");
172 context_msg.size = htons (sizeof context_msg);
173 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
174 listen_handle = GNUNET_SET_listen (config,
175 GNUNET_SET_OPERATION_UNION,
179 oh1 = GNUNET_SET_prepare (&local_id,
182 GNUNET_SET_RESULT_ADDED,
183 (struct GNUNET_SET_Option[]) { 0 },
186 GNUNET_SET_commit (oh1,
192 * Initialize the second set, continue
194 * @param cls closure, unused
197 init_set2 (void *cls)
199 struct GNUNET_SET_Element element;
201 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
203 element.element_type = 0;
205 element.data = "hello";
206 element.size = strlen(element.data);
207 GNUNET_SET_add_element (set2, &element, NULL, NULL);
208 element.data = "quux";
209 element.size = strlen(element.data);
210 GNUNET_SET_add_element (set2, &element, NULL, NULL);
211 element.data = "baz";
212 element.size = strlen(element.data);
213 GNUNET_SET_add_element (set2, &element, &start, NULL);
218 * Initialize the first set, continue.
223 struct GNUNET_SET_Element element;
225 element.element_type = 0;
227 element.data = "hello";
228 element.size = strlen(element.data);
229 GNUNET_SET_add_element (set1, &element, NULL, NULL);
230 element.data = "bar";
231 element.size = strlen(element.data);
232 GNUNET_SET_add_element (set1, &element, init_set2, NULL);
234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235 "initialized set 1\n");
241 const struct GNUNET_SET_Element *element)
245 GNUNET_assert (3 == iter_count);
246 GNUNET_SET_destroy (cls);
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "iter: got element %u\n",
260 struct GNUNET_SET_Element element;
261 struct GNUNET_SET_Handle *iter_set;
263 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
265 element.element_type = 0;
267 element.data = "hello";
268 element.size = strlen(element.data);
269 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
270 element.data = "bar";
271 element.size = strlen(element.data);
272 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
273 element.data = "quux";
274 element.size = strlen(element.data);
275 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
277 GNUNET_SET_iterate (iter_set, iter_cb, iter_set);
282 * Function run on timeout.
287 timeout_fail (void *cls)
290 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
291 "Testcase failed with timeout\n");
292 GNUNET_SCHEDULER_shutdown ();
298 * Function run on shutdown.
303 do_shutdown (void *cls)
307 GNUNET_SCHEDULER_cancel (tt);
312 GNUNET_SET_operation_cancel (oh1);
317 GNUNET_SET_operation_cancel (oh2);
322 GNUNET_SET_destroy (set1);
327 GNUNET_SET_destroy (set2);
330 if (NULL != listen_handle)
332 GNUNET_SET_listen_cancel (listen_handle);
333 listen_handle = NULL;
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 struct GNUNET_SET_OperationHandle *my_oh;
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Running preparatory tests\n");
356 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
359 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
363 GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "my id (from CRYPTO): %s\n",
366 GNUNET_i2s (&local_id));
367 GNUNET_TESTING_peer_get_identity (peer, &local_id);
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "my id (from TESTING): %s\n",
370 GNUNET_i2s (&local_id));
373 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
374 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
375 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
378 ///* test if canceling an uncommited request works! */
379 my_oh = GNUNET_SET_prepare (&local_id,
382 GNUNET_SET_RESULT_ADDED,
383 (struct GNUNET_SET_Option[]) { 0 },
387 GNUNET_SET_operation_cancel (my_oh);
389 /* test the real set reconciliation */
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Running real set-reconciliation\n");
397 main (int argc, char **argv)
399 GNUNET_log_setup ("test_set_api",
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 if (0 != GNUNET_TESTING_peer_run ("test_set_api",