aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2012-02-29 09:48:01 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2012-02-29 09:48:01 +0000
commit168fbc4bd922076424a8585d2a2e0e3a896b40a4 (patch)
tree8c6a94d845fea36169a42b17840b28005205ec2c
parent7aa1787054721f8009d4d195e7dae22831828fb2 (diff)
downloadgnunet-168fbc4bd922076424a8585d2a2e0e3a896b40a4.tar.gz
gnunet-168fbc4bd922076424a8585d2a2e0e3a896b40a4.zip
- put test
-rw-r--r--src/namestore/test_namestore_api_put.c226
1 files changed, 226 insertions, 0 deletions
diff --git a/src/namestore/test_namestore_api_put.c b/src/namestore/test_namestore_api_put.c
new file mode 100644
index 000000000..99f8e25b7
--- /dev/null
+++ b/src/namestore/test_namestore_api_put.c
@@ -0,0 +1,226 @@
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
23 */
24#include "platform.h"
25#include "gnunet_common.h"
26#include "gnunet_namestore_service.h"
27
28#define VERBOSE GNUNET_NO
29
30#define RECORDS 5
31#define TEST_RECORD_TYPE 1234
32#define TEST_RECORD_DATALEN 123
33#define TEST_RECORD_DATA 'a'
34
35#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
36
37static struct GNUNET_NAMESTORE_Handle * nsh;
38
39static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
40static struct GNUNET_OS_Process *arm;
41
42static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
43static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
44static GNUNET_HashCode zone;
45
46struct GNUNET_NAMESTORE_RecordData *rd;
47
48static int res;
49
50static void
51start_arm (const char *cfgname)
52{
53 arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
54 "gnunet-service-arm", "-c", cfgname,
55#if VERBOSE_PEERS
56 "-L", "DEBUG",
57#else
58 "-L", "ERROR",
59#endif
60 NULL);
61}
62
63static void
64stop_arm ()
65{
66 if (NULL != arm)
67 {
68 if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
69 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
70 GNUNET_OS_process_wait (arm);
71 GNUNET_OS_process_close (arm);
72 arm = NULL;
73 }
74}
75
76/**
77 * Re-establish the connection to the service.
78 *
79 * @param cls handle to use to re-connect.
80 * @param tc scheduler context
81 */
82static void
83endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84{
85 if (nsh != NULL)
86 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
87 nsh = NULL;
88
89 if (privkey != NULL)
90 GNUNET_CRYPTO_rsa_key_free (privkey);
91 privkey = NULL;
92
93 if (NULL != arm)
94 stop_arm();
95
96 res = 1;
97}
98
99
100static void
101end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102{
103 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
104 {
105 GNUNET_SCHEDULER_cancel (endbadly_task);
106 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
107 }
108
109 if (privkey != NULL)
110 GNUNET_CRYPTO_rsa_key_free (privkey);
111 privkey = NULL;
112
113 if (nsh != NULL)
114 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
115 nsh = NULL;
116
117 if (NULL != arm)
118 stop_arm();
119}
120
121void
122put_cont (void *cls, int32_t success, const char *emsg)
123{
124 char * name = cls;
125
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
127 if (success == GNUNET_OK)
128 res = 0;
129 else
130 res = 1;
131
132 GNUNET_SCHEDULER_add_now(&end, NULL);
133}
134
135static struct GNUNET_NAMESTORE_RecordData *
136create_record (int count)
137{
138 int c;
139 struct GNUNET_NAMESTORE_RecordData * rd;
140 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
141
142 for (c = 0; c < RECORDS; c++)
143 {
144 rd[c].expiration = GNUNET_TIME_absolute_get();
145 rd[c].record_type = TEST_RECORD_TYPE;
146 rd[c].data_size = TEST_RECORD_DATALEN;
147 rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
148 memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
149 }
150
151 return rd;
152}
153
154static void
155run (void *cls, char *const *args, const char *cfgfile,
156 const struct GNUNET_CONFIGURATION_Handle *cfg)
157{
158 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
159
160 /* load privat key */
161 privkey = GNUNET_CRYPTO_rsa_key_create_from_file("hostkey");
162 GNUNET_assert (privkey != NULL);
163 /* get public key */
164 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
165
166 /* create random zone hash */
167 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &zone);
168
169 struct GNUNET_CRYPTO_RsaSignature signature;
170
171 start_arm (cfgfile);
172 GNUNET_assert (arm != NULL);
173
174 nsh = GNUNET_NAMESTORE_connect (cfg);
175 GNUNET_break (NULL != nsh);
176
177 /* create record */
178 char * name = "dummy.dummy.gnunet";
179 rd = create_record (RECORDS);
180
181 GNUNET_break (rd != NULL);
182 GNUNET_break (name != NULL);
183
184 GNUNET_NAMESTORE_record_put (nsh, &pubkey, name,
185 GNUNET_TIME_absolute_get_forever(),
186 RECORDS, rd, &signature, put_cont, name);
187
188 int c;
189 for (c = 0; c < RECORDS; c++)
190 GNUNET_free_non_null((void *) rd[c].data);
191 GNUNET_free (rd);
192
193}
194
195static int
196check ()
197{
198 static char *const argv[] = { "test-namestore-api",
199 "-c",
200 "test_namestore_api.conf",
201#if VERBOSE
202 "-L", "DEBUG",
203#endif
204 NULL
205 };
206 static struct GNUNET_GETOPT_CommandLineOption options[] = {
207 GNUNET_GETOPT_OPTION_END
208 };
209
210 res = 1;
211 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
212 "nohelp", options, &run, &res);
213 return res;
214}
215
216int
217main (int argc, char *argv[])
218{
219 int ret;
220
221 ret = check ();
222
223 return ret;
224}
225
226/* end of test_namestore_api.c */