2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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
28 #include "gnunet_namecache_service.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_dnsparser_lib.h"
33 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
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'
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
41 #define EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
43 static struct GNUNET_NAMESTORE_Handle *nsh;
45 static struct GNUNET_NAMECACHE_Handle *nch;
47 static struct GNUNET_SCHEDULER_Task *endbadly_task;
49 static struct GNUNET_SCHEDULER_Task *delayed_lookup_task;
51 static struct GNUNET_CRYPTO_EcdsaPrivateKey privkey;
53 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
57 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
59 static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
61 static struct GNUNET_NAMECACHE_QueueEntry *ncqe_shadow;
63 static struct GNUNET_GNSRECORD_Data records[2];
65 static struct GNUNET_TIME_Absolute record_expiration;
67 static struct GNUNET_HashCode derived_hash;
69 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
77 GNUNET_NAMESTORE_disconnect (nsh);
82 GNUNET_NAMECACHE_disconnect (nch);
85 GNUNET_SCHEDULER_shutdown ();
90 * Re-establish the connection to the service.
92 * @param cls handle to use to re-connect.
97 if (NULL != delayed_lookup_task)
99 GNUNET_SCHEDULER_cancel (delayed_lookup_task);
100 delayed_lookup_task = NULL;
104 GNUNET_NAMESTORE_cancel (nsqe);
109 GNUNET_NAMECACHE_cancel (ncqe);
126 rd_decrypt_cb (void *cls,
127 unsigned int rd_count,
128 const struct GNUNET_GNSRECORD_Data *rd)
130 struct GNUNET_GNSRECORD_Data *expected_rd = cls;
131 char rd_cmp_data[TEST_RECORD_DATALEN];
135 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
141 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
145 if (expected_rd == &records[0])
147 /* Expecting active record */
148 memset (rd_cmp_data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
149 if (TEST_RECORD_TYPE != rd[0].record_type)
151 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
155 if (TEST_RECORD_DATALEN != rd[0].data_size)
157 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
161 if (0 != memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN))
163 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
167 if (0 != (GNUNET_GNSRECORD_RF_SHADOW_RECORD & rd[0].flags))
169 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
173 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
174 "Block was decrypted successfully with active record\n");
176 if (expected_rd == &records[1])
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)
182 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
186 if (TEST_RECORD_DATALEN != rd[0].data_size)
188 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
192 if (0 != memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN))
194 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
198 if (0 != (GNUNET_GNSRECORD_RF_SHADOW_RECORD & rd[0].flags))
200 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
204 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
205 "Block was decrypted successfully with former shadow record \n");
206 GNUNET_SCHEDULER_add_now (&end, NULL);
212 name_lookup_active_proc (void *cls,
213 const struct GNUNET_GNSRECORD_Block *block)
215 struct GNUNET_GNSRECORD_Data *expected_rd = cls;
217 GNUNET_assert (NULL != expected_rd);
221 if (endbadly_task != NULL)
223 GNUNET_SCHEDULER_cancel (endbadly_task);
224 endbadly_task = NULL;
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);
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Namestore returned block, decrypting \n");
239 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt (block,
248 name_lookup_shadow (void *cls)
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,
260 put_cont (void *cls, int32_t success, const char *emsg)
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Name store added record for `%s': %s\n",
267 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
269 /* Create derived hash */
270 GNUNET_CRYPTO_ecdsa_key_get_public (&privkey,
272 GNUNET_GNSRECORD_query_from_public_key (&pubkey, TEST_NAME, &derived_hash);
274 if (0 == GNUNET_TIME_absolute_get_remaining (record_expiration).rel_value_us)
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);
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]);
287 delayed_lookup_task = GNUNET_SCHEDULER_add_delayed (
288 GNUNET_TIME_relative_multiply (EXPIRATION, 2), &name_lookup_shadow, NULL);
294 const struct GNUNET_CONFIGURATION_Handle *cfg,
295 struct GNUNET_TESTING_Peer *peer)
297 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
300 GNUNET_CRYPTO_ecdsa_key_create (&privkey);
301 GNUNET_CRYPTO_ecdsa_key_get_public (&privkey,
304 record_expiration = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
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);
313 records[1].expiration_time = GNUNET_TIME_absolute_get ().abs_value_us
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);
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,
335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
336 _ ("Namestore cannot store no block\n"));
339 GNUNET_free_nz ((void *) records[0].data);
340 GNUNET_free_nz ((void *) records[1].data);
344 #include "test_common.c"
348 main (int argc, char *argv[])
350 const char *plugin_name;
353 SETUP_CFG (plugin_name, cfg_name);
356 GNUNET_TESTING_peer_run ("test-namestore-api-lookup-shadow-filter",
363 GNUNET_DISK_purge_cfg_dir (cfg_name,
365 GNUNET_free (cfg_name);
370 /* end of test_namestore_api_lookup_shadow_filter.c */