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/>.
19 * @file namecache/test_namecache_api.c
20 * @brief testcase for namecache_api.c: store a record and perform a lookup
23 #include "gnunet_namecache_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_NAMECACHE_Handle *nsh;
38 static struct GNUNET_SCHEDULER_Task * endbadly_task;
40 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
42 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
46 static struct GNUNET_NAMECACHE_QueueEntry *nsqe;
54 GNUNET_NAMECACHE_disconnect (nsh);
59 GNUNET_free (privkey);
62 GNUNET_SCHEDULER_shutdown ();
67 * Re-establish the connection to the service.
69 * @param cls handle to use to re-connect.
76 GNUNET_NAMECACHE_cancel (nsqe);
93 rd_decrypt_cb (void *cls,
94 unsigned int rd_count,
95 const struct GNUNET_GNSRECORD_Data *rd)
97 char rd_cmp_data[TEST_RECORD_DATALEN];
99 GNUNET_assert (1 == rd_count);
100 GNUNET_assert (NULL != rd);
102 memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
104 GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
105 GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
106 GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109 "Block was decrypted successfully \n");
111 GNUNET_SCHEDULER_add_now (&end, NULL);
116 name_lookup_proc (void *cls,
117 const struct GNUNET_GNSRECORD_Block *block)
119 const char *name = cls;
122 GNUNET_assert (NULL != cls);
124 if (endbadly_task != NULL)
126 GNUNET_SCHEDULER_cancel (endbadly_task);
127 endbadly_task = NULL;
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133 _("Namecache returned no block\n"));
134 if (NULL != endbadly_task)
135 GNUNET_SCHEDULER_cancel (endbadly_task);
136 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141 "Namecache returned block, decrypting \n");
142 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
143 &pubkey, name, &rd_decrypt_cb, (void *) name));
147 cache_cont (void *cls, int32_t success, const char *emsg)
149 const char *name = cls;
150 struct GNUNET_HashCode derived_hash;
152 GNUNET_assert (NULL != cls);
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Name store cached record for `%s': %s\n",
157 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
159 /* Create derived hash */
160 GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &derived_hash);
162 nsqe = GNUNET_NAMECACHE_lookup_block (nsh, &derived_hash,
163 &name_lookup_proc, (void *) name);
169 const struct GNUNET_CONFIGURATION_Handle *cfg,
170 struct GNUNET_TESTING_Peer *peer)
172 struct GNUNET_GNSRECORD_Data rd;
173 struct GNUNET_GNSRECORD_Block *block;
175 const char * name = "dummy.dummy.gnunet";
177 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
179 GNUNET_asprintf (&hostkey_file,
182 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
184 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
185 GNUNET_free (hostkey_file);
186 GNUNET_assert (privkey != NULL);
187 GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
190 rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 10000000000;
191 rd.record_type = TEST_RECORD_TYPE;
192 rd.data_size = TEST_RECORD_DATALEN;
193 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
195 memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
196 block = GNUNET_GNSRECORD_block_create (privkey,
197 GNUNET_TIME_UNIT_FOREVER_ABS,
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 "Namecache cannot cache no block!\n");
203 GNUNET_SCHEDULER_shutdown ();
208 nsh = GNUNET_NAMECACHE_connect (cfg);
211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212 _("Namecache cannot connect to namecache\n"));
213 GNUNET_SCHEDULER_shutdown ();
217 GNUNET_break (NULL != nsh);
219 nsqe = GNUNET_NAMECACHE_block_cache (nsh,
221 &cache_cont, (void *) name);
224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225 _("Namecache cannot cache no block\n"));
228 GNUNET_free ((void *)rd.data);
233 main (int argc, char *argv[])
235 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-namecache/");
238 GNUNET_TESTING_service_run ("test-namecache-api",
240 "test_namecache_api.conf",
248 /* end of test_namecache_api_cache_block.c */