-consistently use struct GNUNET_HashCode
[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 "namestore.h"
28 #include "gnunet_signatures.h"
29
30 #define VERBOSE GNUNET_NO
31
32 #define RECORDS 1
33 #define TEST_RECORD_TYPE 1234
34 #define TEST_RECORD_DATALEN 123
35 #define TEST_RECORD_DATA 'a'
36
37 #define TEST_CREATE_RECORD_TYPE 4321
38 #define TEST_CREATE_RECORD_DATALEN 255
39 #define TEST_CREATE_RECORD_DATA 'b'
40
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
42
43 static struct GNUNET_NAMESTORE_Handle * nsh;
44
45 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
46 static struct GNUNET_OS_Process *arm;
47
48 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
49 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
50 struct GNUNET_CRYPTO_RsaSignature *s_signature;
51 struct GNUNET_CRYPTO_RsaSignature *s_signature_updated;
52 static struct GNUNET_CRYPTO_ShortHashCode s_zone;
53 struct GNUNET_NAMESTORE_RecordData *s_first_record;
54 struct GNUNET_NAMESTORE_RecordData *s_second_record;
55 static char *s_name;
56
57
58
59 static int res;
60
61 static void
62 start_arm (const char *cfgname)
63 {
64   arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
65                                "gnunet-service-arm", "-c", cfgname,
66 #if VERBOSE_PEERS
67                                "-L", "DEBUG",
68 #else
69                                "-L", "ERROR",
70 #endif
71                                NULL);
72 }
73
74 static void
75 stop_arm ()
76 {
77   if (NULL != arm)
78   {
79     if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
80       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
81     GNUNET_OS_process_wait (arm);
82     GNUNET_OS_process_destroy (arm);
83     arm = NULL;
84   }
85 }
86
87 /**
88  * Re-establish the connection to the service.
89  *
90  * @param cls handle to use to re-connect.
91  * @param tc scheduler context
92  */
93 static void
94 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
95 {
96   if (nsh != NULL)
97     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
98   nsh = NULL;
99
100   if (privkey != NULL)
101     GNUNET_CRYPTO_rsa_key_free (privkey);
102   privkey = NULL;
103
104   if (NULL != arm)
105     stop_arm();
106
107   res = 1;
108 }
109
110
111 static void
112 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
113 {
114   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
115   {
116     GNUNET_SCHEDULER_cancel (endbadly_task);
117     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
118   }
119
120   GNUNET_free ((void *) s_first_record->data);
121   GNUNET_free (s_first_record);
122   GNUNET_free_non_null (s_second_record);
123
124   if (privkey != NULL)
125     GNUNET_CRYPTO_rsa_key_free (privkey);
126   privkey = NULL;
127
128   if (nsh != NULL)
129     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
130   nsh = NULL;
131
132   if (NULL != arm)
133     stop_arm();
134 }
135
136 void name_lookup_second_proc (void *cls,
137                             const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
138                             struct GNUNET_TIME_Absolute expire,
139                             const char *n,
140                             unsigned int rd_count,
141                             const struct GNUNET_NAMESTORE_RecordData *rd,
142                             const struct GNUNET_CRYPTO_RsaSignature *signature)
143 {
144   static int found = GNUNET_NO;
145   int failed = GNUNET_NO;
146   int c;
147
148   if (n != NULL)
149   {
150     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n");
151     if (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
152     {
153       GNUNET_break (0);
154       failed = GNUNET_YES;
155     }
156
157     if (0 != strcmp(n, s_name))
158     {
159       GNUNET_break (0);
160       failed = GNUNET_YES;
161     }
162
163     if (2 != rd_count)
164     {
165       GNUNET_break (0);
166       failed = GNUNET_YES;
167     }
168
169     for (c = 0; c < rd_count; c++)
170     {
171       if ((GNUNET_NO == GNUNET_NAMESTORE_records_cmp(&rd[c], s_first_record)) &&
172           (GNUNET_NO == GNUNET_NAMESTORE_records_cmp(&rd[c], s_second_record)))
173       {
174         GNUNET_break (0);
175         failed = GNUNET_YES;
176       }
177     }
178
179     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(zone_key, expire, n, rd_count, rd, signature))
180     {
181       GNUNET_break (0);
182       failed = GNUNET_YES;
183     }
184
185
186     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(&pubkey, expire, n, rd_count, rd, signature))
187     {
188       GNUNET_break (0);
189       failed = GNUNET_YES;
190     }
191
192     struct GNUNET_NAMESTORE_RecordData rd_new[2];
193     rd_new[0] = *s_first_record;
194     rd_new[1] = *s_second_record;
195     s_signature_updated = GNUNET_NAMESTORE_create_signature(privkey, expire, s_name, rd_new, 2);
196
197     if (0 != memcmp (s_signature_updated, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
198     {
199       GNUNET_break (0);
200       failed = GNUNET_YES;
201     }
202
203     found = GNUNET_YES;
204     if (failed == GNUNET_NO)
205       res = 0;
206     else
207       res = 1;
208   }
209   else
210   {
211     if (found != GNUNET_YES)
212     {
213       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to lookup records for name `%s'\n", s_name);
214       res = 1;
215     }
216     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name);
217   }
218   GNUNET_SCHEDULER_add_now(&end, NULL);
219 }
220
221
222 void
223 create_second_cont (void *cls, int32_t success, const char *emsg)
224 {
225   char *name = cls;
226   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create second record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
227   if (success == GNUNET_OK)
228   {
229     res = 0;
230     GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_second_proc, name);
231   }
232   else
233   {
234     res = 1;
235     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
236     GNUNET_SCHEDULER_add_now(&end, NULL);
237   }
238
239 }
240
241 void name_lookup_initial_proc (void *cls,
242                             const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
243                             struct GNUNET_TIME_Absolute expire,
244                             const char *n,
245                             unsigned int rd_count,
246                             const struct GNUNET_NAMESTORE_RecordData *rd,
247                             const struct GNUNET_CRYPTO_RsaSignature *signature)
248 {
249   char * name = cls;
250   static int found = GNUNET_NO;
251   int failed = GNUNET_NO;
252   int c;
253
254   if (n != NULL)
255   {
256     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n");
257     if (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
258     {
259       GNUNET_break (0);
260       failed = GNUNET_YES;
261     }
262
263     if (0 != strcmp(n, s_name))
264     {
265       GNUNET_break (0);
266       failed = GNUNET_YES;
267     }
268
269     if (RECORDS != rd_count)
270     {
271       GNUNET_break (0);
272       failed = GNUNET_YES;
273     }
274
275     for (c = 0; c < RECORDS; c++)
276     {
277       if (GNUNET_NO == GNUNET_NAMESTORE_records_cmp(&rd[c], &s_first_record[c]))
278       {
279         GNUNET_break (0);
280         failed = GNUNET_YES;
281       }
282     }
283
284     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(&pubkey, expire,n, rd_count, rd, signature))
285     {
286       GNUNET_break (0);
287       failed = GNUNET_YES;
288     }
289
290     if (0 != memcmp (s_signature, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
291     {
292       GNUNET_break (0);
293       failed = GNUNET_YES;
294     }
295
296     found = GNUNET_YES;
297     if (failed == GNUNET_NO)
298       res = 0;
299     else
300       res = 1;
301
302     /* create a second record */
303     s_second_record = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_RecordData) + TEST_CREATE_RECORD_DATALEN);
304     s_second_record->expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
305     s_second_record->record_type = TEST_CREATE_RECORD_TYPE;
306     s_second_record->flags = GNUNET_NAMESTORE_RF_AUTHORITY;
307     s_second_record->data = &s_second_record[1];
308     s_second_record->data_size = TEST_CREATE_RECORD_DATALEN;
309     memset ((char *) s_second_record->data, TEST_CREATE_RECORD_DATA, TEST_CREATE_RECORD_DATALEN);
310
311     GNUNET_NAMESTORE_record_create (nsh, privkey, name, s_second_record, &create_second_cont, name);
312
313   }
314   else
315   {
316     if (found != GNUNET_YES)
317     {
318       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to lookup records for name `%s'\n", s_name);
319       res = 1;
320     }
321     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name);
322     GNUNET_SCHEDULER_add_now (&end, NULL);
323   }
324 }
325
326 void
327 create_first_cont (void *cls, int32_t success, const char *emsg)
328 {
329   char *name = cls;
330   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
331   if (success == GNUNET_OK)
332   {
333     res = 0;
334     /* check if record was created correct */
335     GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_initial_proc, name);
336   }
337   else
338   {
339     res = 1;
340     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
341     GNUNET_SCHEDULER_add_now(&end, NULL);
342   }
343
344 }
345
346 void
347 put_cont (void *cls, int32_t success, const char *emsg)
348 {
349   char *name = cls;
350   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
351   if (success == GNUNET_OK)
352   {
353
354   }
355   else
356   {
357     res = 1;
358     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
359     GNUNET_SCHEDULER_add_now(&end, NULL);
360   }
361 }
362
363 static struct GNUNET_NAMESTORE_RecordData *
364 create_record (int count)
365 {
366   int c;
367   struct GNUNET_NAMESTORE_RecordData * rd;
368   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
369
370   for (c = 0; c < count; c++)
371   {
372   rd[c].expiration = GNUNET_TIME_absolute_get();
373   rd[c].record_type = TEST_RECORD_TYPE;
374   rd[c].data_size = TEST_RECORD_DATALEN;
375   rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
376   memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
377   }
378
379   return rd;
380 }
381
382 void
383 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
384 {
385   char *afsdir;
386
387   if (GNUNET_OK ==
388       GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
389                                                "FILENAME", &afsdir))
390   {
391     if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
392       if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
393         if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
394           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
395    GNUNET_free (afsdir);
396   }
397
398 }
399
400 static void
401 run (void *cls, char *const *args, const char *cfgfile,
402      const struct GNUNET_CONFIGURATION_Handle *cfg)
403 {
404   delete_existing_db(cfg);
405
406   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
407   size_t rd_ser_len;
408
409   /* load privat key */
410   char *hostkey_file;
411   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
412       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
413   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
414   privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
415   GNUNET_free (hostkey_file);
416   /* get public key */
417   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
418
419   /* create record */
420   s_name = "dummy.dummy.gnunet";
421   s_first_record = create_record (1);
422
423   rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, s_first_record);
424   char rd_ser[rd_ser_len];
425   GNUNET_NAMESTORE_records_serialize(1, s_first_record, rd_ser_len, rd_ser);
426
427   s_signature = GNUNET_NAMESTORE_create_signature(privkey, s_first_record->expiration, s_name, s_first_record, 1);
428
429   /* create random zone hash */
430   GNUNET_CRYPTO_short_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone);
431
432   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name: `%s' Zone: `%s' \n", s_name, GNUNET_short_h2s (&s_zone));
433
434   start_arm (cfgfile);
435   GNUNET_assert (arm != NULL);
436
437   nsh = GNUNET_NAMESTORE_connect (cfg);
438   GNUNET_break (NULL != nsh);
439
440   GNUNET_break (s_first_record != NULL);
441   GNUNET_break (s_name != NULL);
442
443   /* create initial record */
444   GNUNET_NAMESTORE_record_create (nsh, privkey, s_name, s_first_record, &create_first_cont, s_name);
445 }
446
447 static int
448 check ()
449 {
450   static char *const argv[] = { "test-namestore-api",
451     "-c",
452     "test_namestore_api.conf",
453 #if VERBOSE
454     "-L", "DEBUG",
455 #endif
456     NULL
457   };
458   static struct GNUNET_GETOPT_CommandLineOption options[] = {
459     GNUNET_GETOPT_OPTION_END
460   };
461
462   res = 1;
463   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
464                       "nohelp", options, &run, &res);
465   return res;
466 }
467
468 int
469 main (int argc, char *argv[])
470 {
471   int ret;
472
473   ret = check ();
474   GNUNET_free (s_signature);
475   return ret;
476 }
477
478 /* end of test_namestore_api.c */