use real GNS record types in tests to avoid failures in new extra serialization/deser...
[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 #include "gnunet_dnsparser_lib.h"
31
32 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
33
34 #define TEST_RECORD_DATALEN 123
35
36 #define TEST_RECORD_DATA 'a'
37
38 #define TEST_RECORD_DATALEN2 234
39
40 #define TEST_RECORD_DATA2 'b'
41
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
43
44
45 static struct GNUNET_NAMESTORE_Handle *nsh;
46
47 static struct GNUNET_NAMECACHE_Handle *nch;
48
49 static struct GNUNET_SCHEDULER_Task * endbadly_task;
50
51 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
52
53 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
54
55 static int res;
56
57 static int update_performed;
58
59 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
60
61 static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
62
63 static const char *name = "dummy";
64
65
66 /**
67  * Terminate test with error.
68  *
69  * @param cls handle to use to re-connect.
70  */
71 static void
72 endbadly (void *cls)
73 {
74   GNUNET_break (0);
75   endbadly_task = NULL;
76   GNUNET_SCHEDULER_shutdown ();
77   res = 1;
78 }
79
80
81 static void
82 end (void *cls)
83 {
84   if (NULL != endbadly_task)
85   {
86     GNUNET_SCHEDULER_cancel (endbadly_task);
87     endbadly_task = NULL;
88   }
89   if (NULL != nsqe)
90   {
91     GNUNET_NAMESTORE_cancel (nsqe);
92     nsqe = NULL;
93   }
94   if (NULL != ncqe)
95   {
96     GNUNET_NAMECACHE_cancel (ncqe);
97     ncqe = NULL;
98   }
99   if (NULL != nsh)
100   {
101     GNUNET_NAMESTORE_disconnect (nsh);
102     nsh = NULL;
103   }
104   if (NULL != nch)
105   {
106     GNUNET_NAMECACHE_disconnect (nch);
107     nch = NULL;
108   }
109   if (NULL != privkey)
110   {
111     GNUNET_free (privkey);
112     privkey = NULL;
113   }
114 }
115
116
117 static void
118 put_cont (void *cls,
119           int32_t success,
120           const char *emsg);
121
122
123 static void
124 rd_decrypt_cb (void *cls,
125                unsigned int rd_count,
126                const struct GNUNET_GNSRECORD_Data *rd)
127 {
128   struct GNUNET_GNSRECORD_Data rd_new;
129
130   GNUNET_assert (1 == rd_count);
131   GNUNET_assert (NULL != rd);
132
133   if (GNUNET_NO == update_performed)
134   {
135     char rd_cmp_data[TEST_RECORD_DATALEN];
136
137     memset (rd_cmp_data,
138             TEST_RECORD_DATA,
139             TEST_RECORD_DATALEN);
140     GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
141     GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
142     GNUNET_assert (0 == memcmp (&rd_cmp_data,
143                                 rd[0].data,
144                                 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_TYPE;
152     rd_new.data_size = TEST_RECORD_DATALEN2;
153     rd_new.data = GNUNET_malloc (TEST_RECORD_DATALEN2);
154     memset ((char *) rd_new.data,
155             TEST_RECORD_DATA2,
156             TEST_RECORD_DATALEN2);
157
158     nsqe = GNUNET_NAMESTORE_records_store (nsh,
159                                            privkey,
160                                            name,
161                                            1,
162                                            &rd_new,
163                                            &put_cont,
164                                            (void *) name);
165     update_performed = GNUNET_YES;
166   }
167   else
168   {
169     char rd_cmp_data[TEST_RECORD_DATALEN2];
170
171     memset (rd_cmp_data,
172             TEST_RECORD_DATA2,
173             TEST_RECORD_DATALEN2);
174     GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
175     GNUNET_assert (TEST_RECORD_DATALEN2 == rd[0].data_size);
176     GNUNET_assert (0 == memcmp (&rd_cmp_data,
177                                 rd[0].data,
178                                 TEST_RECORD_DATALEN2));
179     GNUNET_SCHEDULER_shutdown ();
180     res = 0;
181   }
182 }
183
184
185 static void
186 name_lookup_proc (void *cls,
187                   const struct GNUNET_GNSRECORD_Block *block)
188 {
189   const char *name = cls;
190
191   ncqe = NULL;
192   GNUNET_assert (NULL != cls);
193   if (NULL == block)
194   {
195     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
196                 _("Namecache returned no block for `%s'\n"),
197                 name);
198     GNUNET_SCHEDULER_shutdown ();
199     return;
200   }
201
202   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203               "Namecache returned block, decrypting \n");
204   GNUNET_assert (GNUNET_OK ==
205                  GNUNET_GNSRECORD_block_decrypt (block,
206                                                  &pubkey,
207                                                  name,
208                                                  &rd_decrypt_cb,
209                                                  (void *) name));
210 }
211
212
213 static void
214 put_cont (void *cls,
215           int32_t success,
216           const char *emsg)
217 {
218   const char *name = cls;
219   struct GNUNET_HashCode derived_hash;
220
221   nsqe = NULL;
222   GNUNET_assert (NULL != cls);
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224               "Name store added record for `%s': %s\n",
225               name,
226               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
227   /* Create derived hash */
228   GNUNET_GNSRECORD_query_from_private_key (privkey,
229                                            name,
230                                            &derived_hash);
231   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232               "Looking in namecache for `%s'\n",
233               GNUNET_h2s (&derived_hash));
234   ncqe = GNUNET_NAMECACHE_lookup_block (nch,
235                                         &derived_hash,
236                                         &name_lookup_proc, (void *) name);
237 }
238
239
240 static void
241 run (void *cls,
242      const struct GNUNET_CONFIGURATION_Handle *cfg,
243      struct GNUNET_TESTING_Peer *peer)
244 {
245   struct GNUNET_GNSRECORD_Data rd;
246
247   update_performed = GNUNET_NO;
248   GNUNET_SCHEDULER_add_shutdown (&end,
249                                  NULL);
250   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
251                                                 &endbadly,
252                                                 NULL);
253   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
254   GNUNET_assert (privkey != NULL);
255   GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
256                                       &pubkey);
257   rd.flags = GNUNET_GNSRECORD_RF_NONE;
258   rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
259   rd.record_type = TEST_RECORD_TYPE;
260   rd.data_size = TEST_RECORD_DATALEN;
261   rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
262   memset ((char *) rd.data,
263           TEST_RECORD_DATA,
264           TEST_RECORD_DATALEN);
265
266   nsh = GNUNET_NAMESTORE_connect (cfg);
267   GNUNET_break (NULL != nsh);
268   nch = GNUNET_NAMECACHE_connect (cfg);
269   GNUNET_break (NULL != nch);
270   nsqe = GNUNET_NAMESTORE_records_store (nsh,
271                                          privkey,
272                                          name,
273                                          1,
274                                          &rd,
275                                          &put_cont,
276                                          (void *) name);
277   if (NULL == nsqe)
278   {
279     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
280               _("Namestore cannot store no block\n"));
281   }
282   GNUNET_free ((void *)rd.data);
283 }
284
285
286 int
287 main (int argc,
288       char *argv[])
289 {
290   const char *plugin_name;
291   char *cfg_name;
292
293   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
294   GNUNET_asprintf (&cfg_name,
295                    "test_namestore_api_%s.conf",
296                    plugin_name);
297   res = 1;
298   GNUNET_DISK_purge_cfg_dir (cfg_name,
299                              "GNUNET_TEST_HOME");
300   if (0 !=
301       GNUNET_TESTING_peer_run ("test-namestore-api-store-update",
302                                cfg_name,
303                                &run,
304                                NULL))
305   {
306     res = 1;
307   }
308   GNUNET_DISK_purge_cfg_dir (cfg_name,
309                              "GNUNET_TEST_HOME");
310   GNUNET_free (cfg_name);
311   return res;
312 }
313
314
315 /* end of test_namestore_api_store_update.c */