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