fix #3869: outdated FSF address
[oweals/gnunet.git] / src / namestore / test_namestore_api_store_update.c
1 /*
2      This file is part of GNUnet.
3      Copyright (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., 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  */
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 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 static char *directory;
67
68 static void
69 cleanup ()
70 {
71   if (NULL != nsh)
72   {
73     GNUNET_NAMESTORE_disconnect (nsh);
74     nsh = NULL;
75   }
76   if (NULL != nch)
77   {
78     GNUNET_NAMECACHE_disconnect (nch);
79     nch = NULL;
80   }
81   if (NULL != privkey)
82   {
83     GNUNET_free (privkey);
84     privkey = NULL;
85   }
86   GNUNET_SCHEDULER_shutdown ();
87 }
88
89
90 /**
91  * Re-establish the connection to the service.
92  *
93  * @param cls handle to use to re-connect.
94  * @param tc scheduler context
95  */
96 static void
97 endbadly (void *cls,
98           const struct GNUNET_SCHEDULER_TaskContext *tc)
99 {
100   if (NULL != nsqe)
101   {
102     GNUNET_NAMESTORE_cancel (nsqe);
103     nsqe = NULL;
104   }
105   if (NULL != ncqe)
106   {
107     GNUNET_NAMECACHE_cancel (ncqe);
108     ncqe = NULL;
109   }
110   cleanup ();
111   res = 1;
112 }
113
114
115 static void
116 end (void *cls,
117      const struct GNUNET_SCHEDULER_TaskContext *tc)
118 {
119   cleanup ();
120   res = 0;
121 }
122
123
124 static void
125 put_cont (void *cls, int32_t success, const char *emsg);
126
127
128 static void
129 rd_decrypt_cb (void *cls,
130                unsigned int rd_count,
131                const struct GNUNET_GNSRECORD_Data *rd)
132 {
133   struct GNUNET_GNSRECORD_Data rd_new;
134
135   GNUNET_assert (1 == rd_count);
136   GNUNET_assert (NULL != rd);
137
138   if (GNUNET_NO == update_performed)
139   {
140     char rd_cmp_data[TEST_RECORD_DATALEN];
141     memset (rd_cmp_data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
142
143     GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
144     GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
145     GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, 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, TEST_RECORD_DATA2, TEST_RECORD_DATALEN2);
156
157     nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
158                                            1, &rd_new, &put_cont, (void *) name);
159     update_performed = GNUNET_YES;
160   }
161   else
162   {
163     char rd_cmp_data[TEST_RECORD_DATALEN2];
164     memset (rd_cmp_data, TEST_RECORD_DATA2, TEST_RECORD_DATALEN2);
165
166     GNUNET_assert (TEST_RECORD_TYPE2 == rd[0].record_type);
167     GNUNET_assert (TEST_RECORD_DATALEN2 == rd[0].data_size);
168     GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN2));
169
170     GNUNET_SCHEDULER_cancel (endbadly_task);
171     endbadly_task = NULL;
172     GNUNET_SCHEDULER_add_now (&end, NULL);
173   }
174 }
175
176
177 static void
178 name_lookup_proc (void *cls,
179                   const struct GNUNET_GNSRECORD_Block *block)
180 {
181   const char *name = cls;
182
183   ncqe = NULL;
184   GNUNET_assert (NULL != cls);
185   if (NULL == block)
186   {
187     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
188                 _("Namecache returned no block for `%s'\n"),
189                 name);
190     if (endbadly_task != NULL)
191       GNUNET_SCHEDULER_cancel (endbadly_task);
192     endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
193     return;
194   }
195
196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197               "Namecache returned block, decrypting \n");
198   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
199                 &pubkey, name, &rd_decrypt_cb, (void *) name));
200 }
201
202
203 static void
204 put_cont (void *cls, int32_t success, const char *emsg)
205 {
206   const char *name = cls;
207   struct GNUNET_HashCode derived_hash;
208
209   nsqe = NULL;
210   GNUNET_assert (NULL != cls);
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212               "Name store added record for `%s': %s\n",
213               name,
214               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
215   /* Create derived hash */
216   GNUNET_GNSRECORD_query_from_private_key (privkey,
217                                            name,
218                                            &derived_hash);
219   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220               "Looking in namecache for `%s'\n",
221               GNUNET_h2s (&derived_hash));
222   ncqe = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
223                                         &name_lookup_proc, (void *) name);
224 }
225
226
227 static void
228 run (void *cls,
229      const struct GNUNET_CONFIGURATION_Handle *cfg,
230      struct GNUNET_TESTING_Peer *peer)
231 {
232   struct GNUNET_GNSRECORD_Data rd;
233   char *hostkey_file;
234
235   directory = NULL;
236   GNUNET_assert (GNUNET_OK ==
237                  GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "GNUNET_TEST_HOME", &directory));
238   GNUNET_DISK_directory_remove (directory);
239
240   update_performed = GNUNET_NO;
241   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
242                                                 &endbadly, NULL);
243   GNUNET_asprintf (&hostkey_file,
244                    "zonefiles%s%s",
245                    DIR_SEPARATOR_STR,
246                    "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
247   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
248   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
249   GNUNET_free (hostkey_file);
250   GNUNET_assert (privkey != NULL);
251   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
252
253   rd.flags = GNUNET_GNSRECORD_RF_NONE;
254   rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
255   rd.record_type = TEST_RECORD_TYPE;
256   rd.data_size = TEST_RECORD_DATALEN;
257   rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
258   memset ((char *) rd.data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
259
260   nsh = GNUNET_NAMESTORE_connect (cfg);
261   GNUNET_break (NULL != nsh);
262   nch = GNUNET_NAMECACHE_connect (cfg);
263   GNUNET_break (NULL != nch);
264   nsqe = GNUNET_NAMESTORE_records_store (nsh,
265                                          privkey, name,
266                                          1, &rd,
267                                          &put_cont, (void *) name);
268   if (NULL == nsqe)
269   {
270     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
271               _("Namestore cannot store no block\n"));
272   }
273   GNUNET_free ((void *)rd.data);
274 }
275
276
277 int
278 main (int argc, char *argv[])
279 {
280   res = 1;
281   if (0 !=
282       GNUNET_TESTING_peer_run ("test-namestore-api-store-update",
283                                "test_namestore_api.conf",
284                                &run,
285                                NULL))
286   {
287     res = 1;
288   }
289   if (NULL != directory)
290   {
291       GNUNET_DISK_directory_remove (directory);
292       GNUNET_free (directory);
293   }
294   return res;
295 }
296
297
298 /* end of test_namestore_api_store_update.c*/