aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2013-09-11 10:22:00 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2013-09-11 10:22:00 +0000
commit71b4381829495a32998ce5bbd38361a6f1a79836 (patch)
tree6ad8c74025c4b1fa65bf892bac8d24e0f8e1ec00 /src
parentd4db63e2864bf82a7765d6ad9631550bfed1a274 (diff)
downloadgnunet-71b4381829495a32998ce5bbd38361a6f1a79836.tar.gz
gnunet-71b4381829495a32998ce5bbd38361a6f1a79836.zip
new test: store a label and update it
Diffstat (limited to 'src')
-rw-r--r--src/namestore/Makefile.am8
-rw-r--r--src/namestore/test_namestore_api_store_update.c263
2 files changed, 271 insertions, 0 deletions
diff --git a/src/namestore/Makefile.am b/src/namestore/Makefile.am
index 402128983..cfc0b4d32 100644
--- a/src/namestore/Makefile.am
+++ b/src/namestore/Makefile.am
@@ -42,6 +42,7 @@ if HAVE_EXPERIMENTAL
42if HAVE_TESTING 42if HAVE_TESTING
43TESTING_TESTS = \ 43TESTING_TESTS = \
44 test_namestore_api_store \ 44 test_namestore_api_store \
45 test_namestore_api_store_update \
45 test_namestore_api_remove \ 46 test_namestore_api_remove \
46 test_namestore_api_zone_iteration \ 47 test_namestore_api_zone_iteration \
47 test_namestore_api_zone_iteration_specific_zone \ 48 test_namestore_api_zone_iteration_specific_zone \
@@ -188,6 +189,13 @@ test_namestore_api_store_LDADD = \
188 $(top_builddir)/src/util/libgnunetutil.la \ 189 $(top_builddir)/src/util/libgnunetutil.la \
189 $(top_builddir)/src/namestore/libgnunetnamestore.la 190 $(top_builddir)/src/namestore/libgnunetnamestore.la
190 191
192test_namestore_api_store_update_SOURCES = \
193 test_namestore_api_store_update.c
194test_namestore_api_store_update_LDADD = \
195 $(top_builddir)/src/testing/libgnunettesting.la \
196 $(top_builddir)/src/util/libgnunetutil.la \
197 $(top_builddir)/src/namestore/libgnunetnamestore.la
198
191test_namestore_api_put_SOURCES = \ 199test_namestore_api_put_SOURCES = \
192 test_namestore_api_put.c 200 test_namestore_api_put.c
193test_namestore_api_put_LDADD = \ 201test_namestore_api_put_LDADD = \
diff --git a/src/namestore/test_namestore_api_store_update.c b/src/namestore/test_namestore_api_store_update.c
new file mode 100644
index 000000000..9455c6bd6
--- /dev/null
+++ b/src/namestore/test_namestore_api_store_update.c
@@ -0,0 +1,263 @@
1/*
2 This file is part of GNUnet.
3 (C) 2012 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_store_update.c
22 * @brief testcase for namestore_api.c: store a record, update it and perform a lookup
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
36#define TEST_RECORD_TYPE2 4321
37
38#define TEST_RECORD_DATALEN2 234
39
40#define TEST_RECORD_DATA2 'b'
41
42#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
43
44
45static struct GNUNET_NAMESTORE_Handle *nsh;
46
47static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
48
49static struct GNUNET_CRYPTO_EccPrivateKey *privkey;
50
51static struct GNUNET_CRYPTO_EccPublicKey pubkey;
52
53static int res;
54
55static int update_performed;
56
57static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
58
59static const char * name = "dummy.dummy.gnunet";
60
61static void
62cleanup ()
63{
64 if (NULL != nsh)
65 {
66 GNUNET_NAMESTORE_disconnect (nsh);
67 nsh = NULL;
68 }
69 if (NULL != privkey)
70 {
71 GNUNET_free (privkey);
72 privkey = NULL;
73 }
74 GNUNET_SCHEDULER_shutdown ();
75}
76
77
78/**
79 * Re-establish the connection to the service.
80 *
81 * @param cls handle to use to re-connect.
82 * @param tc scheduler context
83 */
84static void
85endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
86{
87 if (NULL != nsqe)
88 {
89 GNUNET_NAMESTORE_cancel (nsqe);
90 nsqe = NULL;
91 }
92 cleanup ();
93 res = 1;
94}
95
96
97static void
98end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
99{
100 cleanup ();
101 res = 0;
102}
103
104static void
105put_cont (void *cls, int32_t success, const char *emsg);
106
107
108static void
109rd_decrypt_cb (void *cls,
110 unsigned int rd_count,
111 const struct GNUNET_NAMESTORE_RecordData *rd)
112{
113 struct GNUNET_NAMESTORE_RecordData rd_new;
114
115 GNUNET_assert (1 == rd_count);
116 GNUNET_assert (NULL != rd);
117
118 if (GNUNET_NO == update_performed)
119 {
120 char rd_cmp_data[TEST_RECORD_DATALEN];
121 memset (rd_cmp_data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
122
123 GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
124 GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
125 GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
126
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 "Block was decrypted successfully, updating record \n");
129
130 rd_new.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
131 rd_new.record_type = TEST_RECORD_TYPE2;
132 rd_new.data_size = TEST_RECORD_DATALEN2;
133 rd_new.data = GNUNET_malloc (TEST_RECORD_DATALEN2);
134 memset ((char *) rd_new.data, TEST_RECORD_DATA2, TEST_RECORD_DATALEN2);
135
136 nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
137 1, &rd_new, &put_cont, (void *) name);
138 update_performed = GNUNET_YES;
139 }
140 else
141 {
142 char rd_cmp_data[TEST_RECORD_DATALEN2];
143 memset (rd_cmp_data, TEST_RECORD_DATA2, TEST_RECORD_DATALEN2);
144
145 GNUNET_assert (TEST_RECORD_TYPE2 == rd[0].record_type);
146 GNUNET_assert (TEST_RECORD_DATALEN2 == rd[0].data_size);
147 GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN2));
148
149 GNUNET_SCHEDULER_add_now (&end, NULL);
150 }
151
152}
153
154static void
155name_lookup_proc (void *cls,
156 const struct GNUNET_NAMESTORE_Block *block)
157{
158 const char *name = cls;
159 nsqe = NULL;
160
161 GNUNET_assert (NULL != cls);
162
163 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
164 {
165 GNUNET_SCHEDULER_cancel (endbadly_task);
166 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
167 }
168
169 if (NULL == block)
170 {
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 _("Namestore returned no block\n"));
173 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
174 GNUNET_SCHEDULER_cancel (endbadly_task);
175 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
176 return;
177 }
178
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "Namestore returned block, decrypting \n");
181 GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_block_decrypt(block,
182 &pubkey, name, &rd_decrypt_cb, (void *) name));
183}
184
185static void
186put_cont (void *cls, int32_t success, const char *emsg)
187{
188 const char *name = cls;
189 struct GNUNET_HashCode derived_hash;
190
191 GNUNET_assert (NULL != cls);
192
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "Name store added record for `%s': %s\n",
195 name,
196 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
197
198 /* Create derived hash */
199 GNUNET_NAMESTORE_query_from_private_key (privkey, name, &derived_hash);
200
201 nsqe = GNUNET_NAMESTORE_lookup_block (nsh, &derived_hash,
202 &name_lookup_proc, (void *) name);
203}
204
205
206static void
207run (void *cls,
208 const struct GNUNET_CONFIGURATION_Handle *cfg,
209 struct GNUNET_TESTING_Peer *peer)
210{
211 struct GNUNET_NAMESTORE_RecordData rd;
212 char *hostkey_file;
213
214 update_performed = GNUNET_NO;
215 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
216 &endbadly, NULL);
217 GNUNET_asprintf (&hostkey_file,
218 "zonefiles%s%s",
219 DIR_SEPARATOR_STR,
220 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
222 privkey = GNUNET_CRYPTO_ecc_key_create_from_file (hostkey_file);
223 GNUNET_free (hostkey_file);
224 GNUNET_assert (privkey != NULL);
225 GNUNET_CRYPTO_ecc_key_get_public (privkey, &pubkey);
226
227
228 rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
229 rd.record_type = TEST_RECORD_TYPE;
230 rd.data_size = TEST_RECORD_DATALEN;
231 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
232 memset ((char *) rd.data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
233
234 nsh = GNUNET_NAMESTORE_connect (cfg);
235 GNUNET_break (NULL != nsh);
236 nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
237 1, &rd, &put_cont, (void *) name);
238 if (NULL == nsqe)
239 {
240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
241 _("Namestore cannot store no block\n"));
242 }
243
244 GNUNET_free ((void *)rd.data);
245}
246
247
248int
249main (int argc, char *argv[])
250{
251 res = 1;
252 if (0 !=
253 GNUNET_TESTING_service_run ("test-namestore-api-store-update",
254 "namestore",
255 "test_namestore_api.conf",
256 &run,
257 NULL))
258 return 1;
259 return res;
260}
261
262
263/* end of test_namestore_api_store_update.c*/