-never store NICKs anywhere but in '+', do not display nicks in gnunet-namestore
[oweals/gnunet.git] / src / namestore / test_namestore_api_lookup_nick.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier 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, const struct GNUNET_SCHEDULER_TaskContext *tc)
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, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 {
97   cleanup ();
98   res = 0;
99 }
100
101 void 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 = GNUNET_SCHEDULER_NO_TASK;
210     GNUNET_SCHEDULER_add_now (&end, NULL );
211   }
212   else
213   {
214     GNUNET_break (0);
215     GNUNET_SCHEDULER_cancel (endbadly_task);
216     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
217     GNUNET_SCHEDULER_add_now (&endbadly, NULL );
218   }
219 }
220
221
222 static void
223 put_cont (void *cls, int32_t success, const char *emsg)
224 {
225   const char *name = cls;
226
227   nsqe = NULL;
228   GNUNET_assert (NULL != cls);
229   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230               "Name store added record for `%s': %s\n",
231               name,
232               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
233
234   if (GNUNET_OK != success)
235   {
236     GNUNET_SCHEDULER_cancel (endbadly_task);
237     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
238     return;
239   }
240   /* Lookup */
241   nsqe = GNUNET_NAMESTORE_records_lookup (nsh, privkey, name, lookup_it, NULL);
242 }
243
244 static void
245 nick_cont (void *cls, int32_t success, const char *emsg)
246 {
247   const char *name = cls;
248
249   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250               "Nick added : %s\n",
251               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
252
253   rd_orig.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
254   rd_orig.record_type = TEST_RECORD_TYPE;
255   rd_orig.data_size = TEST_RECORD_DATALEN;
256   rd_orig.data = GNUNET_malloc (TEST_RECORD_DATALEN);
257   rd_orig.flags = 0;
258   memset ((char *) rd_orig.data, 'a', TEST_RECORD_DATALEN);
259
260   nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
261                                       1, &rd_orig, &put_cont, (void *) name);
262 }
263
264
265 static void
266 run (void *cls,
267      const struct GNUNET_CONFIGURATION_Handle *cfg,
268      struct GNUNET_TESTING_Peer *peer)
269 {
270   char *hostkey_file;
271
272   directory = NULL;
273   GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "GNUNET_TEST_HOME", &directory);
274   GNUNET_DISK_directory_remove (directory);
275
276   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
277                                                 &endbadly, NULL);
278   GNUNET_asprintf (&hostkey_file,
279                    "zonefiles%s%s",
280                    DIR_SEPARATOR_STR,
281                    "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
282   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
283   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
284   GNUNET_free (hostkey_file);
285   GNUNET_assert (privkey != NULL);
286   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
287
288   nsh = GNUNET_NAMESTORE_connect (cfg);
289   GNUNET_break (NULL != nsh);
290
291   nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey, TEST_NICK, &nick_cont, (void *) name);
292   if (NULL == nsqe)
293   {
294     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
295               _("Namestore cannot store no block\n"));
296   }
297 }
298
299
300 int
301 main (int argc, char *argv[])
302 {
303   res = 1;
304   if (0 !=
305       GNUNET_TESTING_peer_run ("test-namestore-api",
306                                "test_namestore_api.conf",
307                                &run,
308                                NULL))
309   {
310     res = 1;
311   }
312   if (NULL != directory)
313   {
314       GNUNET_DISK_directory_remove (directory);
315       GNUNET_free (directory);
316   }
317   return res;
318 }
319
320
321 /* end of test_namestore_api_store.c */