aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/test_namestore_api_lookup.c
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/test_namestore_api_lookup.c
parentef6f05f63769263a5201636c4b7f71eaa455cdf3 (diff)
downloadgnunet-64d3e46cfb99a711e500fa1f114e7c44bdf10040.tar.gz
gnunet-64d3e46cfb99a711e500fa1f114e7c44bdf10040.zip
-bugfixes, code cleanup
Diffstat (limited to 'src/namestore/test_namestore_api_lookup.c')
-rw-r--r--src/namestore/test_namestore_api_lookup.c108
1 files changed, 63 insertions, 45 deletions
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 */