- zone iteration stuff
[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 namestore_api.c zone iteration functionality
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_NAMESTORE_ZoneIterator *zi;
44 static int res;
45
46 struct GNUNET_CRYPTO_RsaSignature *sig_1;
47 char * s_name_1;
48 struct GNUNET_NAMESTORE_RecordData *s_rd_1;
49
50 struct GNUNET_CRYPTO_RsaSignature *sig_2;
51 char * s_name_2;
52 struct GNUNET_NAMESTORE_RecordData *s_rd_2;
53
54 static void
55 start_arm (const char *cfgname)
56 {
57   arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
58                                "gnunet-service-arm", "-c", cfgname,
59 #if VERBOSE_PEERS
60                                "-L", "DEBUG",
61 #else
62                                "-L", "ERROR",
63 #endif
64                                NULL);
65 }
66
67 static void
68 stop_arm ()
69 {
70   if (NULL != arm)
71   {
72     if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
73       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
74     GNUNET_OS_process_wait (arm);
75     GNUNET_OS_process_close (arm);
76     arm = NULL;
77   }
78 }
79
80 /**
81  * Re-establish the connection to the service.
82  *
83  * @param cls handle to use to re-connect.
84  * @param tc scheduler context
85  */
86 static void
87 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 {
89   if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
90   {
91     GNUNET_SCHEDULER_cancel (stopiteration_task);
92     stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
93   }
94
95   if (nsh != NULL)
96     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
97   nsh = NULL;
98
99   GNUNET_free_non_null(sig_1);
100   GNUNET_free_non_null(sig_2);
101   GNUNET_free_non_null(s_name_1);
102   GNUNET_free_non_null(s_name_2);
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
114   if (privkey != NULL)
115     GNUNET_CRYPTO_rsa_key_free (privkey);
116   privkey = NULL;
117
118   if (NULL != arm)
119     stop_arm();
120
121   res = 1;
122 }
123
124
125 static void
126 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
127 {
128   if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
129   {
130     GNUNET_SCHEDULER_cancel (stopiteration_task);
131     stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
132   }
133
134   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
135   {
136     GNUNET_SCHEDULER_cancel (endbadly_task);
137     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
138   }
139
140
141   if (privkey != NULL)
142     GNUNET_CRYPTO_rsa_key_free (privkey);
143   privkey = NULL;
144
145   GNUNET_free_non_null(sig_1);
146   GNUNET_free_non_null(sig_2);
147   GNUNET_free_non_null(s_name_1);
148   GNUNET_free_non_null(s_name_2);
149   if (s_rd_1 != NULL)
150   {
151     GNUNET_free ((void *)s_rd_1->data);
152     GNUNET_free (s_rd_1);
153   }
154   if (s_rd_2 != NULL)
155   {
156     GNUNET_free ((void *)s_rd_2->data);
157     GNUNET_free (s_rd_2);
158   }
159
160   if (nsh != NULL)
161     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
162   nsh = NULL;
163
164
165   if (NULL != arm)
166     stop_arm();
167
168   res = 0;
169 }
170
171 void zone_proc (void *cls,
172                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
173                 struct GNUNET_TIME_Absolute expire,
174                 const char *name,
175                 unsigned int rd_count,
176                 const struct GNUNET_NAMESTORE_RecordData *rd,
177                 const struct GNUNET_CRYPTO_RsaSignature *signature)
178 {
179   int failed = GNUNET_NO;
180   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for zone `%s'\n", GNUNET_h2s (&zone));
181   if ((zone_key == NULL) &&  (name == NULL))
182   {
183     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received last result, iteration done\n");
184     GNUNET_SCHEDULER_add_now (&end, NULL);
185   }
186   else
187   {
188     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing results name %s \n", name);
189     if (0 == strcmp (name, s_name_1))
190     {
191       if (rd_count == 1)
192       {
193         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
194         {
195           failed = GNUNET_YES;
196           GNUNET_break (0);
197         }
198       }
199       else
200       {
201         failed = GNUNET_YES;
202         GNUNET_break (0);
203       }
204       if (0 != memcmp (signature, sig_1, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
205       {
206         failed = GNUNET_YES;
207         GNUNET_break (0);
208       }
209     }
210     else if (0 == strcmp (name, s_name_2))
211     {
212       if (rd_count == 1)
213       {
214         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
215         {
216           failed = GNUNET_YES;
217           GNUNET_break (0);
218         }
219       }
220       else
221       {
222         failed = GNUNET_YES;
223         GNUNET_break (0);
224       }
225       if (0 != memcmp (signature, sig_2, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
226       {
227         failed = GNUNET_YES;
228         GNUNET_break (0);
229       }
230
231     }
232     else
233     {
234       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing result `%s'\n", name);
235       GNUNET_break (0);
236       GNUNET_SCHEDULER_add_now (&end, NULL);
237     }
238
239     if (failed == GNUNET_NO)
240     {
241       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Telling namestore to send the next result\n");
242       GNUNET_NAMESTORE_zone_iterator_next (zi);
243     }
244     else
245     {
246       GNUNET_break (0);
247       GNUNET_SCHEDULER_add_now (&end, NULL);
248     }
249   }
250 }
251
252 void
253 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
254 {
255   char *afsdir;
256
257   if (GNUNET_OK ==
258       GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
259                                                "FILENAME", &afsdir))
260   {
261     if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
262       if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
263         if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
264           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
265    GNUNET_free (afsdir);
266   }
267
268 }
269
270 void
271 put_cont (void *cls, int32_t success, const char *emsg)
272 {
273   static int c = 0;
274
275   if (success == GNUNET_OK)
276   {
277     c++;
278     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
279
280   }
281
282   if (c == 2)
283   {
284     zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
285                                         NULL,
286                                         GNUNET_NAMESTORE_RF_NONE,
287                                         GNUNET_NAMESTORE_RF_NONE,
288                                         zone_proc,
289                                         &zone);
290     if (zi == NULL)
291     {
292       GNUNET_break (0);
293       GNUNET_SCHEDULER_cancel (endbadly_task);
294       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
295     }
296   }
297 }
298
299 static struct GNUNET_NAMESTORE_RecordData *
300 create_record (int count)
301 {
302   int c;
303   struct GNUNET_NAMESTORE_RecordData * rd;
304   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
305
306   for (c = 0; c < count; c++)
307   {
308   rd[c].expiration = GNUNET_TIME_absolute_get();
309   rd[c].record_type = 1111;
310   rd[c].data_size = 50;
311   rd[c].data = GNUNET_malloc(50);
312   memset ((char *) rd[c].data, 'a', 50);
313   }
314   return rd;
315 }
316
317 static void
318 run (void *cls, char *const *args, const char *cfgfile,
319      const struct GNUNET_CONFIGURATION_Handle *cfg)
320 {
321   delete_existing_db(cfg);
322   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,&endbadly, NULL);
323
324   privkey = GNUNET_CRYPTO_rsa_key_create_from_file("hostkey");
325   GNUNET_assert (privkey != NULL);
326   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
327   GNUNET_CRYPTO_hash(&pubkey, sizeof (pubkey), &zone);
328
329   start_arm (cfgfile);
330   GNUNET_assert (arm != NULL);
331
332   nsh = GNUNET_NAMESTORE_connect (cfg);
333   GNUNET_break (NULL != nsh);
334
335   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
336
337
338
339   GNUNET_asprintf(&s_name_1, "dummy1");
340   s_rd_1 = create_record(1);
341   sig_1 = GNUNET_NAMESTORE_create_signature(privkey, s_name_1, s_rd_1, 1);
342   GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_1, s_rd_1, &put_cont, NULL);
343
344
345   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
346   GNUNET_asprintf(&s_name_2, "dummy2");
347   s_rd_2 = create_record(1);
348
349   sig_2 = GNUNET_NAMESTORE_create_signature(privkey, s_name_2, s_rd_2, 1);
350   GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_2, s_rd_2, &put_cont, NULL);
351 }
352
353 static int
354 check ()
355 {
356   static char *const argv[] = { "test_namestore_api_zone_iteration",
357     "-c",
358     "test_namestore_api.conf",
359 #if VERBOSE
360     "-L", "DEBUG",
361 #endif
362     NULL
363   };
364   static struct GNUNET_GETOPT_CommandLineOption options[] = {
365     GNUNET_GETOPT_OPTION_END
366   };
367
368   res = 1;
369   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test_namestore_api_zone_iteration",
370                       "nohelp", options, &run, &res);
371   return res;
372 }
373
374 int
375 main (int argc, char *argv[])
376 {
377   int ret;
378
379   ret = check ();
380
381   return ret;
382 }
383
384 /* end of test_namestore_api_zone_iteration.c */