aboutsummaryrefslogtreecommitdiff
path: root/src/dht
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-06-23 21:23:02 +0000
committerChristian Grothoff <christian@grothoff.org>2012-06-23 21:23:02 +0000
commit67e1a348623250942f76263362f9d5b297850311 (patch)
treeabe2cdfb29474d5a1b5db3bec9f49d1ededa425f /src/dht
parentce0a88b825f63e418e034a1bdfc377e91da5e770 (diff)
downloadgnunet-67e1a348623250942f76263362f9d5b297850311.tar.gz
gnunet-67e1a348623250942f76263362f9d5b297850311.zip
-use testing lib
Diffstat (limited to 'src/dht')
-rw-r--r--src/dht/Makefile.am1
-rw-r--r--src/dht/test_dht_api.c197
2 files changed, 45 insertions, 153 deletions
diff --git a/src/dht/Makefile.am b/src/dht/Makefile.am
index 0b840dc9a..c75525ee3 100644
--- a/src/dht/Makefile.am
+++ b/src/dht/Makefile.am
@@ -132,6 +132,7 @@ test_dht_api_SOURCES = \
132 test_dht_api.c 132 test_dht_api.c
133test_dht_api_LDADD = \ 133test_dht_api_LDADD = \
134 $(top_builddir)/src/util/libgnunetutil.la \ 134 $(top_builddir)/src/util/libgnunetutil.la \
135 $(top_builddir)/src/testing/libgnunettesting.la \
135 $(top_builddir)/src/hello/libgnunethello.la \ 136 $(top_builddir)/src/hello/libgnunethello.la \
136 $(top_builddir)/src/dht/libgnunetdht.la 137 $(top_builddir)/src/dht/libgnunetdht.la
137test_dht_api_DEPENDENCIES = \ 138test_dht_api_DEPENDENCIES = \
diff --git a/src/dht/test_dht_api.c b/src/dht/test_dht_api.c
index 40d7f5b77..b2b62d559 100644
--- a/src/dht/test_dht_api.c
+++ b/src/dht/test_dht_api.c
@@ -25,20 +25,11 @@
25 * 25 *
26 */ 26 */
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_common.h" 28#include "gnunet_util_lib.h"
29#include "gnunet_hello_lib.h" 29#include "gnunet_hello_lib.h"
30#include "gnunet_getopt_lib.h" 30#include "gnunet_testing_lib-new.h"
31#include "gnunet_os_lib.h"
32#include "gnunet_program_lib.h"
33#include "gnunet_scheduler_lib.h"
34#include "gnunet_dht_service.h" 31#include "gnunet_dht_service.h"
35#include "gnunet_hello_lib.h"
36
37#define VERBOSE GNUNET_NO
38
39#define VERBOSE_ARM GNUNET_NO
40 32
41#define START_ARM GNUNET_YES
42 33
43/** 34/**
44 * How long until we really give up on a particular testcase portion? 35 * How long until we really give up on a particular testcase portion?
@@ -52,6 +43,7 @@
52 43
53#define MTYPE 12345 44#define MTYPE 12345
54 45
46
55struct RetryContext 47struct RetryContext
56{ 48{
57 /** 49 /**
@@ -65,37 +57,24 @@ struct RetryContext
65 struct GNUNET_TIME_Relative next_timeout; 57 struct GNUNET_TIME_Relative next_timeout;
66 58
67 /** 59 /**
68 * The context of the peer we are dealing with.
69 */
70 struct PeerContext *peer_ctx;
71
72 /**
73 * The task identifier of the retry task, so it can be cancelled. 60 * The task identifier of the retry task, so it can be cancelled.
74 */ 61 */
75 GNUNET_SCHEDULER_TaskIdentifier retry_task; 62 GNUNET_SCHEDULER_TaskIdentifier retry_task;
76 63
77}; 64};
78 65
79struct PeerContext
80{
81 struct GNUNET_CONFIGURATION_Handle *cfg;
82 struct GNUNET_DHT_Handle *dht_handle;
83 struct GNUNET_PeerIdentity id;
84 struct GNUNET_DHT_GetHandle *get_handle;
85#if START_ARM
86 struct GNUNET_OS_Process *arm_proc;
87#endif
88};
89 66
90static struct PeerContext p1; 67static struct GNUNET_DHT_Handle *dht_handle;
91 68
92struct RetryContext retry_context; 69static struct GNUNET_DHT_GetHandle *get_handle;
93 70
71struct RetryContext retry_context;
94 72
95static int ok; 73static int ok;
96 74
97static GNUNET_SCHEDULER_TaskIdentifier die_task; 75static GNUNET_SCHEDULER_TaskIdentifier die_task;
98 76
77
99#if VERBOSE 78#if VERBOSE
100#define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0) 79#define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
101#else 80#else
@@ -108,43 +87,28 @@ end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108{ 87{
109 GNUNET_SCHEDULER_cancel (die_task); 88 GNUNET_SCHEDULER_cancel (die_task);
110 die_task = GNUNET_SCHEDULER_NO_TASK; 89 die_task = GNUNET_SCHEDULER_NO_TASK;
111 GNUNET_DHT_disconnect (p1.dht_handle); 90 GNUNET_DHT_disconnect (dht_handle);
91 dht_handle = NULL;
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113 "DHT disconnected, returning success!\n"); 93 "DHT disconnected, returning success!\n");
114 ok = 0; 94 ok = 0;
115} 95}
116 96
117static void
118stop_arm (struct PeerContext *p)
119{
120#if START_ARM
121 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
122 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
123 GNUNET_OS_process_wait (p->arm_proc);
124 GNUNET_OS_process_destroy (p->arm_proc);
125 p->arm_proc = NULL;
126#endif
127 GNUNET_CONFIGURATION_destroy (p->cfg);
128}
129
130 97
131static void 98static void
132end_badly () 99end_badly ()
133{ 100{
134 /* do work here */ 101 /* do work here */
135#if VERBOSE
136 FPRINTF (stderr, "%s", "Ending on an unhappy note.\n"); 102 FPRINTF (stderr, "%s", "Ending on an unhappy note.\n");
137#endif 103 if (get_handle != NULL)
138
139 if ((retry_context.peer_ctx != NULL) &&
140 (retry_context.peer_ctx->get_handle != NULL))
141 { 104 {
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping get request!\n"); 105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping get request!\n");
143 GNUNET_DHT_get_stop (retry_context.peer_ctx->get_handle); 106 GNUNET_DHT_get_stop (get_handle);
144 } 107 }
145 if (retry_context.retry_task != GNUNET_SCHEDULER_NO_TASK) 108 if (retry_context.retry_task != GNUNET_SCHEDULER_NO_TASK)
146 GNUNET_SCHEDULER_cancel (retry_context.retry_task); 109 GNUNET_SCHEDULER_cancel (retry_context.retry_task);
147 GNUNET_DHT_disconnect (p1.dht_handle); 110 GNUNET_DHT_disconnect (dht_handle);
111 dht_handle = NULL;
148 ok = 1; 112 ok = 1;
149} 113}
150 114
@@ -158,8 +122,6 @@ end_badly ()
158static void 122static void
159test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 123test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160{ 124{
161 struct PeerContext *peer = cls;
162
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n"); 125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
164 if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0) 126 if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
165 { 127 {
@@ -168,10 +130,10 @@ test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
168 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); 130 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
169 return; 131 return;
170 } 132 }
171 GNUNET_assert (peer->dht_handle != NULL); 133 GNUNET_assert (dht_handle != NULL);
172 GNUNET_DHT_get_stop (peer->get_handle); 134 GNUNET_DHT_get_stop (get_handle);
173 peer->get_handle = NULL; 135 get_handle = NULL;
174 GNUNET_SCHEDULER_add_now (&end, &p1); 136 GNUNET_SCHEDULER_add_now (&end, NULL);
175} 137}
176 138
177 139
@@ -186,11 +148,11 @@ test_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
186{ 148{
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 "test_get_iterator called (we got a result), stopping get request!\n"); 150 "test_get_iterator called (we got a result), stopping get request!\n");
189 151 GNUNET_SCHEDULER_add_continuation (&test_get_stop, NULL,
190 GNUNET_SCHEDULER_add_continuation (&test_get_stop, &p1,
191 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 152 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
192} 153}
193 154
155
194/** 156/**
195 * Signature of the main function of a task. 157 * Signature of the main function of a task.
196 * 158 *
@@ -200,140 +162,69 @@ test_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
200static void 162static void
201test_get (void *cls, int success) 163test_get (void *cls, int success)
202{ 164{
203 struct PeerContext *peer = cls;
204 struct GNUNET_HashCode hash; 165 struct GNUNET_HashCode hash;
205 166
206 memset (&hash, 42, sizeof (struct GNUNET_HashCode)); 167 memset (&hash, 42, sizeof (struct GNUNET_HashCode));
207 168
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n"); 169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
209 170 GNUNET_assert (dht_handle != NULL);
210 GNUNET_assert (peer->dht_handle != NULL);
211 retry_context.real_timeout = GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT); 171 retry_context.real_timeout = GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT);
212 retry_context.next_timeout = BASE_TIMEOUT; 172 retry_context.next_timeout = BASE_TIMEOUT;
213 173
214 peer->get_handle = 174 get_handle =
215 GNUNET_DHT_get_start (peer->dht_handle, 175 GNUNET_DHT_get_start (dht_handle,
216 GNUNET_BLOCK_TYPE_TEST, &hash, 1, 176 GNUNET_BLOCK_TYPE_TEST, &hash, 1,
217 GNUNET_DHT_RO_NONE, NULL, 0, &test_get_iterator, 177 GNUNET_DHT_RO_NONE, NULL, 0, &test_get_iterator,
218 NULL); 178 NULL);
219 179
220 if (peer->get_handle == NULL) 180 if (get_handle == NULL)
221 { 181 {
222 GNUNET_break (0); 182 GNUNET_break (0);
223 GNUNET_SCHEDULER_cancel (die_task); 183 GNUNET_SCHEDULER_cancel (die_task);
224 die_task = GNUNET_SCHEDULER_add_now (&end_badly, &p1); 184 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
225 return; 185 return;
226 } 186 }
227
228 retry_context.peer_ctx = peer;
229} 187}
230 188
231/** 189
232 * Signature of the main function of a task.
233 *
234 * @param cls closure
235 * @param tc context information (why was this task triggered now)
236 */
237static void 190static void
238test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 191run (void *cls,
192 const struct GNUNET_CONFIGURATION_Handle *cfg,
193 struct GNUNET_TESTING_Peer *peer)
239{ 194{
240 struct PeerContext *peer = cls;
241 struct GNUNET_HashCode hash; 195 struct GNUNET_HashCode hash;
242 char *data; 196 char *data;
243 size_t data_size = 42; 197 size_t data_size = 42;
244 198
245 memset (&hash, 42, sizeof (struct GNUNET_HashCode));
246 data = GNUNET_malloc (data_size);
247 memset (data, 43, data_size);
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
249 peer->dht_handle = GNUNET_DHT_connect (peer->cfg, 100);
250
251 GNUNET_assert (peer->dht_handle != NULL);
252
253 GNUNET_DHT_put (peer->dht_handle, &hash, 1, GNUNET_DHT_RO_NONE,
254 GNUNET_BLOCK_TYPE_TEST, data_size, data,
255 GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT),
256 TOTAL_TIMEOUT, &test_get, &p1);
257 GNUNET_free (data);
258}
259
260static void
261setup_peer (struct PeerContext *p, const char *cfgname)
262{
263 p->cfg = GNUNET_CONFIGURATION_create ();
264#if START_ARM
265 p->arm_proc =
266 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
267 "gnunet-service-arm",
268#if VERBOSE_ARM
269 "-L", "DEBUG",
270#endif
271 "-c", cfgname, NULL);
272#endif
273 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
274
275}
276
277static void
278run (void *cls, char *const *args, const char *cfgfile,
279 const struct GNUNET_CONFIGURATION_Handle *cfg)
280{
281 GNUNET_assert (ok == 1); 199 GNUNET_assert (ok == 1);
282 OKPP; 200 OKPP;
283
284 die_task = 201 die_task =
285 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 202 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
286 (GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, 203 (GNUNET_TIME_UNIT_MINUTES, 1), &end_badly,
287 NULL); 204 NULL);
288 205
289 setup_peer (&p1, "test_dht_api_peer1.conf"); 206 memset (&hash, 42, sizeof (struct GNUNET_HashCode));
290 207 data = GNUNET_malloc (data_size);
291 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 208 memset (data, 43, data_size);
292 (GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1); 209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
293} 210 dht_handle = GNUNET_DHT_connect (cfg, 100);
294 211 GNUNET_assert (dht_handle != NULL);
295static int 212 GNUNET_DHT_put (dht_handle, &hash, 1, GNUNET_DHT_RO_NONE,
296check () 213 GNUNET_BLOCK_TYPE_TEST, data_size, data,
297{ 214 GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT),
298 215 TOTAL_TIMEOUT, &test_get, NULL);
299 char *const argv[] = { "test-dht-api", 216 GNUNET_free (data);
300 "-c",
301 "test_dht_api_data.conf",
302#if VERBOSE
303 "-L", "DEBUG",
304#endif
305 NULL
306 };
307
308 struct GNUNET_GETOPT_CommandLineOption options[] = {
309 GNUNET_GETOPT_OPTION_END
310 };
311
312 ok = 1;
313 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
314 "test-dht-api", "nohelp", options, &run, &ok);
315 stop_arm (&p1);
316 return ok;
317} 217}
318 218
319 219
320int 220int
321main (int argc, char *argv[]) 221main (int argc, char *argv[])
322{ 222{
323 int ret; 223 if (0 != GNUNET_TESTING_peer_run ("test-dht-api",
324 224 "test_dht_api_data.conf",
325 GNUNET_log_setup ("test-dht-api", 225 &run, NULL))
326#if VERBOSE 226 return 1;
327 "DEBUG", 227 return ok;
328#else
329 "WARNING",
330#endif
331 NULL);
332 ret = check ();
333
334 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
335
336 return ret;
337} 228}
338 229
339/* end of test_dht_api.c */ 230/* end of test_dht_api.c */