- 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 5
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 static GNUNET_HashCode s_zone;
52 struct GNUNET_NAMESTORE_RecordData *s_rd;
53 struct GNUNET_NAMESTORE_RecordData *s_create_rd;
54 static char *s_name;
55
56
57
58 static int res;
59
60 static void
61 start_arm (const char *cfgname)
62 {
63   arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
64                                "gnunet-service-arm", "-c", cfgname,
65 #if VERBOSE_PEERS
66                                "-L", "DEBUG",
67 #else
68                                "-L", "ERROR",
69 #endif
70                                NULL);
71 }
72
73 static void
74 stop_arm ()
75 {
76   if (NULL != arm)
77   {
78     if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
79       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
80     GNUNET_OS_process_wait (arm);
81     GNUNET_OS_process_close (arm);
82     arm = NULL;
83   }
84 }
85
86 /**
87  * Re-establish the connection to the service.
88  *
89  * @param cls handle to use to re-connect.
90  * @param tc scheduler context
91  */
92 static void
93 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
94 {
95   if (nsh != NULL)
96     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
97   nsh = NULL;
98
99   if (privkey != NULL)
100     GNUNET_CRYPTO_rsa_key_free (privkey);
101   privkey = NULL;
102
103   if (NULL != arm)
104     stop_arm();
105
106   res = 1;
107 }
108
109
110 static void
111 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112 {
113   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
114   {
115     GNUNET_SCHEDULER_cancel (endbadly_task);
116     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
117   }
118
119   int c;
120   for (c = 0; c < RECORDS; c++)
121     GNUNET_free_non_null((void *) s_rd[c].data);
122   GNUNET_free (s_rd);
123   GNUNET_free (s_create_rd);
124
125   if (privkey != NULL)
126     GNUNET_CRYPTO_rsa_key_free (privkey);
127   privkey = NULL;
128
129   if (nsh != NULL)
130     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
131   nsh = NULL;
132
133   if (NULL != arm)
134     stop_arm();
135 }
136
137 void name_lookup_proc (void *cls,
138                             const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
139                             struct GNUNET_TIME_Absolute expire,
140                             const char *n,
141                             unsigned int rd_count,
142                             const struct GNUNET_NAMESTORE_RecordData *rd,
143                             const struct GNUNET_CRYPTO_RsaSignature *signature)
144 {
145   static int found = GNUNET_NO;
146   int failed = GNUNET_NO;
147   int c;
148
149   if (n != NULL)
150   {
151     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n");
152     if (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
153     {
154       GNUNET_break (0);
155       failed = GNUNET_YES;
156     }
157
158     if (0 != strcmp(n, s_name))
159     {
160       GNUNET_break (0);
161       failed = GNUNET_YES;
162     }
163
164     if (RECORDS+1 != rd_count)
165     {
166       GNUNET_break (0);
167       failed = GNUNET_YES;
168     }
169
170     for (c = 0; c < RECORDS; c++)
171     {
172       GNUNET_break (rd[c].expiration.abs_value == s_rd[c].expiration.abs_value);
173       GNUNET_break (rd[c].record_type == TEST_RECORD_TYPE);
174       GNUNET_break (rd[c].data_size == TEST_RECORD_DATALEN);
175       GNUNET_break (0 == memcmp (rd[c].data, s_rd[c].data, TEST_RECORD_DATALEN));
176     }
177
178     if (rd[c].record_type != TEST_CREATE_RECORD_TYPE)
179     {
180       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "record_type %u %u\n", rd[c].record_type , TEST_CREATE_RECORD_TYPE);
181       GNUNET_break (0);
182       failed = GNUNET_YES;
183     }
184     if (rd[c].data_size != TEST_CREATE_RECORD_DATALEN)
185     {
186       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "data_size %u %u\n", rd[c].data_size , TEST_CREATE_RECORD_DATALEN);
187       GNUNET_break (0);
188       failed = GNUNET_YES;
189     }
190     else
191     {
192       char *dummy[TEST_CREATE_RECORD_DATALEN];
193       memset (&dummy, TEST_CREATE_RECORD_DATA, TEST_CREATE_RECORD_DATALEN);
194       if (0 != memcmp (&dummy, s_create_rd->data, TEST_CREATE_RECORD_DATALEN))
195       {
196         GNUNET_break (0);
197         failed = GNUNET_YES;
198       }
199     }
200
201     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(&pubkey, n, rd_count, rd, signature))
202     {
203       GNUNET_break (0);
204       failed = GNUNET_YES;
205     }
206
207     found = GNUNET_YES;
208     if (failed == GNUNET_NO)
209       res = 0;
210     else
211       res = 1;
212   }
213   else
214   {
215     if (found != GNUNET_YES)
216     {
217       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to lookup records for name `%s'\n", s_name);
218       res = 1;
219     }
220     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name);
221   }
222   GNUNET_SCHEDULER_add_now(&end, NULL);
223 }
224
225 void
226 create_cont (void *cls, int32_t success, const char *emsg)
227 {
228   char *name = cls;
229   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
230   if (success == GNUNET_OK)
231   {
232     res = 0;
233
234     GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_proc, name);
235   }
236   else
237   {
238     res = 1;
239     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
240     GNUNET_SCHEDULER_add_now(&end, NULL);
241   }
242
243 }
244
245 void
246 put_cont (void *cls, int32_t success, const char *emsg)
247 {
248   char *name = cls;
249   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
250   if (success == GNUNET_OK)
251   {
252     res = 0;
253     s_create_rd = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_RecordData) + TEST_CREATE_RECORD_DATALEN);
254     s_create_rd->expiration = GNUNET_TIME_absolute_get_forever();
255     s_create_rd->record_type = TEST_CREATE_RECORD_TYPE;
256     s_create_rd->flags = GNUNET_NAMESTORE_RF_AUTHORITY;
257     s_create_rd->data = &s_create_rd[1];
258     s_create_rd->data_size = TEST_CREATE_RECORD_DATALEN;
259     memset ((char *) s_create_rd->data, TEST_CREATE_RECORD_DATA, TEST_CREATE_RECORD_DATALEN);
260
261     GNUNET_NAMESTORE_record_create (nsh, privkey, name, s_create_rd, &create_cont, name);
262   }
263   else
264   {
265     res = 1;
266     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
267     GNUNET_SCHEDULER_add_now(&end, NULL);
268   }
269 }
270
271 static struct GNUNET_NAMESTORE_RecordData *
272 create_record (int count)
273 {
274   int c;
275   struct GNUNET_NAMESTORE_RecordData * rd;
276   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
277
278   for (c = 0; c < RECORDS; c++)
279   {
280   rd[c].expiration = GNUNET_TIME_absolute_get();
281   rd[c].record_type = TEST_RECORD_TYPE;
282   rd[c].data_size = TEST_RECORD_DATALEN;
283   rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
284   memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
285   }
286
287   return rd;
288 }
289
290 static void
291 run (void *cls, char *const *args, const char *cfgfile,
292      const struct GNUNET_CONFIGURATION_Handle *cfg)
293 {
294   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
295   size_t rd_ser_len;
296
297   /* load privat key */
298   privkey = GNUNET_CRYPTO_rsa_key_create_from_file("hostkey");
299   GNUNET_assert (privkey != NULL);
300   /* get public key */
301   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
302
303   /* create record */
304   s_name = "dummy.dummy.gnunet";
305   s_rd = create_record (RECORDS);
306
307   rd_ser_len = GNUNET_NAMESTORE_records_get_size(RECORDS, s_rd);
308   char rd_ser[rd_ser_len];
309   GNUNET_NAMESTORE_records_serialize(RECORDS, s_rd, rd_ser_len, rd_ser);
310
311   s_signature = GNUNET_NAMESTORE_create_signature(privkey, s_name, s_rd, RECORDS);
312
313   /* create random zone hash */
314   GNUNET_CRYPTO_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone);
315
316   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name: `%s' Zone: `%s' \n", s_name, GNUNET_h2s_full(&s_zone));
317
318
319   start_arm (cfgfile);
320   GNUNET_assert (arm != NULL);
321
322   nsh = GNUNET_NAMESTORE_connect (cfg);
323   GNUNET_break (NULL != nsh);
324
325
326
327   GNUNET_break (s_rd != NULL);
328   GNUNET_break (s_name != NULL);
329
330   GNUNET_NAMESTORE_record_put (nsh, &pubkey, s_name,
331                               GNUNET_TIME_absolute_get_forever(),
332                               RECORDS, s_rd, s_signature, put_cont, s_name);
333
334
335
336 }
337
338 static int
339 check ()
340 {
341   static char *const argv[] = { "test-namestore-api",
342     "-c",
343     "test_namestore_api.conf",
344 #if VERBOSE
345     "-L", "DEBUG",
346 #endif
347     NULL
348   };
349   static struct GNUNET_GETOPT_CommandLineOption options[] = {
350     GNUNET_GETOPT_OPTION_END
351   };
352
353   res = 1;
354   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
355                       "nohelp", options, &run, &res);
356   return res;
357 }
358
359 int
360 main (int argc, char *argv[])
361 {
362   int ret;
363
364   ret = check ();
365   GNUNET_free (s_signature);
366   return ret;
367 }
368
369 /* end of test_namestore_api.c */