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_api.c
23 * @brief testcase for set_api.c
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_set_service.h"
31 static struct GNUNET_PeerIdentity local_id;
33 static struct GNUNET_HashCode app_id;
35 static struct GNUNET_SET_Handle *set1;
37 static struct GNUNET_SET_Handle *set2;
39 static struct GNUNET_SET_ListenHandle *listen_handle;
41 static const struct GNUNET_CONFIGURATION_Handle *config;
43 static unsigned int iter_count;
49 result_cb_set1 (void *cls,
50 const struct GNUNET_SET_Element *element,
51 enum GNUNET_SET_Status status)
55 case GNUNET_SET_STATUS_OK:
56 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
57 "set 1: got element\n");
59 case GNUNET_SET_STATUS_FAILURE:
62 "set 1: received failure status!\n");
64 GNUNET_SCHEDULER_shutdown ();
66 case GNUNET_SET_STATUS_DONE:
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
69 GNUNET_SET_destroy (set1);
72 GNUNET_SCHEDULER_shutdown ();
81 result_cb_set2 (void *cls,
82 const struct GNUNET_SET_Element *element,
83 enum GNUNET_SET_Status status)
87 case GNUNET_SET_STATUS_OK:
88 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
89 "set 2: got element\n");
91 case GNUNET_SET_STATUS_FAILURE:
94 "set 2: received failure status\n");
97 case GNUNET_SET_STATUS_DONE:
98 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
100 GNUNET_SET_destroy (set2);
103 GNUNET_SCHEDULER_shutdown ();
112 listen_cb (void *cls,
113 const struct GNUNET_PeerIdentity *other_peer,
114 const struct GNUNET_MessageHeader *context_msg,
115 struct GNUNET_SET_Request *request)
117 struct GNUNET_SET_OperationHandle *oh;
119 GNUNET_assert (NULL != context_msg);
121 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_TEST);
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
124 "listen cb called\n");
125 GNUNET_SET_listen_cancel (listen_handle);
126 oh = GNUNET_SET_accept (request,
127 GNUNET_SET_RESULT_ADDED,
128 &result_cb_set2, NULL);
129 GNUNET_SET_commit (oh, set2);
134 * Start the set operation.
136 * @param cls closure, unused
141 struct GNUNET_SET_OperationHandle *oh;
142 struct GNUNET_MessageHeader context_msg;
144 context_msg.size = htons (sizeof context_msg);
145 context_msg.type = htons (GNUNET_MESSAGE_TYPE_TEST);
147 listen_handle = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
148 &app_id, listen_cb, NULL);
149 oh = GNUNET_SET_prepare (&local_id, &app_id, &context_msg,
150 GNUNET_SET_RESULT_ADDED,
151 result_cb_set1, NULL);
152 GNUNET_SET_commit (oh, set1);
157 * Initialize the second set, continue
159 * @param cls closure, unused
162 init_set2 (void *cls)
164 struct GNUNET_SET_Element element;
166 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
168 element.element_type = 0;
170 element.data = "hello";
171 element.size = strlen(element.data);
172 GNUNET_SET_add_element (set2, &element, NULL, NULL);
173 element.data = "quux";
174 element.size = strlen(element.data);
175 GNUNET_SET_add_element (set2, &element, NULL, NULL);
176 element.data = "baz";
177 element.size = strlen(element.data);
178 GNUNET_SET_add_element (set2, &element, start, NULL);
183 * Initialize the first set, continue.
188 struct GNUNET_SET_Element element;
190 element.element_type = 0;
192 element.data = "hello";
193 element.size = strlen(element.data);
194 GNUNET_SET_add_element (set1, &element, NULL, NULL);
195 element.data = "bar";
196 element.size = strlen(element.data);
197 GNUNET_SET_add_element (set1, &element, init_set2, NULL);
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized set 1\n");
205 const struct GNUNET_SET_Element *element)
209 GNUNET_assert (3 == iter_count);
210 GNUNET_SET_destroy (cls);
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "iter: got element %u\n",
224 struct GNUNET_SET_Element element;
225 struct GNUNET_SET_Handle *iter_set;
227 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
229 element.element_type = 0;
231 element.data = "hello";
232 element.size = strlen(element.data);
233 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
234 element.data = "bar";
235 element.size = strlen(element.data);
236 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
237 element.data = "quux";
238 element.size = strlen(element.data);
239 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
241 GNUNET_SET_iterate (iter_set, iter_cb, iter_set);
246 * Signature of the main function of a task.
249 * @param tc context information (why was this task triggered now)
252 timeout_fail (void *cls,
253 const struct GNUNET_SCHEDULER_TaskContext *tc)
255 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
257 GNUNET_SCHEDULER_shutdown ();
263 * Signature of the 'main' function for a (single-peer) testcase that
264 * is run using 'GNUNET_TESTING_peer_run'.
267 * @param cfg configuration of the peer that was started
268 * @param peer identity of the peer that was created
272 const struct GNUNET_CONFIGURATION_Handle *cfg,
273 struct GNUNET_TESTING_Peer *peer)
276 struct GNUNET_SET_OperationHandle *my_oh;
278 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
279 &timeout_fail, NULL);
282 GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "my id (from CRYPTO): %s\n",
285 GNUNET_i2s (&local_id));
286 GNUNET_TESTING_peer_get_identity (peer, &local_id);
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "my id (from TESTING): %s\n",
289 GNUNET_i2s (&local_id));
292 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
293 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
294 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
297 ///* test if canceling an uncommited request works! */
298 my_oh = GNUNET_SET_prepare (&local_id, &app_id, NULL,
299 GNUNET_SET_RESULT_ADDED, NULL, NULL);
301 GNUNET_SET_operation_cancel (my_oh);
303 /* test the real set reconciliation */
309 main (int argc, char **argv)
311 if (0 != GNUNET_TESTING_peer_run ("test_set_api",