minor fixes + test for shadows record filtering and usage
[oweals/gnunet.git] / src / namestore / test_namestore_api_lookup_shadow_filter.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 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_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, 2)
40
41 static struct GNUNET_NAMESTORE_Handle *nsh;
42
43 static struct GNUNET_NAMECACHE_Handle *nch;
44
45 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
46
47 static GNUNET_SCHEDULER_TaskIdentifier 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 void
70 cleanup ()
71 {
72   if (NULL != nsh)
73   {
74     GNUNET_NAMESTORE_disconnect (nsh);
75     nsh = NULL;
76   }
77   if (NULL != nch)
78   {
79     GNUNET_NAMECACHE_disconnect (nch);
80     nch = NULL;
81   }
82   if (NULL != privkey)
83   {
84     GNUNET_free (privkey);
85     privkey = NULL;
86   }
87   GNUNET_SCHEDULER_shutdown ();
88 }
89
90
91 /**
92  * Re-establish the connection to the service.
93  *
94  * @param cls handle to use to re-connect.
95  * @param tc scheduler context
96  */
97 static void
98 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
99 {
100   if (GNUNET_SCHEDULER_NO_TASK != delayed_lookup_task)
101   {
102     GNUNET_SCHEDULER_cancel (delayed_lookup_task);
103     delayed_lookup_task = GNUNET_SCHEDULER_NO_TASK;
104   }
105   if (NULL != nsqe)
106   {
107     GNUNET_NAMESTORE_cancel (nsqe);
108     nsqe = NULL;
109   }
110   if (NULL != ncqe)
111   {
112     GNUNET_NAMECACHE_cancel (ncqe);
113     ncqe = NULL;
114   }
115   cleanup ();
116   res = 1;
117 }
118
119
120 static void
121 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
122 {
123   cleanup ();
124   res = 0;
125 }
126
127
128 static void
129 rd_decrypt_cb (void *cls,
130                unsigned int rd_count,
131                const struct GNUNET_GNSRECORD_Data *rd)
132 {
133   struct GNUNET_GNSRECORD_Data *expected_rd = cls;
134   char rd_cmp_data[TEST_RECORD_DATALEN];
135
136   if (1 != rd_count)
137   {
138     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
139     GNUNET_break (0);
140     return;
141   }
142   if (NULL == rd)
143   {
144     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
145     GNUNET_break (0);
146     return;
147   }
148   if (expected_rd == &records[0])
149   {
150     /* Expecting active record */
151     memset (rd_cmp_data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
152     GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
153     GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
154     GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
155     GNUNET_assert (0 == (GNUNET_GNSRECORD_RF_SHADOW_RECORD & rd[0].flags));
156     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
157                 "Block was decrypted successfully with active record\n");
158   }
159   if (expected_rd == &records[1])
160   {
161     /* Expecting shadow record  but without shadow flag*/
162     memset (rd_cmp_data, TEST_SHADOW_RECORD_DATA, TEST_RECORD_DATALEN);
163     GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
164     GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
165     GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
166     GNUNET_assert (0 == (GNUNET_GNSRECORD_RF_SHADOW_RECORD & rd[0].flags));
167     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
168                 "Block was decrypted successfully with former shadow record \n");
169     GNUNET_SCHEDULER_add_now (&end, NULL );
170   }
171 }
172
173
174 static void
175 name_lookup_active_proc (void *cls,
176                   const struct GNUNET_GNSRECORD_Block *block)
177 {
178   struct GNUNET_GNSRECORD_Data *expected_rd = cls;
179   GNUNET_assert (NULL != expected_rd);
180
181   ncqe = NULL;
182   ncqe_shadow = NULL;
183   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
184   {
185     GNUNET_SCHEDULER_cancel (endbadly_task);
186     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
187   }
188
189   if (NULL == block)
190   {
191     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
192               _("Namestore returned no block\n"));
193     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
194       GNUNET_SCHEDULER_cancel (endbadly_task);
195     endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
196     return;
197   }
198
199   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200               "Namestore returned block, decrypting \n");
201   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
202                 &pubkey, TEST_NAME, &rd_decrypt_cb, expected_rd));
203 }
204
205 static void
206 name_lookup_shadow (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
207 {
208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209               "Performing lookup for shadow record \n");
210   delayed_lookup_task = GNUNET_SCHEDULER_NO_TASK;
211   ncqe_shadow = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
212       &name_lookup_active_proc, &records[1]);
213 }
214
215
216 static void
217 put_cont (void *cls, int32_t success, const char *emsg)
218 {
219   nsqe = NULL;
220
221   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222               "Name store added record for `%s': %s\n",
223               TEST_NAME,
224               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
225
226   /* Create derived hash */
227   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
228   GNUNET_GNSRECORD_query_from_public_key (&pubkey, TEST_NAME, &derived_hash);
229
230   if (0 == GNUNET_TIME_absolute_get_remaining(record_expiration).rel_value_us )
231   {
232     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233                 "Test to too long to store records, cannot run test!\n");
234     GNUNET_SCHEDULER_add_now (&end, NULL );
235     return;
236   }
237   /* Lookup active record now */
238   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
239               "Performing lookup for active record \n");
240   ncqe = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
241                                         &name_lookup_active_proc, &records[0]);
242
243   delayed_lookup_task = GNUNET_SCHEDULER_add_delayed (EXPIRATION, &name_lookup_shadow, NULL);
244 }
245
246
247 static void
248 run (void *cls,
249      const struct GNUNET_CONFIGURATION_Handle *cfg,
250      struct GNUNET_TESTING_Peer *peer)
251 {
252   char *hostkey_file;
253
254   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
255                                                 &endbadly, NULL);
256   GNUNET_asprintf (&hostkey_file,
257                    "zonefiles%s%s",
258                    DIR_SEPARATOR_STR,
259                    "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
260   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
261   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
262   GNUNET_free (hostkey_file);
263   GNUNET_assert (privkey != NULL);
264   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
265
266   record_expiration = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), EXPIRATION);
267   records[0].expiration_time = record_expiration.abs_value_us;
268   records[0].record_type = TEST_RECORD_TYPE;
269   records[0].data_size = TEST_RECORD_DATALEN;
270   records[0].data = GNUNET_malloc (TEST_RECORD_DATALEN);
271   records[0].flags = GNUNET_GNSRECORD_RF_NONE;
272   memset ((char *) records[0].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
273
274   records[1].expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
275   records[1].record_type = TEST_RECORD_TYPE;
276   records[1].data_size = TEST_RECORD_DATALEN;
277   records[1].data = GNUNET_malloc (TEST_RECORD_DATALEN);
278   records[1].flags = GNUNET_GNSRECORD_RF_SHADOW_RECORD;
279   memset ((char *) records[1].data, TEST_SHADOW_RECORD_DATA, TEST_RECORD_DATALEN);
280
281   nsh = GNUNET_NAMESTORE_connect (cfg);
282   nch = GNUNET_NAMECACHE_connect (cfg);
283   GNUNET_break (NULL != nsh);
284   GNUNET_break (NULL != nch);
285   nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, TEST_NAME,
286                                       2, records, &put_cont, NULL);
287   if (NULL == nsqe)
288   {
289     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
290               _("Namestore cannot store no block\n"));
291   }
292
293   GNUNET_free ((void *) records[0].data);
294   GNUNET_free ((void *) records[1].data);
295 }
296
297
298 int
299 main (int argc, char *argv[])
300 {
301   res = 1;
302   if (0 !=
303       GNUNET_TESTING_peer_run ("test-namestore-api",
304                                "test_namestore_api.conf",
305                                &run,
306                                NULL))
307     return 1;
308   return res;
309 }
310
311
312 /* end of test_namestore_api_lookup_shadow_filter.c */