-fix
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_iteration_specific_zone.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_specific_zone.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, 10)
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_close (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
196   if (NULL != arm)
197     stop_arm();
198 }
199
200 void zone_proc (void *cls,
201                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
202                 struct GNUNET_TIME_Absolute expire,
203                 const char *name,
204                 unsigned int rd_count,
205                 const struct GNUNET_NAMESTORE_RecordData *rd,
206                 const struct GNUNET_CRYPTO_RsaSignature *signature)
207 {
208   int failed = GNUNET_NO;
209   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for zone `%s'\n", GNUNET_h2s (&zone));
210   if ((zone_key == NULL) &&  (name == NULL))
211   {
212     GNUNET_break (2 == returned_records);
213     if (2 == returned_records)
214       res = 0;
215     else
216       res = 1;
217
218     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received last result, iteration done after %u records\n", returned_records);
219     GNUNET_SCHEDULER_add_now (&end, NULL);
220   }
221   else
222   {
223     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing results name %s \n", name);
224     if (0 == strcmp (name, s_name_1))
225     {
226       if (rd_count == 1)
227       {
228         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
229         {
230           failed = GNUNET_YES;
231           GNUNET_break (0);
232         }
233       }
234       else
235       {
236         failed = GNUNET_YES;
237         GNUNET_break (0);
238       }
239       if (0 != memcmp (signature, sig_1, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
240       {
241         failed = GNUNET_YES;
242         GNUNET_break (0);
243       }
244     }
245     else if (0 == strcmp (name, s_name_2))
246     {
247       if (rd_count == 1)
248       {
249         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
250         {
251           failed = GNUNET_YES;
252           GNUNET_break (0);
253         }
254       }
255       else
256       {
257         failed = GNUNET_YES;
258         GNUNET_break (0);
259       }
260       if (0 != memcmp (signature, sig_2, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
261       {
262         failed = GNUNET_YES;
263         GNUNET_break (0);
264       }
265     }
266     else
267     {
268       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing result failed: got name `%s'\n", name);
269       res = 1;
270       GNUNET_break (0);
271       GNUNET_SCHEDULER_add_now (&end, NULL);
272     }
273
274     if (failed == GNUNET_NO)
275     {
276       returned_records ++;
277       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Telling namestore to send the next result\n");
278       GNUNET_NAMESTORE_zone_iterator_next (zi);
279     }
280     else
281     {
282       GNUNET_break (0);
283       GNUNET_SCHEDULER_add_now (&end, NULL);
284     }
285   }
286 }
287
288 void
289 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
290 {
291   char *afsdir;
292
293   if (GNUNET_OK ==
294       GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
295                                                "FILENAME", &afsdir))
296   {
297     if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
298       if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
299         if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
300           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
301    GNUNET_free (afsdir);
302   }
303
304 }
305
306 void
307 put_cont (void *cls, int32_t success, const char *emsg)
308 {
309   static int c = 0;
310
311   if (success == GNUNET_OK)
312   {
313     c++;
314     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
315   }
316   else
317   {
318     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
319     GNUNET_break (0);
320     GNUNET_SCHEDULER_cancel (endbadly_task);
321     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
322   }
323
324   if (c == 3)
325   {
326     res = 1;
327     returned_records = 0;
328     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over zone `%s'\n",
329         GNUNET_h2s(&zone));
330     zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
331                                         &zone,
332                                         GNUNET_NAMESTORE_RF_NONE,
333                                         GNUNET_NAMESTORE_RF_NONE,
334                                         zone_proc,
335                                         &zone);
336     if (zi == NULL)
337     {
338       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
339       GNUNET_break (0);
340       GNUNET_SCHEDULER_cancel (endbadly_task);
341       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
342     }
343   }
344 }
345
346 static struct GNUNET_NAMESTORE_RecordData *
347 create_record (int count)
348 {
349   int c;
350   struct GNUNET_NAMESTORE_RecordData * rd;
351   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
352
353   for (c = 0; c < count; c++)
354   {
355     rd[c].expiration = GNUNET_TIME_absolute_get();
356     rd[c].record_type = 1111;
357     rd[c].data_size = 50;
358     rd[c].data = GNUNET_malloc(50);
359     memset ((char *) rd[c].data, 'a', 50);
360   }
361   return rd;
362 }
363
364 static void
365 run (void *cls, char *const *args, const char *cfgfile,
366      const struct GNUNET_CONFIGURATION_Handle *cfg)
367 {
368   delete_existing_db(cfg);
369   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,&endbadly, NULL);
370
371   char *hostkey_file;
372   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, "4UCICULTINKC87UO4326KEEDQ9MTEP2AJT88MJFVGTGNK12QNGMQI2S41VI07UUU6EO19BTB06PDL0HE6VP1OM50HOJEI75RHP4JP80.zkey");
373   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
374   privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
375   GNUNET_free (hostkey_file);
376   GNUNET_assert (privkey != NULL);
377   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
378   GNUNET_CRYPTO_hash(&pubkey, sizeof (pubkey), &zone);
379
380   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, "KJI3AL00K91EDPFJF58DAJM7H61D189TLP70N56JL8SVDCJE1SJ3SNNBOQPPONTL37FMHPS39SMK2NMVC0GQMGA6QCMHITT78O8GF80.zkey");
381   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
382   privkey2 = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
383   GNUNET_free (hostkey_file);
384   GNUNET_assert (privkey2 != NULL);
385   GNUNET_CRYPTO_rsa_key_get_public(privkey2, &pubkey2);
386   GNUNET_CRYPTO_hash(&pubkey2, sizeof (pubkey), &zone2);
387
388
389   start_arm (cfgfile);
390   GNUNET_assert (arm != NULL);
391
392   nsh = GNUNET_NAMESTORE_connect (cfg);
393   GNUNET_break (NULL != nsh);
394
395   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
396
397   GNUNET_asprintf(&s_name_1, "dummy1");
398   s_rd_1 = create_record(1);
399   sig_1 = GNUNET_NAMESTORE_create_signature(privkey, s_rd_1[0].expiration ,s_name_1, s_rd_1, 1);
400   GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_1, s_rd_1, &put_cont, NULL);
401
402
403   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
404   GNUNET_asprintf(&s_name_2, "dummy2");
405   s_rd_2 = create_record(1);
406
407   sig_2 = GNUNET_NAMESTORE_create_signature(privkey, s_rd_2[0].expiration, s_name_2, s_rd_2, 1);
408   GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_2, s_rd_2, &put_cont, NULL);
409
410   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
411   /* name in different zone */
412   GNUNET_asprintf(&s_name_3, "dummy3");
413   s_rd_3 = create_record(1);
414   sig_3 = GNUNET_NAMESTORE_create_signature(privkey, s_rd_3[0].expiration, s_name_3, s_rd_3, 1);
415   GNUNET_NAMESTORE_record_put (nsh, &pubkey2, s_name_3, GNUNET_TIME_absolute_get_forever(), 1, s_rd_3, sig_3, &put_cont, NULL);
416 }
417
418 static int
419 check ()
420 {
421   static char *const argv[] = { "test_namestore_api_zone_iteration",
422     "-c",
423     "test_namestore_api.conf",
424 #if VERBOSE
425     "-L", "DEBUG",
426 #endif
427     NULL
428   };
429   static struct GNUNET_GETOPT_CommandLineOption options[] = {
430     GNUNET_GETOPT_OPTION_END
431   };
432
433   res = 1;
434   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test_namestore_api_zone_iteration",
435                       "nohelp", options, &run, &res);
436   return res;
437 }
438
439 int
440 main (int argc, char *argv[])
441 {
442   int ret;
443
444   ret = check ();
445
446   return ret;
447 }
448
449 /* end of test_namestore_api_zone_iteration_specific_zone.c */