aboutsummaryrefslogtreecommitdiff
path: root/src/namestore
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-10-26 22:13:01 +0000
committerChristian Grothoff <christian@grothoff.org>2012-10-26 22:13:01 +0000
commit64d3e46cfb99a711e500fa1f114e7c44bdf10040 (patch)
tree83093d92761d5c1b8b2fded713adaa19af29274c /src/namestore
parentef6f05f63769263a5201636c4b7f71eaa455cdf3 (diff)
downloadgnunet-64d3e46cfb99a711e500fa1f114e7c44bdf10040.tar.gz
gnunet-64d3e46cfb99a711e500fa1f114e7c44bdf10040.zip
-bugfixes, code cleanup
Diffstat (limited to 'src/namestore')
-rw-r--r--src/namestore/test_namestore_api.c74
-rw-r--r--src/namestore/test_namestore_api_lookup.c108
2 files changed, 101 insertions, 81 deletions
diff --git a/src/namestore/test_namestore_api.c b/src/namestore/test_namestore_api.c
index ca6a174ee..c0b20268b 100644
--- a/src/namestore/test_namestore_api.c
+++ b/src/namestore/test_namestore_api.c
@@ -49,6 +49,24 @@ static int res;
49 49
50static struct GNUNET_NAMESTORE_QueueEntry *nsqe; 50static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
51 51
52
53static void
54cleanup ()
55{
56 if (NULL != nsh)
57 {
58 GNUNET_NAMESTORE_disconnect (nsh);
59 nsh = NULL;
60 }
61 if (NULL != privkey)
62 {
63 GNUNET_CRYPTO_rsa_key_free (privkey);
64 privkey = NULL;
65 }
66 GNUNET_SCHEDULER_shutdown ();
67}
68
69
52/** 70/**
53 * Re-establish the connection to the service. 71 * Re-establish the connection to the service.
54 * 72 *
@@ -63,17 +81,7 @@ endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
63 GNUNET_NAMESTORE_cancel (nsqe); 81 GNUNET_NAMESTORE_cancel (nsqe);
64 nsqe = NULL; 82 nsqe = NULL;
65 } 83 }
66 if (nsh != NULL) 84 cleanup ();
67 {
68 GNUNET_NAMESTORE_disconnect (nsh);
69 nsh = NULL;
70 }
71 if (privkey != NULL)
72 {
73 GNUNET_CRYPTO_rsa_key_free (privkey);
74 privkey = NULL;
75 }
76 GNUNET_SCHEDULER_shutdown ();
77 res = 1; 85 res = 1;
78} 86}
79 87
@@ -81,21 +89,7 @@ endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81static void 89static void
82end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 90end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83{ 91{
84 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK) 92 cleanup ();
85 {
86 GNUNET_SCHEDULER_cancel (endbadly_task);
87 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
88 }
89 if (privkey != NULL)
90 {
91 GNUNET_CRYPTO_rsa_key_free (privkey);
92 privkey = NULL;
93 }
94 if (nsh != NULL)
95 {
96 GNUNET_NAMESTORE_disconnect (nsh);
97 nsh = NULL;
98 }
99 res = 0; 93 res = 0;
100} 94}
101 95
@@ -113,8 +107,12 @@ name_lookup_proc (void *cls,
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114 "Namestore lookup result %p `%s' %i %p %p\n", 108 "Namestore lookup result %p `%s' %i %p %p\n",
115 zone_key, name, rd_count, rd, signature); 109 zone_key, name, rd_count, rd, signature);
116 res = 0; 110 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
117 GNUNET_SCHEDULER_add_now(&end, NULL); 111 {
112 GNUNET_SCHEDULER_cancel (endbadly_task);
113 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
114 }
115 GNUNET_SCHEDULER_add_now (&end, NULL);
118} 116}
119 117
120 118
@@ -124,9 +122,11 @@ put_cont (void *cls, int32_t success, const char *emsg)
124 const char *name = cls; 122 const char *name = cls;
125 123
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL"); 125 "Name store added record for `%s': %s\n",
128 126 name,
129 nsqe = GNUNET_NAMESTORE_lookup_record (nsh, &zone, name, 0, &name_lookup_proc, NULL); 127 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
128 nsqe = GNUNET_NAMESTORE_lookup_record (nsh, &zone, name, 0,
129 &name_lookup_proc, NULL);
130} 130}
131 131
132 132
@@ -142,19 +142,21 @@ run (void *cls,
142 142
143 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, 143 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
144 &endbadly, NULL); 144 &endbadly, NULL);
145 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, 145 GNUNET_asprintf (&hostkey_file,
146 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"); 146 "zonefiles%s%s",
147 DIR_SEPARATOR_STR,
148 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file); 149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
148 privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file); 150 privkey = GNUNET_CRYPTO_rsa_key_create_from_file (hostkey_file);
149 GNUNET_free (hostkey_file); 151 GNUNET_free (hostkey_file);
150 GNUNET_assert (privkey != NULL); 152 GNUNET_assert (privkey != NULL);
151 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey); 153 GNUNET_CRYPTO_rsa_key_get_public (privkey, &pubkey);
152 GNUNET_CRYPTO_short_hash (&pubkey, sizeof (pubkey), &zone); 154 GNUNET_CRYPTO_short_hash (&pubkey, sizeof (pubkey), &zone);
153 memset (&signature, '\0', sizeof (signature)); 155 memset (&signature, '\0', sizeof (signature));
154 rd.expiration_time = GNUNET_TIME_absolute_get().abs_value; 156 rd.expiration_time = GNUNET_TIME_absolute_get().abs_value;
155 rd.record_type = TEST_RECORD_TYPE; 157 rd.record_type = TEST_RECORD_TYPE;
156 rd.data_size = TEST_RECORD_DATALEN; 158 rd.data_size = TEST_RECORD_DATALEN;
157 rd.data = GNUNET_malloc(TEST_RECORD_DATALEN); 159 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
158 memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN); 160 memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
159 nsh = GNUNET_NAMESTORE_connect (cfg); 161 nsh = GNUNET_NAMESTORE_connect (cfg);
160 GNUNET_break (NULL != nsh); 162 GNUNET_break (NULL != nsh);
diff --git a/src/namestore/test_namestore_api_lookup.c b/src/namestore/test_namestore_api_lookup.c
index 6828e034c..0f1c133bb 100644
--- a/src/namestore/test_namestore_api_lookup.c
+++ b/src/namestore/test_namestore_api_lookup.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors) 3 (C) 2012 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -18,7 +18,7 @@
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20/** 20/**
21 * @file namestore/test_namestore_api.c 21 * @file namestore/test_namestore_api_lookup.c
22 * @brief testcase for namestore_api.c 22 * @brief testcase for namestore_api.c
23 */ 23 */
24#include "platform.h" 24#include "platform.h"
@@ -53,11 +53,30 @@ static struct GNUNET_CRYPTO_ShortHashCode s_zone;
53 53
54static struct GNUNET_NAMESTORE_RecordData *s_rd; 54static struct GNUNET_NAMESTORE_RecordData *s_rd;
55 55
56static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
57
56static char *s_name; 58static char *s_name;
57 59
58static int res; 60static int res;
59 61
60 62
63static void
64cleanup ()
65{
66 if (NULL != nsh)
67 {
68 GNUNET_NAMESTORE_disconnect (nsh);
69 nsh = NULL;
70 }
71 if (NULL != privkey)
72 {
73 GNUNET_CRYPTO_rsa_key_free (privkey);
74 privkey = NULL;
75 }
76 GNUNET_SCHEDULER_shutdown ();
77}
78
79
61/** 80/**
62 * Re-establish the connection to the service. 81 * Re-establish the connection to the service.
63 * 82 *
@@ -67,12 +86,7 @@ static int res;
67static void 86static void
68endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 87endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
69{ 88{
70 if (nsh != NULL) 89 cleanup ();
71 GNUNET_NAMESTORE_disconnect (nsh);
72 nsh = NULL;
73 if (privkey != NULL)
74 GNUNET_CRYPTO_rsa_key_free (privkey);
75 privkey = NULL;
76 res = 1; 90 res = 1;
77} 91}
78 92
@@ -80,22 +94,12 @@ endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80static void 94static void
81end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 95end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82{ 96{
83 int c; 97 unsigned int c;
84 98
85 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
86 {
87 GNUNET_SCHEDULER_cancel (endbadly_task);
88 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
89 }
90 for (c = 0; c < RECORDS; c++) 99 for (c = 0; c < RECORDS; c++)
91 GNUNET_free_non_null((void *) s_rd[c].data); 100 GNUNET_free_non_null((void *) s_rd[c].data);
92 GNUNET_free (s_rd); 101 GNUNET_free (s_rd);
93 if (privkey != NULL) 102 cleanup ();
94 GNUNET_CRYPTO_rsa_key_free (privkey);
95 privkey = NULL;
96 if (nsh != NULL)
97 GNUNET_NAMESTORE_disconnect (nsh);
98 nsh = NULL;
99} 103}
100 104
101 105
@@ -111,19 +115,21 @@ name_lookup_proc (void *cls,
111 static int found = GNUNET_NO; 115 static int found = GNUNET_NO;
112 int c; 116 int c;
113 117
114 if (n != NULL) 118 if (NULL != n)
115 { 119 {
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n"); 120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n");
117 if (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))) 121 if (0 != memcmp (zone_key, &pubkey,
122 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
118 { 123 {
119 GNUNET_break (0); 124 GNUNET_break (0);
120 } 125 }
121 GNUNET_assert (NULL != signature); 126 GNUNET_assert (NULL != signature);
122 if (0 != memcmp (signature, s_signature, sizeof (struct GNUNET_CRYPTO_RsaSignature))) 127 if (0 != memcmp (signature, s_signature,
128 sizeof (struct GNUNET_CRYPTO_RsaSignature)))
123 { 129 {
124 GNUNET_break (0); 130 GNUNET_break (0);
125 } 131 }
126 if (0 != strcmp(n, s_name)) 132 if (0 != strcmp (n, s_name))
127 { 133 {
128 GNUNET_break (0); 134 GNUNET_break (0);
129 } 135 }
@@ -143,14 +149,20 @@ name_lookup_proc (void *cls,
143 } 149 }
144 else 150 else
145 { 151 {
146 if (found != GNUNET_YES) 152 if (GNUNET_YES != found)
147 { 153 {
148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to lookup records for name `%s'\n", s_name); 154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155 "Failed to lookup records for name `%s'\n", s_name);
149 res = 1; 156 res = 1;
150 } 157 }
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name); 158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name);
152 } 159 }
153 GNUNET_SCHEDULER_add_now(&end, NULL); 160 if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
161 {
162 GNUNET_SCHEDULER_cancel (endbadly_task);
163 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
164 }
165 GNUNET_SCHEDULER_add_now (&end, NULL);
154} 166}
155 167
156 168
@@ -159,17 +171,22 @@ put_cont (void *cls, int32_t success, const char *emsg)
159{ 171{
160 char * name = cls; 172 char * name = cls;
161 173
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL"); 174 nsqe = NULL;
163 if (success == GNUNET_OK) 175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "Name store added record for `%s': %s\n", name,
177 (GNUNET_OK == success) ? "SUCCESS" : "FAIL");
178 if (GNUNET_OK == success)
164 { 179 {
165 res = 0; 180 res = 0;
166 GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_proc, NULL); 181 GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0,
182 &name_lookup_proc, NULL);
167 } 183 }
168 else 184 else
169 { 185 {
170 res = 1; 186 res = 1;
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name); 187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 GNUNET_SCHEDULER_add_now(&end, NULL); 188 "Failed to put records for name `%s'\n", name);
189 GNUNET_SCHEDULER_shutdown ();
173 } 190 }
174} 191}
175 192
@@ -200,12 +217,14 @@ run (void *cls,
200{ 217{
201 size_t rd_ser_len; 218 size_t rd_ser_len;
202 struct GNUNET_TIME_Absolute et; 219 struct GNUNET_TIME_Absolute et;
220 char rd_ser[rd_ser_len];
203 221
204 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL); 222 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
223 &endbadly, NULL);
205 224
206 /* load privat key from file not included in zonekey dir */ 225 /* load privat key from file not included in zonekey dir */
207 privkey = GNUNET_CRYPTO_rsa_key_create_from_file("test_hostkey"); 226 privkey = GNUNET_CRYPTO_rsa_key_create_from_file("test_hostkey");
208 GNUNET_assert (privkey != NULL); 227 GNUNET_assert (NULL != privkey);
209 /* get public key */ 228 /* get public key */
210 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey); 229 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
211 230
@@ -214,24 +233,23 @@ run (void *cls,
214 s_rd = create_record (RECORDS); 233 s_rd = create_record (RECORDS);
215 234
216 rd_ser_len = GNUNET_NAMESTORE_records_get_size(RECORDS, s_rd); 235 rd_ser_len = GNUNET_NAMESTORE_records_get_size(RECORDS, s_rd);
217 char rd_ser[rd_ser_len];
218 GNUNET_NAMESTORE_records_serialize(RECORDS, s_rd, rd_ser_len, rd_ser); 236 GNUNET_NAMESTORE_records_serialize(RECORDS, s_rd, rd_ser_len, rd_ser);
219 237
220 /* sign */ 238 /* sign */
221 et.abs_value = s_rd[0].expiration_time; 239 et.abs_value = s_rd[0].expiration_time;
222 s_signature = GNUNET_NAMESTORE_create_signature(privkey, et, s_name, s_rd, RECORDS); 240 s_signature = GNUNET_NAMESTORE_create_signature (privkey, et, s_name,
241 s_rd, RECORDS);
223 242
224 /* create random zone hash */ 243 /* create random zone hash */
225 GNUNET_CRYPTO_short_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone); 244 GNUNET_CRYPTO_short_hash (&pubkey,
245 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
246 &s_zone);
226 nsh = GNUNET_NAMESTORE_connect (cfg); 247 nsh = GNUNET_NAMESTORE_connect (cfg);
227 GNUNET_break (NULL != nsh); 248 GNUNET_break (NULL != nsh);
228 249 nsqe = GNUNET_NAMESTORE_record_put (nsh, &pubkey, s_name,
229 GNUNET_break (s_rd != NULL); 250 GNUNET_TIME_UNIT_FOREVER_ABS,
230 GNUNET_break (s_name != NULL); 251 RECORDS, s_rd, s_signature,
231 252 &put_cont, s_name);
232 GNUNET_NAMESTORE_record_put (nsh, &pubkey, s_name,
233 GNUNET_TIME_UNIT_FOREVER_ABS,
234 RECORDS, s_rd, s_signature, put_cont, s_name);
235} 253}
236 254
237 255
@@ -251,4 +269,4 @@ main (int argc, char *argv[])
251} 269}
252 270
253 271
254/* end of test_namestore_api.c */ 272/* end of test_namestore_api_lookup.c */