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