deprecate ill-defined set_nick API
[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_gns_service.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_dnsparser_lib.h"
29
30 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
31
32 #define TEST_RECORD_DATALEN 123
33
34 #define TEST_NICK "gnunettestnick"
35
36 #define TEST_RECORD_DATA 'a'
37
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
39
40 static struct GNUNET_NAMESTORE_Handle *nsh;
41
42 static struct GNUNET_SCHEDULER_Task *endbadly_task;
43
44 static struct GNUNET_CRYPTO_EcdsaPrivateKey privkey;
45
46 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
47
48 static int res;
49
50 static struct GNUNET_GNSRECORD_Data rd_orig;
51
52 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
53
54 // static const char * name = "dummy.dummy.gnunet";
55 static const char *name = "d";
56
57
58 static void
59 cleanup ()
60 {
61   GNUNET_free_non_null ((void *) rd_orig.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  */
77 static void
78 endbadly (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
90 static void
91 end (void *cls)
92 {
93   cleanup ();
94   res = 0;
95 }
96
97
98 static void
99 lookup_it (void *cls,
100            const struct GNUNET_CRYPTO_EcdsaPrivateKey *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
219 static void
220 fail_cb (void *cls)
221 {
222   GNUNET_assert (0);
223 }
224
225
226 static void
227 put_cont (void *cls, int32_t success, const char *emsg)
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               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
237
238   if (GNUNET_OK != success)
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
255 static void
256 nick_cont (void *cls, int32_t success, const char *emsg)
257 {
258   const char *name = cls;
259
260   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261               "Nick added : %s\n",
262               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
263
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);
268   rd_orig.flags = 0;
269   memset ((char *) rd_orig.data, 'a', TEST_RECORD_DATALEN);
270
271   nsqe = GNUNET_NAMESTORE_records_store (nsh, &privkey,
272                                          name,
273                                          1,
274                                          &rd_orig,
275                                          &put_cont, (void *) name);
276 }
277
278
279 static void
280 run (void *cls,
281      const struct GNUNET_CONFIGURATION_Handle *cfg,
282      struct GNUNET_TESTING_Peer *peer)
283 {
284   struct GNUNET_GNSRECORD_Data rd;
285
286   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
287                                                 &endbadly,
288                                                 NULL);
289   GNUNET_CRYPTO_ecdsa_key_create (&privkey);
290   GNUNET_CRYPTO_ecdsa_key_get_public (&privkey,
291                                       &pubkey);
292
293   nsh = GNUNET_NAMESTORE_connect (cfg);
294   GNUNET_break (NULL != nsh);
295
296   memset (&rd, 0, sizeof(rd));
297   rd.data = TEST_NICK;
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,
303                                          &privkey,
304                                          GNUNET_GNS_EMPTY_LABEL_AT,
305                                          1,
306                                          &rd,
307                                          &nick_cont,
308                                          (void *) name);
309
310   if (NULL == nsqe)
311   {
312     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
313                 _ ("Namestore cannot store no block\n"));
314   }
315 }
316
317
318 #include "test_common.c"
319
320
321 int
322 main (int argc, char *argv[])
323 {
324   const char *plugin_name;
325   char *cfg_name;
326
327   SETUP_CFG (plugin_name, cfg_name);
328   res = 1;
329   if (0 !=
330       GNUNET_TESTING_peer_run ("test-namestore-api-lookup-nick",
331                                cfg_name,
332                                &run,
333                                NULL))
334   {
335     res = 1;
336   }
337   GNUNET_DISK_purge_cfg_dir (cfg_name,
338                              "GNUNET_TEST_HOME");
339   GNUNET_free (cfg_name);
340   return res;
341 }
342
343
344 /* end of test_namestore_api_store.c */