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.c
17 * @brief testcase for namestore_api.c: store a record and perform a lookup
20 #include "gnunet_namecache_service.h"
21 #include "gnunet_namestore_service.h"
22 #include "gnunet_testing_lib.h"
23 #include "gnunet_dnsparser_lib.h"
25 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
27 #define TEST_RECORD_DATALEN 123
29 #define TEST_RECORD_DATA 'a'
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
34 static struct GNUNET_NAMESTORE_Handle *nsh;
36 static struct GNUNET_NAMECACHE_Handle *nch;
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_NAMESTORE_QueueEntry *nsqe;
48 static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
56 GNUNET_NAMESTORE_disconnect (nsh);
61 GNUNET_NAMECACHE_disconnect (nch);
66 GNUNET_free (privkey);
69 GNUNET_SCHEDULER_shutdown ();
74 * Re-establish the connection to the service.
76 * @param cls handle to use to re-connect.
83 GNUNET_NAMESTORE_cancel (nsqe);
88 GNUNET_NAMECACHE_cancel (ncqe);
105 rd_decrypt_cb (void *cls,
106 unsigned int rd_count,
107 const struct GNUNET_GNSRECORD_Data *rd)
109 char rd_cmp_data[TEST_RECORD_DATALEN];
111 GNUNET_assert (1 == rd_count);
112 GNUNET_assert (NULL != rd);
114 memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
116 GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
117 GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
118 GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121 "Block was decrypted successfully \n");
123 GNUNET_SCHEDULER_add_now (&end, NULL);
128 name_lookup_proc (void *cls,
129 const struct GNUNET_GNSRECORD_Block *block)
131 const char *name = cls;
134 GNUNET_assert (NULL != cls);
136 if (endbadly_task != NULL)
138 GNUNET_SCHEDULER_cancel (endbadly_task);
139 endbadly_task = NULL;
144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145 _("Namestore returned no block\n"));
146 if (endbadly_task != NULL)
147 GNUNET_SCHEDULER_cancel (endbadly_task);
148 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "Namestore returned block, decrypting \n");
154 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
155 &pubkey, name, &rd_decrypt_cb, (void *) name));
160 put_cont (void *cls, int32_t success, const char *emsg)
162 const char *name = cls;
163 struct GNUNET_HashCode derived_hash;
164 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
167 GNUNET_assert (NULL != cls);
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "Name store added record for `%s': %s\n",
171 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
173 /* Create derived hash */
174 GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
175 GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &derived_hash);
177 ncqe = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
178 &name_lookup_proc, (void *) name);
184 const struct GNUNET_CONFIGURATION_Handle *cfg,
185 struct GNUNET_TESTING_Peer *peer)
187 struct GNUNET_GNSRECORD_Data rd;
188 const char * name = "dummy.dummy.gnunet";
190 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
193 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
194 GNUNET_assert (privkey != NULL);
195 GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
198 rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
199 rd.record_type = TEST_RECORD_TYPE;
200 rd.data_size = TEST_RECORD_DATALEN;
201 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
203 memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
205 nsh = GNUNET_NAMESTORE_connect (cfg);
206 nch = GNUNET_NAMECACHE_connect (cfg);
207 GNUNET_break (NULL != nsh);
208 GNUNET_break (NULL != nch);
209 nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
210 1, &rd, &put_cont, (void *) name);
213 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
214 _("Namestore cannot store no block\n"));
217 GNUNET_free ((void *)rd.data);
222 main (int argc, char *argv[])
224 const char *plugin_name;
227 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
228 GNUNET_asprintf (&cfg_name,
229 "test_namestore_api_%s.conf",
231 GNUNET_DISK_purge_cfg_dir (cfg_name,
235 GNUNET_TESTING_peer_run ("test-namestore-api",
242 GNUNET_DISK_purge_cfg_dir (cfg_name,
244 GNUNET_free (cfg_name);
249 /* end of test_namestore_api_lookup_public.c */