2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file namestore/test_namestore_api_store_update.c
22 * @brief testcase for namestore_api.c: store a record, update it and perform a lookup
23 * @author Matthias Wachs
26 #include "gnunet_namecache_service.h"
27 #include "gnunet_namestore_service.h"
28 #include "gnunet_testing_lib.h"
30 #define TEST_RECORD_TYPE 1234
32 #define TEST_RECORD_DATALEN 123
34 #define TEST_RECORD_DATA 'a'
37 #define TEST_RECORD_TYPE2 4321
39 #define TEST_RECORD_DATALEN2 234
41 #define TEST_RECORD_DATA2 'b'
43 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
46 static struct GNUNET_NAMESTORE_Handle *nsh;
48 static struct GNUNET_NAMECACHE_Handle *nch;
50 static struct GNUNET_SCHEDULER_Task * endbadly_task;
52 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
54 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
58 static int update_performed;
60 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
62 static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
64 static const char *name = "dummy";
66 static char *directory;
73 GNUNET_NAMESTORE_disconnect (nsh);
78 GNUNET_NAMECACHE_disconnect (nch);
83 GNUNET_free (privkey);
86 GNUNET_SCHEDULER_shutdown ();
91 * Re-establish the connection to the service.
93 * @param cls handle to use to re-connect.
100 GNUNET_NAMESTORE_cancel (nsqe);
105 GNUNET_NAMECACHE_cancel (ncqe);
122 put_cont (void *cls, int32_t success, const char *emsg);
126 rd_decrypt_cb (void *cls,
127 unsigned int rd_count,
128 const struct GNUNET_GNSRECORD_Data *rd)
130 struct GNUNET_GNSRECORD_Data rd_new;
132 GNUNET_assert (1 == rd_count);
133 GNUNET_assert (NULL != rd);
135 if (GNUNET_NO == update_performed)
137 char rd_cmp_data[TEST_RECORD_DATALEN];
138 memset (rd_cmp_data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
140 GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
141 GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
142 GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 "Block was decrypted successfully, updating record \n");
147 rd_new.flags = GNUNET_GNSRECORD_RF_NONE;
148 rd_new.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
149 rd_new.record_type = TEST_RECORD_TYPE2;
150 rd_new.data_size = TEST_RECORD_DATALEN2;
151 rd_new.data = GNUNET_malloc (TEST_RECORD_DATALEN2);
152 memset ((char *) rd_new.data, TEST_RECORD_DATA2, TEST_RECORD_DATALEN2);
154 nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
155 1, &rd_new, &put_cont, (void *) name);
156 update_performed = GNUNET_YES;
160 char rd_cmp_data[TEST_RECORD_DATALEN2];
161 memset (rd_cmp_data, TEST_RECORD_DATA2, TEST_RECORD_DATALEN2);
163 GNUNET_assert (TEST_RECORD_TYPE2 == rd[0].record_type);
164 GNUNET_assert (TEST_RECORD_DATALEN2 == rd[0].data_size);
165 GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN2));
167 GNUNET_SCHEDULER_cancel (endbadly_task);
168 endbadly_task = NULL;
169 GNUNET_SCHEDULER_add_now (&end, NULL);
175 name_lookup_proc (void *cls,
176 const struct GNUNET_GNSRECORD_Block *block)
178 const char *name = cls;
181 GNUNET_assert (NULL != cls);
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185 _("Namecache returned no block for `%s'\n"),
187 if (endbadly_task != NULL)
188 GNUNET_SCHEDULER_cancel (endbadly_task);
189 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "Namecache returned block, decrypting \n");
195 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
196 &pubkey, name, &rd_decrypt_cb, (void *) name));
201 put_cont (void *cls, int32_t success, const char *emsg)
203 const char *name = cls;
204 struct GNUNET_HashCode derived_hash;
207 GNUNET_assert (NULL != cls);
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209 "Name store added record for `%s': %s\n",
211 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
212 /* Create derived hash */
213 GNUNET_GNSRECORD_query_from_private_key (privkey,
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "Looking in namecache for `%s'\n",
218 GNUNET_h2s (&derived_hash));
219 ncqe = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
220 &name_lookup_proc, (void *) name);
226 const struct GNUNET_CONFIGURATION_Handle *cfg,
227 struct GNUNET_TESTING_Peer *peer)
229 struct GNUNET_GNSRECORD_Data rd;
233 GNUNET_assert (GNUNET_OK ==
234 GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "GNUNET_TEST_HOME", &directory));
235 GNUNET_DISK_directory_remove (directory);
237 update_performed = GNUNET_NO;
238 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
240 GNUNET_asprintf (&hostkey_file,
243 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
245 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
246 GNUNET_free (hostkey_file);
247 GNUNET_assert (privkey != NULL);
248 GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
250 rd.flags = GNUNET_GNSRECORD_RF_NONE;
251 rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
252 rd.record_type = TEST_RECORD_TYPE;
253 rd.data_size = TEST_RECORD_DATALEN;
254 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
255 memset ((char *) rd.data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
257 nsh = GNUNET_NAMESTORE_connect (cfg);
258 GNUNET_break (NULL != nsh);
259 nch = GNUNET_NAMECACHE_connect (cfg);
260 GNUNET_break (NULL != nch);
261 nsqe = GNUNET_NAMESTORE_records_store (nsh,
264 &put_cont, (void *) name);
267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
268 _("Namestore cannot store no block\n"));
270 GNUNET_free ((void *)rd.data);
275 main (int argc, char *argv[])
277 const char *plugin_name;
280 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
281 GNUNET_asprintf (&cfg_name,
282 "test_namestore_api_%s.conf",
286 GNUNET_TESTING_peer_run ("test-namestore-api-store-update",
293 GNUNET_free (cfg_name);
294 if (NULL != directory)
296 GNUNET_DISK_directory_remove (directory);
297 GNUNET_free (directory);
303 /* end of test_namestore_api_store_update.c*/