-more fixes to namestore
[oweals/gnunet.git] / src / namestore / test_namestore_api_create.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.c
22  * @brief testcase for namestore_api.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib-new.h"
28 #include "namestore.h"
29 #include "gnunet_signatures.h"
30
31
32 #define RECORDS 1
33
34 #define TEST_RECORD_TYPE 1234
35
36 #define TEST_RECORD_DATALEN 123
37
38 #define TEST_RECORD_DATA 'a'
39
40 #define TEST_CREATE_RECORD_TYPE 4321
41
42 #define TEST_CREATE_RECORD_DATALEN 255
43
44 #define TEST_CREATE_RECORD_DATA 'b'
45
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
47
48
49 static struct GNUNET_NAMESTORE_Handle * nsh;
50
51 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
52
53 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
54
55 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
56
57 static struct GNUNET_CRYPTO_RsaSignature *s_signature;
58
59 static struct GNUNET_CRYPTO_RsaSignature *s_signature_updated;
60
61 static struct GNUNET_CRYPTO_ShortHashCode s_zone;
62
63 static struct GNUNET_NAMESTORE_RecordData *s_first_record;
64
65 static struct GNUNET_NAMESTORE_RecordData *s_second_record;
66
67 static char *s_name;
68
69 static int res;
70
71
72 /**
73  * Re-establish the connection to the service.
74  *
75  * @param cls handle to use to re-connect.
76  * @param tc scheduler context
77  */
78 static void
79 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 {
81   if (nsh != NULL)
82     GNUNET_NAMESTORE_disconnect (nsh);
83   nsh = NULL;
84   if (privkey != NULL)
85     GNUNET_CRYPTO_rsa_key_free (privkey);
86   privkey = NULL;
87   res = 1;
88 }
89
90
91 static void
92 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 {
94   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
95   {
96     GNUNET_SCHEDULER_cancel (endbadly_task);
97     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
98   }
99   GNUNET_free ((void *) s_first_record->data);
100   GNUNET_free (s_first_record);
101   GNUNET_free_non_null (s_second_record);
102   if (privkey != NULL)
103     GNUNET_CRYPTO_rsa_key_free (privkey);
104   privkey = NULL;
105   if (nsh != NULL)
106     GNUNET_NAMESTORE_disconnect (nsh);
107   nsh = NULL;
108 }
109
110
111 static void 
112 name_lookup_second_proc (void *cls,
113                          const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
114                          struct GNUNET_TIME_Absolute expire,
115                          const char *n,
116                          unsigned int rd_count,
117                          const struct GNUNET_NAMESTORE_RecordData *rd,
118                          const struct GNUNET_CRYPTO_RsaSignature *signature)
119 {
120   static int found = GNUNET_NO;
121   int failed = GNUNET_NO;
122   int c;
123
124   if (n != NULL)
125   {
126     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n");
127     if (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
128     {
129       GNUNET_break (0);
130       failed = GNUNET_YES;
131     }
132
133     if (0 != strcmp(n, s_name))
134     {
135       GNUNET_break (0);
136       failed = GNUNET_YES;
137     }
138
139     if (2 != rd_count)
140     {
141       GNUNET_break (0);
142       failed = GNUNET_YES;
143     }
144
145     for (c = 0; c < rd_count; c++)
146     {
147       if ((GNUNET_NO == GNUNET_NAMESTORE_records_cmp(&rd[c], s_first_record)) &&
148           (GNUNET_NO == GNUNET_NAMESTORE_records_cmp(&rd[c], s_second_record)))
149       {
150         GNUNET_break (0);
151         failed = GNUNET_YES;
152       }
153     }
154
155     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (zone_key, expire, n, rd_count, rd, signature))
156     {
157       GNUNET_break (0);
158       failed = GNUNET_YES;
159     }
160
161     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (&pubkey, expire, n, rd_count, rd, signature))
162     {
163       GNUNET_break (0);
164       failed = GNUNET_YES;
165     }
166
167     struct GNUNET_NAMESTORE_RecordData rd_new[2];
168     rd_new[0] = *s_first_record;
169     rd_new[1] = *s_second_record;
170     rd_new[1].flags = 0; /* unset GNUNET_NAMESTORE_RF_AUTHORITY */
171     s_signature_updated = GNUNET_NAMESTORE_create_signature (privkey, expire, s_name, rd_new, 2);
172
173     if (0 != memcmp (s_signature_updated, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
174     {
175       GNUNET_break (0);
176       failed = GNUNET_YES;
177     }
178
179     found = GNUNET_YES;
180     if (failed == GNUNET_NO)
181       res = 0;
182     else
183       res = 1;
184   }
185   else
186   {
187     if (found != GNUNET_YES)
188     {
189       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to lookup records for name `%s'\n", s_name);
190       res = 1;
191     }
192     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name);
193   }
194   GNUNET_SCHEDULER_add_now(&end, NULL);
195 }
196
197
198 static void
199 create_second_cont (void *cls, int32_t success, const char *emsg)
200 {
201   char *name = cls;
202
203   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create second record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
204   if (success == GNUNET_OK)
205   {
206     res = 0;
207     GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_second_proc, name);
208   }
209   else
210   {
211     res = 1;
212     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
213     GNUNET_SCHEDULER_add_now(&end, NULL);
214   }
215 }
216
217
218 static void 
219 name_lookup_initial_proc (void *cls,
220                           const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
221                           struct GNUNET_TIME_Absolute expire,
222                           const char *n,
223                           unsigned int rd_count,
224                           const struct GNUNET_NAMESTORE_RecordData *rd,
225                           const struct GNUNET_CRYPTO_RsaSignature *signature)
226 {
227   char * name = cls;
228   static int found = GNUNET_NO;
229   int failed = GNUNET_NO;
230   int c;
231
232   if (n != NULL)
233   {
234     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n");
235     if (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
236     {
237       GNUNET_break (0);
238       failed = GNUNET_YES;
239     }
240
241     if (0 != strcmp(n, s_name))
242     {
243       GNUNET_break (0);
244       failed = GNUNET_YES;
245     }
246
247     if (RECORDS != rd_count)
248     {
249       GNUNET_break (0);
250       failed = GNUNET_YES;
251     }
252
253     for (c = 0; c < RECORDS; c++)
254     {
255       if (GNUNET_NO == GNUNET_NAMESTORE_records_cmp(&rd[c], &s_first_record[c]))
256       {
257         GNUNET_break (0);
258         failed = GNUNET_YES;
259       }
260     }
261
262     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (&pubkey, expire, n, rd_count, rd, signature))
263     {
264       GNUNET_break (0);
265       failed = GNUNET_YES;
266     }
267
268     if (0 != memcmp (s_signature, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
269     {
270       GNUNET_break (0);
271       failed = GNUNET_YES;
272     }
273
274     found = GNUNET_YES;
275     if (failed == GNUNET_NO)
276       res = 0;
277     else
278       res = 1;
279
280     /* create a second record */
281     s_second_record = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_RecordData) + TEST_CREATE_RECORD_DATALEN);
282     s_second_record->expiration_time = UINT64_MAX;
283     s_second_record->record_type = TEST_CREATE_RECORD_TYPE;
284     s_second_record->flags = GNUNET_NAMESTORE_RF_AUTHORITY;
285     s_second_record->data = &s_second_record[1];
286     s_second_record->data_size = TEST_CREATE_RECORD_DATALEN;
287     memset ((char *) s_second_record->data, TEST_CREATE_RECORD_DATA, TEST_CREATE_RECORD_DATALEN);
288
289     GNUNET_NAMESTORE_record_create (nsh, privkey, name, s_second_record, &create_second_cont, name);
290
291   }
292   else
293   {
294     if (found != GNUNET_YES)
295     {
296       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to lookup records for name `%s'\n", s_name);
297       res = 1;
298     }
299     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name);
300     GNUNET_SCHEDULER_add_now (&end, NULL);
301   }
302 }
303
304
305 static void
306 create_first_cont (void *cls, int32_t success, const char *emsg)
307 {
308   char *name = cls;
309   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
310   if (success == GNUNET_OK)
311   {
312     res = 0;
313     /* check if record was created correct */
314     GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_initial_proc, name);
315   }
316   else
317   {
318     res = 1;
319     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
320     GNUNET_SCHEDULER_add_now(&end, NULL);
321   }
322 }
323
324
325 static struct GNUNET_NAMESTORE_RecordData *
326 create_record (unsigned int count)
327 {
328   unsigned int c;
329   struct GNUNET_NAMESTORE_RecordData * rd;
330
331   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
332   for (c = 0; c < count; c++)
333   {
334     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value;
335     rd[c].record_type = TEST_RECORD_TYPE;
336     rd[c].data_size = TEST_RECORD_DATALEN;
337     rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
338     memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
339   }
340   return rd;
341 }
342
343
344 static void
345 run (void *cls, 
346      const struct GNUNET_CONFIGURATION_Handle *cfg,
347      struct GNUNET_TESTING_Peer *peer)
348 {
349   size_t rd_ser_len;
350   char *hostkey_file;
351   struct GNUNET_TIME_Absolute et;
352
353   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,endbadly, NULL);
354   /* load privat key */
355   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
356       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
357   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
358   privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
359   GNUNET_free (hostkey_file);
360   /* get public key */
361   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
362
363   /* create record */
364   s_name = "dummy.dummy.gnunet";
365   s_first_record = create_record (1);
366
367   rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, s_first_record);
368   char rd_ser[rd_ser_len];
369   GNUNET_NAMESTORE_records_serialize(1, s_first_record, rd_ser_len, rd_ser);
370   et.abs_value = s_first_record->expiration_time;
371   s_signature = GNUNET_NAMESTORE_create_signature(privkey, et, s_name, s_first_record, 1);
372
373   /* create random zone hash */
374   GNUNET_CRYPTO_short_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone);
375
376   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name: `%s' Zone: `%s' \n", s_name, GNUNET_short_h2s (&s_zone));
377   nsh = GNUNET_NAMESTORE_connect (cfg);
378   GNUNET_break (NULL != nsh);
379
380   GNUNET_break (s_first_record != NULL);
381   GNUNET_break (s_name != NULL);
382
383   /* create initial record */
384   GNUNET_NAMESTORE_record_create (nsh, privkey, s_name, s_first_record, &create_first_cont, s_name);
385 }
386
387
388 int
389 main (int argc, char *argv[])
390 {
391   res = 1;
392   if (0 != 
393       GNUNET_TESTING_service_run ("test-namestore-api-create",
394                                   "namestore",
395                                   "test_namestore_api.conf",
396                                   &run,
397                                   NULL))
398     return 1;
399   GNUNET_free (s_signature);
400   return res;
401 }
402
403 /* end of test_namestore_api_create.c */