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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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
24 * @author Christian Grothoff
27 #include "gnunet_namecache_service.h"
28 #include "gnunet_namestore_service.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_dnsparser_lib.h"
32 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
34 #define TEST_RECORD_DATALEN 123
36 #define TEST_RECORD_DATA 'a'
38 #define TEST_RECORD_DATALEN2 234
40 #define TEST_RECORD_DATA2 'b'
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
45 static struct GNUNET_NAMESTORE_Handle *nsh;
47 static struct GNUNET_NAMECACHE_Handle *nch;
49 static struct GNUNET_SCHEDULER_Task *endbadly_task;
51 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
53 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
57 static int update_performed;
59 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
61 static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
63 static const char *name = "dummy";
67 * Terminate test with error.
69 * @param cls handle to use to re-connect.
76 GNUNET_SCHEDULER_shutdown ();
84 if (NULL != endbadly_task)
86 GNUNET_SCHEDULER_cancel (endbadly_task);
91 GNUNET_NAMESTORE_cancel (nsqe);
96 GNUNET_NAMECACHE_cancel (ncqe);
101 GNUNET_NAMESTORE_disconnect (nsh);
106 GNUNET_NAMECACHE_disconnect (nch);
111 GNUNET_free (privkey);
124 rd_decrypt_cb (void *cls,
125 unsigned int rd_count,
126 const struct GNUNET_GNSRECORD_Data *rd)
128 struct GNUNET_GNSRECORD_Data rd_new;
130 GNUNET_assert (1 == rd_count);
131 GNUNET_assert (NULL != rd);
133 if (GNUNET_NO == update_performed)
135 char rd_cmp_data[TEST_RECORD_DATALEN];
139 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,
144 TEST_RECORD_DATALEN));
146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147 "Block was decrypted successfully, updating record \n");
149 rd_new.flags = GNUNET_GNSRECORD_RF_NONE;
150 rd_new.expiration_time = GNUNET_TIME_absolute_get ().abs_value_us
152 rd_new.record_type = TEST_RECORD_TYPE;
153 rd_new.data_size = TEST_RECORD_DATALEN2;
154 rd_new.data = GNUNET_malloc (TEST_RECORD_DATALEN2);
155 memset ((char *) rd_new.data,
157 TEST_RECORD_DATALEN2);
159 nsqe = GNUNET_NAMESTORE_records_store (nsh,
166 update_performed = GNUNET_YES;
170 char rd_cmp_data[TEST_RECORD_DATALEN2];
174 TEST_RECORD_DATALEN2);
175 GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
176 GNUNET_assert (TEST_RECORD_DATALEN2 == rd[0].data_size);
177 GNUNET_assert (0 == memcmp (&rd_cmp_data,
179 TEST_RECORD_DATALEN2));
180 GNUNET_SCHEDULER_shutdown ();
187 name_lookup_proc (void *cls,
188 const struct GNUNET_GNSRECORD_Block *block)
190 const char *name = cls;
193 GNUNET_assert (NULL != cls);
196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197 _ ("Namecache returned no block for `%s'\n"),
199 GNUNET_SCHEDULER_shutdown ();
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "Namecache returned block, decrypting \n");
205 GNUNET_assert (GNUNET_OK ==
206 GNUNET_GNSRECORD_block_decrypt (block,
219 const char *name = cls;
220 struct GNUNET_HashCode derived_hash;
223 GNUNET_assert (NULL != cls);
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Name store added record for `%s': %s\n",
227 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
228 /* Create derived hash */
229 GNUNET_GNSRECORD_query_from_private_key (privkey,
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Looking in namecache for `%s'\n",
234 GNUNET_h2s (&derived_hash));
235 ncqe = GNUNET_NAMECACHE_lookup_block (nch,
237 &name_lookup_proc, (void *) name);
243 const struct GNUNET_CONFIGURATION_Handle *cfg,
244 struct GNUNET_TESTING_Peer *peer)
246 struct GNUNET_GNSRECORD_Data rd;
248 update_performed = GNUNET_NO;
249 GNUNET_SCHEDULER_add_shutdown (&end,
251 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
254 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
255 GNUNET_assert (privkey != NULL);
256 GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
258 rd.flags = GNUNET_GNSRECORD_RF_NONE;
259 rd.expiration_time = GNUNET_TIME_absolute_get ().abs_value_us + 1000000000;
260 rd.record_type = TEST_RECORD_TYPE;
261 rd.data_size = TEST_RECORD_DATALEN;
262 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
263 memset ((char *) rd.data,
265 TEST_RECORD_DATALEN);
267 nsh = GNUNET_NAMESTORE_connect (cfg);
268 GNUNET_break (NULL != nsh);
269 nch = GNUNET_NAMECACHE_connect (cfg);
270 GNUNET_break (NULL != nch);
271 nsqe = GNUNET_NAMESTORE_records_store (nsh,
280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
281 _ ("Namestore cannot store no block\n"));
283 GNUNET_free ((void *) rd.data);
287 #include "test_common.c"
294 const char *plugin_name;
297 SETUP_CFG (plugin_name, cfg_name);
300 GNUNET_TESTING_peer_run ("test-namestore-api-store-update",
307 GNUNET_DISK_purge_cfg_dir (cfg_name,
309 GNUNET_free (cfg_name);
314 /* end of test_namestore_api_store_update.c */