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