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_TEST);
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,
154 GNUNET_SET_commit (oh,
160 * Start the set operation.
162 * @param cls closure, unused
167 struct GNUNET_MessageHeader context_msg;
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "Starting reconciliation\n");
171 context_msg.size = htons (sizeof context_msg);
172 context_msg.type = htons (GNUNET_MESSAGE_TYPE_TEST);
173 listen_handle = GNUNET_SET_listen (config,
174 GNUNET_SET_OPERATION_UNION,
178 oh1 = GNUNET_SET_prepare (&local_id,
181 GNUNET_SET_RESULT_ADDED,
184 GNUNET_SET_commit (oh1,
190 * Initialize the second set, continue
192 * @param cls closure, unused
195 init_set2 (void *cls)
197 struct GNUNET_SET_Element element;
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
201 element.element_type = 0;
203 element.data = "hello";
204 element.size = strlen(element.data);
205 GNUNET_SET_add_element (set2, &element, NULL, NULL);
206 element.data = "quux";
207 element.size = strlen(element.data);
208 GNUNET_SET_add_element (set2, &element, NULL, NULL);
209 element.data = "baz";
210 element.size = strlen(element.data);
211 GNUNET_SET_add_element (set2, &element, &start, NULL);
216 * Initialize the first set, continue.
221 struct GNUNET_SET_Element element;
223 element.element_type = 0;
225 element.data = "hello";
226 element.size = strlen(element.data);
227 GNUNET_SET_add_element (set1, &element, NULL, NULL);
228 element.data = "bar";
229 element.size = strlen(element.data);
230 GNUNET_SET_add_element (set1, &element, init_set2, NULL);
232 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
233 "initialized set 1\n");
239 const struct GNUNET_SET_Element *element)
243 GNUNET_assert (3 == iter_count);
244 GNUNET_SET_destroy (cls);
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 "iter: got element %u\n",
258 struct GNUNET_SET_Element element;
259 struct GNUNET_SET_Handle *iter_set;
261 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
263 element.element_type = 0;
265 element.data = "hello";
266 element.size = strlen(element.data);
267 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
268 element.data = "bar";
269 element.size = strlen(element.data);
270 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
271 element.data = "quux";
272 element.size = strlen(element.data);
273 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
275 GNUNET_SET_iterate (iter_set, iter_cb, iter_set);
280 * Function run on timeout.
285 timeout_fail (void *cls)
288 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
289 "Testcase failed with timeout\n");
290 GNUNET_SCHEDULER_shutdown ();
296 * Function run on shutdown.
301 do_shutdown (void *cls)
305 GNUNET_SCHEDULER_cancel (tt);
310 GNUNET_SET_operation_cancel (oh1);
315 GNUNET_SET_operation_cancel (oh2);
320 GNUNET_SET_destroy (set1);
325 GNUNET_SET_destroy (set2);
328 if (NULL != listen_handle)
330 GNUNET_SET_listen_cancel (listen_handle);
331 listen_handle = NULL;
337 * Signature of the 'main' function for a (single-peer) testcase that
338 * is run using 'GNUNET_TESTING_peer_run'.
341 * @param cfg configuration of the peer that was started
342 * @param peer identity of the peer that was created
346 const struct GNUNET_CONFIGURATION_Handle *cfg,
347 struct GNUNET_TESTING_Peer *peer)
350 struct GNUNET_SET_OperationHandle *my_oh;
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Running preparatory tests\n");
354 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
357 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
361 GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "my id (from CRYPTO): %s\n",
364 GNUNET_i2s (&local_id));
365 GNUNET_TESTING_peer_get_identity (peer, &local_id);
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "my id (from TESTING): %s\n",
368 GNUNET_i2s (&local_id));
371 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
372 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
373 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
376 ///* test if canceling an uncommited request works! */
377 my_oh = GNUNET_SET_prepare (&local_id,
380 GNUNET_SET_RESULT_ADDED,
384 GNUNET_SET_operation_cancel (my_oh);
386 /* test the real set reconciliation */
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Running real set-reconciliation\n");
394 main (int argc, char **argv)
396 GNUNET_log_setup ("test_set_api",
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401 if (0 != GNUNET_TESTING_peer_run ("test_set_api",