remove CYGWIN codeblocks, drop vendored Windows openvpn, drop win32 specific files.
[oweals/gnunet.git] / src / set / test_set_union_copy.c
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  */
36 static int ret;
37
38 static struct GNUNET_PeerIdentity local_id;
39
40 static struct GNUNET_SET_Handle *set1;
41
42 static struct GNUNET_SET_Handle *set2;
43
44 static const struct GNUNET_CONFIGURATION_Handle *config;
45
46 static struct GNUNET_SCHEDULER_Task *tt;
47
48
49 static void
50 add_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
65 static void
66 remove_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  */
86 static void
87 timeout_fail(void *cls)
88 {
89   tt = NULL;
90   GNUNET_SCHEDULER_shutdown();
91   ret = 1;
92 }
93
94
95 struct CountIterClosure {
96   unsigned int expected_count;
97   unsigned int ongoing_count;
98   GNUNET_SCHEDULER_TaskCallback cont;
99   void *cont_cls;
100   char *what;
101 };
102
103
104 static int
105 check_count_iter(void *cls,
106                  const struct GNUNET_SET_Element *element)
107 {
108   struct CountIterClosure *ci_cls = cls;
109
110   if (NULL == element)
111     {
112       if (ci_cls->expected_count != ci_cls->ongoing_count)
113         {
114           GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
115                      "Expected count (what: %s) to be %u, but it's actually %u\n",
116                      ci_cls->what,
117                      ci_cls->expected_count,
118                      ci_cls->ongoing_count);
119           ret = 1;
120           GNUNET_SCHEDULER_shutdown();
121           return GNUNET_NO;
122         }
123       ci_cls->cont(ci_cls->cont_cls);
124       GNUNET_free(ci_cls);
125       return GNUNET_NO;
126     }
127   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
128              "Set `%s' has element %.*s\n",
129              ci_cls->what,
130              (int)element->size,
131              (const char *)element->data);
132
133   ci_cls->ongoing_count++;
134   return GNUNET_YES;
135 }
136
137
138 static void
139 check_count(struct GNUNET_SET_Handle *set,
140             char *what,
141             unsigned int expected_count,
142             GNUNET_SCHEDULER_TaskCallback cont,
143             void *cont_cls)
144 {
145   struct CountIterClosure *ci_cls = GNUNET_new(struct CountIterClosure);
146
147   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
148              "Checking count of %s\n",
149              what);
150
151   ci_cls->expected_count = expected_count;
152   ci_cls->ongoing_count = 0;
153   ci_cls->cont = cont;
154   ci_cls->cont_cls = cont_cls;
155   ci_cls->what = what;
156
157   GNUNET_assert(GNUNET_YES ==
158                 GNUNET_SET_iterate(set,
159                                    &check_count_iter,
160                                    ci_cls));
161 }
162
163
164 static void
165 test_done(void *cls)
166 {
167   GNUNET_SCHEDULER_shutdown();
168 }
169
170
171 static void
172 check_new_set_count(void *cls)
173 {
174   check_count(set2,
175               "new set",
176               3,
177               &test_done,
178               NULL);
179 }
180
181
182 static void
183 copy_done(void *cls,
184           struct GNUNET_SET_Handle *new_set)
185 {
186   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
187              "copy done\n");
188   set2 = new_set;
189   remove_element_str(set2,
190                      "k5555");
191   add_element_str(set2,
192                   "n66666");
193   add_element_str(set2,
194                   "new2butremoved");
195   remove_element_str(set2,
196                      "new2butremoved");
197   remove_element_str(set2,
198                      "new3justremoved");
199   // Check that set1 didn't change.
200   check_count(set1,
201               "old set",
202               3,
203               &check_new_set_count,
204               NULL);
205 }
206
207
208 static void
209 test_copy(void *cls)
210 {
211   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
212              "about to copy\n");
213   GNUNET_SET_copy_lazy(set1,
214                        &copy_done,
215                        NULL);
216 }
217
218
219 /**
220  * Function run on shutdown.
221  *
222  * @param cls closure
223  */
224 static void
225 do_shutdown(void *cls)
226 {
227   if (NULL != tt)
228     {
229       GNUNET_SCHEDULER_cancel(tt);
230       tt = NULL;
231     }
232   if (NULL != set1)
233     {
234       GNUNET_SET_destroy(set1);
235       set1 = NULL;
236     }
237   if (NULL != set2)
238     {
239       GNUNET_SET_destroy(set2);
240       set2 = NULL;
241     }
242 }
243
244
245 /**
246  * Signature of the 'main' function for a (single-peer) testcase that
247  * is run using #GNUNET_TESTING_peer_run().
248  *
249  * @param cls closure
250  * @param cfg configuration of the peer that was started
251  * @param peer identity of the peer that was created
252  */
253 static void
254 run(void *cls,
255     const struct GNUNET_CONFIGURATION_Handle *cfg,
256     struct GNUNET_TESTING_Peer *peer)
257 {
258   tt = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
259                                     &timeout_fail,
260                                     NULL);
261   GNUNET_SCHEDULER_add_shutdown(&do_shutdown,
262                                 NULL);
263   config = cfg;
264   GNUNET_TESTING_peer_get_identity(peer,
265                                    &local_id);
266
267   set1 = GNUNET_SET_create(cfg,
268                            GNUNET_SET_OPERATION_UNION);
269   add_element_str(set1,
270                   "333");
271   add_element_str(set1,
272                   "k444");
273   /* duplicate -- ignored */
274   add_element_str(set1,
275                   "k444");
276   remove_element_str(set1,
277                      "333");
278   /* non-existent -- ignored */
279   remove_element_str(set1,
280                      "999999999");
281   add_element_str(set1,
282                   "k5555");
283   /* duplicate -- ignored */
284   remove_element_str(set1,
285                      "333");
286   add_element_str(set1,
287                   "k2");
288
289   check_count(set1,
290               "initial test",
291               3,
292               &test_copy,
293               NULL);
294 }
295
296
297 int
298 main(int argc, char **argv)
299 {
300   if (0 != GNUNET_TESTING_peer_run("test_set_union_copy",
301                                    "test_set.conf",
302                                    &run, NULL))
303     {
304       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
305                  "failed to start testing peer\n");
306       return 1;
307     }
308   return ret;
309 }