-start tests with fresh/empty database
[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_namestore_service.h"
27 #include "gnunet_testing_lib.h"
28
29 #define TEST_RECORD_TYPE 1234
30
31 #define TEST_RECORD_DATALEN 123
32
33 #define TEST_RECORD_DATA 'a'
34
35
36 #define TEST_RECORD_TYPE2 4321
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 GNUNET_SCHEDULER_TaskIdentifier 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 const char * name = "dummy.dummy.gnunet";
60
61
62 static void
63 cleanup ()
64 {
65   if (NULL != nsh)
66   {
67     GNUNET_NAMESTORE_disconnect (nsh);
68     nsh = NULL;
69   }
70   if (NULL != privkey)
71   {
72     GNUNET_free (privkey);
73     privkey = NULL;
74   }
75   GNUNET_SCHEDULER_shutdown ();
76 }
77
78
79 /**
80  * Re-establish the connection to the service.
81  *
82  * @param cls handle to use to re-connect.
83  * @param tc scheduler context
84  */
85 static void
86 endbadly (void *cls,
87           const struct GNUNET_SCHEDULER_TaskContext *tc)
88 {
89   if (NULL != nsqe)
90   {
91     GNUNET_NAMESTORE_cancel (nsqe);
92     nsqe = NULL;
93   }
94   cleanup ();
95   res = 1;
96 }
97
98
99 static void
100 end (void *cls,
101      const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   cleanup ();
104   res = 0;
105 }
106
107
108 static void
109 put_cont (void *cls, int32_t success, const char *emsg);
110
111
112 static void
113 rd_decrypt_cb (void *cls,
114                unsigned int rd_count,
115                const struct GNUNET_NAMESTORE_RecordData *rd)
116 {
117   struct GNUNET_NAMESTORE_RecordData rd_new;
118
119   GNUNET_assert (1 == rd_count);
120   GNUNET_assert (NULL != rd);
121
122   if (GNUNET_NO == update_performed)
123   {
124     char rd_cmp_data[TEST_RECORD_DATALEN];
125     memset (rd_cmp_data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
126
127     GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
128     GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
129     GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
130
131     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132                 "Block was decrypted successfully, updating record \n");
133
134     rd_new.flags = GNUNET_NAMESTORE_RF_NONE;
135     rd_new.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
136     rd_new.record_type = TEST_RECORD_TYPE2;
137     rd_new.data_size = TEST_RECORD_DATALEN2;
138     rd_new.data = GNUNET_malloc (TEST_RECORD_DATALEN2);
139     memset ((char *) rd_new.data, TEST_RECORD_DATA2, TEST_RECORD_DATALEN2);
140
141     nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
142                                            1, &rd_new, &put_cont, (void *) name);
143     update_performed = GNUNET_YES;
144   }
145   else
146   {
147     char rd_cmp_data[TEST_RECORD_DATALEN2];
148     memset (rd_cmp_data, TEST_RECORD_DATA2, TEST_RECORD_DATALEN2);
149
150     GNUNET_assert (TEST_RECORD_TYPE2 == rd[0].record_type);
151     GNUNET_assert (TEST_RECORD_DATALEN2 == rd[0].data_size);
152     GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN2));
153
154     GNUNET_SCHEDULER_add_now (&end, NULL);
155   }
156 }
157
158
159 static void
160 name_lookup_proc (void *cls,
161                   const struct GNUNET_NAMESTORE_Block *block)
162 {
163   const char *name = cls;
164   nsqe = NULL;
165
166   GNUNET_assert (NULL != cls);
167
168   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
169   {
170     GNUNET_SCHEDULER_cancel (endbadly_task);
171     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
172   }
173
174   if (NULL == block)
175   {
176     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
177               _("Namestore returned no block\n"));
178     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
179       GNUNET_SCHEDULER_cancel (endbadly_task);
180     endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
181     return;
182   }
183
184   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185               "Namestore returned block, decrypting \n");
186   GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_block_decrypt(block,
187                 &pubkey, name, &rd_decrypt_cb, (void *) name));
188 }
189
190
191 static void
192 put_cont (void *cls, int32_t success, const char *emsg)
193 {
194   const char *name = cls;
195   struct GNUNET_HashCode derived_hash;
196
197   GNUNET_assert (NULL != cls);
198
199   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200               "Name store added record for `%s': %s\n",
201               name,
202               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
203
204   /* Create derived hash */
205   GNUNET_NAMESTORE_query_from_private_key (privkey, name, &derived_hash);
206
207   nsqe = GNUNET_NAMESTORE_lookup_block (nsh, &derived_hash,
208                                          &name_lookup_proc, (void *) name);
209 }
210
211
212 static void
213 run (void *cls,
214      const struct GNUNET_CONFIGURATION_Handle *cfg,
215      struct GNUNET_TESTING_Peer *peer)
216 {
217   struct GNUNET_NAMESTORE_RecordData rd;
218   char *hostkey_file;
219
220   update_performed = GNUNET_NO;
221   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
222                                                 &endbadly, NULL);
223   GNUNET_asprintf (&hostkey_file,
224                    "zonefiles%s%s",
225                    DIR_SEPARATOR_STR,
226                    "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
227   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
228   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
229   GNUNET_free (hostkey_file);
230   GNUNET_assert (privkey != NULL);
231   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
232
233   rd.flags = GNUNET_NAMESTORE_RF_NONE;
234   rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
235   rd.record_type = TEST_RECORD_TYPE;
236   rd.data_size = TEST_RECORD_DATALEN;
237   rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
238   memset ((char *) rd.data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
239
240   nsh = GNUNET_NAMESTORE_connect (cfg);
241   GNUNET_break (NULL != nsh);
242   nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
243                                       1, &rd, &put_cont, (void *) name);
244   if (NULL == nsqe)
245   {
246     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
247               _("Namestore cannot store no block\n"));
248   }
249
250   GNUNET_free ((void *)rd.data);
251 }
252
253
254 int
255 main (int argc, char *argv[])
256 {
257   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-namestore/");
258   res = 1;
259   if (0 !=
260       GNUNET_TESTING_service_run ("test-namestore-api-store-update",
261                                   "namestore",
262                                   "test_namestore_api.conf",
263                                   &run,
264                                   NULL))
265     return 1;
266   return res;
267 }
268
269
270 /* end of test_namestore_api_store_update.c*/