fdf6bf9df3848f5bd496e673074c62f0c70c040f
[oweals/gnunet.git] / src / set / test_set_union_copy.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2015 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  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_common.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_set_service.h"
30
31
32 /**
33  * Value to return from #main().
34  */
35 static int ret;
36
37 static struct GNUNET_PeerIdentity local_id;
38
39 static struct GNUNET_SET_Handle *set1;
40
41 static struct GNUNET_SET_Handle *set2;
42
43 static const struct GNUNET_CONFIGURATION_Handle *config;
44
45
46 static void
47 add_element_str (struct GNUNET_SET_Handle *set, char *str)
48 {
49   struct GNUNET_SET_Element element;
50
51   element.element_type = 0;
52   element.data = str;
53   element.size = strlen (str);
54
55   GNUNET_SET_add_element (set, &element, NULL, NULL);
56 }
57
58
59 static void
60 remove_element_str (struct GNUNET_SET_Handle *set, char *str)
61 {
62   struct GNUNET_SET_Element element;
63
64   element.element_type = 0;
65   element.data = str;
66   element.size = strlen (str);
67
68   GNUNET_SET_remove_element (set, &element, NULL, NULL);
69 }
70
71
72 /**
73  * Signature of the main function of a task.
74  *
75  * @param cls closure
76  */
77 static void
78 timeout_fail (void *cls)
79 {
80   const struct GNUNET_SCHEDULER_TaskContext *tc;
81
82   tc = GNUNET_SCHEDULER_get_task_context ();
83   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
84     return;
85   GNUNET_SCHEDULER_shutdown ();
86   ret = 1;
87 }
88
89 typedef void (*Continuation) (void *cls);
90
91
92 struct CountIterClosure
93 {
94   unsigned int expected_count;
95   unsigned int ongoing_count;
96   Continuation cont;
97   void *cont_cls;
98   char *what;
99 };
100
101
102 static int
103 check_count_iter (void *cls,
104                   const struct GNUNET_SET_Element *element)
105 {
106   struct CountIterClosure *ci_cls = cls;
107
108   if (NULL == element)
109   {
110     if (ci_cls->expected_count != ci_cls->ongoing_count)
111     {
112       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
113                   "Expected count (what: %s) to be %u, but it's actually %u\n",
114                   ci_cls->what,
115                   ci_cls->expected_count, ci_cls->ongoing_count);
116       ret = 1;
117       return GNUNET_NO;
118     }
119     ci_cls->cont (ci_cls->cont_cls);
120     return GNUNET_NO;
121   }
122
123   ci_cls->ongoing_count += 1;
124   return GNUNET_YES;
125 }
126
127
128
129 void
130 check_count (struct GNUNET_SET_Handle *set,
131              char *what,
132              unsigned int expected_count,
133              Continuation cont,
134              void *cont_cls)
135 {
136   struct CountIterClosure *ci_cls = GNUNET_new (struct CountIterClosure);
137
138   ci_cls->expected_count = expected_count;
139   ci_cls->ongoing_count = 0;
140   ci_cls->cont = cont;
141   ci_cls->cont_cls = cont_cls;
142   ci_cls->what = what;
143
144   GNUNET_assert (GNUNET_YES == GNUNET_SET_iterate (set, check_count_iter, ci_cls));
145 }
146
147
148 void test_done (void *cls)
149 {
150   if (NULL != set1)
151     GNUNET_SET_destroy (set1);
152   if (NULL != set2)
153     GNUNET_SET_destroy (set2);
154
155   GNUNET_SCHEDULER_shutdown ();
156 }
157
158
159 void check_new_set_count (void *cls)
160 {
161   check_count (set2, "new set", 4, &test_done, NULL);
162 }
163
164
165 void copy_done (void *cls, struct GNUNET_SET_Handle *new_set)
166 {
167   printf ("copy done\n");
168   set2 = new_set;
169   remove_element_str (set2, "spam");
170   add_element_str (set2, "new1");
171   add_element_str (set2, "new2");
172   remove_element_str (set2, "new2");
173   remove_element_str (set2, "new3");
174   // Check that set1 didn't change.
175   check_count (set1, "old set", 3,
176                &check_new_set_count, NULL);
177 }
178
179
180 void test_copy (void *cls)
181 {
182   printf ("about to copy\n");
183   GNUNET_SET_copy_lazy (set1, copy_done, NULL);
184 }
185
186
187
188 /**
189  * Signature of the 'main' function for a (single-peer) testcase that
190  * is run using 'GNUNET_TESTING_peer_run'.
191  *
192  * @param cls closure
193  * @param cfg configuration of the peer that was started
194  * @param peer identity of the peer that was created
195  */
196 static void
197 run (void *cls,
198      const struct GNUNET_CONFIGURATION_Handle *cfg,
199      struct GNUNET_TESTING_Peer *peer)
200 {
201   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
202                                 &timeout_fail,
203                                 NULL);
204
205   config = cfg;
206   GNUNET_TESTING_peer_get_identity (peer,
207                                     &local_id);
208
209   set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
210   add_element_str (set1, "foo");
211   add_element_str (set1, "bar");
212   /* duplicate -- ignored */
213   add_element_str (set1, "bar");
214   remove_element_str (set1, "foo");
215   /* non-existent -- ignored */
216   remove_element_str (set1, "nonexist1");
217   add_element_str (set1, "spam");
218   /* duplicate -- ignored */
219   remove_element_str (set1, "foo");
220   add_element_str (set1, "eggs");
221
222   check_count (set1, "initial test", 3, &test_copy, NULL);
223 }
224
225
226 int
227 main (int argc, char **argv)
228 {
229   if (0 != GNUNET_TESTING_peer_run ("test_set_union_copy",
230                                     "test_set.conf",
231                                     &run, NULL))
232   {
233     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failed to start testing peer\n");
234     return 1;
235   }
236   return ret;
237 }