glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / namestore / test_namestore_api_lookup_shadow.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file namestore/test_namestore_api_lookup_shadow.c
17  * @brief testcase for namestore_api.c: store a shadow record and perform a lookup
18  * test passes if test returns the record but without the shadow flag since no
19  * other valid record is available
20  */
21 #include "platform.h"
22 #include "gnunet_namecache_service.h"
23 #include "gnunet_namestore_service.h"
24 #include "gnunet_testing_lib.h"
25 #include "gnunet_dnsparser_lib.h"
26
27 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
28
29 #define TEST_RECORD_DATALEN 123
30
31 #define TEST_RECORD_DATA 'a'
32
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
34
35
36 static struct GNUNET_NAMESTORE_Handle *nsh;
37
38 static struct GNUNET_NAMECACHE_Handle *nch;
39
40 static struct GNUNET_SCHEDULER_Task * endbadly_task;
41
42 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
43
44 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
45
46 static int res;
47
48 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
49
50 static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
51
52
53 static void
54 cleanup ()
55 {
56   if (NULL != nsh)
57   {
58     GNUNET_NAMESTORE_disconnect (nsh);
59     nsh = NULL;
60   }
61   if (NULL != nch)
62   {
63     GNUNET_NAMECACHE_disconnect (nch);
64     nch = NULL;
65   }
66   if (NULL != privkey)
67   {
68     GNUNET_free (privkey);
69     privkey = NULL;
70   }
71   GNUNET_SCHEDULER_shutdown ();
72 }
73
74
75 /**
76  * Re-establish the connection to the service.
77  *
78  * @param cls handle to use to re-connect.
79  */
80 static void
81 endbadly (void *cls)
82 {
83   if (NULL != nsqe)
84   {
85     GNUNET_NAMESTORE_cancel (nsqe);
86     nsqe = NULL;
87   }
88   if (NULL != ncqe)
89   {
90     GNUNET_NAMECACHE_cancel (ncqe);
91     ncqe = NULL;
92   }
93   cleanup ();
94   res = 1;
95 }
96
97
98 static void
99 end (void *cls)
100 {
101   cleanup ();
102   res = 0;
103 }
104
105
106 static void
107 rd_decrypt_cb (void *cls,
108                unsigned int rd_count,
109                const struct GNUNET_GNSRECORD_Data *rd)
110 {
111   char rd_cmp_data[TEST_RECORD_DATALEN];
112
113   if (1 != rd_count)
114   {
115     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
116     GNUNET_break (0);
117     return;
118   }
119   if (NULL == rd)
120   {
121     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
122     GNUNET_break (0);
123     return;
124   }
125   memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
126
127   if (TEST_RECORD_TYPE != rd[0].record_type)
128   {
129     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
130     GNUNET_break (0);
131     return;
132   }
133   if (TEST_RECORD_DATALEN != rd[0].data_size)
134   {
135     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
136     GNUNET_break (0);
137     return;
138   }
139   if (0 != memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN))
140   {
141     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
142     GNUNET_break (0);
143     return;
144   }
145   if (0 != (GNUNET_GNSRECORD_RF_SHADOW_RECORD & rd[0].flags))
146   {
147     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
148     GNUNET_break (0);
149     return;
150   }
151
152   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153               "Block was decrypted successfully \n");
154
155         GNUNET_SCHEDULER_add_now (&end, NULL);
156 }
157
158
159 static void
160 name_lookup_proc (void *cls,
161                   const struct GNUNET_GNSRECORD_Block *block)
162 {
163   const char *name = cls;
164
165   ncqe = NULL;
166   GNUNET_assert (NULL != cls);
167
168   if (endbadly_task != NULL)
169   {
170     GNUNET_SCHEDULER_cancel (endbadly_task);
171     endbadly_task = NULL;
172   }
173
174   if (NULL == block)
175   {
176     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
177               _("Namestore returned no block\n"));
178     if (endbadly_task != NULL)
179       GNUNET_SCHEDULER_cancel (endbadly_task);
180     endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
181     return;
182   }
183
184   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185               "Namestore returned block, decrypting \n");
186   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
187                 &pubkey, name, &rd_decrypt_cb, (void *) name));
188 }
189
190
191 static void
192 put_cont (void *cls, int32_t success, const char *emsg)
193 {
194   const char *name = cls;
195   struct GNUNET_HashCode derived_hash;
196   struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
197
198   nsqe = NULL;
199   GNUNET_assert (NULL != cls);
200   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201               "Name store added record for `%s': %s\n",
202               name,
203               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
204
205   /* Create derived hash */
206   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
207   GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &derived_hash);
208
209   ncqe = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
210                                         &name_lookup_proc, (void *) name);
211 }
212
213
214 static void
215 run (void *cls,
216      const struct GNUNET_CONFIGURATION_Handle *cfg,
217      struct GNUNET_TESTING_Peer *peer)
218 {
219   struct GNUNET_GNSRECORD_Data rd;
220   const char * name = "dummy.dummy.gnunet";
221
222   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
223                                                 &endbadly,
224                                                 NULL);
225   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
226   GNUNET_assert (privkey != NULL);
227   GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
228                                       &pubkey);
229   rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
230   rd.record_type = TEST_RECORD_TYPE;
231   rd.data_size = TEST_RECORD_DATALEN;
232   rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
233   rd.flags = GNUNET_GNSRECORD_RF_SHADOW_RECORD;
234   memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
235
236   nsh = GNUNET_NAMESTORE_connect (cfg);
237   nch = GNUNET_NAMECACHE_connect (cfg);
238   GNUNET_break (NULL != nsh);
239   GNUNET_break (NULL != nch);
240   nsqe = GNUNET_NAMESTORE_records_store (nsh,
241                                          privkey,
242                                          name,
243                                          1,
244                                          &rd,
245                                          &put_cont,
246                                          (void *) name);
247   if (NULL == nsqe)
248   {
249     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250               _("Namestore cannot store no block\n"));
251   }
252   GNUNET_free ((void *)rd.data);
253 }
254
255
256 int
257 main (int argc, char *argv[])
258 {
259   const char *plugin_name;
260   char *cfg_name;
261
262   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
263   GNUNET_asprintf (&cfg_name,
264                    "test_namestore_api_%s.conf",
265                    plugin_name);
266   GNUNET_DISK_purge_cfg_dir (cfg_name,
267                              "GNUNET_TEST_HOME");
268   res = 1;
269   if (0 !=
270       GNUNET_TESTING_peer_run ("test-namestore-api-lookup-shadow",
271                                cfg_name,
272                                &run,
273                                NULL))
274   {
275     res = 1;
276   }
277   GNUNET_free (cfg_name);
278   GNUNET_DISK_purge_cfg_dir (cfg_name,
279                              "GNUNET_TEST_HOME");
280   return res;
281 }
282
283
284 /* end of test_namestore_api_lookup_shadow.c */