glitch in the license text detected by hyazinthe, thank you!
[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 /**
16  * @file namestore/test_namestore_api_lookup_nick.c
17  * @brief testcase for namestore_api.c: NICK records
18  */
19 #include "platform.h"
20 #include "gnunet_namestore_service.h"
21 #include "gnunet_testing_lib.h"
22 #include "gnunet_dnsparser_lib.h"
23
24 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
25
26 #define TEST_RECORD_DATALEN 123
27
28 #define TEST_NICK "gnunettestnick"
29
30 #define TEST_RECORD_DATA 'a'
31
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33
34 static struct GNUNET_NAMESTORE_Handle *nsh;
35
36 static struct GNUNET_SCHEDULER_Task * endbadly_task;
37
38 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
39
40 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
41
42 static int res;
43
44 static struct GNUNET_GNSRECORD_Data rd_orig;
45
46 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
47
48 //static const char * name = "dummy.dummy.gnunet";
49 static const char * name = "d";
50
51
52 static void
53 cleanup ()
54 {
55   GNUNET_free_non_null ((void *)rd_orig.data);
56   if (NULL != nsh)
57   {
58     GNUNET_NAMESTORE_disconnect (nsh);
59     nsh = NULL;
60   }
61   if (NULL != privkey)
62   {
63     GNUNET_free (privkey);
64     privkey = 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 != memcmp(privkey, zone, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
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
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, name,
272                                       1, &rd_orig, &put_cont, (void *) name);
273 }
274
275
276 static void
277 run (void *cls,
278      const struct GNUNET_CONFIGURATION_Handle *cfg,
279      struct GNUNET_TESTING_Peer *peer)
280 {
281   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
282                                                 &endbadly,
283                                                 NULL);
284   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
285   GNUNET_assert (privkey != NULL);
286   GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
287                                       &pubkey);
288
289   nsh = GNUNET_NAMESTORE_connect (cfg);
290   GNUNET_break (NULL != nsh);
291
292   nsqe = GNUNET_NAMESTORE_set_nick (nsh,
293                                     privkey,
294                                     TEST_NICK,
295                                     &nick_cont,
296                                     (void *) name);
297   if (NULL == nsqe)
298   {
299     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
300               _("Namestore cannot store no block\n"));
301   }
302 }
303
304
305 int
306 main (int argc, char *argv[])
307 {
308   const char *plugin_name;
309   char *cfg_name;
310
311   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
312   GNUNET_asprintf (&cfg_name,
313                    "test_namestore_api_%s.conf",
314                    plugin_name);
315   GNUNET_DISK_purge_cfg_dir (cfg_name,
316                              "GNUNET_TEST_HOME");
317   res = 1;
318   if (0 !=
319       GNUNET_TESTING_peer_run ("test-namestore-api-lookup-nick",
320                                cfg_name,
321                                &run,
322                                NULL))
323   {
324     res = 1;
325   }
326   GNUNET_DISK_purge_cfg_dir (cfg_name,
327                              "GNUNET_TEST_HOME");
328   GNUNET_free (cfg_name);
329   return res;
330 }
331
332
333 /* end of test_namestore_api_store.c */