paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / namecache / test_namecache_api_cache_block.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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file namecache/test_namecache_api.c
20  * @brief testcase for namecache_api.c: store a record and perform a lookup
21  */
22 #include "platform.h"
23 #include "gnunet_namecache_service.h"
24 #include "gnunet_testing_lib.h"
25 #include "gnunet_dnsparser_lib.h"
26
27 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
28
29 #define TEST_RECORD_DATALEN 123
30
31 #define TEST_RECORD_DATA 'a'
32
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
34
35
36 static struct GNUNET_NAMECACHE_Handle *nsh;
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_NAMECACHE_QueueEntry *nsqe;
47
48
49 static void
50 cleanup ()
51 {
52   if (NULL != nsh)
53   {
54     GNUNET_NAMECACHE_disconnect (nsh);
55     nsh = NULL;
56   }
57   if (NULL != privkey)
58   {
59     GNUNET_free (privkey);
60     privkey = NULL;
61   }
62   GNUNET_SCHEDULER_shutdown ();
63 }
64
65
66 /**
67  * Re-establish the connection to the service.
68  *
69  * @param cls handle to use to re-connect.
70  */
71 static void
72 endbadly (void *cls)
73 {
74   if (NULL != nsqe)
75   {
76     GNUNET_NAMECACHE_cancel (nsqe);
77     nsqe = NULL;
78   }
79   cleanup ();
80   res = 1;
81 }
82
83
84 static void
85 end (void *cls)
86 {
87   cleanup ();
88   res = 0;
89 }
90
91
92 static void
93 rd_decrypt_cb (void *cls,
94                unsigned int rd_count,
95                const struct GNUNET_GNSRECORD_Data *rd)
96 {
97   char rd_cmp_data[TEST_RECORD_DATALEN];
98
99   GNUNET_assert (1 == rd_count);
100   GNUNET_assert (NULL != rd);
101
102   memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
103
104   GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
105   GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
106   GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
107
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109               "Block was decrypted successfully \n");
110
111   GNUNET_SCHEDULER_add_now (&end, NULL);
112 }
113
114
115 static void
116 name_lookup_proc (void *cls,
117                   const struct GNUNET_GNSRECORD_Block *block)
118 {
119   const char *name = cls;
120   nsqe = NULL;
121
122   GNUNET_assert (NULL != cls);
123
124   if (endbadly_task != NULL)
125   {
126     GNUNET_SCHEDULER_cancel (endbadly_task);
127     endbadly_task = NULL;
128   }
129
130   if (NULL == block)
131   {
132     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133               _("Namecache returned no block\n"));
134     if (NULL != endbadly_task)
135       GNUNET_SCHEDULER_cancel (endbadly_task);
136     endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
137     return;
138   }
139
140   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141               "Namecache returned block, decrypting \n");
142   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
143                 &pubkey, name, &rd_decrypt_cb, (void *) name));
144 }
145
146 static void
147 cache_cont (void *cls, int32_t success, const char *emsg)
148 {
149   const char *name = cls;
150   struct GNUNET_HashCode derived_hash;
151
152   GNUNET_assert (NULL != cls);
153
154   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155               "Name store cached record for `%s': %s\n",
156               name,
157               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
158
159   /* Create derived hash */
160   GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &derived_hash);
161
162   nsqe = GNUNET_NAMECACHE_lookup_block (nsh, &derived_hash,
163                                          &name_lookup_proc, (void *) name);
164 }
165
166
167 static void
168 run (void *cls,
169      const struct GNUNET_CONFIGURATION_Handle *cfg,
170      struct GNUNET_TESTING_Peer *peer)
171 {
172   struct GNUNET_GNSRECORD_Data rd;
173   struct GNUNET_GNSRECORD_Block *block;
174   char *hostkey_file;
175   const char * name = "dummy.dummy.gnunet";
176
177   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
178                                                 &endbadly, NULL);
179   GNUNET_asprintf (&hostkey_file,
180                    "zonefiles%s%s",
181                    DIR_SEPARATOR_STR,
182                    "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
183   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
184   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
185   GNUNET_free (hostkey_file);
186   GNUNET_assert (privkey != NULL);
187   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
188
189
190   rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 10000000000;
191   rd.record_type = TEST_RECORD_TYPE;
192   rd.data_size = TEST_RECORD_DATALEN;
193   rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
194   rd.flags = 0;
195   memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
196   block = GNUNET_GNSRECORD_block_create (privkey,
197                                          GNUNET_TIME_UNIT_FOREVER_ABS,
198                                          name, &rd, 1);
199   if (NULL == block)
200   {
201     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202                 "Namecache cannot cache no block!\n");
203     GNUNET_SCHEDULER_shutdown ();
204     GNUNET_free (block);
205     return;
206   }
207
208   nsh = GNUNET_NAMECACHE_connect (cfg);
209   if (NULL == nsh)
210   {
211     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212               _("Namecache cannot connect to namecache\n"));
213     GNUNET_SCHEDULER_shutdown ();
214     GNUNET_free (block);
215     return;
216   }
217   GNUNET_break (NULL != nsh);
218
219   nsqe = GNUNET_NAMECACHE_block_cache (nsh,
220                                        block,
221                                        &cache_cont, (void *) name);
222   if (NULL == nsqe)
223   {
224     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225               _("Namecache cannot cache no block\n"));
226   }
227   GNUNET_free (block);
228   GNUNET_free ((void *)rd.data);
229 }
230
231
232 int
233 main (int argc, char *argv[])
234 {
235   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-namecache/");
236   res = 1;
237   if (0 !=
238       GNUNET_TESTING_service_run ("test-namecache-api",
239                                   "namecache",
240                                   "test_namecache_api.conf",
241                                   &run,
242                                   NULL))
243     return 1;
244   return res;
245 }
246
247
248 /* end of test_namecache_api_cache_block.c */