implement new functions in libgnunetsq, clean up sqlite namestore plugin, implement...
[oweals/gnunet.git] / src / namestore / test_namestore_api_store_update.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012, 2013, 2018 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_update.c
22  * @brief testcase for namestore_api.c: store a record, update it and perform a lookup
23  * @author Matthias Wachs
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_namecache_service.h"
28 #include "gnunet_namestore_service.h"
29 #include "gnunet_testing_lib.h"
30
31 #define TEST_RECORD_TYPE 1234
32
33 #define TEST_RECORD_DATALEN 123
34
35 #define TEST_RECORD_DATA 'a'
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 struct GNUNET_SCHEDULER_Task * 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 /**
68  * Terminate test with error.
69  *
70  * @param cls handle to use to re-connect.
71  */
72 static void
73 endbadly (void *cls)
74 {
75   GNUNET_break (0);
76   endbadly_task = NULL;
77   GNUNET_SCHEDULER_shutdown ();
78   res = 1;
79 }
80
81
82 static void
83 end (void *cls)
84 {
85   if (NULL != endbadly_task)
86   {
87     GNUNET_SCHEDULER_cancel (endbadly_task);
88     endbadly_task = NULL;
89   }
90   if (NULL != nsqe)
91   {
92     GNUNET_NAMESTORE_cancel (nsqe);
93     nsqe = NULL;
94   }
95   if (NULL != ncqe)
96   {
97     GNUNET_NAMECACHE_cancel (ncqe);
98     ncqe = NULL;
99   }
100   if (NULL != nsh)
101   {
102     GNUNET_NAMESTORE_disconnect (nsh);
103     nsh = NULL;
104   }
105   if (NULL != nch)
106   {
107     GNUNET_NAMECACHE_disconnect (nch);
108     nch = NULL;
109   }
110   if (NULL != privkey)
111   {
112     GNUNET_free (privkey);
113     privkey = NULL;
114   }
115 }
116
117
118 static void
119 put_cont (void *cls,
120           int32_t success,
121           const char *emsg);
122
123
124 static void
125 rd_decrypt_cb (void *cls,
126                unsigned int rd_count,
127                const struct GNUNET_GNSRECORD_Data *rd)
128 {
129   struct GNUNET_GNSRECORD_Data rd_new;
130
131   GNUNET_assert (1 == rd_count);
132   GNUNET_assert (NULL != rd);
133
134   if (GNUNET_NO == update_performed)
135   {
136     char rd_cmp_data[TEST_RECORD_DATALEN];
137
138     memset (rd_cmp_data,
139             TEST_RECORD_DATA,
140             TEST_RECORD_DATALEN);
141     GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
142     GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
143     GNUNET_assert (0 == memcmp (&rd_cmp_data,
144                                 rd[0].data,
145                                 TEST_RECORD_DATALEN));
146
147     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148                 "Block was decrypted successfully, updating record \n");
149
150     rd_new.flags = GNUNET_GNSRECORD_RF_NONE;
151     rd_new.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
152     rd_new.record_type = TEST_RECORD_TYPE2;
153     rd_new.data_size = TEST_RECORD_DATALEN2;
154     rd_new.data = GNUNET_malloc (TEST_RECORD_DATALEN2);
155     memset ((char *) rd_new.data,
156             TEST_RECORD_DATA2,
157             TEST_RECORD_DATALEN2);
158
159     nsqe = GNUNET_NAMESTORE_records_store (nsh,
160                                            privkey,
161                                            name,
162                                            1,
163                                            &rd_new,
164                                            &put_cont,
165                                            (void *) name);
166     update_performed = GNUNET_YES;
167   }
168   else
169   {
170     char rd_cmp_data[TEST_RECORD_DATALEN2];
171
172     memset (rd_cmp_data,
173             TEST_RECORD_DATA2,
174             TEST_RECORD_DATALEN2);
175     GNUNET_assert (TEST_RECORD_TYPE2 == rd[0].record_type);
176     GNUNET_assert (TEST_RECORD_DATALEN2 == rd[0].data_size);
177     GNUNET_assert (0 == memcmp (&rd_cmp_data,
178                                 rd[0].data,
179                                 TEST_RECORD_DATALEN2));
180     GNUNET_SCHEDULER_shutdown ();
181     res = 0;
182   }
183 }
184
185
186 static void
187 name_lookup_proc (void *cls,
188                   const struct GNUNET_GNSRECORD_Block *block)
189 {
190   const char *name = cls;
191
192   ncqe = NULL;
193   GNUNET_assert (NULL != cls);
194   if (NULL == block)
195   {
196     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197                 _("Namecache returned no block for `%s'\n"),
198                 name);
199     GNUNET_SCHEDULER_shutdown ();
200     return;
201   }
202
203   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204               "Namecache returned block, decrypting \n");
205   GNUNET_assert (GNUNET_OK ==
206                  GNUNET_GNSRECORD_block_decrypt (block,
207                                                  &pubkey,
208                                                  name,
209                                                  &rd_decrypt_cb,
210                                                  (void *) name));
211 }
212
213
214 static void
215 put_cont (void *cls,
216           int32_t success,
217           const char *emsg)
218 {
219   const char *name = cls;
220   struct GNUNET_HashCode derived_hash;
221
222   nsqe = NULL;
223   GNUNET_assert (NULL != cls);
224   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225               "Name store added record for `%s': %s\n",
226               name,
227               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
228   /* Create derived hash */
229   GNUNET_GNSRECORD_query_from_private_key (privkey,
230                                            name,
231                                            &derived_hash);
232   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233               "Looking in namecache for `%s'\n",
234               GNUNET_h2s (&derived_hash));
235   ncqe = GNUNET_NAMECACHE_lookup_block (nch,
236                                         &derived_hash,
237                                         &name_lookup_proc, (void *) name);
238 }
239
240
241 static void
242 run (void *cls,
243      const struct GNUNET_CONFIGURATION_Handle *cfg,
244      struct GNUNET_TESTING_Peer *peer)
245 {
246   struct GNUNET_GNSRECORD_Data rd;
247
248   update_performed = GNUNET_NO;
249   GNUNET_SCHEDULER_add_shutdown (&end,
250                                  NULL);
251   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
252                                                 &endbadly,
253                                                 NULL);
254   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
255   GNUNET_assert (privkey != NULL);
256   GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
257                                       &pubkey);
258   rd.flags = GNUNET_GNSRECORD_RF_NONE;
259   rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
260   rd.record_type = TEST_RECORD_TYPE;
261   rd.data_size = TEST_RECORD_DATALEN;
262   rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
263   memset ((char *) rd.data,
264           TEST_RECORD_DATA,
265           TEST_RECORD_DATALEN);
266
267   nsh = GNUNET_NAMESTORE_connect (cfg);
268   GNUNET_break (NULL != nsh);
269   nch = GNUNET_NAMECACHE_connect (cfg);
270   GNUNET_break (NULL != nch);
271   nsqe = GNUNET_NAMESTORE_records_store (nsh,
272                                          privkey,
273                                          name,
274                                          1,
275                                          &rd,
276                                          &put_cont,
277                                          (void *) name);
278   if (NULL == nsqe)
279   {
280     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
281               _("Namestore cannot store no block\n"));
282   }
283   GNUNET_free ((void *)rd.data);
284 }
285
286
287 int
288 main (int argc,
289       char *argv[])
290 {
291   const char *plugin_name;
292   char *cfg_name;
293
294   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
295   GNUNET_asprintf (&cfg_name,
296                    "test_namestore_api_%s.conf",
297                    plugin_name);
298   res = 1;
299   GNUNET_DISK_purge_cfg_dir (cfg_name,
300                              "GNUNET_TEST_HOME");
301   if (0 !=
302       GNUNET_TESTING_peer_run ("test-namestore-api-store-update",
303                                cfg_name,
304                                &run,
305                                NULL))
306   {
307     res = 1;
308   }
309   GNUNET_DISK_purge_cfg_dir (cfg_name,
310                              "GNUNET_TEST_HOME");
311   GNUNET_free (cfg_name);
312   return res;
313 }
314
315
316 /* end of test_namestore_api_store_update.c */