aboutsummaryrefslogtreecommitdiff
path: root/src/contrib/service/set/test_set_union_copy.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/service/set/test_set_union_copy.c')
-rw-r--r--src/contrib/service/set/test_set_union_copy.c311
1 files changed, 311 insertions, 0 deletions
diff --git a/src/contrib/service/set/test_set_union_copy.c b/src/contrib/service/set/test_set_union_copy.c
new file mode 100644
index 000000000..908527017
--- /dev/null
+++ b/src/contrib/service/set/test_set_union_copy.c
@@ -0,0 +1,311 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2015, 2016 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file set/test_set_union_copy.c
23 * @brief testcase for lazy copying of union sets
24 * @author Florian Dold
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_common.h"
29#include "gnunet_testing_lib.h"
30#include "gnunet_set_service.h"
31
32
33/**
34 * Value to return from #main().
35 */
36static int ret;
37
38static struct GNUNET_PeerIdentity local_id;
39
40static struct GNUNET_SET_Handle *set1;
41
42static struct GNUNET_SET_Handle *set2;
43
44static const struct GNUNET_CONFIGURATION_Handle *config;
45
46static struct GNUNET_SCHEDULER_Task *tt;
47
48
49static void
50add_element_str (struct GNUNET_SET_Handle *set,
51 char *str)
52{
53 struct GNUNET_SET_Element element;
54
55 element.element_type = 0;
56 element.data = str;
57 element.size = strlen (str);
58 GNUNET_SET_add_element (set,
59 &element,
60 NULL,
61 NULL);
62}
63
64
65static void
66remove_element_str (struct GNUNET_SET_Handle *set,
67 char *str)
68{
69 struct GNUNET_SET_Element element;
70
71 element.element_type = 0;
72 element.data = str;
73 element.size = strlen (str);
74 GNUNET_SET_remove_element (set,
75 &element,
76 NULL,
77 NULL);
78}
79
80
81/**
82 * Signature of the main function of a task.
83 *
84 * @param cls closure
85 */
86static void
87timeout_fail (void *cls)
88{
89 tt = NULL;
90 GNUNET_SCHEDULER_shutdown ();
91 ret = 1;
92}
93
94
95struct CountIterClosure
96{
97 unsigned int expected_count;
98 unsigned int ongoing_count;
99 GNUNET_SCHEDULER_TaskCallback cont;
100 void *cont_cls;
101 char *what;
102};
103
104
105static int
106check_count_iter (void *cls,
107 const struct GNUNET_SET_Element *element)
108{
109 struct CountIterClosure *ci_cls = cls;
110
111 if (NULL == element)
112 {
113 if (ci_cls->expected_count != ci_cls->ongoing_count)
114 {
115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 "Expected count (what: %s) to be %u, but it's actually %u\n",
117 ci_cls->what,
118 ci_cls->expected_count,
119 ci_cls->ongoing_count);
120 ret = 1;
121 GNUNET_SCHEDULER_shutdown ();
122 return GNUNET_NO;
123 }
124 ci_cls->cont (ci_cls->cont_cls);
125 GNUNET_free (ci_cls);
126 return GNUNET_NO;
127 }
128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129 "Set `%s' has element %.*s\n",
130 ci_cls->what,
131 (int) element->size,
132 (const char *) element->data);
133
134 ci_cls->ongoing_count++;
135 return GNUNET_YES;
136}
137
138
139static void
140check_count (struct GNUNET_SET_Handle *set,
141 char *what,
142 unsigned int expected_count,
143 GNUNET_SCHEDULER_TaskCallback cont,
144 void *cont_cls)
145{
146 struct CountIterClosure *ci_cls = GNUNET_new (struct CountIterClosure);
147
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Checking count of %s\n",
150 what);
151
152 ci_cls->expected_count = expected_count;
153 ci_cls->ongoing_count = 0;
154 ci_cls->cont = cont;
155 ci_cls->cont_cls = cont_cls;
156 ci_cls->what = what;
157
158 GNUNET_assert (GNUNET_YES ==
159 GNUNET_SET_iterate (set,
160 &check_count_iter,
161 ci_cls));
162}
163
164
165static void
166test_done (void *cls)
167{
168 GNUNET_SCHEDULER_shutdown ();
169}
170
171
172static void
173check_new_set_count (void *cls)
174{
175 check_count (set2,
176 "new set",
177 3,
178 &test_done,
179 NULL);
180}
181
182
183static void
184copy_done (void *cls,
185 struct GNUNET_SET_Handle *new_set)
186{
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 "copy done\n");
189 set2 = new_set;
190 remove_element_str (set2,
191 "k5555");
192 add_element_str (set2,
193 "n66666");
194 add_element_str (set2,
195 "new2butremoved");
196 remove_element_str (set2,
197 "new2butremoved");
198 remove_element_str (set2,
199 "new3justremoved");
200 // Check that set1 didn't change.
201 check_count (set1,
202 "old set",
203 3,
204 &check_new_set_count,
205 NULL);
206}
207
208
209static void
210test_copy (void *cls)
211{
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "about to copy\n");
214 GNUNET_SET_copy_lazy (set1,
215 &copy_done,
216 NULL);
217}
218
219
220/**
221 * Function run on shutdown.
222 *
223 * @param cls closure
224 */
225static void
226do_shutdown (void *cls)
227{
228 if (NULL != tt)
229 {
230 GNUNET_SCHEDULER_cancel (tt);
231 tt = NULL;
232 }
233 if (NULL != set1)
234 {
235 GNUNET_SET_destroy (set1);
236 set1 = NULL;
237 }
238 if (NULL != set2)
239 {
240 GNUNET_SET_destroy (set2);
241 set2 = NULL;
242 }
243}
244
245
246/**
247 * Signature of the 'main' function for a (single-peer) testcase that
248 * is run using #GNUNET_TESTING_peer_run().
249 *
250 * @param cls closure
251 * @param cfg configuration of the peer that was started
252 * @param peer identity of the peer that was created
253 */
254static void
255run (void *cls,
256 const struct GNUNET_CONFIGURATION_Handle *cfg,
257 struct GNUNET_TESTING_Peer *peer)
258{
259 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
260 GNUNET_TIME_UNIT_SECONDS, 5),
261 &timeout_fail,
262 NULL);
263 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
264 NULL);
265 config = cfg;
266 GNUNET_TESTING_peer_get_identity (peer,
267 &local_id);
268
269 set1 = GNUNET_SET_create (cfg,
270 GNUNET_SET_OPERATION_UNION);
271 add_element_str (set1,
272 "333");
273 add_element_str (set1,
274 "k444");
275 /* duplicate -- ignored */
276 add_element_str (set1,
277 "k444");
278 remove_element_str (set1,
279 "333");
280 /* non-existent -- ignored */
281 remove_element_str (set1,
282 "999999999");
283 add_element_str (set1,
284 "k5555");
285 /* duplicate -- ignored */
286 remove_element_str (set1,
287 "333");
288 add_element_str (set1,
289 "k2");
290
291 check_count (set1,
292 "initial test",
293 3,
294 &test_copy,
295 NULL);
296}
297
298
299int
300main (int argc, char **argv)
301{
302 if (0 != GNUNET_TESTING_peer_run ("test_set_union_copy",
303 "test_set.conf",
304 &run, NULL))
305 {
306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307 "failed to start testing peer\n");
308 return 1;
309 }
310 return ret;
311}