0b027b7e11803541ec7f2512900a6b55458092e3
[oweals/gnunet.git] / src / namestore / test_namestore_api_remove.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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.c
22  * @brief testcase for namestore_api.c to: remove record
23  */
24 #include "platform.h"
25 #include "gnunet_common.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 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
36
37
38 static struct GNUNET_NAMESTORE_Handle *nsh;
39
40 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
41
42 static struct GNUNET_CRYPTO_EccPrivateKey *privkey;
43
44 static struct GNUNET_CRYPTO_EccPublicSignKey pubkey;
45
46 static struct GNUNET_HashCode derived_hash;
47
48 static int res;
49
50 static int removed;
51
52 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
53
54
55 static void
56 cleanup ()
57 {
58   if (NULL != nsh)
59   {
60     GNUNET_NAMESTORE_disconnect (nsh);
61     nsh = NULL;
62   }
63   if (NULL != privkey)
64   {
65     GNUNET_free (privkey);
66     privkey = NULL;
67   }
68   GNUNET_SCHEDULER_shutdown ();
69 }
70
71
72 /**
73  * Re-establish the connection to the service.
74  *
75  * @param cls handle to use to re-connect.
76  * @param tc scheduler context
77  */
78 static void
79 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 {
81   if (NULL != nsqe)
82   {
83     GNUNET_NAMESTORE_cancel (nsqe);
84     nsqe = NULL;
85   }
86   cleanup ();
87   res = 1;
88 }
89
90
91 static void
92 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 {
94   cleanup ();
95   res = 0;
96 }
97
98 static void
99 name_lookup_proc (void *cls, const struct GNUNET_NAMESTORE_Block *block);
100
101 static void
102 remove_cont (void *cls, int32_t success, const char *emsg)
103 {
104   const char *name = cls;
105   if (GNUNET_YES != success)
106   {
107     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
108               _("Records could not be removed: `%s'\n"), emsg);
109     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
110       GNUNET_SCHEDULER_cancel (endbadly_task);
111     endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
112     return;
113   }
114
115   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
116               "Records were removed, perform lookup\n");
117
118   removed = GNUNET_YES;
119   nsqe = GNUNET_NAMESTORE_lookup_block (nsh, &derived_hash,
120                                          &name_lookup_proc, (void *) name);
121   if (NULL == nsqe)
122   {
123         GNUNET_break (0);
124     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
125               _("Namestore cannot perform lookup for removed record\n"));
126     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
127       GNUNET_SCHEDULER_cancel (endbadly_task);
128     endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
129     return;
130   }
131 }
132
133
134 static void
135 rd_decrypt_cb (void *cls,
136                                                  unsigned int rd_count,
137                                                  const struct GNUNET_NAMESTORE_RecordData *rd)
138 {
139   const char *name = cls;
140   char rd_cmp_data[TEST_RECORD_DATALEN];
141   if (GNUNET_NO == removed)
142   {
143         GNUNET_assert (1 == rd_count);
144         GNUNET_assert (NULL != rd);
145
146                 memset (rd_cmp_data, 'a', 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_INFO,
153                                         "Block was decrypted successfully, removing records \n");
154
155                 nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
156                                                                 0, NULL, &remove_cont, (void *) name);
157   }
158   else
159   {
160         if ((0 != rd_count) /*|| (NULL != rd)*/)
161         {
162                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163                                         _("Record was not removed \n"));
164       if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
165         GNUNET_SCHEDULER_cancel (endbadly_task);
166       endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
167       return;
168         }
169
170                 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
171                                 _("Record was removed \n"));
172         GNUNET_SCHEDULER_add_now (&end, NULL);
173   }
174 }
175
176 static void
177 name_lookup_proc (void *cls,
178                                                                         const struct GNUNET_NAMESTORE_Block *block)
179 {
180   const char *name = cls;
181   nsqe = NULL;
182
183   GNUNET_assert (NULL != cls);
184
185   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
186   {
187     GNUNET_SCHEDULER_cancel (endbadly_task);
188     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
189   }
190
191   if (NULL == block)
192   {
193     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
194               _("Namestore returned no block\n"));
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_INFO,
202               "Namestore returned block, decrypting \n");
203         GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_block_decrypt(block,
204                 &pubkey, name, &rd_decrypt_cb, (void *) name));
205 }
206
207 static void
208 put_cont (void *cls, int32_t success, const char *emsg)
209 {
210   const char *name = cls;
211
212   GNUNET_assert (NULL != cls);
213   if (GNUNET_SYSERR == success)
214   {
215         GNUNET_break (0);
216     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
217               _("Namestore could not store record: `%s'\n"), emsg);
218     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
219       GNUNET_SCHEDULER_cancel (endbadly_task);
220     endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
221     return;
222   }
223
224   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225               "Name store added record for `%s': %s\n",
226               name,
227               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
228
229   /* Create derived hash */
230   GNUNET_NAMESTORE_query_from_private_key (privkey, name, &derived_hash);
231
232   nsqe = GNUNET_NAMESTORE_lookup_block (nsh, &derived_hash,
233                                          &name_lookup_proc, (void *) name);
234   if (NULL == nsqe)
235   {
236         GNUNET_break (0);
237     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238               _("Namestore cannot perform lookup\n"));
239     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
240       GNUNET_SCHEDULER_cancel (endbadly_task);
241     endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
242     return;
243   }
244 }
245
246
247 static void
248 run (void *cls, 
249      const struct GNUNET_CONFIGURATION_Handle *cfg,
250      struct GNUNET_TESTING_Peer *peer)
251 {
252   struct GNUNET_NAMESTORE_RecordData rd;
253   char *hostkey_file;
254   const char * name = "dummy.dummy.gnunet";
255
256   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
257                                                 &endbadly, NULL);
258   GNUNET_asprintf (&hostkey_file,
259                    "zonefiles%s%s",
260                    DIR_SEPARATOR_STR,
261                    "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
262   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
263   privkey = GNUNET_CRYPTO_ecc_key_create_from_file (hostkey_file);
264   GNUNET_free (hostkey_file);
265   GNUNET_assert (privkey != NULL);
266   GNUNET_CRYPTO_ecc_key_get_public_for_signature (privkey, &pubkey);
267
268   removed = GNUNET_NO;
269
270   rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
271   rd.record_type = TEST_RECORD_TYPE;
272   rd.data_size = TEST_RECORD_DATALEN;
273   rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
274   memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
275
276   nsh = GNUNET_NAMESTORE_connect (cfg);
277   GNUNET_break (NULL != nsh);
278   nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
279                                       1, &rd, &put_cont, (void *) name);
280   if (NULL == nsqe)
281   {
282     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283               _("Namestore cannot store no block\n"));
284   }
285   GNUNET_free ((void *)rd.data);
286 }
287
288
289 int
290 main (int argc, char *argv[])
291 {
292   res = 1;
293   if (0 != 
294       GNUNET_TESTING_service_run ("test-namestore-api",
295                                   "namestore",
296                                   "test_namestore_api.conf",
297                                   &run,
298                                   NULL))
299     return 1;
300   return res;
301 }
302
303 /* end of test_namestore_api.c */