update tests to use new MQ API
[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  */
98 static void
99 endbadly (void *cls)
100 {
101   if (NULL != delayed_lookup_task)
102   {
103     GNUNET_SCHEDULER_cancel (delayed_lookup_task);
104     delayed_lookup_task = NULL;
105   }
106   if (NULL != nsqe)
107   {
108     GNUNET_NAMESTORE_cancel (nsqe);
109     nsqe = NULL;
110   }
111   if (NULL != ncqe)
112   {
113     GNUNET_NAMECACHE_cancel (ncqe);
114     ncqe = NULL;
115   }
116   cleanup ();
117   res = 1;
118 }
119
120
121 static void
122 end (void *cls)
123 {
124   cleanup ();
125   res = 0;
126 }
127
128
129 static void
130 rd_decrypt_cb (void *cls,
131                unsigned int rd_count,
132                const struct GNUNET_GNSRECORD_Data *rd)
133 {
134   struct GNUNET_GNSRECORD_Data *expected_rd = cls;
135   char rd_cmp_data[TEST_RECORD_DATALEN];
136
137   if (1 != rd_count)
138   {
139     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
140     GNUNET_break (0);
141     return;
142   }
143   if (NULL == rd)
144   {
145     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
146     GNUNET_break (0);
147     return;
148   }
149   if (expected_rd == &records[0])
150   {
151     /* Expecting active record */
152     memset (rd_cmp_data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
153     if (TEST_RECORD_TYPE != rd[0].record_type)
154     {
155       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
156       GNUNET_break (0);
157       return;
158     }
159     if (TEST_RECORD_DATALEN != rd[0].data_size)
160     {
161       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
162       GNUNET_break (0);
163       return;
164     }
165     if (0 != memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN))
166     {
167       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
168       GNUNET_break (0);
169       return;
170     }
171     if (0 != (GNUNET_GNSRECORD_RF_SHADOW_RECORD & rd[0].flags))
172     {
173       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
174       GNUNET_break (0);
175       return;
176     }
177     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
178                 "Block was decrypted successfully with active record\n");
179   }
180   if (expected_rd == &records[1])
181   {
182     /* Expecting shadow record  but without shadow flag*/
183     memset (rd_cmp_data, TEST_SHADOW_RECORD_DATA, TEST_RECORD_DATALEN);
184     if (TEST_RECORD_TYPE != rd[0].record_type)
185     {
186       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
187       GNUNET_break (0);
188       return;
189     }
190     if (TEST_RECORD_DATALEN != rd[0].data_size)
191     {
192       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
193       GNUNET_break (0);
194       return;
195     }
196     if (0 != memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN))
197     {
198       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
199       GNUNET_break (0);
200       return;
201     }
202     if (0 != (GNUNET_GNSRECORD_RF_SHADOW_RECORD & rd[0].flags))
203     {
204       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
205       GNUNET_break (0);
206       return;
207     }
208     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
209                 "Block was decrypted successfully with former shadow record \n");
210     GNUNET_SCHEDULER_add_now (&end, NULL );
211   }
212 }
213
214
215 static void
216 name_lookup_active_proc (void *cls,
217                   const struct GNUNET_GNSRECORD_Block *block)
218 {
219   struct GNUNET_GNSRECORD_Data *expected_rd = cls;
220   GNUNET_assert (NULL != expected_rd);
221
222   ncqe = NULL;
223   ncqe_shadow = NULL;
224   if (endbadly_task != NULL)
225   {
226     GNUNET_SCHEDULER_cancel (endbadly_task);
227     endbadly_task = NULL;
228   }
229
230   if (NULL == block)
231   {
232     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233               _("Namestore returned no block\n"));
234     if (endbadly_task != NULL)
235       GNUNET_SCHEDULER_cancel (endbadly_task);
236     endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
237     return;
238   }
239
240   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241               "Namestore returned block, decrypting \n");
242   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
243                 &pubkey, TEST_NAME, &rd_decrypt_cb, 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, &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 */