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