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