Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_to_name.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file namestore/test_namestore_api_zone_to_name.c
22  * @brief testcase for zone to name translation
23  */
24 #include "platform.h"
25 #include "gnunet_namestore_service.h"
26 #include "gnunet_testing_lib.h"
27 #include "namestore.h"
28
29 #define RECORDS 5
30
31 #define TEST_RECORD_TYPE 1234
32
33 #define TEST_RECORD_DATALEN 123
34
35 #define TEST_RECORD_DATA 'a'
36
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
38
39
40 static struct GNUNET_NAMESTORE_Handle *nsh;
41
42 static struct GNUNET_SCHEDULER_Task *endbadly_task;
43
44 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
45
46 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
47
48 static struct GNUNET_CRYPTO_EcdsaPublicKey s_zone_value;
49
50 static char * s_name;
51
52 static int res;
53
54 static char *directory;
55
56 static struct GNUNET_NAMESTORE_QueueEntry *qe;
57
58
59 /**
60  * Re-establish the connection to the service.
61  *
62  * @param cls handle to use to re-connect.
63  */
64 static void
65 endbadly (void *cls)
66 {
67   (void) cls;
68   GNUNET_SCHEDULER_shutdown ();
69   res = 1;
70 }
71
72
73 static void
74 end (void *cls)
75 {
76   if (NULL != qe)
77   {
78     GNUNET_NAMESTORE_cancel (qe);
79     qe = NULL;
80   }
81   if (NULL != endbadly_task)
82   {
83     GNUNET_SCHEDULER_cancel (endbadly_task);
84     endbadly_task = NULL;
85   }
86   if (NULL != privkey)
87   {
88     GNUNET_free (privkey);
89     privkey = NULL;
90   }
91   if (NULL != nsh)
92   {
93     GNUNET_NAMESTORE_disconnect (nsh);
94     nsh = NULL;
95   }
96 }
97
98
99 static void
100 zone_to_name_proc (void *cls,
101                    const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
102                    const char *n,
103                    unsigned int rd_count,
104                    const struct GNUNET_GNSRECORD_Data *rd)
105 {
106   int fail = GNUNET_NO;
107
108   qe = NULL;
109   if ( (NULL == zone_key) &&
110        (NULL == n) &&
111        (0 == rd_count) &&
112        (NULL == rd) )
113   {
114     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
115                 "No result found\n");
116     res = 1;
117   }
118   else
119   {
120     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121                 "Result found: `%s'\n",
122                 n);
123     if ( (NULL == n) ||
124          (0 != strcmp (n,
125                        s_name)))
126     {
127       fail = GNUNET_YES;
128       GNUNET_break (0);
129     }
130     if (1 != rd_count)
131     {
132       fail = GNUNET_YES;
133       GNUNET_break (0);
134     }
135     if ( (NULL == zone_key) ||
136          (0 != memcmp (zone_key,
137                        privkey,
138                        sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))))
139     {
140       fail = GNUNET_YES;
141       GNUNET_break (0);
142     }
143     if (fail == GNUNET_NO)
144       res = 0;
145     else
146       res = 1;
147   }
148   GNUNET_SCHEDULER_add_now (&end,
149                             NULL);
150 }
151
152
153 static void
154 error_cb (void *cls)
155 {
156   (void) cls;
157   qe = NULL;
158   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159               "Not found!\n");
160   GNUNET_SCHEDULER_shutdown ();
161   res = 2;
162 }
163
164
165 static void
166 put_cont (void *cls,
167           int32_t success,
168           const char *emsg)
169 {
170   char *name = cls;
171
172   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173               "Name store added record for `%s': %s\n",
174               name,
175               (success == GNUNET_OK) ? "SUCCESS" : emsg);
176   if (success == GNUNET_OK)
177   {
178     res = 0;
179
180     qe = GNUNET_NAMESTORE_zone_to_name (nsh,
181                                         privkey,
182                                         &s_zone_value,
183                                         &error_cb,
184                                         NULL,
185                                         &zone_to_name_proc,
186                                         NULL);
187   }
188   else
189   {
190     res = 1;
191     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
192                 "Failed to put records for name `%s'\n",
193                 name);
194     GNUNET_SCHEDULER_add_now (&end,
195                               NULL);
196   }
197 }
198
199
200 static void
201 run (void *cls,
202      const struct GNUNET_CONFIGURATION_Handle *cfg,
203      struct GNUNET_TESTING_Peer *peer)
204 {
205   (void) cls;
206   (void) peer;
207   directory = NULL;
208   GNUNET_assert (GNUNET_OK ==
209                  GNUNET_CONFIGURATION_get_value_string (cfg,
210                                                         "PATHS",
211                                                         "GNUNET_TEST_HOME",
212                                                         &directory));
213   GNUNET_DISK_directory_remove (directory);
214
215   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
216                                                 &endbadly,
217                                                 NULL);
218   GNUNET_SCHEDULER_add_shutdown (&end,
219                                  NULL);
220   GNUNET_asprintf (&s_name, "dummy");
221   /* load privat key */
222   {
223     char *zonekey_file;
224
225     GNUNET_asprintf (&zonekey_file,
226                      "zonefiles%s%s",
227                      DIR_SEPARATOR_STR,
228                      "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
229     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230                 "Using zonekey file `%s'\n",
231                 zonekey_file);
232     privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (zonekey_file);
233     GNUNET_free (zonekey_file);
234   }
235   GNUNET_assert (NULL != privkey);
236   /* get public key */
237   GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
238                                       &pubkey);
239
240   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
241                               &s_zone_value,
242                               sizeof (s_zone_value));
243   {
244     struct GNUNET_GNSRECORD_Data rd;
245
246     rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
247     rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
248     rd.data_size = sizeof (s_zone_value);
249     rd.data = &s_zone_value;
250     rd.flags = 0;
251
252     nsh = GNUNET_NAMESTORE_connect (cfg);
253     GNUNET_break (NULL != nsh);
254     GNUNET_NAMESTORE_records_store (nsh,
255                                     privkey,
256                                     s_name,
257                                     1,
258                                     &rd,
259                                     &put_cont,
260                                     NULL);
261   }
262 }
263
264
265 int
266 main (int argc,
267       char *argv[])
268 {
269   const char *plugin_name;
270   char *cfg_name;
271
272   (void) argc;
273   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
274   GNUNET_asprintf (&cfg_name,
275                    "test_namestore_api_%s.conf",
276                    plugin_name);
277   res = 1;
278   if (0 !=
279       GNUNET_TESTING_peer_run ("test-namestore-api-zone-to-name",
280                                cfg_name,
281                                &run,
282                                NULL))
283   {
284     res = 1;
285   }
286   GNUNET_free (cfg_name);
287   if (NULL != directory)
288   {
289     GNUNET_DISK_directory_remove (directory);
290     GNUNET_free (directory);
291   }
292   return res;
293 }
294
295 /* end of test_namestore_api_zone_to_name.c */