note about performance issue, indentation fix
[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_store.c
22  * @brief testcase for namestore_api.c: store a record
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 static char *directory;
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 != memcmp(privkey, zone, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
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
207   /* Done */
208   if ((GNUNET_YES == found_nick) && (GNUNET_YES == found_record))
209   {
210     GNUNET_SCHEDULER_cancel (endbadly_task);
211     endbadly_task = NULL;
212     GNUNET_SCHEDULER_add_now (&end, NULL );
213   }
214   else
215   {
216     GNUNET_break (0);
217     GNUNET_SCHEDULER_cancel (endbadly_task);
218     endbadly_task = NULL;
219     GNUNET_SCHEDULER_add_now (&endbadly, NULL );
220   }
221 }
222
223
224 static void
225 fail_cb (void *cls)
226 {
227   GNUNET_assert (0);
228 }
229
230
231 static void
232 put_cont (void *cls, int32_t success, const char *emsg)
233 {
234   const char *name = cls;
235
236   nsqe = NULL;
237   GNUNET_assert (NULL != cls);
238   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239               "Name store added record for `%s': %s\n",
240               name,
241               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
242
243   if (GNUNET_OK != success)
244   {
245     GNUNET_SCHEDULER_cancel (endbadly_task);
246     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
247     return;
248   }
249   /* Lookup */
250   nsqe = GNUNET_NAMESTORE_records_lookup (nsh,
251                                           privkey,
252                                           name,
253                                           &fail_cb,
254                                           NULL,
255                                           &lookup_it,
256                                           NULL);
257 }
258
259
260 static void
261 nick_cont (void *cls, int32_t success, const char *emsg)
262 {
263   const char *name = cls;
264
265   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266               "Nick added : %s\n",
267               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
268
269   rd_orig.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
270   rd_orig.record_type = TEST_RECORD_TYPE;
271   rd_orig.data_size = TEST_RECORD_DATALEN;
272   rd_orig.data = GNUNET_malloc (TEST_RECORD_DATALEN);
273   rd_orig.flags = 0;
274   memset ((char *) rd_orig.data, 'a', TEST_RECORD_DATALEN);
275
276   nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
277                                       1, &rd_orig, &put_cont, (void *) name);
278 }
279
280
281 static void
282 run (void *cls,
283      const struct GNUNET_CONFIGURATION_Handle *cfg,
284      struct GNUNET_TESTING_Peer *peer)
285 {
286   char *hostkey_file;
287
288   directory = NULL;
289   GNUNET_assert (GNUNET_OK ==
290                  GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "GNUNET_TEST_HOME", &directory));
291   GNUNET_DISK_directory_remove (directory);
292
293   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
294                                                 &endbadly, NULL);
295   GNUNET_asprintf (&hostkey_file,
296                    "zonefiles%s%s",
297                    DIR_SEPARATOR_STR,
298                    "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
299   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
300   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
301   GNUNET_free (hostkey_file);
302   GNUNET_assert (privkey != NULL);
303   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
304
305   nsh = GNUNET_NAMESTORE_connect (cfg);
306   GNUNET_break (NULL != nsh);
307
308   nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey, TEST_NICK, &nick_cont, (void *) name);
309   if (NULL == nsqe)
310   {
311     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
312               _("Namestore cannot store no block\n"));
313   }
314 }
315
316
317 int
318 main (int argc, char *argv[])
319 {
320   res = 1;
321   if (0 !=
322       GNUNET_TESTING_peer_run ("test-namestore-api",
323                                "test_namestore_api.conf",
324                                &run,
325                                NULL))
326   {
327     res = 1;
328   }
329   if (NULL != directory)
330   {
331       GNUNET_DISK_directory_remove (directory);
332       GNUNET_free (directory);
333   }
334   return res;
335 }
336
337
338 /* end of test_namestore_api_store.c */