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