aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/test_namestore_api_lookup_shadow_filter.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/namestore/test_namestore_api_lookup_shadow_filter.c')
-rw-r--r--src/namestore/test_namestore_api_lookup_shadow_filter.c371
1 files changed, 0 insertions, 371 deletions
diff --git a/src/namestore/test_namestore_api_lookup_shadow_filter.c b/src/namestore/test_namestore_api_lookup_shadow_filter.c
deleted file mode 100644
index 0bcd130f9..000000000
--- a/src/namestore/test_namestore_api_lookup_shadow_filter.c
+++ /dev/null
@@ -1,371 +0,0 @@
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 "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
43static struct GNUNET_NAMESTORE_Handle *nsh;
44
45static struct GNUNET_NAMECACHE_Handle *nch;
46
47static struct GNUNET_SCHEDULER_Task *endbadly_task;
48
49static struct GNUNET_SCHEDULER_Task *delayed_lookup_task;
50
51static struct GNUNET_IDENTITY_PrivateKey privkey;
52
53static struct GNUNET_IDENTITY_PublicKey pubkey;
54
55static int res;
56
57static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
58
59static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
60
61static struct GNUNET_NAMECACHE_QueueEntry *ncqe_shadow;
62
63static struct GNUNET_GNSRECORD_Data records[2];
64
65static struct GNUNET_TIME_Absolute record_expiration;
66
67static struct GNUNET_HashCode derived_hash;
68
69static struct GNUNET_IDENTITY_PublicKey pubkey;
70
71
72static void
73cleanup ()
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 */
94static void
95endbadly (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
117static void
118end (void *cls)
119{
120 cleanup ();
121 res = 0;
122}
123
124
125static void
126rd_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
211static void
212name_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
247static void
248name_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
259static void
260put_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_IDENTITY_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
292static void
293run (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 privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
301 GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key);
302 GNUNET_IDENTITY_key_get_public (&privkey,
303 &pubkey);
304
305 record_expiration = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
306 EXPIRATION);
307 records[0].expiration_time = record_expiration.abs_value_us;
308 records[0].record_type = TEST_RECORD_TYPE;
309 records[0].data_size = TEST_RECORD_DATALEN;
310 records[0].data = GNUNET_malloc (TEST_RECORD_DATALEN);
311 records[0].flags = GNUNET_GNSRECORD_RF_NONE;
312 memset ((char *) records[0].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
313
314 records[1].expiration_time = GNUNET_TIME_absolute_get ().abs_value_us
315 + 1000000000;
316 records[1].record_type = TEST_RECORD_TYPE;
317 records[1].data_size = TEST_RECORD_DATALEN;
318 records[1].data = GNUNET_malloc (TEST_RECORD_DATALEN);
319 records[1].flags = GNUNET_GNSRECORD_RF_SHADOW_RECORD;
320 memset ((char *) records[1].data, TEST_SHADOW_RECORD_DATA,
321 TEST_RECORD_DATALEN);
322
323 nsh = GNUNET_NAMESTORE_connect (cfg);
324 nch = GNUNET_NAMECACHE_connect (cfg);
325 GNUNET_break (NULL != nsh);
326 GNUNET_break (NULL != nch);
327 nsqe = GNUNET_NAMESTORE_records_store (nsh,
328 &privkey,
329 TEST_NAME,
330 2,
331 records,
332 &put_cont,
333 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_nz ((void *) records[0].data);
341 GNUNET_free_nz ((void *) records[1].data);
342}
343
344
345#include "test_common.c"
346
347
348int
349main (int argc, char *argv[])
350{
351 const char *plugin_name;
352 char *cfg_name;
353
354 SETUP_CFG (plugin_name, cfg_name);
355 res = 1;
356 if (0 !=
357 GNUNET_TESTING_peer_run ("test-namestore-api-lookup-shadow-filter",
358 cfg_name,
359 &run,
360 NULL))
361 {
362 res = 1;
363 }
364 GNUNET_DISK_purge_cfg_dir (cfg_name,
365 "GNUNET_TEST_HOME");
366 GNUNET_free (cfg_name);
367 return res;
368}
369
370
371/* end of test_namestore_api_lookup_shadow_filter.c */