- fixes
[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 GNUNET_HashCode s_zone;
53 struct GNUNET_NAMESTORE_RecordData *s_rd;
54 struct GNUNET_NAMESTORE_RecordData *s_create_rd;
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_close (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   int c;
121   for (c = 0; c < RECORDS; c++)
122     GNUNET_free_non_null((void *) s_rd[c].data);
123   GNUNET_free (s_rd);
124   //GNUNET_free (s_create_rd);
125
126   if (privkey != NULL)
127     GNUNET_CRYPTO_rsa_key_free (privkey);
128   privkey = NULL;
129
130   if (nsh != NULL)
131     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
132   nsh = NULL;
133
134   if (NULL != arm)
135     stop_arm();
136 }
137
138 void name_lookup_proc (void *cls,
139                             const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
140                             struct GNUNET_TIME_Absolute expire,
141                             const char *n,
142                             unsigned int rd_count,
143                             const struct GNUNET_NAMESTORE_RecordData *rd,
144                             const struct GNUNET_CRYPTO_RsaSignature *signature)
145 {
146   static int found = GNUNET_NO;
147   int failed = GNUNET_NO;
148   int c;
149
150   if (n != NULL)
151   {
152     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n");
153     if (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
154     {
155       GNUNET_break (0);
156       failed = GNUNET_YES;
157     }
158
159     if (0 != strcmp(n, s_name))
160     {
161       GNUNET_break (0);
162       failed = GNUNET_YES;
163     }
164
165     if (RECORDS != rd_count)
166     {
167       GNUNET_break (0);
168       failed = GNUNET_YES;
169     }
170
171     for (c = 0; c < RECORDS; c++)
172     {
173       GNUNET_break (rd[c].expiration.abs_value == s_rd[c].expiration.abs_value);
174       GNUNET_break (rd[c].record_type == TEST_RECORD_TYPE);
175       GNUNET_break (rd[c].data_size == TEST_RECORD_DATALEN);
176       GNUNET_break (0 == memcmp (rd[c].data, s_rd[c].data, TEST_RECORD_DATALEN));
177     }
178
179     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(&pubkey, n, rd_count, rd, signature))
180     {
181       GNUNET_break (0);
182       failed = GNUNET_YES;
183     }
184
185     if (0 != memcmp (s_signature, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
186     {
187       GNUNET_break (0);
188       failed = GNUNET_YES;
189     }
190
191     found = GNUNET_YES;
192     if (failed == GNUNET_NO)
193       res = 0;
194     else
195       res = 1;
196   }
197   else
198   {
199     if (found != GNUNET_YES)
200     {
201       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to lookup records for name `%s'\n", s_name);
202       res = 1;
203     }
204     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name);
205   }
206   GNUNET_SCHEDULER_add_now(&end, NULL);
207 }
208
209 void
210 create_cont (void *cls, int32_t success, const char *emsg)
211 {
212   char *name = cls;
213   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
214   if (success == GNUNET_OK)
215   {
216     res = 0;
217     GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_proc, name);
218   }
219   else
220   {
221     res = 1;
222     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
223     GNUNET_SCHEDULER_add_now(&end, NULL);
224   }
225
226 }
227
228 void
229 put_cont (void *cls, int32_t success, const char *emsg)
230 {
231   char *name = cls;
232   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
233   if (success == GNUNET_OK)
234   {
235     res = 0;
236     s_create_rd = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_RecordData) + TEST_CREATE_RECORD_DATALEN);
237     s_create_rd->expiration = GNUNET_TIME_absolute_get_forever();
238     s_create_rd->record_type = TEST_CREATE_RECORD_TYPE;
239     s_create_rd->flags = GNUNET_NAMESTORE_RF_AUTHORITY;
240     s_create_rd->data = &s_create_rd[1];
241     s_create_rd->data_size = TEST_CREATE_RECORD_DATALEN;
242     memset ((char *) s_create_rd->data, TEST_CREATE_RECORD_DATA, TEST_CREATE_RECORD_DATALEN);
243
244     GNUNET_NAMESTORE_record_create (nsh, privkey, name, s_create_rd, &create_cont, name);
245   }
246   else
247   {
248     res = 1;
249     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
250     GNUNET_SCHEDULER_add_now(&end, NULL);
251   }
252 }
253
254 static struct GNUNET_NAMESTORE_RecordData *
255 create_record (int count)
256 {
257   int c;
258   struct GNUNET_NAMESTORE_RecordData * rd;
259   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
260
261   for (c = 0; c < count; c++)
262   {
263   rd[c].expiration = GNUNET_TIME_absolute_get();
264   rd[c].record_type = TEST_RECORD_TYPE;
265   rd[c].data_size = TEST_RECORD_DATALEN;
266   rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
267   memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
268   }
269
270   return rd;
271 }
272
273 void
274 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
275 {
276   char *afsdir;
277
278   if (GNUNET_OK ==
279       GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
280                                                "FILENAME", &afsdir))
281   {
282     if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
283       if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
284         if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
285           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
286    GNUNET_free (afsdir);
287   }
288
289 }
290
291 static void
292 run (void *cls, char *const *args, const char *cfgfile,
293      const struct GNUNET_CONFIGURATION_Handle *cfg)
294 {
295   delete_existing_db(cfg);
296
297   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
298   size_t rd_ser_len;
299
300   /* load privat key */
301   privkey = GNUNET_CRYPTO_rsa_key_create_from_file("hostkey");
302   GNUNET_assert (privkey != NULL);
303   /* get public key */
304   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
305
306   /* create record */
307   s_name = "dummy.dummy.gnunet";
308   s_rd = create_record (1);
309
310   rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, s_rd);
311   char rd_ser[rd_ser_len];
312   GNUNET_NAMESTORE_records_serialize(1, s_rd, rd_ser_len, rd_ser);
313
314   s_signature = GNUNET_NAMESTORE_create_signature(privkey, s_name, s_rd, 1);
315
316   /* create random zone hash */
317   GNUNET_CRYPTO_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone);
318
319   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name: `%s' Zone: `%s' \n", s_name, GNUNET_h2s_full(&s_zone));
320
321   start_arm (cfgfile);
322   GNUNET_assert (arm != NULL);
323
324   nsh = GNUNET_NAMESTORE_connect (cfg);
325   GNUNET_break (NULL != nsh);
326
327   GNUNET_break (s_rd != NULL);
328   GNUNET_break (s_name != NULL);
329
330   /* create initial record */
331   GNUNET_NAMESTORE_record_create (nsh, privkey, s_name, s_rd, &create_cont, s_name);
332 }
333
334 static int
335 check ()
336 {
337   static char *const argv[] = { "test-namestore-api",
338     "-c",
339     "test_namestore_api.conf",
340 #if VERBOSE
341     "-L", "DEBUG",
342 #endif
343     NULL
344   };
345   static struct GNUNET_GETOPT_CommandLineOption options[] = {
346     GNUNET_GETOPT_OPTION_END
347   };
348
349   res = 1;
350   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
351                       "nohelp", options, &run, &res);
352   return res;
353 }
354
355 int
356 main (int argc, char *argv[])
357 {
358   int ret;
359
360   ret = check ();
361   GNUNET_free (s_signature);
362   return ret;
363 }
364
365 /* end of test_namestore_api.c */