add response headers, add replace api to namestore
[oweals/gnunet.git] / src / namestore / test_namestore_api_lookup_shadow_filter.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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file namestore/test_namestore_api_lookup_shadow_filter.c
22  * @brief testcase for namestore_api.c: store a record with short expiration
23  *      and a shadow record, perform lookup:
24  *      - when active record is valid, expect only active record
25  *      - when active record is expired, expect shadow record only
26  */
27 #include "platform.h"
28 #include "gnunet_namecache_service.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_dnsparser_lib.h"
32
33 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
34
35 #define TEST_NAME "dummy.dummy.gnunet"
36 #define TEST_RECORD_DATALEN 123
37 #define TEST_RECORD_DATA 'a'
38 #define TEST_SHADOW_RECORD_DATA 'b'
39
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
41 #define EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
42
43 static struct GNUNET_NAMESTORE_Handle *nsh;
44
45 static struct GNUNET_NAMECACHE_Handle *nch;
46
47 static struct GNUNET_SCHEDULER_Task *endbadly_task;
48
49 static struct GNUNET_SCHEDULER_Task *delayed_lookup_task;
50
51 static struct GNUNET_CRYPTO_EcdsaPrivateKey privkey;
52
53 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
54
55 static int res;
56
57 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
58
59 static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
60
61 static struct GNUNET_NAMECACHE_QueueEntry *ncqe_shadow;
62
63 static struct GNUNET_GNSRECORD_Data records[2];
64
65 static struct GNUNET_TIME_Absolute record_expiration;
66
67 static struct GNUNET_HashCode derived_hash;
68
69 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
70
71
72 static void
73 cleanup ()
74 {
75   if (NULL != nsh)
76   {
77     GNUNET_NAMESTORE_disconnect (nsh);
78     nsh = NULL;
79   }
80   if (NULL != nch)
81   {
82     GNUNET_NAMECACHE_disconnect (nch);
83     nch = NULL;
84   }
85   GNUNET_SCHEDULER_shutdown ();
86 }
87
88
89 /**
90  * Re-establish the connection to the service.
91  *
92  * @param cls handle to use to re-connect.
93  */
94 static void
95 endbadly (void *cls)
96 {
97   if (NULL != delayed_lookup_task)
98   {
99     GNUNET_SCHEDULER_cancel (delayed_lookup_task);
100     delayed_lookup_task = NULL;
101   }
102   if (NULL != nsqe)
103   {
104     GNUNET_NAMESTORE_cancel (nsqe);
105     nsqe = NULL;
106   }
107   if (NULL != ncqe)
108   {
109     GNUNET_NAMECACHE_cancel (ncqe);
110     ncqe = NULL;
111   }
112   cleanup ();
113   res = 1;
114 }
115
116
117 static void
118 end (void *cls)
119 {
120   cleanup ();
121   res = 0;
122 }
123
124
125 static void
126 rd_decrypt_cb (void *cls,
127                unsigned int rd_count,
128                const struct GNUNET_GNSRECORD_Data *rd)
129 {
130   struct GNUNET_GNSRECORD_Data *expected_rd = cls;
131   char rd_cmp_data[TEST_RECORD_DATALEN];
132
133   if (1 != rd_count)
134   {
135     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
136     GNUNET_break (0);
137     return;
138   }
139   if (NULL == rd)
140   {
141     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
142     GNUNET_break (0);
143     return;
144   }
145   if (expected_rd == &records[0])
146   {
147     /* Expecting active record */
148     memset (rd_cmp_data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
149     if (TEST_RECORD_TYPE != rd[0].record_type)
150     {
151       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
152       GNUNET_break (0);
153       return;
154     }
155     if (TEST_RECORD_DATALEN != rd[0].data_size)
156     {
157       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
158       GNUNET_break (0);
159       return;
160     }
161     if (0 != memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN))
162     {
163       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
164       GNUNET_break (0);
165       return;
166     }
167     if (0 != (GNUNET_GNSRECORD_RF_SHADOW_RECORD & rd[0].flags))
168     {
169       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
170       GNUNET_break (0);
171       return;
172     }
173     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
174                 "Block was decrypted successfully with active record\n");
175   }
176   if (expected_rd == &records[1])
177   {
178     /* Expecting shadow record  but without shadow flag*/
179     memset (rd_cmp_data, TEST_SHADOW_RECORD_DATA, TEST_RECORD_DATALEN);
180     if (TEST_RECORD_TYPE != rd[0].record_type)
181     {
182       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
183       GNUNET_break (0);
184       return;
185     }
186     if (TEST_RECORD_DATALEN != rd[0].data_size)
187     {
188       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
189       GNUNET_break (0);
190       return;
191     }
192     if (0 != memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN))
193     {
194       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
195       GNUNET_break (0);
196       return;
197     }
198     if (0 != (GNUNET_GNSRECORD_RF_SHADOW_RECORD & rd[0].flags))
199     {
200       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
201       GNUNET_break (0);
202       return;
203     }
204     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
205                 "Block was decrypted successfully with former shadow record \n");
206     GNUNET_SCHEDULER_add_now (&end, NULL);
207   }
208 }
209
210
211 static void
212 name_lookup_active_proc (void *cls,
213                          const struct GNUNET_GNSRECORD_Block *block)
214 {
215   struct GNUNET_GNSRECORD_Data *expected_rd = cls;
216
217   GNUNET_assert (NULL != expected_rd);
218
219   ncqe = NULL;
220   ncqe_shadow = NULL;
221   if (endbadly_task != NULL)
222   {
223     GNUNET_SCHEDULER_cancel (endbadly_task);
224     endbadly_task = NULL;
225   }
226
227   if (NULL == block)
228   {
229     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230                 _ ("Namestore returned no block\n"));
231     if (endbadly_task != NULL)
232       GNUNET_SCHEDULER_cancel (endbadly_task);
233     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
234     return;
235   }
236
237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238               "Namestore returned block, decrypting \n");
239   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt (block,
240                                                               &pubkey,
241                                                               TEST_NAME,
242                                                               &rd_decrypt_cb,
243                                                               expected_rd));
244 }
245
246
247 static void
248 name_lookup_shadow (void *cls)
249 {
250   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251               "Performing lookup for shadow record \n");
252   delayed_lookup_task = NULL;
253   ncqe_shadow = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
254                                                &name_lookup_active_proc,
255                                                &records[1]);
256 }
257
258
259 static void
260 put_cont (void *cls, int32_t success, const char *emsg)
261 {
262   nsqe = NULL;
263
264   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265               "Name store added record for `%s': %s\n",
266               TEST_NAME,
267               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
268
269   /* Create derived hash */
270   GNUNET_CRYPTO_ecdsa_key_get_public (&privkey,
271                                       &pubkey);
272   GNUNET_GNSRECORD_query_from_public_key (&pubkey, TEST_NAME, &derived_hash);
273
274   if (0 == GNUNET_TIME_absolute_get_remaining (record_expiration).rel_value_us)
275   {
276     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
277                 "Test to too long to store records, cannot run test!\n");
278     GNUNET_SCHEDULER_add_now (&end, NULL);
279     return;
280   }
281   /* Lookup active record now */
282   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
283               "Performing lookup for active record \n");
284   ncqe = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
285                                         &name_lookup_active_proc, &records[0]);
286
287   delayed_lookup_task = GNUNET_SCHEDULER_add_delayed (
288     GNUNET_TIME_relative_multiply (EXPIRATION, 2), &name_lookup_shadow, NULL);
289 }
290
291
292 static void
293 run (void *cls,
294      const struct GNUNET_CONFIGURATION_Handle *cfg,
295      struct GNUNET_TESTING_Peer *peer)
296 {
297   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
298                                                 &endbadly,
299                                                 NULL);
300   GNUNET_CRYPTO_ecdsa_key_create (&privkey);
301   GNUNET_CRYPTO_ecdsa_key_get_public (&privkey,
302                                       &pubkey);
303
304   record_expiration = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
305                                                 EXPIRATION);
306   records[0].expiration_time = record_expiration.abs_value_us;
307   records[0].record_type = TEST_RECORD_TYPE;
308   records[0].data_size = TEST_RECORD_DATALEN;
309   records[0].data = GNUNET_malloc (TEST_RECORD_DATALEN);
310   records[0].flags = GNUNET_GNSRECORD_RF_NONE;
311   memset ((char *) records[0].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
312
313   records[1].expiration_time = GNUNET_TIME_absolute_get ().abs_value_us
314                                + 1000000000;
315   records[1].record_type = TEST_RECORD_TYPE;
316   records[1].data_size = TEST_RECORD_DATALEN;
317   records[1].data = GNUNET_malloc (TEST_RECORD_DATALEN);
318   records[1].flags = GNUNET_GNSRECORD_RF_SHADOW_RECORD;
319   memset ((char *) records[1].data, TEST_SHADOW_RECORD_DATA,
320           TEST_RECORD_DATALEN);
321
322   nsh = GNUNET_NAMESTORE_connect (cfg);
323   nch = GNUNET_NAMECACHE_connect (cfg);
324   GNUNET_break (NULL != nsh);
325   GNUNET_break (NULL != nch);
326   nsqe = GNUNET_NAMESTORE_records_store (nsh,
327                                          &privkey,
328                                          TEST_NAME,
329                                          2,
330                                          records,
331                                          &put_cont,
332                                          NULL);
333   if (NULL == nsqe)
334   {
335     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
336                 _ ("Namestore cannot store no block\n"));
337   }
338
339   GNUNET_free_nz ((void *) records[0].data);
340   GNUNET_free_nz ((void *) records[1].data);
341 }
342
343
344 #include "test_common.c"
345
346
347 int
348 main (int argc, char *argv[])
349 {
350   const char *plugin_name;
351   char *cfg_name;
352
353   SETUP_CFG (plugin_name, cfg_name);
354   res = 1;
355   if (0 !=
356       GNUNET_TESTING_peer_run ("test-namestore-api-lookup-shadow-filter",
357                                cfg_name,
358                                &run,
359                                NULL))
360   {
361     res = 1;
362   }
363   GNUNET_DISK_purge_cfg_dir (cfg_name,
364                              "GNUNET_TEST_HOME");
365   GNUNET_free (cfg_name);
366   return res;
367 }
368
369
370 /* end of test_namestore_api_lookup_shadow_filter.c */