-fixing some namestore tests
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_iteration.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file namestore/test_namestore_api_zone_iteration.c
22  * @brief testcase for zone iteration functionality: iterate of a specific zone
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib.h"
28 #include "namestore.h"
29
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
32
33
34 static struct GNUNET_NAMESTORE_Handle * nsh;
35
36 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
37
38 static GNUNET_SCHEDULER_TaskIdentifier stopiteration_task;
39
40 static struct GNUNET_CRYPTO_EccPrivateKey * privkey;
41
42 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pubkey;
43
44 static struct GNUNET_HashCode zone;
45
46 static struct GNUNET_CRYPTO_EccPrivateKey * privkey2;
47
48 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pubkey2;
49
50 static struct GNUNET_HashCode zone2;
51
52 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
53
54 static int res;
55
56 static int returned_records;
57
58 static struct GNUNET_CRYPTO_EccSignature *sig_1;
59
60 static char * s_name_1;
61
62 static struct GNUNET_NAMESTORE_RecordData *s_rd_1;
63
64 static struct GNUNET_CRYPTO_EccSignature *sig_2;
65
66 static char * s_name_2;
67
68 static struct GNUNET_NAMESTORE_RecordData *s_rd_2;
69
70 static struct GNUNET_CRYPTO_EccSignature *sig_3;
71
72 static char * s_name_3;
73
74 static struct GNUNET_NAMESTORE_RecordData *s_rd_3;
75
76
77 /**
78  * Re-establish the connection to the service.
79  *
80  * @param cls handle to use to re-connect.
81  * @param tc scheduler context
82  */
83 static void
84 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 {
86   if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
87   {
88     GNUNET_SCHEDULER_cancel (stopiteration_task);
89     stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
90   }
91
92   if (nsh != NULL)
93     GNUNET_NAMESTORE_disconnect (nsh);
94   nsh = NULL;
95
96   GNUNET_free_non_null(sig_1);
97   GNUNET_free_non_null(sig_2);
98   GNUNET_free_non_null(sig_3);
99   GNUNET_free_non_null(s_name_1);
100   GNUNET_free_non_null(s_name_2);
101   GNUNET_free_non_null(s_name_3);
102
103   if (s_rd_1 != NULL)
104   {
105     GNUNET_free ((void *)s_rd_1->data);
106     GNUNET_free (s_rd_1);
107   }
108   if (s_rd_2 != NULL)
109   {
110     GNUNET_free ((void *)s_rd_2->data);
111     GNUNET_free (s_rd_2);
112   }
113   if (s_rd_3 != NULL)
114   {
115     GNUNET_free ((void *)s_rd_3->data);
116     GNUNET_free (s_rd_3);
117   }
118
119   if (privkey != NULL)
120     GNUNET_CRYPTO_ecc_key_free (privkey);
121   privkey = NULL;
122
123   if (privkey2 != NULL)
124     GNUNET_CRYPTO_ecc_key_free (privkey2);
125   privkey2 = NULL;
126   res = 1;
127 }
128
129
130 static void
131 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
132 {
133   if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
134   {
135     GNUNET_SCHEDULER_cancel (stopiteration_task);
136     stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
137   }
138
139   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
140   {
141     GNUNET_SCHEDULER_cancel (endbadly_task);
142     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
143   }
144
145   if (privkey != NULL)
146     GNUNET_CRYPTO_ecc_key_free (privkey);
147   privkey = NULL;
148
149   if (privkey2 != NULL)
150     GNUNET_CRYPTO_ecc_key_free (privkey2);
151   privkey2 = NULL;
152
153   GNUNET_free (sig_1);
154   GNUNET_free (sig_2);
155   GNUNET_free (sig_3);
156   GNUNET_free (s_name_1);
157   GNUNET_free (s_name_2);
158   GNUNET_free (s_name_3);
159   if (s_rd_1 != NULL)
160   {
161     GNUNET_free ((void *)s_rd_1->data);
162     GNUNET_free (s_rd_1);
163   }
164   if (s_rd_2 != NULL)
165   {
166     GNUNET_free ((void *)s_rd_2->data);
167     GNUNET_free (s_rd_2);
168   }
169   if (s_rd_3 != NULL)
170   {
171     GNUNET_free ((void *)s_rd_3->data);
172     GNUNET_free (s_rd_3);
173   }
174   if (nsh != NULL)
175     GNUNET_NAMESTORE_disconnect (nsh);
176   nsh = NULL;
177 }
178
179
180 static void
181 zone_proc (void *cls,
182            const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
183            struct GNUNET_TIME_Absolute expire,
184            const char *name,
185            unsigned int rd_count,
186            const struct GNUNET_NAMESTORE_RecordData *rd,
187            const struct GNUNET_CRYPTO_EccSignature *signature)
188 {
189   int failed = GNUNET_NO;
190   if ((zone_key == NULL) && (name == NULL))
191   {
192     GNUNET_break (3 == returned_records);
193     if (3 == returned_records)
194       res = 0;
195     else
196       res = 1;
197
198     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received last result, iteration done after receing %u results\n",returned_records );
199     GNUNET_SCHEDULER_add_now (&end, NULL);
200   }
201   else
202   {
203     /* verify signature returned from name store */
204     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (zone_key, expire, name, rd_count, rd, signature))
205     {
206       struct GNUNET_HashCode zone_key_hash;
207       GNUNET_CRYPTO_hash (zone_key, sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded), &zone_key_hash);
208       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Verifying signature for `%s' in zone `%s' with %u records  and expiration %llu failed\n", name, GNUNET_h2s(&zone_key_hash), rd_count, expire.abs_value);
209
210       failed = GNUNET_YES;
211       GNUNET_break (0);
212     }
213
214     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing results name %s \n", name);
215     if (0 == strcmp (name, s_name_1))
216     {
217       if (rd_count == 1)
218       {
219         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
220         {
221           failed = GNUNET_YES;
222           GNUNET_break (0);
223         }
224       }
225       else
226       {
227         failed = GNUNET_YES;
228         GNUNET_break (0);
229       }
230       if (0 != memcmp (signature, sig_1, sizeof (struct GNUNET_CRYPTO_EccSignature)))
231       {
232         failed = GNUNET_YES;
233         GNUNET_break (0);
234       }
235     }
236     else if (0 == strcmp (name, s_name_2))
237     {
238       if (rd_count == 1)
239       {
240         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
241         {
242           failed = GNUNET_YES;
243           GNUNET_break (0);
244         }
245       }
246       else
247       {
248         failed = GNUNET_YES;
249         GNUNET_break (0);
250       }
251       if (0 != memcmp (signature, sig_2, sizeof (struct GNUNET_CRYPTO_EccSignature)))
252       {
253         failed = GNUNET_YES;
254         GNUNET_break (0);
255       }
256     }
257     else if (0 == strcmp (name, s_name_3))
258     {
259       if (rd_count == 1)
260       {
261         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_3))
262         {
263           failed = GNUNET_YES;
264           GNUNET_break (0);
265         }
266       }
267       else
268       {
269         failed = GNUNET_YES;
270         GNUNET_break (0);
271       }
272       if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(zone_key, expire, name, rd_count, rd, signature))
273       {
274         failed = GNUNET_YES;
275         GNUNET_break (0);
276       }
277     }
278     else
279     {
280       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing result failed: got name `%s'\n", name);
281       res = 1;
282       GNUNET_break (0);
283       GNUNET_SCHEDULER_add_now (&end, NULL);
284     }
285
286     if (failed == GNUNET_NO)
287     {
288       returned_records ++;
289       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Telling namestore to send the next result\n");
290       GNUNET_NAMESTORE_zone_iterator_next (zi);
291     }
292     else
293     {
294       GNUNET_break (0);
295       GNUNET_SCHEDULER_add_now (&end, NULL);
296     }
297   }
298 }
299
300
301 static void
302 put_cont (void *cls, int32_t success, const char *emsg)
303 {
304   static int c = 0;
305
306   if (success == GNUNET_OK)
307   {
308     c++;
309     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
310   }
311   else
312   {
313     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
314     GNUNET_break (0);
315     GNUNET_SCHEDULER_cancel (endbadly_task);
316     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
317   }
318
319   if (c == 3)
320   {
321     res = 1;
322     returned_records = 0;
323     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
324     zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
325                                                NULL,
326                                                GNUNET_NAMESTORE_RF_NONE,
327                                                GNUNET_NAMESTORE_RF_NONE,
328                                                &zone_proc,
329                                                &zone);
330     if (zi == NULL)
331     {
332       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
333       GNUNET_break (0);
334       GNUNET_SCHEDULER_cancel (endbadly_task);
335       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
336     }
337   }
338 }
339
340
341 static struct GNUNET_NAMESTORE_RecordData *
342 create_record (unsigned int count)
343 {
344   unsigned int c;
345   struct GNUNET_NAMESTORE_RecordData * rd;
346
347   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
348   for (c = 0; c < count; c++)
349   {
350     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value;
351     rd[c].record_type = 1111;
352     rd[c].data_size = 50;
353     rd[c].data = GNUNET_malloc(50);
354     memset ((char *) rd[c].data, 'a', 50);
355   }
356   return rd;
357 }
358
359
360 /**
361  * Callback called from the zone iterator when we iterate over
362  * the empty zone.  Check that we got no records and then
363  * start the actual tests by filling the zone.
364  */
365 static void
366 empty_zone_proc (void *cls,
367                  const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
368                  struct GNUNET_TIME_Absolute expire,
369                  const char *name,
370                  unsigned int rd_count,
371                  const struct GNUNET_NAMESTORE_RecordData *rd,
372                  const struct GNUNET_CRYPTO_EccSignature *signature)
373 {
374   char *hostkey_file;
375   struct GNUNET_TIME_Absolute et;
376
377   GNUNET_assert (nsh == cls);
378   GNUNET_assert (NULL == name);
379   GNUNET_assert (NULL == zone_key);
380   zi = NULL;
381   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
382       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
383   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
384   privkey = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
385   GNUNET_free (hostkey_file);
386   GNUNET_assert (privkey != NULL);
387   GNUNET_CRYPTO_ecc_key_get_public(privkey, &pubkey);
388   GNUNET_CRYPTO_hash(&pubkey, sizeof (pubkey), &zone);
389
390
391   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
392       "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
393   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
394   privkey2 = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
395   GNUNET_free (hostkey_file);
396
397   GNUNET_assert (privkey2 != NULL);
398   GNUNET_CRYPTO_ecc_key_get_public(privkey2, &pubkey2);
399   GNUNET_CRYPTO_hash(&pubkey2, sizeof (pubkey), &zone2);
400
401   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
402
403   GNUNET_asprintf(&s_name_1, "dummy1");
404   s_rd_1 = create_record(1);
405   et.abs_value = s_rd_1->expiration_time;
406   sig_1 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_1, s_rd_1, 1);
407   GNUNET_NAMESTORE_record_put_by_authority (nsh, privkey, s_name_1, 
408                                             1, s_rd_1, 
409                                             &put_cont, NULL);
410
411
412   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
413   GNUNET_asprintf(&s_name_2, "dummy2");
414   s_rd_2 = create_record(1);
415
416   et.abs_value = s_rd_2->expiration_time;
417   sig_2 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_2, s_rd_2, 1);
418   GNUNET_NAMESTORE_record_put_by_authority (nsh, privkey, s_name_2,
419                                             1, s_rd_2, 
420                                             &put_cont, NULL);
421
422   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
423   /* name in different zone */
424   GNUNET_asprintf(&s_name_3, "dummy3");
425   s_rd_3 = create_record(1);
426   et.abs_value = s_rd_3->expiration_time;
427   sig_3 = GNUNET_NAMESTORE_create_signature(privkey2, et, s_name_3, s_rd_3, 1);
428   GNUNET_NAMESTORE_record_put (nsh, &pubkey2, s_name_3, GNUNET_TIME_UNIT_FOREVER_ABS, 1, s_rd_3, sig_3, &put_cont, NULL);
429 }
430
431
432 static void
433 run (void *cls, 
434      const struct GNUNET_CONFIGURATION_Handle *cfg,
435      struct GNUNET_TESTING_Peer *peer)
436 {
437   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
438   nsh = GNUNET_NAMESTORE_connect (cfg);
439   GNUNET_break (NULL != nsh);
440   /* first, iterate over empty namestore */
441   zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
442                                              NULL,
443                                              GNUNET_NAMESTORE_RF_NONE,
444                                              GNUNET_NAMESTORE_RF_NONE,
445                                              &empty_zone_proc,
446                                              nsh);
447   if (NULL == zi)
448   {
449     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
450     GNUNET_break (0);
451     GNUNET_SCHEDULER_cancel (endbadly_task);
452     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
453   }
454 }
455
456
457 int
458 main (int argc, char *argv[])
459 {
460   res = 1;
461   if (0 != 
462       GNUNET_TESTING_service_run ("test-namestore-api-zone-iteration",
463                                   "namestore",
464                                   "test_namestore_api.conf",
465                                   &run,
466                                   NULL))
467     return 1;
468   return res;
469 }
470
471
472 /* end of test_namestore_api_zone_iteration.c */