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