145a3e885367a6f4104fa42bdb95dc03aa74f71f
[oweals/gnunet.git] / src / namestore / test_namestore_api_lookup_nick.c
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_testing_lib.h"
27 #include "gnunet_dnsparser_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
39 static struct GNUNET_NAMESTORE_Handle *nsh;
40
41 static struct GNUNET_SCHEDULER_Task *endbadly_task;
42
43 static struct GNUNET_CRYPTO_EcdsaPrivateKey privkey;
44
45 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
46
47 static int res;
48
49 static struct GNUNET_GNSRECORD_Data rd_orig;
50
51 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
52
53 // static const char * name = "dummy.dummy.gnunet";
54 static const char *name = "d";
55
56
57 static void
58 cleanup ()
59 {
60   GNUNET_free_non_null ((void *) rd_orig.data);
61   if (NULL != nsh)
62   {
63     GNUNET_NAMESTORE_disconnect (nsh);
64     nsh = NULL;
65   }
66   GNUNET_SCHEDULER_shutdown ();
67 }
68
69
70 /**
71  * Re-establish the connection to the service.
72  *
73  * @param cls handle to use to re-connect.
74  * @param tc scheduler context
75  */
76 static void
77 endbadly (void *cls)
78 {
79   if (NULL != nsqe)
80   {
81     GNUNET_NAMESTORE_cancel (nsqe);
82     nsqe = NULL;
83   }
84   cleanup ();
85   res = 1;
86 }
87
88
89 static void
90 end (void *cls)
91 {
92   cleanup ();
93   res = 0;
94 }
95
96
97 static void
98 lookup_it (void *cls,
99            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
100            const char *label,
101            unsigned int rd_count,
102            const struct GNUNET_GNSRECORD_Data *rd)
103 {
104   nsqe = NULL;
105   int c;
106   int found_record = GNUNET_NO;
107   int found_nick = GNUNET_NO;
108
109   if (0 != GNUNET_memcmp (&privkey, zone))
110   {
111     GNUNET_break (0);
112     GNUNET_SCHEDULER_cancel (endbadly_task);
113     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
114     return;
115   }
116
117   if (NULL == label)
118   {
119     GNUNET_break (0);
120     GNUNET_SCHEDULER_cancel (endbadly_task);
121     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
122     return;
123   }
124
125   if (0 != strcmp (label, name))
126   {
127     GNUNET_break (0);
128     GNUNET_SCHEDULER_cancel (endbadly_task);
129     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
130     return;
131   }
132
133   if (2 != rd_count)
134   {
135     GNUNET_break (0);
136     GNUNET_SCHEDULER_cancel (endbadly_task);
137     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
138     return;
139   }
140
141   for (c = 0; c < rd_count; c++)
142   {
143     if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
144     {
145       if (rd[c].data_size != strlen (TEST_NICK) + 1)
146       {
147         GNUNET_break (0);
148         GNUNET_SCHEDULER_cancel (endbadly_task);
149         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
150         return;
151       }
152       if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_PRIVATE))
153       {
154         GNUNET_break (0);
155         GNUNET_SCHEDULER_cancel (endbadly_task);
156         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
157         return;
158       }
159       if (0 != strcmp (rd[c].data, TEST_NICK))
160       {
161         GNUNET_SCHEDULER_cancel (endbadly_task);
162         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
163         return;
164       }
165       found_nick = GNUNET_YES;
166     }
167     else
168     {
169       if (rd[c].record_type != TEST_RECORD_TYPE)
170       {
171         GNUNET_break (0);
172         GNUNET_SCHEDULER_cancel (endbadly_task);
173         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
174         return;
175       }
176       if (rd[c].data_size != TEST_RECORD_DATALEN)
177       {
178         GNUNET_break (0);
179         GNUNET_SCHEDULER_cancel (endbadly_task);
180         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
181         return;
182       }
183       if (0 != memcmp (rd[c].data, rd_orig.data, TEST_RECORD_DATALEN))
184       {
185         GNUNET_break (0);
186         GNUNET_SCHEDULER_cancel (endbadly_task);
187         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
188         return;
189       }
190       if (rd[c].flags != rd->flags)
191       {
192         GNUNET_break (0);
193         GNUNET_SCHEDULER_cancel (endbadly_task);
194         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
195         return;
196       }
197       found_record = GNUNET_YES;
198     }
199   }
200
201   /* Done */
202   if ((GNUNET_YES == found_nick) && (GNUNET_YES == found_record))
203   {
204     GNUNET_SCHEDULER_cancel (endbadly_task);
205     endbadly_task = NULL;
206     GNUNET_SCHEDULER_add_now (&end, NULL);
207   }
208   else
209   {
210     GNUNET_break (0);
211     GNUNET_SCHEDULER_cancel (endbadly_task);
212     endbadly_task = NULL;
213     GNUNET_SCHEDULER_add_now (&endbadly, NULL);
214   }
215 }
216
217
218 static void
219 fail_cb (void *cls)
220 {
221   GNUNET_assert (0);
222 }
223
224
225 static void
226 put_cont (void *cls, int32_t success, const char *emsg)
227 {
228   const char *name = cls;
229
230   nsqe = NULL;
231   GNUNET_assert (NULL != cls);
232   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233               "Name store added record for `%s': %s\n",
234               name,
235               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
236
237   if (GNUNET_OK != success)
238   {
239     GNUNET_SCHEDULER_cancel (endbadly_task);
240     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
241     return;
242   }
243   /* Lookup */
244   nsqe = GNUNET_NAMESTORE_records_lookup (nsh,
245                                           &privkey,
246                                           name,
247                                           &fail_cb,
248                                           NULL,
249                                           &lookup_it,
250                                           NULL);
251 }
252
253
254 static void
255 nick_cont (void *cls, int32_t success, const char *emsg)
256 {
257   const char *name = cls;
258
259   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260               "Nick added : %s\n",
261               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
262
263   rd_orig.expiration_time = GNUNET_TIME_absolute_get ().abs_value_us;
264   rd_orig.record_type = TEST_RECORD_TYPE;
265   rd_orig.data_size = TEST_RECORD_DATALEN;
266   rd_orig.data = GNUNET_malloc (TEST_RECORD_DATALEN);
267   rd_orig.flags = 0;
268   memset ((char *) rd_orig.data, 'a', TEST_RECORD_DATALEN);
269
270   nsqe = GNUNET_NAMESTORE_records_store (nsh, &privkey,
271                                          name,
272                                          1,
273                                          &rd_orig,
274                                          &put_cont, (void *) name);
275 }
276
277
278 static void
279 run (void *cls,
280      const struct GNUNET_CONFIGURATION_Handle *cfg,
281      struct GNUNET_TESTING_Peer *peer)
282 {
283   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
284                                                 &endbadly,
285                                                 NULL);
286   GNUNET_CRYPTO_ecdsa_key_create (&privkey);
287   GNUNET_CRYPTO_ecdsa_key_get_public (&privkey,
288                                       &pubkey);
289
290   nsh = GNUNET_NAMESTORE_connect (cfg);
291   GNUNET_break (NULL != nsh);
292
293   nsqe = GNUNET_NAMESTORE_set_nick (nsh,
294                                     &privkey,
295                                     TEST_NICK,
296                                     &nick_cont,
297                                     (void *) name);
298   if (NULL == nsqe)
299   {
300     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
301                 _ ("Namestore cannot store no block\n"));
302   }
303 }
304
305
306 #include "test_common.c"
307
308
309 int
310 main (int argc, char *argv[])
311 {
312   const char *plugin_name;
313   char *cfg_name;
314
315   SETUP_CFG (plugin_name, cfg_name);
316   res = 1;
317   if (0 !=
318       GNUNET_TESTING_peer_run ("test-namestore-api-lookup-nick",
319                                cfg_name,
320                                &run,
321                                NULL))
322   {
323     res = 1;
324   }
325   GNUNET_DISK_purge_cfg_dir (cfg_name,
326                              "GNUNET_TEST_HOME");
327   GNUNET_free (cfg_name);
328   return res;
329 }
330
331
332 /* end of test_namestore_api_store.c */