reduce loop counters to more practical levels
[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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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
28 #define TEST_RECORD_TYPE 1234
29
30 #define TEST_RECORD_DATALEN 123
31
32 #define TEST_NICK "gnunettestnick"
33
34 #define TEST_RECORD_DATA 'a'
35
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
37
38 static struct GNUNET_NAMESTORE_Handle *nsh;
39
40 static struct GNUNET_SCHEDULER_Task * endbadly_task;
41
42 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
43
44 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
45
46 static int res;
47
48 static struct GNUNET_GNSRECORD_Data rd_orig;
49
50 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
51
52 //static const char * name = "dummy.dummy.gnunet";
53 static const char * name = "d";
54
55
56 static void
57 cleanup ()
58 {
59   GNUNET_free_non_null ((void *)rd_orig.data);
60   if (NULL != nsh)
61   {
62     GNUNET_NAMESTORE_disconnect (nsh);
63     nsh = NULL;
64   }
65   if (NULL != privkey)
66   {
67     GNUNET_free (privkey);
68     privkey = NULL;
69   }
70   GNUNET_SCHEDULER_shutdown ();
71 }
72
73
74 /**
75  * Re-establish the connection to the service.
76  *
77  * @param cls handle to use to re-connect.
78  * @param tc scheduler context
79  */
80 static void
81 endbadly (void *cls)
82 {
83   if (NULL != nsqe)
84   {
85     GNUNET_NAMESTORE_cancel (nsqe);
86     nsqe = NULL;
87   }
88   cleanup ();
89   res = 1;
90 }
91
92
93 static void
94 end (void *cls)
95 {
96   cleanup ();
97   res = 0;
98 }
99
100
101 static void
102 lookup_it (void *cls,
103            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
104            const char *label,
105            unsigned int rd_count,
106            const struct GNUNET_GNSRECORD_Data *rd)
107 {
108   nsqe = NULL;
109   int c;
110   int found_record = GNUNET_NO;
111   int found_nick = GNUNET_NO;
112
113   if (0 != memcmp(privkey, zone, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
114   {
115     GNUNET_break(0);
116     GNUNET_SCHEDULER_cancel (endbadly_task);
117     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
118     return;
119   }
120
121   if (NULL == label)
122   {
123     GNUNET_break(0);
124     GNUNET_SCHEDULER_cancel (endbadly_task);
125     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
126     return;
127   }
128
129   if (0 != strcmp (label, name))
130   {
131     GNUNET_break(0);
132     GNUNET_SCHEDULER_cancel (endbadly_task);
133     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
134     return;
135   }
136
137   if (2 != rd_count)
138   {
139     GNUNET_break(0);
140     GNUNET_SCHEDULER_cancel (endbadly_task);
141     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
142     return;
143   }
144
145   for (c = 0; c < rd_count; c++)
146   {
147     if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
148     {
149       if (rd[c].data_size != strlen(TEST_NICK)+1)
150       {
151         GNUNET_break(0);
152         GNUNET_SCHEDULER_cancel (endbadly_task);
153         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
154         return;
155       }
156       if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_PRIVATE))
157       {
158         GNUNET_break(0);
159         GNUNET_SCHEDULER_cancel (endbadly_task);
160         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
161         return;
162       }
163       if (0 != strcmp(rd[c].data, TEST_NICK))
164       {
165         GNUNET_SCHEDULER_cancel (endbadly_task);
166         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
167         return;
168       }
169       found_nick = GNUNET_YES;
170     }
171     else
172     {
173       if (rd[c].record_type != TEST_RECORD_TYPE)
174       {
175         GNUNET_break(0);
176         GNUNET_SCHEDULER_cancel (endbadly_task);
177         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
178         return;
179       }
180       if (rd[c].data_size != TEST_RECORD_DATALEN)
181       {
182         GNUNET_break(0);
183         GNUNET_SCHEDULER_cancel (endbadly_task);
184         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
185         return;
186       }
187       if (0 != memcmp (rd[c].data, rd_orig.data, TEST_RECORD_DATALEN))
188       {
189         GNUNET_break(0);
190         GNUNET_SCHEDULER_cancel (endbadly_task);
191         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
192         return;
193       }
194       if (rd[c].flags != rd->flags)
195       {
196         GNUNET_break(0);
197         GNUNET_SCHEDULER_cancel (endbadly_task);
198         endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
199         return;
200       }
201       found_record = GNUNET_YES;
202     }
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 int
310 main (int argc, char *argv[])
311 {
312   const char *plugin_name;
313   char *cfg_name;
314
315   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
316   GNUNET_asprintf (&cfg_name,
317                    "test_namestore_api_%s.conf",
318                    plugin_name);
319   GNUNET_DISK_purge_cfg_dir (cfg_name,
320                              "GNUNET_TEST_HOME");
321   res = 1;
322   if (0 !=
323       GNUNET_TESTING_peer_run ("test-namestore-api-lookup-nick",
324                                cfg_name,
325                                &run,
326                                NULL))
327   {
328     res = 1;
329   }
330   GNUNET_DISK_purge_cfg_dir (cfg_name,
331                              "GNUNET_TEST_HOME");
332   GNUNET_free (cfg_name);
333   return res;
334 }
335
336
337 /* end of test_namestore_api_store.c */