return NULL if rd_count is 0
[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_EccPublicKey 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
106   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
107               "Records were removed, perform lookup\n");
108
109   removed = GNUNET_YES;
110   nsqe = GNUNET_NAMESTORE_lookup_block (nsh, &derived_hash,
111                                          &name_lookup_proc, (void *) name);
112 }
113
114
115 static void
116 rd_decrypt_cb (void *cls,
117                                                  unsigned int rd_count,
118                                                  const struct GNUNET_NAMESTORE_RecordData *rd)
119 {
120   const char *name = cls;
121   char rd_cmp_data[TEST_RECORD_DATALEN];
122   if (GNUNET_NO == removed)
123   {
124         GNUNET_assert (1 == rd_count);
125         GNUNET_assert (NULL != rd);
126
127                 memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
128
129                 GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
130                 GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
131                 GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
132
133                 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
134                                         "Block was decrypted successfully, removing records \n");
135
136                 nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
137                                                                 0, NULL, &remove_cont, (void *) name);
138   }
139   else
140   {
141         GNUNET_assert (0 == rd_count);
142
143                 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
144                                         "Record was removed \n");
145
146         GNUNET_SCHEDULER_add_now (&end, NULL);
147   }
148 }
149
150 static void
151 name_lookup_proc (void *cls,
152                                                                         const struct GNUNET_NAMESTORE_Block *block)
153 {
154   const char *name = cls;
155   nsqe = NULL;
156
157   GNUNET_assert (NULL != cls);
158
159   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
160   {
161     GNUNET_SCHEDULER_cancel (endbadly_task);
162     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
163   }
164
165   if (NULL == block)
166   {
167     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168               "Namestore returned no block\n");
169     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
170       GNUNET_SCHEDULER_cancel (endbadly_task);
171       endbadly_task =  GNUNET_SCHEDULER_add_now (&endbadly, NULL);
172   }
173
174   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
175               "Namestore returned block, decrypting \n");
176
177         GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_block_decrypt(block,
178                 &pubkey, name, &rd_decrypt_cb, (void *) name));
179 }
180
181 static void
182 put_cont (void *cls, int32_t success, const char *emsg)
183 {
184   const char *name = cls;
185
186   GNUNET_assert (NULL != cls);
187
188   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
189               "Name store added record for `%s': %s\n",
190               name,
191               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
192
193   /* Create derived hash */
194   GNUNET_NAMESTORE_query_from_private_key (privkey, name, &derived_hash);
195
196   nsqe = GNUNET_NAMESTORE_lookup_block (nsh, &derived_hash,
197                                          &name_lookup_proc, (void *) name);
198 }
199
200
201 static void
202 run (void *cls, 
203      const struct GNUNET_CONFIGURATION_Handle *cfg,
204      struct GNUNET_TESTING_Peer *peer)
205 {
206   struct GNUNET_NAMESTORE_RecordData rd;
207   char *hostkey_file;
208   const char * name = "dummy.dummy.gnunet";
209
210   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
211                                                 &endbadly, NULL);
212   GNUNET_asprintf (&hostkey_file,
213                    "zonefiles%s%s",
214                    DIR_SEPARATOR_STR,
215                    "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
216   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
217   privkey = GNUNET_CRYPTO_ecc_key_create_from_file (hostkey_file);
218   GNUNET_free (hostkey_file);
219   GNUNET_assert (privkey != NULL);
220   GNUNET_CRYPTO_ecc_key_get_public (privkey, &pubkey);
221
222   removed = GNUNET_NO;
223
224   rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
225   rd.record_type = TEST_RECORD_TYPE;
226   rd.data_size = TEST_RECORD_DATALEN;
227   rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
228   memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
229
230   nsh = GNUNET_NAMESTORE_connect (cfg);
231   GNUNET_break (NULL != nsh);
232   nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
233                                       1, &rd, &put_cont, (void *) name);
234   GNUNET_free ((void *)rd.data);
235 }
236
237
238 int
239 main (int argc, char *argv[])
240 {
241   res = 1;
242   if (0 != 
243       GNUNET_TESTING_service_run ("test-namestore-api",
244                                   "namestore",
245                                   "test_namestore_api.conf",
246                                   &run,
247                                   NULL))
248     return 1;
249   return res;
250 }
251
252 /* end of test_namestore_api.c */