aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/test_namestore_api_remove.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2013-08-22 08:51:11 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2013-08-22 08:51:11 +0000
commit50f299a80eda44a1380edec20f4b9d91dbc85d07 (patch)
treef28690c08b6f6d4d55b39998d704b1327de72a66 /src/namestore/test_namestore_api_remove.c
parent8cfdd55547ca1163f168af6d0254a17e5e0328c0 (diff)
downloadgnunet-50f299a80eda44a1380edec20f4b9d91dbc85d07.tar.gz
gnunet-50f299a80eda44a1380edec20f4b9d91dbc85d07.zip
test if records are removed correctly
Diffstat (limited to 'src/namestore/test_namestore_api_remove.c')
-rw-r--r--src/namestore/test_namestore_api_remove.c285
1 files changed, 126 insertions, 159 deletions
diff --git a/src/namestore/test_namestore_api_remove.c b/src/namestore/test_namestore_api_remove.c
index e666eac0e..9fd2e8d26 100644
--- a/src/namestore/test_namestore_api_remove.c
+++ b/src/namestore/test_namestore_api_remove.c
@@ -19,16 +19,12 @@
19*/ 19*/
20/** 20/**
21 * @file namestore/test_namestore_api.c 21 * @file namestore/test_namestore_api.c
22 * @brief testcase for namestore_api.c 22 * @brief testcase for namestore_api.c to remove record
23 */ 23 */
24#include "platform.h" 24#include "platform.h"
25#include "gnunet_common.h" 25#include "gnunet_common.h"
26#include "gnunet_namestore_service.h" 26#include "gnunet_namestore_service.h"
27#include "gnunet_testing_lib.h" 27#include "gnunet_testing_lib.h"
28#include "namestore.h"
29#include "gnunet_signatures.h"
30
31#define RECORDS 5
32 28
33#define TEST_RECORD_TYPE 1234 29#define TEST_RECORD_TYPE 1234
34 30
@@ -36,32 +32,41 @@
36 32
37#define TEST_RECORD_DATA 'a' 33#define TEST_RECORD_DATA 'a'
38 34
39#define TEST_REMOVE_RECORD_TYPE 4321
40
41#define TEST_REMOVE_RECORD_DATALEN 255
42
43#define TEST_REMOVE_RECORD_DATA 'b'
44
45#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100) 35#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
46 36
47 37
48static struct GNUNET_NAMESTORE_Handle * nsh; 38static struct GNUNET_NAMESTORE_Handle *nsh;
49 39
50static GNUNET_SCHEDULER_TaskIdentifier endbadly_task; 40static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
51 41
52static struct GNUNET_CRYPTO_EccPrivateKey * privkey; 42static struct GNUNET_CRYPTO_EccPrivateKey *privkey;
53 43
54static struct GNUNET_CRYPTO_EccPublicKey pubkey; 44static struct GNUNET_CRYPTO_EccPublicKey pubkey;
55 45
56static struct GNUNET_CRYPTO_EccSignature *s_signature; 46static struct GNUNET_HashCode derived_hash;
57 47
58static struct GNUNET_CRYPTO_ShortHashCode s_zone; 48static int res;
59 49
60static struct GNUNET_NAMESTORE_RecordData *s_rd; 50static int removed;
61 51
62static char *s_name; 52static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
63 53
64static int res; 54
55static void
56cleanup ()
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}
65 70
66 71
67/** 72/**
@@ -73,13 +78,12 @@ static int res;
73static void 78static void
74endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 79endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
75{ 80{
76 if (nsh != NULL) 81 if (NULL != nsqe)
77 GNUNET_NAMESTORE_disconnect (nsh); 82 {
78 nsh = NULL; 83 GNUNET_NAMESTORE_cancel (nsqe);
79 if (privkey != NULL) 84 nsqe = NULL;
80 GNUNET_free (privkey); 85 }
81 privkey = NULL; 86 cleanup ();
82 GNUNET_free_non_null (s_name);
83 res = 1; 87 res = 1;
84} 88}
85 89
@@ -87,137 +91,110 @@ endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
87static void 91static void
88end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 92end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
89{ 93{
90 int c; 94 cleanup ();
91 95 res = 0;
92 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
93 {
94 GNUNET_SCHEDULER_cancel (endbadly_task);
95 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
96 }
97 for (c = 0; c < RECORDS; c++)
98 GNUNET_free_non_null((void *) s_rd[c].data);
99 GNUNET_free (s_rd);
100 if (privkey != NULL)
101 GNUNET_free (privkey);
102 privkey = NULL;
103 if (nsh != NULL)
104 GNUNET_NAMESTORE_disconnect (nsh);
105 nsh = NULL;
106 GNUNET_free_non_null (s_name);
107} 96}
108 97
98static void
99name_lookup_proc (void *cls, const struct GNUNET_NAMESTORE_Block *block);
109 100
110static void 101static void
111name_lookup_proc (void *cls, 102remove_cont (void *cls, int32_t success, const char *emsg)
112 const struct GNUNET_CRYPTO_EccPublicKey *zone_key,
113 struct GNUNET_TIME_Absolute expire,
114 const char *n,
115 unsigned int rd_count,
116 const struct GNUNET_NAMESTORE_RecordData *rd,
117 const struct GNUNET_CRYPTO_EccSignature *signature)
118{ 103{
119 int failed = GNUNET_NO; 104 const char *name = cls;
120 105
121 if (NULL != n) 106 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
122 { 107 "Records were removed, perform lookup\n");
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 108
124 "Lookup for name `%s' returned %u records\n", n, rd_count); 109 removed = GNUNET_YES;
125 if (0 != memcmp (zone_key, 110 nsqe = GNUNET_NAMESTORE_lookup_block (nsh, &derived_hash,
126 &pubkey, 111 &name_lookup_proc, (void *) name);
127 sizeof (struct GNUNET_CRYPTO_EccPublicKey)))
128 {
129 GNUNET_break (0);
130 failed = GNUNET_YES;
131 }
132
133 if (0 != strcmp(n, s_name))
134 {
135 GNUNET_break (0);
136 failed = GNUNET_YES;
137 }
138
139 if (0 != rd_count)
140 {
141 GNUNET_break (0);
142 failed = GNUNET_YES;
143 }
144 if (failed == GNUNET_NO)
145 res = 0;
146 else
147 res = 1;
148 }
149 GNUNET_SCHEDULER_add_now (&end, NULL);
150} 112}
151 113
152 114
153static void 115static void
154remove_cont (void *cls, int32_t success, const char *emsg) 116rd_decrypt_cb (void *cls,
117 unsigned int rd_count,
118 const struct GNUNET_NAMESTORE_RecordData *rd)
155{ 119{
156 char *name = cls; 120 const char *name = cls;
157 121 char rd_cmp_data[TEST_RECORD_DATALEN];
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 122 if (GNUNET_NO == removed)
159 "Remove record for `%s': %s\n", name, (success == GNUNET_YES) ? "SUCCESS" : "FAIL",
160 emsg);
161 if (success == GNUNET_OK)
162 { 123 {
163 res = 0; 124 GNUNET_assert (1 == rd_count);
164 GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_proc, name); 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);
165 } 138 }
166 else 139 else
167 { 140 {
168 res = 1; 141 GNUNET_assert (0 == rd_count);
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove record for name `%s'\n", name);
170 GNUNET_SCHEDULER_add_now(&end, NULL);
171 }
172 142
173} 143 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
144 "Record was removed \n");
174 145
146 GNUNET_SCHEDULER_add_now (&end, NULL);
147 }
148}
175 149
176static void 150static void
177put_cont (void *cls, int32_t success, const char *emsg) 151name_lookup_proc (void *cls,
152 const struct GNUNET_NAMESTORE_Block *block)
178{ 153{
179 char *name = cls; 154 const char *name = cls;
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL"); 155 nsqe = NULL;
181 if (success == GNUNET_OK)
182 {
183 res = 0;
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for `%s'\n", name);
185 156
186 GNUNET_NAMESTORE_record_put_by_authority (nsh, privkey, name, 157 GNUNET_assert (NULL != cls);
187 0, NULL, 158
188 &remove_cont, name); 159 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
160 {
161 GNUNET_SCHEDULER_cancel (endbadly_task);
162 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
189 } 163 }
190 else 164
165 if (NULL == block)
191 { 166 {
192 res = 1; 167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name); 168 "Namestore returned no block\n");
194 GNUNET_SCHEDULER_add_now(&end, NULL); 169 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
170 GNUNET_SCHEDULER_cancel (endbadly_task);
171 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
195 } 172 }
196}
197 173
174 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
175 "Namestore returned block, decrypting \n");
198 176
199static struct GNUNET_NAMESTORE_RecordData * 177 GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_block_decrypt(block,
200create_record (unsigned int count) 178 &pubkey, name, &rd_decrypt_cb, (void *) name));
179}
180
181static void
182put_cont (void *cls, int32_t success, const char *emsg)
201{ 183{
202 unsigned int c; 184 const char *name = cls;
203 struct GNUNET_NAMESTORE_RecordData * rd; 185
204 186 GNUNET_assert (NULL != cls);
205 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData)); 187
206 rd[0].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us; 188 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
207 rd[0].record_type = 0; 189 "Name store added record for `%s': %s\n",
208 rd[0].data_size = TEST_REMOVE_RECORD_DATALEN; 190 name,
209 rd[0].data = GNUNET_malloc(TEST_REMOVE_RECORD_DATALEN); 191 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
210 memset ((char *) rd[0].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
211 for (c = 1; c < count; c++)
212 {
213 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
214 rd[c].record_type = TEST_RECORD_TYPE;
215 rd[c].data_size = TEST_RECORD_DATALEN;
216 rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
217 memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
218 }
219 192
220 return rd; 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);
221} 198}
222 199
223 200
@@ -226,44 +203,35 @@ run (void *cls,
226 const struct GNUNET_CONFIGURATION_Handle *cfg, 203 const struct GNUNET_CONFIGURATION_Handle *cfg,
227 struct GNUNET_TESTING_Peer *peer) 204 struct GNUNET_TESTING_Peer *peer)
228{ 205{
229 size_t rd_ser_len; 206 struct GNUNET_NAMESTORE_RecordData rd;
230 char *hostkey_file; 207 char *hostkey_file;
231 struct GNUNET_TIME_Absolute et; 208 const char * name = "dummy.dummy.gnunet";
232 209
233 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL); 210 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
234 /* load privat key */ 211 &endbadly, NULL);
235 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, 212 GNUNET_asprintf (&hostkey_file,
236 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"); 213 "zonefiles%s%s",
214 DIR_SEPARATOR_STR,
215 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file); 216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
238 privkey = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file); 217 privkey = GNUNET_CRYPTO_ecc_key_create_from_file (hostkey_file);
239 GNUNET_free (hostkey_file); 218 GNUNET_free (hostkey_file);
240 GNUNET_assert (privkey != NULL); 219 GNUNET_assert (privkey != NULL);
241 /* get public key */ 220 GNUNET_CRYPTO_ecc_key_get_public (privkey, &pubkey);
242 GNUNET_CRYPTO_ecc_key_get_public(privkey, &pubkey);
243
244 /* create record */
245 s_name = GNUNET_NAMESTORE_normalize_string ("DUMMY.dummy.gnunet");
246 s_rd = create_record (RECORDS);
247
248 rd_ser_len = GNUNET_NAMESTORE_records_get_size(RECORDS, s_rd);
249 char rd_ser[rd_ser_len];
250 GNUNET_NAMESTORE_records_serialize(RECORDS, s_rd, rd_ser_len, rd_ser);
251 221
252 /* sign */ 222 removed = GNUNET_NO;
253 et.abs_value_us = s_rd[0].expiration_time;
254 s_signature = GNUNET_NAMESTORE_create_signature(privkey, et, s_name, s_rd, RECORDS);
255 223
256 /* create random zone hash */ 224 rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
257 GNUNET_CRYPTO_short_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_EccPublicKey), &s_zone); 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);
258 229
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name: `%s' Zone: `%s' \n", s_name, GNUNET_NAMESTORE_short_h2s (&s_zone));
260 nsh = GNUNET_NAMESTORE_connect (cfg); 230 nsh = GNUNET_NAMESTORE_connect (cfg);
261 GNUNET_break (NULL != nsh); 231 GNUNET_break (NULL != nsh);
262 GNUNET_break (s_rd != NULL); 232 nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
263 GNUNET_break (s_name != NULL); 233 1, &rd, &put_cont, (void *) name);
264 GNUNET_NAMESTORE_record_put (nsh, &pubkey, s_name, 234 GNUNET_free ((void *)rd.data);
265 GNUNET_TIME_UNIT_FOREVER_ABS,
266 RECORDS, s_rd, s_signature, put_cont, s_name);
267} 235}
268 236
269 237
@@ -272,13 +240,12 @@ main (int argc, char *argv[])
272{ 240{
273 res = 1; 241 res = 1;
274 if (0 != 242 if (0 !=
275 GNUNET_TESTING_service_run ("test-namestore-api-remove", 243 GNUNET_TESTING_service_run ("test-namestore-api",
276 "namestore", 244 "namestore",
277 "test_namestore_api.conf", 245 "test_namestore_api.conf",
278 &run, 246 &run,
279 NULL)) 247 NULL))
280 return 1; 248 return 1;
281 GNUNET_free_non_null (s_signature);
282 return res; 249 return res;
283} 250}
284 251