-rasp pi redefines slow
[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_testing_lib-new.h"
27 #include "gnunet_namestore_service.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_RsaPrivateKey * privkey;
41
42 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
43
44 static struct GNUNET_CRYPTO_ShortHashCode zone;
45
46 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey2;
47
48 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey2;
49
50 static struct GNUNET_CRYPTO_ShortHashCode 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_RsaSignature *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_RsaSignature *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_RsaSignature *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   GNUNET_free_non_null(sig_1);
96   GNUNET_free_non_null(sig_2);
97   GNUNET_free_non_null(sig_3);
98   GNUNET_free_non_null(s_name_1);
99   GNUNET_free_non_null(s_name_2);
100   GNUNET_free_non_null(s_name_3);
101   if (s_rd_1 != NULL)
102   {
103     GNUNET_free ((void *)s_rd_1->data);
104     GNUNET_free (s_rd_1);
105   }
106   if (s_rd_2 != NULL)
107   {
108     GNUNET_free ((void *)s_rd_2->data);
109     GNUNET_free (s_rd_2);
110   }
111   if (s_rd_3 != NULL)
112   {
113     GNUNET_free ((void *)s_rd_3->data);
114     GNUNET_free (s_rd_3);
115   }
116
117   if (privkey != NULL)
118     GNUNET_CRYPTO_rsa_key_free (privkey);
119   privkey = NULL;
120
121   if (privkey2 != NULL)
122     GNUNET_CRYPTO_rsa_key_free (privkey2);
123   privkey2 = NULL;
124   res = 1;
125 }
126
127
128 static void
129 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
130 {
131   if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
132   {
133     GNUNET_SCHEDULER_cancel (stopiteration_task);
134     stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
135   }
136   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
137   {
138     GNUNET_SCHEDULER_cancel (endbadly_task);
139     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
140   }
141   if (privkey != NULL)
142     GNUNET_CRYPTO_rsa_key_free (privkey);
143   privkey = NULL;
144   if (privkey2 != NULL)
145     GNUNET_CRYPTO_rsa_key_free (privkey2);
146   privkey2 = NULL;
147
148   GNUNET_free (sig_1);
149   GNUNET_free (sig_2);
150   GNUNET_free (sig_3);
151   GNUNET_free (s_name_1);
152   GNUNET_free (s_name_2);
153   GNUNET_free (s_name_3);
154   if (s_rd_1 != NULL)
155   {
156     GNUNET_free ((void *)s_rd_1->data);
157     GNUNET_free (s_rd_1);
158   }
159   if (s_rd_2 != NULL)
160   {
161     GNUNET_free ((void *)s_rd_2->data);
162     GNUNET_free (s_rd_2);
163   }
164   if (s_rd_3 != NULL)
165   {
166     GNUNET_free ((void *)s_rd_3->data);
167     GNUNET_free (s_rd_3);
168   }
169   if (nsh != NULL)
170     GNUNET_NAMESTORE_disconnect (nsh);
171   nsh = NULL;
172 }
173
174
175 static void
176 zone_proc (void *cls,
177            const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
178            struct GNUNET_TIME_Absolute expire,
179            const char *name,
180            unsigned int rd_count,
181            const struct GNUNET_NAMESTORE_RecordData *rd,
182            const struct GNUNET_CRYPTO_RsaSignature *signature)
183 {
184   int failed = GNUNET_NO;
185   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for zone `%s'\n", GNUNET_short_h2s (&zone));
186   if ((zone_key == NULL) &&  (name == NULL))
187   {
188     GNUNET_break (2 == returned_records);
189     if (2 == returned_records)
190       res = 0;
191     else
192       res = 1;
193
194     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received last result, iteration done after %u records\n", returned_records);
195     GNUNET_SCHEDULER_add_now (&end, NULL);
196   }
197   else
198   {
199     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing results name %s \n", name);
200     if (0 == strcmp (name, s_name_1))
201     {
202       if (rd_count == 1)
203       {
204         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
205         {
206           failed = GNUNET_YES;
207           GNUNET_break (0);
208         }
209       }
210       else
211       {
212         failed = GNUNET_YES;
213         GNUNET_break (0);
214       }
215       if (0 != memcmp (signature, sig_1, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
216       {
217         failed = GNUNET_YES;
218         GNUNET_break (0);
219       }
220     }
221     else if (0 == strcmp (name, s_name_2))
222     {
223       if (rd_count == 1)
224       {
225         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
226         {
227           failed = GNUNET_YES;
228           GNUNET_break (0);
229         }
230       }
231       else
232       {
233         failed = GNUNET_YES;
234         GNUNET_break (0);
235       }
236       if (0 != memcmp (signature, sig_2, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
237       {
238         failed = GNUNET_YES;
239         GNUNET_break (0);
240       }
241     }
242     else
243     {
244       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing result failed: got name `%s'\n", name);
245       res = 1;
246       GNUNET_break (0);
247       GNUNET_SCHEDULER_add_now (&end, NULL);
248     }
249
250     if (failed == GNUNET_NO)
251     {
252       returned_records ++;
253       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Telling namestore to send the next result\n");
254       GNUNET_NAMESTORE_zone_iterator_next (zi);
255     }
256     else
257     {
258       GNUNET_break (0);
259       GNUNET_SCHEDULER_add_now (&end, NULL);
260     }
261   }
262 }
263
264
265 static void
266 put_cont (void *cls, int32_t success, const char *emsg)
267 {
268   static int c = 0;
269
270   if (success == GNUNET_OK)
271   {
272     c++;
273     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
274   }
275   else
276   {
277     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
278     GNUNET_break (0);
279     GNUNET_SCHEDULER_cancel (endbadly_task);
280     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
281   }
282
283   if (c == 3)
284   {
285     res = 1;
286     returned_records = 0;
287     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over zone `%s'\n",
288         GNUNET_short_h2s(&zone));
289     zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
290                                         &zone,
291                                         GNUNET_NAMESTORE_RF_NONE,
292                                         GNUNET_NAMESTORE_RF_NONE,
293                                         zone_proc,
294                                         &zone);
295     if (zi == NULL)
296     {
297       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
298       GNUNET_break (0);
299       GNUNET_SCHEDULER_cancel (endbadly_task);
300       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
301     }
302   }
303 }
304
305
306 static struct GNUNET_NAMESTORE_RecordData *
307 create_record (unsigned int count)
308 {
309   unsigned int c;
310   struct GNUNET_NAMESTORE_RecordData * rd;
311
312   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
313   for (c = 0; c < count; c++)
314   {
315     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value;
316     rd[c].record_type = 1111;
317     rd[c].data_size = 50;
318     rd[c].data = GNUNET_malloc(50);
319     memset ((char *) rd[c].data, 'a', 50);
320   }
321   return rd;
322 }
323
324
325 static void
326 run (void *cls, 
327      const struct GNUNET_CONFIGURATION_Handle *cfg,
328      struct GNUNET_TESTING_Peer *peer)
329 {
330   char *hostkey_file;
331   struct GNUNET_TIME_Absolute et;
332
333   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,&endbadly, NULL);
334   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
335       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
336   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
337   privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
338   GNUNET_free (hostkey_file);
339   GNUNET_assert (privkey != NULL);
340   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
341   GNUNET_CRYPTO_short_hash (&pubkey, sizeof (pubkey), &zone);
342
343   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
344       "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
345   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
346   privkey2 = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
347   GNUNET_free (hostkey_file);
348   GNUNET_assert (privkey2 != NULL);
349   GNUNET_CRYPTO_rsa_key_get_public(privkey2, &pubkey2);
350   GNUNET_CRYPTO_short_hash (&pubkey2, sizeof (pubkey), &zone2);
351   nsh = GNUNET_NAMESTORE_connect (cfg);
352   GNUNET_break (NULL != nsh);
353
354   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
355   GNUNET_asprintf(&s_name_1, "dummy1");
356   s_rd_1 = create_record(1);
357   et.abs_value = s_rd_1[0].expiration_time;
358   sig_1 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_1, s_rd_1, 1);
359   GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_1, s_rd_1, &put_cont, NULL);
360
361   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
362   GNUNET_asprintf(&s_name_2, "dummy2");
363   s_rd_2 = create_record(1);
364   et.abs_value = s_rd_2[0].expiration_time;
365   sig_2 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_2, s_rd_2, 1);
366   GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_2, s_rd_2, &put_cont, NULL);
367
368   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
369   /* name in different zone */
370   GNUNET_asprintf(&s_name_3, "dummy3");
371   s_rd_3 = create_record(1);
372   et.abs_value = s_rd_3[0].expiration_time;
373   sig_3 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_3, s_rd_3, 1);
374   GNUNET_NAMESTORE_record_put (nsh, &pubkey2, s_name_3, GNUNET_TIME_UNIT_FOREVER_ABS, 1, s_rd_3, sig_3, &put_cont, NULL);
375 }
376
377
378 int
379 main (int argc, char *argv[])
380 {
381   res = 1;
382   if (0 != 
383       GNUNET_TESTING_service_run ("test-namestore-api-zone-iteration-specific-zone",
384                                   "namestore",
385                                   "test_namestore_api.conf",
386                                   &run,
387                                   NULL))
388     return 1;
389   return res;
390 }
391
392 /* end of test_namestore_api_zone_iteration_specific_zone.c */