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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file namestore/test_namestore_api_lookup_nick.c
22 * @brief testcase for namestore_api.c: NICK records
25 #include "gnunet_namestore_service.h"
26 #include "gnunet_gns_service.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_dnsparser_lib.h"
30 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
32 #define TEST_RECORD_DATALEN 123
34 #define TEST_NICK "gnunettestnick"
36 #define TEST_RECORD_DATA 'a'
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
40 static struct GNUNET_NAMESTORE_Handle *nsh;
42 static struct GNUNET_SCHEDULER_Task *endbadly_task;
44 static struct GNUNET_CRYPTO_EcdsaPrivateKey privkey;
46 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
50 static struct GNUNET_GNSRECORD_Data rd_orig;
52 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
54 // static const char * name = "dummy.dummy.gnunet";
55 static const char *name = "d";
61 GNUNET_free_non_null ((void *) rd_orig.data);
64 GNUNET_NAMESTORE_disconnect (nsh);
67 GNUNET_SCHEDULER_shutdown ();
72 * Re-establish the connection to the service.
74 * @param cls handle to use to re-connect.
75 * @param tc scheduler context
82 GNUNET_NAMESTORE_cancel (nsqe);
100 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
102 unsigned int rd_count,
103 const struct GNUNET_GNSRECORD_Data *rd)
107 int found_record = GNUNET_NO;
108 int found_nick = GNUNET_NO;
110 if (0 != GNUNET_memcmp (&privkey, zone))
113 GNUNET_SCHEDULER_cancel (endbadly_task);
114 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
121 GNUNET_SCHEDULER_cancel (endbadly_task);
122 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
126 if (0 != strcmp (label, name))
129 GNUNET_SCHEDULER_cancel (endbadly_task);
130 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
137 GNUNET_SCHEDULER_cancel (endbadly_task);
138 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
142 for (c = 0; c < rd_count; c++)
144 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
146 if (rd[c].data_size != strlen (TEST_NICK) + 1)
149 GNUNET_SCHEDULER_cancel (endbadly_task);
150 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
153 if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_PRIVATE))
156 GNUNET_SCHEDULER_cancel (endbadly_task);
157 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
160 if (0 != strcmp (rd[c].data, TEST_NICK))
162 GNUNET_SCHEDULER_cancel (endbadly_task);
163 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
166 found_nick = GNUNET_YES;
170 if (rd[c].record_type != TEST_RECORD_TYPE)
173 GNUNET_SCHEDULER_cancel (endbadly_task);
174 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
177 if (rd[c].data_size != TEST_RECORD_DATALEN)
180 GNUNET_SCHEDULER_cancel (endbadly_task);
181 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
184 if (0 != memcmp (rd[c].data, rd_orig.data, TEST_RECORD_DATALEN))
187 GNUNET_SCHEDULER_cancel (endbadly_task);
188 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
191 if (rd[c].flags != rd->flags)
194 GNUNET_SCHEDULER_cancel (endbadly_task);
195 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
198 found_record = GNUNET_YES;
203 if ((GNUNET_YES == found_nick) && (GNUNET_YES == found_record))
205 GNUNET_SCHEDULER_cancel (endbadly_task);
206 endbadly_task = NULL;
207 GNUNET_SCHEDULER_add_now (&end, NULL);
212 GNUNET_SCHEDULER_cancel (endbadly_task);
213 endbadly_task = NULL;
214 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
227 put_cont (void *cls, int32_t success, const char *emsg)
229 const char *name = cls;
232 GNUNET_assert (NULL != cls);
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "Name store added record for `%s': %s\n",
236 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
238 if (GNUNET_OK != success)
240 GNUNET_SCHEDULER_cancel (endbadly_task);
241 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
245 nsqe = GNUNET_NAMESTORE_records_lookup (nsh,
256 nick_cont (void *cls, int32_t success, const char *emsg)
258 const char *name = cls;
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
264 rd_orig.expiration_time = GNUNET_TIME_absolute_get ().abs_value_us;
265 rd_orig.record_type = TEST_RECORD_TYPE;
266 rd_orig.data_size = TEST_RECORD_DATALEN;
267 rd_orig.data = GNUNET_malloc (TEST_RECORD_DATALEN);
269 memset ((char *) rd_orig.data, 'a', TEST_RECORD_DATALEN);
271 nsqe = GNUNET_NAMESTORE_records_store (nsh, &privkey,
275 &put_cont, (void *) name);
281 const struct GNUNET_CONFIGURATION_Handle *cfg,
282 struct GNUNET_TESTING_Peer *peer)
284 struct GNUNET_GNSRECORD_Data rd;
286 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
289 GNUNET_CRYPTO_ecdsa_key_create (&privkey);
290 GNUNET_CRYPTO_ecdsa_key_get_public (&privkey,
293 nsh = GNUNET_NAMESTORE_connect (cfg);
294 GNUNET_break (NULL != nsh);
296 memset (&rd, 0, sizeof(rd));
298 rd.data_size = strlen (TEST_NICK) + 1;
299 rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
300 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
301 rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
302 nsqe = GNUNET_NAMESTORE_records_store (nsh,
304 GNUNET_GNS_EMPTY_LABEL_AT,
312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
313 _ ("Namestore cannot store no block\n"));
318 #include "test_common.c"
322 main (int argc, char *argv[])
324 const char *plugin_name;
327 SETUP_CFG (plugin_name, cfg_name);
330 GNUNET_TESTING_peer_run ("test-namestore-api-lookup-nick",
337 GNUNET_DISK_purge_cfg_dir (cfg_name,
339 GNUNET_free (cfg_name);
344 /* end of test_namestore_api_store.c */