glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / namestore / test_namestore_api_lookup_public.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.c
17  * @brief testcase for namestore_api.c: store a record and perform a lookup
18  */
19 #include "platform.h"
20 #include "gnunet_namecache_service.h"
21 #include "gnunet_namestore_service.h"
22 #include "gnunet_testing_lib.h"
23 #include "gnunet_dnsparser_lib.h"
24
25 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
26
27 #define TEST_RECORD_DATALEN 123
28
29 #define TEST_RECORD_DATA 'a'
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
32
33
34 static struct GNUNET_NAMESTORE_Handle *nsh;
35
36 static struct GNUNET_NAMECACHE_Handle *nch;
37
38 static struct GNUNET_SCHEDULER_Task * endbadly_task;
39
40 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
41
42 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
43
44 static int res;
45
46 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
47
48 static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
49
50
51 static void
52 cleanup ()
53 {
54   if (NULL != nsh)
55   {
56     GNUNET_NAMESTORE_disconnect (nsh);
57     nsh = NULL;
58   }
59   if (NULL != nch)
60   {
61     GNUNET_NAMECACHE_disconnect (nch);
62     nch = NULL;
63   }
64   if (NULL != privkey)
65   {
66     GNUNET_free (privkey);
67     privkey = NULL;
68   }
69   GNUNET_SCHEDULER_shutdown ();
70 }
71
72
73 /**
74  * Re-establish the connection to the service.
75  *
76  * @param cls handle to use to re-connect.
77  */
78 static void
79 endbadly (void *cls)
80 {
81   if (NULL != nsqe)
82   {
83     GNUNET_NAMESTORE_cancel (nsqe);
84     nsqe = NULL;
85   }
86   if (NULL != ncqe)
87   {
88     GNUNET_NAMECACHE_cancel (ncqe);
89     ncqe = NULL;
90   }
91   cleanup ();
92   res = 1;
93 }
94
95
96 static void
97 end (void *cls)
98 {
99   cleanup ();
100   res = 0;
101 }
102
103
104 static void
105 rd_decrypt_cb (void *cls,
106                unsigned int rd_count,
107                const struct GNUNET_GNSRECORD_Data *rd)
108 {
109   char rd_cmp_data[TEST_RECORD_DATALEN];
110
111   GNUNET_assert (1 == rd_count);
112   GNUNET_assert (NULL != rd);
113
114   memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
115
116   GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
117   GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
118   GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
119
120   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121               "Block was decrypted successfully \n");
122
123         GNUNET_SCHEDULER_add_now (&end, NULL);
124 }
125
126
127 static void
128 name_lookup_proc (void *cls,
129                   const struct GNUNET_GNSRECORD_Block *block)
130 {
131   const char *name = cls;
132
133   ncqe = NULL;
134   GNUNET_assert (NULL != cls);
135
136   if (endbadly_task != NULL)
137   {
138     GNUNET_SCHEDULER_cancel (endbadly_task);
139     endbadly_task = NULL;
140   }
141
142   if (NULL == block)
143   {
144     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145               _("Namestore returned no block\n"));
146     if (endbadly_task != NULL)
147       GNUNET_SCHEDULER_cancel (endbadly_task);
148     endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
149     return;
150   }
151
152   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153               "Namestore returned block, decrypting \n");
154   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
155                 &pubkey, name, &rd_decrypt_cb, (void *) name));
156 }
157
158
159 static void
160 put_cont (void *cls, int32_t success, const char *emsg)
161 {
162   const char *name = cls;
163   struct GNUNET_HashCode derived_hash;
164   struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
165
166   nsqe = NULL;
167   GNUNET_assert (NULL != cls);
168   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169               "Name store added record for `%s': %s\n",
170               name,
171               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
172
173   /* Create derived hash */
174   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
175   GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &derived_hash);
176
177   ncqe = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
178                                         &name_lookup_proc, (void *) name);
179 }
180
181
182 static void
183 run (void *cls,
184      const struct GNUNET_CONFIGURATION_Handle *cfg,
185      struct GNUNET_TESTING_Peer *peer)
186 {
187   struct GNUNET_GNSRECORD_Data rd;
188   const char * name = "dummy.dummy.gnunet";
189
190   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
191                                                 &endbadly,
192                                                 NULL);
193   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
194   GNUNET_assert (privkey != NULL);
195   GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
196                                       &pubkey);
197
198   rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
199   rd.record_type = TEST_RECORD_TYPE;
200   rd.data_size = TEST_RECORD_DATALEN;
201   rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
202   rd.flags = 0;
203   memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
204
205   nsh = GNUNET_NAMESTORE_connect (cfg);
206   nch = GNUNET_NAMECACHE_connect (cfg);
207   GNUNET_break (NULL != nsh);
208   GNUNET_break (NULL != nch);
209   nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
210                                       1, &rd, &put_cont, (void *) name);
211   if (NULL == nsqe)
212   {
213     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
214               _("Namestore cannot store no block\n"));
215   }
216
217   GNUNET_free ((void *)rd.data);
218 }
219
220
221 int
222 main (int argc, char *argv[])
223 {
224   const char *plugin_name;
225   char *cfg_name;
226
227   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
228   GNUNET_asprintf (&cfg_name,
229                    "test_namestore_api_%s.conf",
230                    plugin_name);
231   GNUNET_DISK_purge_cfg_dir (cfg_name,
232                              "GNUNET_TEST_HOME");
233   res = 1;
234   if (0 !=
235       GNUNET_TESTING_peer_run ("test-namestore-api",
236                                cfg_name,
237                                &run,
238                                NULL))
239   {
240     res = 1;
241   }
242   GNUNET_DISK_purge_cfg_dir (cfg_name,
243                              "GNUNET_TEST_HOME");
244   GNUNET_free (cfg_name);
245   return res;
246 }
247
248
249 /* end of test_namestore_api_lookup_public.c */