diff options
Diffstat (limited to 'src/service/namestore/test_namestore_api_monitoring.c')
-rw-r--r-- | src/service/namestore/test_namestore_api_monitoring.c | 378 |
1 files changed, 378 insertions, 0 deletions
diff --git a/src/service/namestore/test_namestore_api_monitoring.c b/src/service/namestore/test_namestore_api_monitoring.c new file mode 100644 index 000000000..0b879786c --- /dev/null +++ b/src/service/namestore/test_namestore_api_monitoring.c | |||
@@ -0,0 +1,378 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2013, 2018 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 | * @file namestore/test_namestore_api_monitoring.c | ||
22 | * @brief testcase for zone monitoring functionality: monitor first, then add records | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_namestore_service.h" | ||
26 | #include "gnunet_testing_lib.h" | ||
27 | #include "../service/namestore/namestore.h" | ||
28 | |||
29 | #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT | ||
30 | |||
31 | |||
32 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100) | ||
33 | |||
34 | |||
35 | static struct GNUNET_NAMESTORE_Handle *nsh; | ||
36 | |||
37 | static struct GNUNET_SCHEDULER_Task *endbadly_task; | ||
38 | |||
39 | static struct GNUNET_CRYPTO_PrivateKey privkey; | ||
40 | |||
41 | static struct GNUNET_CRYPTO_PrivateKey privkey2; | ||
42 | |||
43 | static struct GNUNET_NAMESTORE_ZoneMonitor *zm; | ||
44 | |||
45 | static int res; | ||
46 | |||
47 | static char *s_name_1; | ||
48 | |||
49 | static struct GNUNET_GNSRECORD_Data *s_rd_1; | ||
50 | |||
51 | static char *s_name_2; | ||
52 | |||
53 | static struct GNUNET_GNSRECORD_Data *s_rd_2; | ||
54 | |||
55 | static char *s_name_3; | ||
56 | |||
57 | static struct GNUNET_GNSRECORD_Data *s_rd_3; | ||
58 | |||
59 | struct GNUNET_NAMESTORE_QueueEntry *ns_ops[3]; | ||
60 | |||
61 | |||
62 | static void | ||
63 | do_shutdown () | ||
64 | { | ||
65 | if (NULL != zm) | ||
66 | { | ||
67 | GNUNET_NAMESTORE_zone_monitor_stop (zm); | ||
68 | zm = NULL; | ||
69 | } | ||
70 | if (NULL != ns_ops[0]) | ||
71 | { | ||
72 | GNUNET_NAMESTORE_cancel (ns_ops[0]); | ||
73 | ns_ops[0] = NULL; | ||
74 | } | ||
75 | if (NULL != ns_ops[1]) | ||
76 | { | ||
77 | GNUNET_NAMESTORE_cancel (ns_ops[1]); | ||
78 | ns_ops[1] = NULL; | ||
79 | } | ||
80 | if (NULL != ns_ops[2]) | ||
81 | { | ||
82 | GNUNET_NAMESTORE_cancel (ns_ops[2]); | ||
83 | ns_ops[2] = NULL; | ||
84 | } | ||
85 | if (NULL != nsh) | ||
86 | { | ||
87 | GNUNET_NAMESTORE_disconnect (nsh); | ||
88 | nsh = NULL; | ||
89 | } | ||
90 | GNUNET_free (s_name_1); | ||
91 | GNUNET_free (s_name_2); | ||
92 | GNUNET_free (s_name_3); | ||
93 | |||
94 | if (s_rd_1 != NULL) | ||
95 | { | ||
96 | GNUNET_free_nz ((void *) s_rd_1->data); | ||
97 | GNUNET_free (s_rd_1); | ||
98 | } | ||
99 | if (s_rd_2 != NULL) | ||
100 | { | ||
101 | GNUNET_free_nz ((void *) s_rd_2->data); | ||
102 | GNUNET_free (s_rd_2); | ||
103 | } | ||
104 | if (s_rd_3 != NULL) | ||
105 | { | ||
106 | GNUNET_free_nz ((void *) s_rd_3->data); | ||
107 | GNUNET_free (s_rd_3); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | |||
112 | /** | ||
113 | * Re-establish the connection to the service. | ||
114 | * | ||
115 | * @param cls handle to use to re-connect. | ||
116 | */ | ||
117 | static void | ||
118 | endbadly (void *cls) | ||
119 | { | ||
120 | do_shutdown (); | ||
121 | res = 1; | ||
122 | } | ||
123 | |||
124 | |||
125 | static void | ||
126 | end (void *cls) | ||
127 | { | ||
128 | do_shutdown (); | ||
129 | res = 0; | ||
130 | } | ||
131 | |||
132 | |||
133 | static void | ||
134 | zone_proc (void *cls, | ||
135 | const struct GNUNET_CRYPTO_PrivateKey *zone_key, | ||
136 | const char *name, | ||
137 | unsigned int rd_count, | ||
138 | const struct GNUNET_GNSRECORD_Data *rd) | ||
139 | { | ||
140 | static int returned_records; | ||
141 | static int fail = GNUNET_NO; | ||
142 | |||
143 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
144 | "Comparing results name %s\n", | ||
145 | name); | ||
146 | if (0 != GNUNET_memcmp (zone_key, | ||
147 | &privkey)) | ||
148 | { | ||
149 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
150 | "Monitoring returned wrong zone key\n"); | ||
151 | GNUNET_break (0); | ||
152 | GNUNET_SCHEDULER_cancel (endbadly_task); | ||
153 | endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL); | ||
154 | return; | ||
155 | } | ||
156 | |||
157 | if (0 == strcmp (name, s_name_1)) | ||
158 | { | ||
159 | if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1)) | ||
160 | { | ||
161 | GNUNET_break (0); | ||
162 | fail = GNUNET_YES; | ||
163 | } | ||
164 | } | ||
165 | else if (0 == strcmp (name, s_name_2)) | ||
166 | { | ||
167 | if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_2)) | ||
168 | { | ||
169 | GNUNET_break (0); | ||
170 | fail = GNUNET_YES; | ||
171 | } | ||
172 | } | ||
173 | else | ||
174 | { | ||
175 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
176 | "Invalid name %s\n", | ||
177 | name); | ||
178 | GNUNET_break (0); | ||
179 | fail = GNUNET_YES; | ||
180 | } | ||
181 | GNUNET_NAMESTORE_zone_monitor_next (zm, | ||
182 | 1); | ||
183 | if (2 == ++returned_records) | ||
184 | { | ||
185 | if (endbadly_task != NULL) | ||
186 | { | ||
187 | GNUNET_SCHEDULER_cancel (endbadly_task); | ||
188 | endbadly_task = NULL; | ||
189 | } | ||
190 | if (GNUNET_YES == fail) | ||
191 | GNUNET_SCHEDULER_add_now (&endbadly, NULL); | ||
192 | else | ||
193 | GNUNET_SCHEDULER_add_now (&end, NULL); | ||
194 | } | ||
195 | } | ||
196 | |||
197 | |||
198 | static void | ||
199 | put_cont (void *cls, | ||
200 | enum GNUNET_ErrorCode ec) | ||
201 | { | ||
202 | static int c = 0; | ||
203 | char *label = cls; | ||
204 | |||
205 | if (0 == strcmp (label, s_name_1)) | ||
206 | ns_ops[0] = NULL; | ||
207 | else if (0 == strcmp (label, s_name_2)) | ||
208 | ns_ops[1] = NULL; | ||
209 | else if (0 == strcmp (label, s_name_3)) | ||
210 | ns_ops[2] = NULL; | ||
211 | |||
212 | if (GNUNET_EC_NONE == ec) | ||
213 | { | ||
214 | c++; | ||
215 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
216 | "Created record %u: `%s'\n", | ||
217 | c, | ||
218 | label); | ||
219 | } | ||
220 | else | ||
221 | { | ||
222 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
223 | "Failed to create record `%s'\n", | ||
224 | label); | ||
225 | GNUNET_break (0); | ||
226 | GNUNET_SCHEDULER_cancel (endbadly_task); | ||
227 | endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, | ||
228 | NULL); | ||
229 | } | ||
230 | } | ||
231 | |||
232 | |||
233 | static struct GNUNET_GNSRECORD_Data * | ||
234 | create_record (unsigned int count) | ||
235 | { | ||
236 | struct GNUNET_GNSRECORD_Data *rd; | ||
237 | |||
238 | rd = GNUNET_new_array (count, | ||
239 | struct GNUNET_GNSRECORD_Data); | ||
240 | for (unsigned int c = 0; c < count; c++) | ||
241 | { | ||
242 | rd[c].expiration_time = GNUNET_TIME_relative_to_absolute ( | ||
243 | GNUNET_TIME_UNIT_HOURS).abs_value_us; | ||
244 | rd[c].record_type = TEST_RECORD_TYPE; | ||
245 | rd[c].data_size = 50; | ||
246 | rd[c].data = GNUNET_malloc (50); | ||
247 | rd[c].flags = 0; | ||
248 | memset ((char *) rd[c].data, 'a', 50); | ||
249 | } | ||
250 | return rd; | ||
251 | } | ||
252 | |||
253 | |||
254 | static void | ||
255 | fail_cb (void *cls) | ||
256 | { | ||
257 | GNUNET_assert (0); | ||
258 | } | ||
259 | |||
260 | |||
261 | static void | ||
262 | sync_cb (void *cls) | ||
263 | { | ||
264 | /* do nothing */ | ||
265 | } | ||
266 | |||
267 | |||
268 | static void | ||
269 | run (void *cls, | ||
270 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
271 | struct GNUNET_TESTING_Peer *peer) | ||
272 | { | ||
273 | res = 1; | ||
274 | privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); | ||
275 | GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key); | ||
276 | /* Start monitoring */ | ||
277 | zm = GNUNET_NAMESTORE_zone_monitor_start (cfg, | ||
278 | &privkey, | ||
279 | GNUNET_YES, | ||
280 | &fail_cb, | ||
281 | NULL, | ||
282 | &zone_proc, | ||
283 | NULL, | ||
284 | &sync_cb, | ||
285 | NULL); | ||
286 | if (NULL == zm) | ||
287 | { | ||
288 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
289 | "Failed to create zone monitor\n"); | ||
290 | GNUNET_break (0); | ||
291 | endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL); | ||
292 | return; | ||
293 | } | ||
294 | |||
295 | endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL); | ||
296 | /* Connect to namestore */ | ||
297 | nsh = GNUNET_NAMESTORE_connect (cfg); | ||
298 | if (NULL == nsh) | ||
299 | { | ||
300 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connect to namestore\n"); | ||
301 | GNUNET_break (0); | ||
302 | endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL); | ||
303 | return; | ||
304 | } | ||
305 | |||
306 | privkey2.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); | ||
307 | GNUNET_CRYPTO_ecdsa_key_create (&privkey2.ecdsa_key); | ||
308 | |||
309 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
310 | "Created record 3\n"); | ||
311 | /* name in different zone */ | ||
312 | GNUNET_asprintf (&s_name_3, "dummy3"); | ||
313 | s_rd_3 = create_record (1); | ||
314 | GNUNET_assert (NULL != (ns_ops[2] = | ||
315 | GNUNET_NAMESTORE_records_store (nsh, | ||
316 | &privkey2, | ||
317 | s_name_3, | ||
318 | 1, | ||
319 | s_rd_3, | ||
320 | &put_cont, | ||
321 | s_name_3))); | ||
322 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
323 | "Created record 1\n"); | ||
324 | GNUNET_asprintf (&s_name_1, "dummy1"); | ||
325 | s_rd_1 = create_record (1); | ||
326 | GNUNET_assert (NULL != (ns_ops[0] = | ||
327 | GNUNET_NAMESTORE_records_store (nsh, | ||
328 | &privkey, | ||
329 | s_name_1, | ||
330 | 1, | ||
331 | s_rd_1, | ||
332 | &put_cont, | ||
333 | s_name_1))); | ||
334 | |||
335 | |||
336 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n"); | ||
337 | GNUNET_asprintf (&s_name_2, "dummy2"); | ||
338 | s_rd_2 = create_record (1); | ||
339 | GNUNET_assert (NULL != (ns_ops[1] = | ||
340 | GNUNET_NAMESTORE_records_store (nsh, | ||
341 | &privkey, | ||
342 | s_name_2, | ||
343 | 1, | ||
344 | s_rd_2, | ||
345 | &put_cont, | ||
346 | s_name_2))); | ||
347 | } | ||
348 | |||
349 | |||
350 | #include "test_common.c" | ||
351 | |||
352 | |||
353 | int | ||
354 | main (int argc, | ||
355 | char *argv[]) | ||
356 | { | ||
357 | char *plugin_name; | ||
358 | char *cfg_name; | ||
359 | |||
360 | SETUP_CFG (plugin_name, cfg_name); | ||
361 | res = 1; | ||
362 | if (0 != | ||
363 | GNUNET_TESTING_peer_run ("test-namestore-api-monitoring", | ||
364 | cfg_name, | ||
365 | &run, | ||
366 | NULL)) | ||
367 | { | ||
368 | res = 1; | ||
369 | } | ||
370 | GNUNET_DISK_purge_cfg_dir (cfg_name, | ||
371 | "GNUNET_TEST_HOME"); | ||
372 | GNUNET_free (plugin_name); | ||
373 | GNUNET_free (cfg_name); | ||
374 | return res; | ||
375 | } | ||
376 | |||
377 | |||
378 | /* end of test_namestore_api_monitoring.c */ | ||