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