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.
16 * @file namestore/test_namestore_api_lookup_shadow.c
17 * @brief testcase for namestore_api.c: store a shadow record and perform a lookup
18 * test passes if test returns the record but without the shadow flag since no
19 * other valid record is available
22 #include "gnunet_namecache_service.h"
23 #include "gnunet_namestore_service.h"
24 #include "gnunet_testing_lib.h"
25 #include "gnunet_dnsparser_lib.h"
27 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
29 #define TEST_RECORD_DATALEN 123
31 #define TEST_RECORD_DATA 'a'
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
36 static struct GNUNET_NAMESTORE_Handle *nsh;
38 static struct GNUNET_NAMECACHE_Handle *nch;
40 static struct GNUNET_SCHEDULER_Task * endbadly_task;
42 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
44 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
48 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
50 static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
58 GNUNET_NAMESTORE_disconnect (nsh);
63 GNUNET_NAMECACHE_disconnect (nch);
68 GNUNET_free (privkey);
71 GNUNET_SCHEDULER_shutdown ();
76 * Re-establish the connection to the service.
78 * @param cls handle to use to re-connect.
85 GNUNET_NAMESTORE_cancel (nsqe);
90 GNUNET_NAMECACHE_cancel (ncqe);
107 rd_decrypt_cb (void *cls,
108 unsigned int rd_count,
109 const struct GNUNET_GNSRECORD_Data *rd)
111 char rd_cmp_data[TEST_RECORD_DATALEN];
115 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
121 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
125 memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
127 if (TEST_RECORD_TYPE != rd[0].record_type)
129 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
133 if (TEST_RECORD_DATALEN != rd[0].data_size)
135 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
139 if (0 != memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN))
141 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
145 if (0 != (GNUNET_GNSRECORD_RF_SHADOW_RECORD & rd[0].flags))
147 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "Block was decrypted successfully \n");
155 GNUNET_SCHEDULER_add_now (&end, NULL);
160 name_lookup_proc (void *cls,
161 const struct GNUNET_GNSRECORD_Block *block)
163 const char *name = cls;
166 GNUNET_assert (NULL != cls);
168 if (endbadly_task != NULL)
170 GNUNET_SCHEDULER_cancel (endbadly_task);
171 endbadly_task = NULL;
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
177 _("Namestore returned no block\n"));
178 if (endbadly_task != NULL)
179 GNUNET_SCHEDULER_cancel (endbadly_task);
180 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 "Namestore returned block, decrypting \n");
186 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
187 &pubkey, name, &rd_decrypt_cb, (void *) name));
192 put_cont (void *cls, int32_t success, const char *emsg)
194 const char *name = cls;
195 struct GNUNET_HashCode derived_hash;
196 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
199 GNUNET_assert (NULL != cls);
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201 "Name store added record for `%s': %s\n",
203 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
205 /* Create derived hash */
206 GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
207 GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &derived_hash);
209 ncqe = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
210 &name_lookup_proc, (void *) name);
216 const struct GNUNET_CONFIGURATION_Handle *cfg,
217 struct GNUNET_TESTING_Peer *peer)
219 struct GNUNET_GNSRECORD_Data rd;
220 const char * name = "dummy.dummy.gnunet";
222 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
225 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
226 GNUNET_assert (privkey != NULL);
227 GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
229 rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
230 rd.record_type = TEST_RECORD_TYPE;
231 rd.data_size = TEST_RECORD_DATALEN;
232 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
233 rd.flags = GNUNET_GNSRECORD_RF_SHADOW_RECORD;
234 memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
236 nsh = GNUNET_NAMESTORE_connect (cfg);
237 nch = GNUNET_NAMECACHE_connect (cfg);
238 GNUNET_break (NULL != nsh);
239 GNUNET_break (NULL != nch);
240 nsqe = GNUNET_NAMESTORE_records_store (nsh,
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 _("Namestore cannot store no block\n"));
252 GNUNET_free ((void *)rd.data);
257 main (int argc, char *argv[])
259 const char *plugin_name;
262 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
263 GNUNET_asprintf (&cfg_name,
264 "test_namestore_api_%s.conf",
266 GNUNET_DISK_purge_cfg_dir (cfg_name,
270 GNUNET_TESTING_peer_run ("test-namestore-api-lookup-shadow",
277 GNUNET_free (cfg_name);
278 GNUNET_DISK_purge_cfg_dir (cfg_name,
284 /* end of test_namestore_api_lookup_shadow.c */