-fixing #2405
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_iteration_stop.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 "namestore.h"
28
29 #define VERBOSE GNUNET_NO
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
32
33 static struct GNUNET_NAMESTORE_Handle * nsh;
34
35 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
36 static GNUNET_SCHEDULER_TaskIdentifier stopiteration_task;
37 static struct GNUNET_OS_Process *arm;
38
39 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
40 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
41 static GNUNET_HashCode zone;
42
43 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey2;
44 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey2;
45 static GNUNET_HashCode zone2;
46
47 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
48 static int res;
49 static int returned_records;
50
51 struct GNUNET_CRYPTO_RsaSignature *sig_1;
52 char * s_name_1;
53 struct GNUNET_NAMESTORE_RecordData *s_rd_1;
54
55 struct GNUNET_CRYPTO_RsaSignature *sig_2;
56 char * s_name_2;
57 struct GNUNET_NAMESTORE_RecordData *s_rd_2;
58
59 struct GNUNET_CRYPTO_RsaSignature *sig_3;
60 char * s_name_3;
61 struct GNUNET_NAMESTORE_RecordData *s_rd_3;
62
63 static void
64 start_arm (const char *cfgname)
65 {
66   arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
67                                "gnunet-service-arm", "-c", cfgname,
68 #if VERBOSE_PEERS
69                                "-L", "DEBUG",
70 #else
71                                "-L", "ERROR",
72 #endif
73                                NULL);
74 }
75
76 static void
77 stop_arm ()
78 {
79   if (NULL != arm)
80   {
81     if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
82       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
83     GNUNET_OS_process_wait (arm);
84     GNUNET_OS_process_destroy (arm);
85     arm = NULL;
86   }
87 }
88
89 /**
90  * Re-establish the connection to the service.
91  *
92  * @param cls handle to use to re-connect.
93  * @param tc scheduler context
94  */
95 static void
96 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 {
98   if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
99   {
100     GNUNET_SCHEDULER_cancel (stopiteration_task);
101     stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
102   }
103
104   if (nsh != NULL)
105     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
106   nsh = NULL;
107
108   GNUNET_free_non_null(sig_1);
109   GNUNET_free_non_null(sig_2);
110   GNUNET_free_non_null(sig_3);
111   GNUNET_free_non_null(s_name_1);
112   GNUNET_free_non_null(s_name_2);
113   GNUNET_free_non_null(s_name_3);
114
115   if (s_rd_1 != NULL)
116   {
117     GNUNET_free ((void *)s_rd_1->data);
118     GNUNET_free (s_rd_1);
119   }
120   if (s_rd_2 != NULL)
121   {
122     GNUNET_free ((void *)s_rd_2->data);
123     GNUNET_free (s_rd_2);
124   }
125   if (s_rd_3 != NULL)
126   {
127     GNUNET_free ((void *)s_rd_3->data);
128     GNUNET_free (s_rd_3);
129   }
130
131   if (privkey != NULL)
132     GNUNET_CRYPTO_rsa_key_free (privkey);
133   privkey = NULL;
134
135   if (privkey2 != NULL)
136     GNUNET_CRYPTO_rsa_key_free (privkey2);
137   privkey2 = NULL;
138
139   if (NULL != arm)
140     stop_arm();
141
142   res = 1;
143 }
144
145
146 static void
147 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
148 {
149   if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
150   {
151     GNUNET_SCHEDULER_cancel (stopiteration_task);
152     stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
153   }
154
155   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
156   {
157     GNUNET_SCHEDULER_cancel (endbadly_task);
158     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
159   }
160
161   if (privkey != NULL)
162     GNUNET_CRYPTO_rsa_key_free (privkey);
163   privkey = NULL;
164
165   if (privkey2 != NULL)
166     GNUNET_CRYPTO_rsa_key_free (privkey2);
167   privkey2 = NULL;
168
169   GNUNET_free (sig_1);
170   GNUNET_free (sig_2);
171   GNUNET_free (sig_3);
172   GNUNET_free (s_name_1);
173   GNUNET_free (s_name_2);
174   GNUNET_free (s_name_3);
175   if (s_rd_1 != NULL)
176   {
177     GNUNET_free ((void *)s_rd_1->data);
178     GNUNET_free (s_rd_1);
179   }
180   if (s_rd_2 != NULL)
181   {
182     GNUNET_free ((void *)s_rd_2->data);
183     GNUNET_free (s_rd_2);
184   }
185   if (s_rd_3 != NULL)
186   {
187     GNUNET_free ((void *)s_rd_3->data);
188     GNUNET_free (s_rd_3);
189   }
190
191   if (nsh != NULL)
192     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
193   nsh = NULL;
194
195   if (NULL != arm)
196     stop_arm();
197   if (returned_records == 1)
198     res = 0;
199   else
200     res = 1;
201
202 }
203
204 void zone_proc (void *cls,
205                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
206                 struct GNUNET_TIME_Absolute expire,
207                 const char *name,
208                 unsigned int rd_count,
209                 const struct GNUNET_NAMESTORE_RecordData *rd,
210                 const struct GNUNET_CRYPTO_RsaSignature *signature)
211 {
212   int failed = GNUNET_NO;
213
214   if ((zone_key == NULL) &&  (name == NULL))
215   {
216     GNUNET_break (3 == returned_records);
217     if (3 == returned_records)
218       res = 0;
219     else
220       res = 1;
221
222     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received last result, iteration done after receing %u results\n",returned_records );
223     GNUNET_SCHEDULER_add_now (&end, NULL);
224   }
225   else
226   {
227
228     /* verify signature returned from name store */
229     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(zone_key, expire, name, rd_count, rd, signature))
230     {
231       failed = GNUNET_YES;
232       GNUNET_break (0);
233     }
234
235
236     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing results name %s \n", name);
237     if (0 == strcmp (name, s_name_1))
238     { /* name_1 */
239       if (rd_count == 1)
240       {
241         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
242         {
243           failed = GNUNET_YES;
244           GNUNET_break (0);
245         }
246       }
247       else
248       {
249         failed = GNUNET_YES;
250         GNUNET_break (0);
251       }
252       if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(&pubkey, expire, s_name_1, 1, s_rd_1, signature))
253       {
254         failed = GNUNET_YES;
255         GNUNET_break (0);
256       }
257     }
258     else if (0 == strcmp (name, s_name_2))
259     { /* name_2 */
260       if (rd_count == 1)
261       {
262         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
263         {
264           failed = GNUNET_YES;
265           GNUNET_break (0);
266         }
267       }
268       else
269       {
270         failed = GNUNET_YES;
271         GNUNET_break (0);
272       }
273
274       if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(&pubkey, expire, s_name_2, 1, s_rd_2, signature))
275       {
276         failed = GNUNET_YES;
277         GNUNET_break (0);
278       }
279     }
280     else if (0 == strcmp (name, s_name_3))
281     { /* name_3 */
282       if (rd_count == 1)
283       {
284         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_3))
285         {
286           failed = GNUNET_YES;
287           GNUNET_break (0);
288         }
289       }
290       else
291       {
292         failed = GNUNET_YES;
293         GNUNET_break (0);
294       }
295       if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(&pubkey2, expire, s_name_3, 1, s_rd_3, signature))
296       {
297         failed = GNUNET_YES;
298         GNUNET_break (0);
299       }
300     }
301     else
302     {
303       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing result failed: got name `%s'\n", name);
304       res = 1;
305       GNUNET_break (0);
306       GNUNET_SCHEDULER_add_now (&end, NULL);
307     }
308
309     if (failed == GNUNET_NO)
310     {
311       returned_records ++;
312
313       if (1 == returned_records)
314       {
315         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping zone iteration after %u received record \n",returned_records );
316         GNUNET_NAMESTORE_zone_iteration_stop (zi);
317         if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
318         {
319           GNUNET_SCHEDULER_cancel (endbadly_task);
320           endbadly_task = GNUNET_SCHEDULER_NO_TASK;
321         }
322         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &end , NULL);
323         return;
324       }
325       else
326       {
327         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Telling namestore to send the next result\n");
328         GNUNET_NAMESTORE_zone_iterator_next (zi);
329       }
330     }
331     else
332     {
333       GNUNET_break (0);
334       GNUNET_SCHEDULER_add_now (&end, NULL);
335     }
336   }
337 }
338
339 void
340 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
341 {
342   char *afsdir;
343
344   if (GNUNET_OK ==
345       GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
346                                                "FILENAME", &afsdir))
347   {
348     if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
349       if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
350         if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
351           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
352    GNUNET_free (afsdir);
353   }
354
355 }
356
357 void
358 put_cont (void *cls, int32_t success, const char *emsg)
359 {
360   static int c = 0;
361
362   if (success == GNUNET_OK)
363   {
364     c++;
365     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
366   }
367   else
368   {
369     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
370     GNUNET_break (0);
371     GNUNET_SCHEDULER_cancel (endbadly_task);
372     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
373   }
374
375   if (c == 3)
376   {
377     res = 1;
378     returned_records = 0;
379     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
380     zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
381                                         NULL,
382                                         GNUNET_NAMESTORE_RF_NONE,
383                                         GNUNET_NAMESTORE_RF_NONE,
384                                         zone_proc,
385                                         &zone);
386     if (zi == NULL)
387     {
388       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
389       GNUNET_break (0);
390       GNUNET_SCHEDULER_cancel (endbadly_task);
391       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
392     }
393   }
394 }
395
396 static struct GNUNET_NAMESTORE_RecordData *
397 create_record (int count)
398 {
399   int c;
400   struct GNUNET_NAMESTORE_RecordData * rd;
401   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
402
403   for (c = 0; c < count; c++)
404   {
405     rd[c].expiration = GNUNET_TIME_absolute_get();
406     rd[c].record_type = 1111;
407     rd[c].data_size = 50;
408     rd[c].data = GNUNET_malloc(50);
409     memset ((char *) rd[c].data, 'a', 50);
410   }
411   return rd;
412 }
413
414 static void
415 run (void *cls, char *const *args, const char *cfgfile,
416      const struct GNUNET_CONFIGURATION_Handle *cfg)
417 {
418   delete_existing_db(cfg);
419   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,&endbadly, NULL);
420
421   char *hostkey_file;
422   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
423       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
424   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
425   privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
426   GNUNET_free (hostkey_file);
427   GNUNET_assert (privkey != NULL);
428   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
429   GNUNET_CRYPTO_hash(&pubkey, sizeof (pubkey), &zone);
430
431   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
432       "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
433   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
434   privkey2 = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
435   GNUNET_free (hostkey_file);
436   GNUNET_assert (privkey2 != NULL);
437   GNUNET_CRYPTO_rsa_key_get_public(privkey2, &pubkey2);
438   GNUNET_CRYPTO_hash(&pubkey2, sizeof (pubkey), &zone2);
439
440
441   start_arm (cfgfile);
442   GNUNET_assert (arm != NULL);
443
444   nsh = GNUNET_NAMESTORE_connect (cfg);
445   GNUNET_break (NULL != nsh);
446
447   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
448
449   GNUNET_asprintf(&s_name_1, "dummy1");
450   s_rd_1 = create_record(1);
451   sig_1 = GNUNET_NAMESTORE_create_signature(privkey, s_rd_1[0].expiration, s_name_1, s_rd_1, 1);
452   GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_1, s_rd_1, &put_cont, NULL);
453
454
455   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
456   GNUNET_asprintf(&s_name_2, "dummy2");
457   s_rd_2 = create_record(1);
458
459   sig_2 = GNUNET_NAMESTORE_create_signature(privkey, s_rd_2[0].expiration, s_name_2, s_rd_2, 1);
460   GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_2, s_rd_2, &put_cont, NULL);
461
462   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
463   /* name in different zone */
464   GNUNET_asprintf(&s_name_3, "dummy3");
465   s_rd_3 = create_record(1);
466   sig_3 = GNUNET_NAMESTORE_create_signature(privkey2, s_rd_3[0].expiration, s_name_3, s_rd_3, 1);
467   GNUNET_NAMESTORE_record_put (nsh, &pubkey2, s_name_3, GNUNET_TIME_UNIT_FOREVER_ABS, 1, s_rd_3, sig_3, &put_cont, NULL);
468 }
469
470 static int
471 check ()
472 {
473   static char *const argv[] = { "test_namestore_api_zone_iteration",
474     "-c",
475     "test_namestore_api.conf",
476 #if VERBOSE
477     "-L", "DEBUG",
478 #endif
479     NULL
480   };
481   static struct GNUNET_GETOPT_CommandLineOption options[] = {
482     GNUNET_GETOPT_OPTION_END
483   };
484
485   res = 1;
486   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test_namestore_api_zone_iteration",
487                       "nohelp", options, &run, &res);
488   return res;
489 }
490
491 int
492 main (int argc, char *argv[])
493 {
494   int ret;
495
496   ret = check ();
497
498   return ret;
499 }
500
501 /* end of test_namestore_api_zone_iteration.c */