aboutsummaryrefslogtreecommitdiff
path: root/src/service/namestore/test_namestore_api_lookup_nick.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/namestore/test_namestore_api_lookup_nick.c')
-rw-r--r--src/service/namestore/test_namestore_api_lookup_nick.c347
1 files changed, 347 insertions, 0 deletions
diff --git a/src/service/namestore/test_namestore_api_lookup_nick.c b/src/service/namestore/test_namestore_api_lookup_nick.c
new file mode 100644
index 000000000..22734baab
--- /dev/null
+++ b/src/service/namestore/test_namestore_api_lookup_nick.c
@@ -0,0 +1,347 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file namestore/test_namestore_api_lookup_nick.c
22 * @brief testcase for namestore_api.c: NICK records
23 */
24#include "platform.h"
25#include "gnunet_namestore_service.h"
26#include "gnunet_gns_service.h"
27#include "gnunet_testing_lib.h"
28
29#define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
30
31#define TEST_RECORD_DATALEN 123
32
33#define TEST_NICK "gnunettestnick"
34
35#define TEST_RECORD_DATA 'a'
36
37#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
38
39static struct GNUNET_NAMESTORE_Handle *nsh;
40
41static struct GNUNET_SCHEDULER_Task *endbadly_task;
42
43static struct GNUNET_CRYPTO_PrivateKey privkey;
44
45static struct GNUNET_CRYPTO_PublicKey pubkey;
46
47static int res;
48
49static struct GNUNET_GNSRECORD_Data rd_orig;
50
51static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
52
53// static const char * name = "dummy.dummy.gnunet";
54static const char *name = "d";
55
56static char *record_data;
57
58static void
59cleanup ()
60{
61 GNUNET_free (record_data);
62 if (NULL != nsh)
63 {
64 GNUNET_NAMESTORE_disconnect (nsh);
65 nsh = NULL;
66 }
67 GNUNET_SCHEDULER_shutdown ();
68}
69
70
71/**
72 * Re-establish the connection to the service.
73 *
74 * @param cls handle to use to re-connect.
75 * @param tc scheduler context
76 */
77static void
78endbadly (void *cls)
79{
80 if (NULL != nsqe)
81 {
82 GNUNET_NAMESTORE_cancel (nsqe);
83 nsqe = NULL;
84 }
85 cleanup ();
86 res = 1;
87}
88
89
90static void
91end (void *cls)
92{
93 cleanup ();
94 res = 0;
95}
96
97
98static void
99lookup_it (void *cls,
100 const struct GNUNET_CRYPTO_PrivateKey *zone,
101 const char *label,
102 unsigned int rd_count,
103 const struct GNUNET_GNSRECORD_Data *rd)
104{
105 nsqe = NULL;
106 int c;
107 int found_record = GNUNET_NO;
108 int found_nick = GNUNET_NO;
109
110 if (0 != GNUNET_memcmp (&privkey, zone))
111 {
112 GNUNET_break (0);
113 GNUNET_SCHEDULER_cancel (endbadly_task);
114 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
115 return;
116 }
117
118 if (NULL == label)
119 {
120 GNUNET_break (0);
121 GNUNET_SCHEDULER_cancel (endbadly_task);
122 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
123 return;
124 }
125
126 if (0 != strcmp (label, name))
127 {
128 GNUNET_break (0);
129 GNUNET_SCHEDULER_cancel (endbadly_task);
130 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
131 return;
132 }
133
134 if (2 != rd_count)
135 {
136 GNUNET_break (0);
137 GNUNET_SCHEDULER_cancel (endbadly_task);
138 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
139 return;
140 }
141
142 for (c = 0; c < rd_count; c++)
143 {
144 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
145 {
146 if (rd[c].data_size != strlen (TEST_NICK) + 1)
147 {
148 GNUNET_break (0);
149 GNUNET_SCHEDULER_cancel (endbadly_task);
150 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
151 return;
152 }
153 if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_PRIVATE))
154 {
155 GNUNET_break (0);
156 GNUNET_SCHEDULER_cancel (endbadly_task);
157 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
158 return;
159 }
160 if (0 != strcmp (rd[c].data, TEST_NICK))
161 {
162 GNUNET_SCHEDULER_cancel (endbadly_task);
163 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
164 return;
165 }
166 found_nick = GNUNET_YES;
167 }
168 else
169 {
170 if (rd[c].record_type != TEST_RECORD_TYPE)
171 {
172 GNUNET_break (0);
173 GNUNET_SCHEDULER_cancel (endbadly_task);
174 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
175 return;
176 }
177 if (rd[c].data_size != TEST_RECORD_DATALEN)
178 {
179 GNUNET_break (0);
180 GNUNET_SCHEDULER_cancel (endbadly_task);
181 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
182 return;
183 }
184 if (0 != memcmp (rd[c].data, rd_orig.data, TEST_RECORD_DATALEN))
185 {
186 GNUNET_break (0);
187 GNUNET_SCHEDULER_cancel (endbadly_task);
188 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
189 return;
190 }
191 if (rd[c].flags != rd->flags)
192 {
193 GNUNET_break (0);
194 GNUNET_SCHEDULER_cancel (endbadly_task);
195 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
196 return;
197 }
198 found_record = GNUNET_YES;
199 }
200 }
201
202 /* Done */
203 if ((GNUNET_YES == found_nick) && (GNUNET_YES == found_record))
204 {
205 GNUNET_SCHEDULER_cancel (endbadly_task);
206 endbadly_task = NULL;
207 GNUNET_SCHEDULER_add_now (&end, NULL);
208 }
209 else
210 {
211 GNUNET_break (0);
212 GNUNET_SCHEDULER_cancel (endbadly_task);
213 endbadly_task = NULL;
214 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
215 }
216}
217
218
219static void
220fail_cb (void *cls)
221{
222 GNUNET_assert (0);
223}
224
225
226static void
227put_cont (void *cls, enum GNUNET_ErrorCode ec)
228{
229 const char *name = cls;
230
231 nsqe = NULL;
232 GNUNET_assert (NULL != cls);
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "Name store added record for `%s': %s\n",
235 name,
236 (ec == GNUNET_EC_NONE) ? "SUCCESS" : "FAIL");
237
238 if (GNUNET_EC_NONE != ec)
239 {
240 GNUNET_SCHEDULER_cancel (endbadly_task);
241 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
242 return;
243 }
244 /* Lookup */
245 nsqe = GNUNET_NAMESTORE_records_lookup (nsh,
246 &privkey,
247 name,
248 &fail_cb,
249 NULL,
250 &lookup_it,
251 NULL);
252}
253
254
255static void
256nick_cont (void *cls, enum GNUNET_ErrorCode ec)
257{
258 const char *name = cls;
259
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Nick added : %s\n",
262 (ec == GNUNET_EC_NONE) ? "SUCCESS" : "FAIL");
263
264 rd_orig.expiration_time = GNUNET_TIME_UNIT_HOURS.rel_value_us;
265 rd_orig.record_type = TEST_RECORD_TYPE;
266 rd_orig.data_size = TEST_RECORD_DATALEN;
267 record_data = GNUNET_malloc (TEST_RECORD_DATALEN);
268 rd_orig.data = record_data;
269 rd_orig.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
270 memset ((char *) rd_orig.data, 'a', TEST_RECORD_DATALEN);
271
272 nsqe = GNUNET_NAMESTORE_records_store (nsh, &privkey,
273 name,
274 1,
275 &rd_orig,
276 &put_cont, (void *) name);
277}
278
279
280static void
281run (void *cls,
282 const struct GNUNET_CONFIGURATION_Handle *cfg,
283 struct GNUNET_TESTING_Peer *peer)
284{
285 struct GNUNET_GNSRECORD_Data rd;
286
287 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
288 &endbadly,
289 NULL);
290 privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
291 GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key);
292 GNUNET_CRYPTO_key_get_public (&privkey,
293 &pubkey);
294
295 nsh = GNUNET_NAMESTORE_connect (cfg);
296 GNUNET_break (NULL != nsh);
297
298 memset (&rd, 0, sizeof(rd));
299 rd.data = TEST_NICK;
300 rd.data_size = strlen (TEST_NICK) + 1;
301 rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
302 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
303 rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
304 nsqe = GNUNET_NAMESTORE_records_store (nsh,
305 &privkey,
306 GNUNET_GNS_EMPTY_LABEL_AT,
307 1,
308 &rd,
309 &nick_cont,
310 (void *) name);
311
312 if (NULL == nsqe)
313 {
314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
315 _ ("Namestore cannot store no block\n"));
316 }
317}
318
319
320#include "test_common.c"
321
322
323int
324main (int argc, char *argv[])
325{
326 char *plugin_name;
327 char *cfg_name;
328
329 SETUP_CFG (plugin_name, cfg_name);
330 res = 1;
331 if (0 !=
332 GNUNET_TESTING_peer_run ("test-namestore-api-lookup-nick",
333 cfg_name,
334 &run,
335 NULL))
336 {
337 res = 1;
338 }
339 GNUNET_DISK_purge_cfg_dir (cfg_name,
340 "GNUNET_TEST_HOME");
341 GNUNET_free (plugin_name);
342 GNUNET_free (cfg_name);
343 return res;
344}
345
346
347/* end of test_namestore_api_store.c */