added lookup test + fix in test
[oweals/gnunet.git] / src / namestore / test_namestore_api_store_update.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012, 2013 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_update.c
22  * @brief testcase for namestore_api.c: store a record, update it and perform a lookup
23  * @author Matthias Wachs
24  */
25 #include "platform.h"
26 #include "gnunet_namecache_service.h"
27 #include "gnunet_namestore_service.h"
28 #include "gnunet_testing_lib.h"
29
30 #define TEST_RECORD_TYPE 1234
31
32 #define TEST_RECORD_DATALEN 123
33
34 #define TEST_RECORD_DATA 'a'
35
36
37 #define TEST_RECORD_TYPE2 4321
38
39 #define TEST_RECORD_DATALEN2 234
40
41 #define TEST_RECORD_DATA2 'b'
42
43 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
44
45
46 static struct GNUNET_NAMESTORE_Handle *nsh;
47
48 static struct GNUNET_NAMECACHE_Handle *nch;
49
50 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
51
52 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
53
54 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
55
56 static int res;
57
58 static int update_performed;
59
60 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
61
62 static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
63
64 static const char *name = "dummy";
65
66
67 static void
68 cleanup ()
69 {
70   if (NULL != nsh)
71   {
72     GNUNET_NAMESTORE_disconnect (nsh);
73     nsh = NULL;
74   }
75   if (NULL != nch)
76   {
77     GNUNET_NAMECACHE_disconnect (nch);
78     nch = NULL;
79   }
80   if (NULL != privkey)
81   {
82     GNUNET_free (privkey);
83     privkey = NULL;
84   }
85   GNUNET_SCHEDULER_shutdown ();
86 }
87
88
89 /**
90  * Re-establish the connection to the service.
91  *
92  * @param cls handle to use to re-connect.
93  * @param tc scheduler context
94  */
95 static void
96 endbadly (void *cls,
97           const struct GNUNET_SCHEDULER_TaskContext *tc)
98 {
99   if (NULL != nsqe)
100   {
101     GNUNET_NAMESTORE_cancel (nsqe);
102     nsqe = NULL;
103   }
104   if (NULL != ncqe)
105   {
106     GNUNET_NAMECACHE_cancel (ncqe);
107     ncqe = NULL;
108   }
109   cleanup ();
110   res = 1;
111 }
112
113
114 static void
115 end (void *cls,
116      const struct GNUNET_SCHEDULER_TaskContext *tc)
117 {
118   cleanup ();
119   res = 0;
120 }
121
122
123 static void
124 put_cont (void *cls, int32_t success, const char *emsg);
125
126
127 static void
128 rd_decrypt_cb (void *cls,
129                unsigned int rd_count,
130                const struct GNUNET_GNSRECORD_Data *rd)
131 {
132   struct GNUNET_GNSRECORD_Data rd_new;
133
134   GNUNET_assert (1 == rd_count);
135   GNUNET_assert (NULL != rd);
136
137   if (GNUNET_NO == update_performed)
138   {
139     char rd_cmp_data[TEST_RECORD_DATALEN];
140     memset (rd_cmp_data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
141
142     GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
143     GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
144     GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
145
146     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147                 "Block was decrypted successfully, updating record \n");
148
149     rd_new.flags = GNUNET_GNSRECORD_RF_NONE;
150     rd_new.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
151     rd_new.record_type = TEST_RECORD_TYPE2;
152     rd_new.data_size = TEST_RECORD_DATALEN2;
153     rd_new.data = GNUNET_malloc (TEST_RECORD_DATALEN2);
154     memset ((char *) rd_new.data, TEST_RECORD_DATA2, TEST_RECORD_DATALEN2);
155
156     nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
157                                            1, &rd_new, &put_cont, (void *) name);
158     update_performed = GNUNET_YES;
159   }
160   else
161   {
162     char rd_cmp_data[TEST_RECORD_DATALEN2];
163     memset (rd_cmp_data, TEST_RECORD_DATA2, TEST_RECORD_DATALEN2);
164
165     GNUNET_assert (TEST_RECORD_TYPE2 == rd[0].record_type);
166     GNUNET_assert (TEST_RECORD_DATALEN2 == rd[0].data_size);
167     GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN2));
168
169     GNUNET_SCHEDULER_cancel (endbadly_task);
170     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
171     GNUNET_SCHEDULER_add_now (&end, NULL);
172   }
173 }
174
175
176 static void
177 name_lookup_proc (void *cls,
178                   const struct GNUNET_GNSRECORD_Block *block)
179 {
180   const char *name = cls;
181
182   ncqe = NULL;
183   GNUNET_assert (NULL != cls);
184   if (NULL == block)
185   {
186     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
187                 _("Namecache returned no block for `%s'\n"),
188                 name);
189     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
190       GNUNET_SCHEDULER_cancel (endbadly_task);
191     endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
192     return;
193   }
194
195   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196               "Namecache returned block, decrypting \n");
197   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
198                 &pubkey, name, &rd_decrypt_cb, (void *) name));
199 }
200
201
202 static void
203 put_cont (void *cls, int32_t success, const char *emsg)
204 {
205   const char *name = cls;
206   struct GNUNET_HashCode derived_hash;
207
208   nsqe = NULL;
209   GNUNET_assert (NULL != cls);
210   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211               "Name store added record for `%s': %s\n",
212               name,
213               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
214   /* Create derived hash */
215   GNUNET_GNSRECORD_query_from_private_key (privkey,
216                                            name,
217                                            &derived_hash);
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219               "Looking in namecache for `%s'\n",
220               GNUNET_h2s (&derived_hash));
221   ncqe = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
222                                         &name_lookup_proc, (void *) name);
223 }
224
225
226 static void
227 run (void *cls,
228      const struct GNUNET_CONFIGURATION_Handle *cfg,
229      struct GNUNET_TESTING_Peer *peer)
230 {
231   struct GNUNET_GNSRECORD_Data rd;
232   char *hostkey_file;
233
234   update_performed = GNUNET_NO;
235   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
236                                                 &endbadly, NULL);
237   GNUNET_asprintf (&hostkey_file,
238                    "zonefiles%s%s",
239                    DIR_SEPARATOR_STR,
240                    "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
241   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
242   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
243   GNUNET_free (hostkey_file);
244   GNUNET_assert (privkey != NULL);
245   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
246
247   rd.flags = GNUNET_GNSRECORD_RF_NONE;
248   rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
249   rd.record_type = TEST_RECORD_TYPE;
250   rd.data_size = TEST_RECORD_DATALEN;
251   rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
252   memset ((char *) rd.data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
253
254   nsh = GNUNET_NAMESTORE_connect (cfg);
255   GNUNET_break (NULL != nsh);
256   nch = GNUNET_NAMECACHE_connect (cfg);
257   GNUNET_break (NULL != nch);
258   nsqe = GNUNET_NAMESTORE_records_store (nsh,
259                                          privkey, name,
260                                          1, &rd,
261                                          &put_cont, (void *) name);
262   if (NULL == nsqe)
263   {
264     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
265               _("Namestore cannot store no block\n"));
266   }
267   GNUNET_free ((void *)rd.data);
268 }
269
270
271 int
272 main (int argc, char *argv[])
273 {
274   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-namestore/");
275   res = 1;
276   if (0 !=
277       GNUNET_TESTING_peer_run ("test-namestore-api-store-update",
278                                "test_namestore_api.conf",
279                                &run,
280                                NULL))
281     return 1;
282   return res;
283 }
284
285
286 /* end of test_namestore_api_store_update.c*/