changing time measurement from milliseconds to microseconds
[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.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_EccPrivateKey * privkey;
41
42 static struct GNUNET_CRYPTO_EccPublicKey pubkey;
43
44 static struct GNUNET_CRYPTO_ShortHashCode zone;
45
46 static struct GNUNET_CRYPTO_EccPrivateKey * privkey2;
47
48 static struct GNUNET_CRYPTO_EccPublicKey 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_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   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_ecc_key_free (privkey);
119   privkey = NULL;
120
121   if (privkey2 != NULL)
122     GNUNET_CRYPTO_ecc_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_ecc_key_free (privkey);
143   privkey = NULL;
144   if (privkey2 != NULL)
145     GNUNET_CRYPTO_ecc_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_EccPublicKey *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_EccSignature *signature)
183 {
184   int failed = GNUNET_NO;
185   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for zone `%s'\n", GNUNET_NAMESTORE_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     }
216     else if (0 == strcmp (name, s_name_2))
217     {
218       if (rd_count == 1)
219       {
220         if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
221         {
222           failed = GNUNET_YES;
223           GNUNET_break (0);
224         }
225       }
226       else
227       {
228         failed = GNUNET_YES;
229         GNUNET_break (0);
230       }
231     }
232     else
233     {
234       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing result failed: got name `%s'\n", name);
235       res = 1;
236       GNUNET_break (0);
237       GNUNET_SCHEDULER_add_now (&end, NULL);
238     }
239
240     if (failed == GNUNET_NO)
241     {
242       returned_records ++;
243       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Telling namestore to send the next result\n");
244       GNUNET_NAMESTORE_zone_iterator_next (zi);
245     }
246     else
247     {
248       GNUNET_break (0);
249       GNUNET_SCHEDULER_add_now (&end, NULL);
250     }
251   }
252 }
253
254
255 static void
256 put_cont (void *cls, int32_t success, const char *emsg)
257 {
258   static int c = 0;
259
260   if (success == GNUNET_OK)
261   {
262     c++;
263     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
264   }
265   else
266   {
267     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
268     GNUNET_break (0);
269     GNUNET_SCHEDULER_cancel (endbadly_task);
270     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
271   }
272
273   if (c == 3)
274   {
275     res = 1;
276     returned_records = 0;
277     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over zone `%s'\n",
278         GNUNET_NAMESTORE_short_h2s(&zone));
279     zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
280                                         &zone,
281                                         GNUNET_NAMESTORE_RF_NONE,
282                                         GNUNET_NAMESTORE_RF_NONE,
283                                         zone_proc,
284                                         &zone);
285     if (zi == NULL)
286     {
287       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
288       GNUNET_break (0);
289       GNUNET_SCHEDULER_cancel (endbadly_task);
290       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
291     }
292   }
293 }
294
295
296 static struct GNUNET_NAMESTORE_RecordData *
297 create_record (unsigned int count)
298 {
299   unsigned int c;
300   struct GNUNET_NAMESTORE_RecordData * rd;
301
302   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
303   for (c = 0; c < count; c++)
304   {
305     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
306     rd[c].record_type = 1111;
307     rd[c].data_size = 50;
308     rd[c].data = GNUNET_malloc(50);
309     memset ((char *) rd[c].data, 'a', 50);
310   }
311   return rd;
312 }
313
314
315 static void
316 run (void *cls, 
317      const struct GNUNET_CONFIGURATION_Handle *cfg,
318      struct GNUNET_TESTING_Peer *peer)
319 {
320   char *hostkey_file;
321   struct GNUNET_TIME_Absolute et;
322
323   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,&endbadly, NULL);
324   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
325       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
326   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
327   privkey = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
328   GNUNET_free (hostkey_file);
329   GNUNET_assert (privkey != NULL);
330   GNUNET_CRYPTO_ecc_key_get_public(privkey, &pubkey);
331   GNUNET_CRYPTO_short_hash (&pubkey, sizeof (pubkey), &zone);
332
333   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
334       "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
335   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
336   privkey2 = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
337   GNUNET_free (hostkey_file);
338   GNUNET_assert (privkey2 != NULL);
339   GNUNET_CRYPTO_ecc_key_get_public(privkey2, &pubkey2);
340   GNUNET_CRYPTO_short_hash (&pubkey2, sizeof (pubkey), &zone2);
341   nsh = GNUNET_NAMESTORE_connect (cfg);
342   GNUNET_break (NULL != nsh);
343
344   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
345   GNUNET_asprintf(&s_name_1, "dummy1");
346   s_rd_1 = create_record(1);
347   et.abs_value_us = s_rd_1[0].expiration_time;
348   sig_1 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_1, s_rd_1, 1);
349   GNUNET_NAMESTORE_record_put_by_authority (nsh, privkey, s_name_1,
350                                             1, s_rd_1, 
351                                             &put_cont, NULL);
352
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
354   GNUNET_asprintf(&s_name_2, "dummy2");
355   s_rd_2 = create_record(1);
356   et.abs_value_us = s_rd_2[0].expiration_time;
357   sig_2 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_2, s_rd_2, 1);
358   GNUNET_NAMESTORE_record_put_by_authority (nsh, privkey, s_name_2,
359                                             1, s_rd_2, 
360                                             &put_cont, NULL);
361
362   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
363   /* name in different zone */
364   GNUNET_asprintf(&s_name_3, "dummy3");
365   s_rd_3 = create_record(1);
366   et.abs_value_us = s_rd_3[0].expiration_time;
367   sig_3 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_3, s_rd_3, 1);
368   GNUNET_NAMESTORE_record_put (nsh, &pubkey2, s_name_3,
369                                GNUNET_TIME_UNIT_FOREVER_ABS, 1, s_rd_3, sig_3, &put_cont, NULL);
370 }
371
372
373 int
374 main (int argc, char *argv[])
375 {
376   res = 1;
377   if (0 != 
378       GNUNET_TESTING_service_run ("test-namestore-api-zone-iteration-specific-zone",
379                                   "namestore",
380                                   "test_namestore_api.conf",
381                                   &run,
382                                   NULL))
383     return 1;
384   return res;
385 }
386
387 /* end of test_namestore_api_zone_iteration_specific_zone.c */