allow empty/NULL context message
[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 put_cont (void *cls, int32_t success, const char *emsg)
226 {
227   const char *name = cls;
228
229   nsqe = NULL;
230   GNUNET_assert (NULL != cls);
231   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232               "Name store added record for `%s': %s\n",
233               name,
234               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
235
236   if (GNUNET_OK != success)
237   {
238     GNUNET_SCHEDULER_cancel (endbadly_task);
239     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
240     return;
241   }
242   /* Lookup */
243   nsqe = GNUNET_NAMESTORE_records_lookup (nsh, privkey, name, lookup_it, NULL);
244 }
245
246 static void
247 nick_cont (void *cls, int32_t success, const char *emsg)
248 {
249   const char *name = cls;
250
251   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252               "Nick added : %s\n",
253               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
254
255   rd_orig.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
256   rd_orig.record_type = TEST_RECORD_TYPE;
257   rd_orig.data_size = TEST_RECORD_DATALEN;
258   rd_orig.data = GNUNET_malloc (TEST_RECORD_DATALEN);
259   rd_orig.flags = 0;
260   memset ((char *) rd_orig.data, 'a', TEST_RECORD_DATALEN);
261
262   nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
263                                       1, &rd_orig, &put_cont, (void *) name);
264 }
265
266
267 static void
268 run (void *cls,
269      const struct GNUNET_CONFIGURATION_Handle *cfg,
270      struct GNUNET_TESTING_Peer *peer)
271 {
272   char *hostkey_file;
273
274   directory = NULL;
275   GNUNET_assert (GNUNET_OK ==
276                  GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "GNUNET_TEST_HOME", &directory));
277   GNUNET_DISK_directory_remove (directory);
278
279   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
280                                                 &endbadly, NULL);
281   GNUNET_asprintf (&hostkey_file,
282                    "zonefiles%s%s",
283                    DIR_SEPARATOR_STR,
284                    "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
285   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
286   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
287   GNUNET_free (hostkey_file);
288   GNUNET_assert (privkey != NULL);
289   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
290
291   nsh = GNUNET_NAMESTORE_connect (cfg);
292   GNUNET_break (NULL != nsh);
293
294   nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey, TEST_NICK, &nick_cont, (void *) name);
295   if (NULL == nsqe)
296   {
297     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
298               _("Namestore cannot store no block\n"));
299   }
300 }
301
302
303 int
304 main (int argc, char *argv[])
305 {
306   res = 1;
307   if (0 !=
308       GNUNET_TESTING_peer_run ("test-namestore-api",
309                                "test_namestore_api.conf",
310                                &run,
311                                NULL))
312   {
313     res = 1;
314   }
315   if (NULL != directory)
316   {
317       GNUNET_DISK_directory_remove (directory);
318       GNUNET_free (directory);
319   }
320   return res;
321 }
322
323
324 /* end of test_namestore_api_store.c */