terminate instantly, even on failure
[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
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.
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      General Public License for more details.
14
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.
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 {
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
105 static int
106 check_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, 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     return GNUNET_NO;
125   }
126
127   ci_cls->ongoing_count += 1;
128   return GNUNET_YES;
129 }
130
131
132 static void
133 check_count (struct GNUNET_SET_Handle *set,
134              char *what,
135              unsigned int expected_count,
136              GNUNET_SCHEDULER_TaskCallback cont,
137              void *cont_cls)
138 {
139   struct CountIterClosure *ci_cls = GNUNET_new (struct CountIterClosure);
140
141   ci_cls->expected_count = expected_count;
142   ci_cls->ongoing_count = 0;
143   ci_cls->cont = cont;
144   ci_cls->cont_cls = cont_cls;
145   ci_cls->what = what;
146
147   GNUNET_assert (GNUNET_YES ==
148                  GNUNET_SET_iterate (set,
149                                      &check_count_iter,
150                                      ci_cls));
151 }
152
153
154 static void
155 test_done (void *cls)
156 {
157   GNUNET_SCHEDULER_shutdown ();
158 }
159
160
161 static void
162 check_new_set_count (void *cls)
163 {
164   check_count (set2,
165                "new set",
166                4,
167                &test_done,
168                NULL);
169 }
170
171
172 static void
173 copy_done (void *cls,
174            struct GNUNET_SET_Handle *new_set)
175 {
176   printf ("copy done\n");
177   set2 = new_set;
178   remove_element_str (set2, "spam");
179   add_element_str (set2, "new1");
180   add_element_str (set2, "new2");
181   remove_element_str (set2, "new2");
182   remove_element_str (set2, "new3");
183   // Check that set1 didn't change.
184   check_count (set1, "old set", 3,
185                &check_new_set_count,
186                NULL);
187 }
188
189
190 static void
191 test_copy (void *cls)
192 {
193   printf ("about to copy\n");
194   GNUNET_SET_copy_lazy (set1,
195                         &copy_done,
196                         NULL);
197 }
198
199
200 /**
201  * Function run on shutdown.
202  *
203  * @param cls closure
204  */
205 static void
206 do_shutdown (void *cls)
207 {
208   if (NULL != tt)
209   {
210     GNUNET_SCHEDULER_cancel (tt);
211     tt = NULL;
212   }
213   if (NULL != set1)
214   {
215     GNUNET_SET_destroy (set1);
216     set1 = NULL;
217   }
218   if (NULL != set2)
219   {
220     GNUNET_SET_destroy (set2);
221     set2 = NULL;
222   }
223 }
224
225
226 /**
227  * Signature of the 'main' function for a (single-peer) testcase that
228  * is run using #GNUNET_TESTING_peer_run().
229  *
230  * @param cls closure
231  * @param cfg configuration of the peer that was started
232  * @param peer identity of the peer that was created
233  */
234 static void
235 run (void *cls,
236      const struct GNUNET_CONFIGURATION_Handle *cfg,
237      struct GNUNET_TESTING_Peer *peer)
238 {
239   tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
240                                      &timeout_fail,
241                                      NULL);
242   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
243                                  NULL);
244   config = cfg;
245   GNUNET_TESTING_peer_get_identity (peer,
246                                     &local_id);
247
248   set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
249   add_element_str (set1, "foo");
250   add_element_str (set1, "bar");
251   /* duplicate -- ignored */
252   add_element_str (set1, "bar");
253   remove_element_str (set1, "foo");
254   /* non-existent -- ignored */
255   remove_element_str (set1, "nonexist1");
256   add_element_str (set1, "spam");
257   /* duplicate -- ignored */
258   remove_element_str (set1, "foo");
259   add_element_str (set1, "eggs");
260
261   check_count (set1,
262                "initial test",
263                3,
264                &test_copy,
265                NULL);
266 }
267
268
269 int
270 main (int argc, char **argv)
271 {
272   if (0 != GNUNET_TESTING_peer_run ("test_set_union_copy",
273                                     "test_set.conf",
274                                     &run, NULL))
275   {
276     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
277                 "failed to start testing peer\n");
278     return 1;
279   }
280   return ret;
281 }