2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2018 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_store_update.c
17 * @brief testcase for namestore_api.c: store a record, update it and perform a lookup
18 * @author Matthias Wachs
19 * @author Christian Grothoff
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 TEST_RECORD_DATALEN2 234
35 #define TEST_RECORD_DATA2 'b'
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
40 static struct GNUNET_NAMESTORE_Handle *nsh;
42 static struct GNUNET_NAMECACHE_Handle *nch;
44 static struct GNUNET_SCHEDULER_Task * endbadly_task;
46 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
48 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
52 static int update_performed;
54 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
56 static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
58 static const char *name = "dummy";
62 * Terminate test with error.
64 * @param cls handle to use to re-connect.
71 GNUNET_SCHEDULER_shutdown ();
79 if (NULL != endbadly_task)
81 GNUNET_SCHEDULER_cancel (endbadly_task);
86 GNUNET_NAMESTORE_cancel (nsqe);
91 GNUNET_NAMECACHE_cancel (ncqe);
96 GNUNET_NAMESTORE_disconnect (nsh);
101 GNUNET_NAMECACHE_disconnect (nch);
106 GNUNET_free (privkey);
119 rd_decrypt_cb (void *cls,
120 unsigned int rd_count,
121 const struct GNUNET_GNSRECORD_Data *rd)
123 struct GNUNET_GNSRECORD_Data rd_new;
125 GNUNET_assert (1 == rd_count);
126 GNUNET_assert (NULL != rd);
128 if (GNUNET_NO == update_performed)
130 char rd_cmp_data[TEST_RECORD_DATALEN];
134 TEST_RECORD_DATALEN);
135 GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
136 GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
137 GNUNET_assert (0 == memcmp (&rd_cmp_data,
139 TEST_RECORD_DATALEN));
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142 "Block was decrypted successfully, updating record \n");
144 rd_new.flags = GNUNET_GNSRECORD_RF_NONE;
145 rd_new.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
146 rd_new.record_type = TEST_RECORD_TYPE;
147 rd_new.data_size = TEST_RECORD_DATALEN2;
148 rd_new.data = GNUNET_malloc (TEST_RECORD_DATALEN2);
149 memset ((char *) rd_new.data,
151 TEST_RECORD_DATALEN2);
153 nsqe = GNUNET_NAMESTORE_records_store (nsh,
160 update_performed = GNUNET_YES;
164 char rd_cmp_data[TEST_RECORD_DATALEN2];
168 TEST_RECORD_DATALEN2);
169 GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
170 GNUNET_assert (TEST_RECORD_DATALEN2 == rd[0].data_size);
171 GNUNET_assert (0 == memcmp (&rd_cmp_data,
173 TEST_RECORD_DATALEN2));
174 GNUNET_SCHEDULER_shutdown ();
181 name_lookup_proc (void *cls,
182 const struct GNUNET_GNSRECORD_Block *block)
184 const char *name = cls;
187 GNUNET_assert (NULL != cls);
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191 _("Namecache returned no block for `%s'\n"),
193 GNUNET_SCHEDULER_shutdown ();
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Namecache returned block, decrypting \n");
199 GNUNET_assert (GNUNET_OK ==
200 GNUNET_GNSRECORD_block_decrypt (block,
213 const char *name = cls;
214 struct GNUNET_HashCode derived_hash;
217 GNUNET_assert (NULL != cls);
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "Name store added record for `%s': %s\n",
221 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
222 /* Create derived hash */
223 GNUNET_GNSRECORD_query_from_private_key (privkey,
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "Looking in namecache for `%s'\n",
228 GNUNET_h2s (&derived_hash));
229 ncqe = GNUNET_NAMECACHE_lookup_block (nch,
231 &name_lookup_proc, (void *) name);
237 const struct GNUNET_CONFIGURATION_Handle *cfg,
238 struct GNUNET_TESTING_Peer *peer)
240 struct GNUNET_GNSRECORD_Data rd;
242 update_performed = GNUNET_NO;
243 GNUNET_SCHEDULER_add_shutdown (&end,
245 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
248 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
249 GNUNET_assert (privkey != NULL);
250 GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
252 rd.flags = GNUNET_GNSRECORD_RF_NONE;
253 rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
254 rd.record_type = TEST_RECORD_TYPE;
255 rd.data_size = TEST_RECORD_DATALEN;
256 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
257 memset ((char *) rd.data,
259 TEST_RECORD_DATALEN);
261 nsh = GNUNET_NAMESTORE_connect (cfg);
262 GNUNET_break (NULL != nsh);
263 nch = GNUNET_NAMECACHE_connect (cfg);
264 GNUNET_break (NULL != nch);
265 nsqe = GNUNET_NAMESTORE_records_store (nsh,
274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
275 _("Namestore cannot store no block\n"));
277 GNUNET_free ((void *)rd.data);
285 const char *plugin_name;
288 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
289 GNUNET_asprintf (&cfg_name,
290 "test_namestore_api_%s.conf",
293 GNUNET_DISK_purge_cfg_dir (cfg_name,
296 GNUNET_TESTING_peer_run ("test-namestore-api-store-update",
303 GNUNET_DISK_purge_cfg_dir (cfg_name,
305 GNUNET_free (cfg_name);
310 /* end of test_namestore_api_store_update.c */