aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_api_restart_1peer.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2011-11-21 22:02:20 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2011-11-21 22:02:20 +0000
commitffabb052fa64eded69dffa1387b72df0ad1b0211 (patch)
treefb5fb6a36a0f885adcc7c44e84347a1cfa7e75b8 /src/transport/test_transport_api_restart_1peer.c
parentd3871e80fec5e7fcd5ff7cc7ff64284bb7051b38 (diff)
downloadgnunet-ffabb052fa64eded69dffa1387b72df0ad1b0211.tar.gz
gnunet-ffabb052fa64eded69dffa1387b72df0ad1b0211.zip
transport testing:
peer restart functionality + testcases if peers reconnect when restarted
Diffstat (limited to 'src/transport/test_transport_api_restart_1peer.c')
-rw-r--r--src/transport/test_transport_api_restart_1peer.c446
1 files changed, 446 insertions, 0 deletions
diff --git a/src/transport/test_transport_api_restart_1peer.c b/src/transport/test_transport_api_restart_1peer.c
new file mode 100644
index 000000000..fb06c26ab
--- /dev/null
+++ b/src/transport/test_transport_api_restart_1peer.c
@@ -0,0 +1,446 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009, 2010 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 transport/test_transport_api_restart_1peer.c
22 * @brief base test case for transport implementations
23 *
24 * This test case starts 2 peers, connects and exchanges a message
25 * 1 peer is restarted and tested if peers reconnect
26 * C code apparently.
27 */
28#include "platform.h"
29#include "gnunet_common.h"
30#include "gnunet_hello_lib.h"
31#include "gnunet_getopt_lib.h"
32#include "gnunet_os_lib.h"
33#include "gnunet_program_lib.h"
34#include "gnunet_scheduler_lib.h"
35#include "gnunet_transport_service.h"
36#include "transport.h"
37#include "transport-testing.h"
38
39#define VERBOSE GNUNET_EXTRA_LOGGING
40#define VERBOSE_ARM GNUNET_EXTRA_LOGGING
41
42#define START_ARM GNUNET_YES
43
44/**
45 * How long until we give up on transmitting the message?
46 */
47#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
48
49/**
50 * How long until we give up on transmitting the message?
51 */
52#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
53
54#define MTYPE 12345
55
56static char *test_source;
57
58static char *test_plugin;
59
60static char *test_name;
61
62static int ok;
63
64static GNUNET_SCHEDULER_TaskIdentifier die_task;
65
66static GNUNET_SCHEDULER_TaskIdentifier send_task;
67
68struct PeerContext *p1;
69
70struct PeerContext *p2;
71
72static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
73
74struct GNUNET_TRANSPORT_TransmitHandle *th;
75
76struct GNUNET_TRANSPORT_TESTING_handle *tth;
77
78char *cfg_file_p1;
79
80char *cfg_file_p2;
81
82static int restarted;
83
84#if VERBOSE
85#define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
86#else
87#define OKPP do { ok++; } while (0)
88#endif
89
90
91static void
92end ()
93{
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
95
96 if (send_task != GNUNET_SCHEDULER_NO_TASK)
97 GNUNET_SCHEDULER_cancel (send_task);
98
99 if (die_task != GNUNET_SCHEDULER_NO_TASK)
100 GNUNET_SCHEDULER_cancel (die_task);
101
102 if (th != NULL)
103 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
104 th = NULL;
105
106 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
107 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
108}
109
110static void
111end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112{
113 die_task = GNUNET_SCHEDULER_NO_TASK;
114
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
116
117 if (restarted == GNUNET_YES)
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted\n");
119
120 if (restarted == GNUNET_NO)
121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NO restarted\n");
122
123 if (send_task != GNUNET_SCHEDULER_NO_TASK)
124 GNUNET_SCHEDULER_cancel (send_task);
125
126 if (cc != NULL)
127 {
128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
129 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
130 cc = NULL;
131 }
132
133 if (th != NULL)
134 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
135 th = NULL;
136
137 if (p1 != NULL)
138 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
139 if (p2 != NULL)
140 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
141
142 ok = GNUNET_SYSERR;
143}
144
145static
146void restart (struct PeerContext *p , char * cfg_file)
147{
148 GNUNET_assert (p != NULL);
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "Restarting peer %u (`%4s')\n",
151 p->no,
152 GNUNET_i2s (&p->id));
153 GNUNET_TRANSPORT_TESTING_restart_peer (tth, p1, cfg_file);
154 return;
155}
156
157static void
158notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
159 const struct GNUNET_MessageHeader *message,
160 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
161{
162 struct PeerContext *p = cls;
163 struct PeerContext *t = NULL;
164
165 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
166 t = p1;
167 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
168 t = p2;
169 GNUNET_assert (t != NULL);
170
171 char *ps = strdup (GNUNET_i2s (&p->id));
172
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
175 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
176 GNUNET_i2s (&t->id));
177
178 if ((MTYPE == ntohs (message->type)) &&
179 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
180 {
181 restarted = GNUNET_YES;
182 restart (p1, cfg_file_p1);
183 }
184 else
185 {
186 GNUNET_break (0);
187 ok = 1;
188 if (die_task != GNUNET_SCHEDULER_NO_TASK)
189 GNUNET_SCHEDULER_cancel (die_task);
190 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
191 }
192}
193
194
195static size_t
196notify_ready (void *cls, size_t size, void *buf)
197{
198 struct PeerContext *p = cls;
199 struct GNUNET_MessageHeader *hdr;
200
201 th = NULL;
202
203 if (buf == NULL)
204 {
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206 "Timeout occurred while waiting for transmit_ready\n");
207 if (GNUNET_SCHEDULER_NO_TASK != die_task)
208 GNUNET_SCHEDULER_cancel (die_task);
209 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
210 ok = 42;
211 return 0;
212 }
213
214 GNUNET_assert (size >= 256);
215
216 if (buf != NULL)
217 {
218 hdr = buf;
219 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
220 hdr->type = htons (MTYPE);
221 }
222 char *ps = strdup (GNUNET_i2s (&p2->id));
223
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
226 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
227 GNUNET_i2s (&p->id));
228 GNUNET_free (ps);
229 return sizeof (struct GNUNET_MessageHeader);
230}
231
232
233static void
234sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
235{
236 send_task = GNUNET_SCHEDULER_NO_TASK;
237
238 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
239 return;
240 char *receiver_s = strdup (GNUNET_i2s (&p1->id));
241
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
244 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
245 GNUNET_free (receiver_s);
246
247 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0,
248 TIMEOUT_TRANSMIT, &notify_ready,
249 p1);
250}
251
252
253static void
254notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
255 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
256{
257 static int c;
258
259 c++;
260 struct PeerContext *p = cls;
261 struct PeerContext *t = NULL;
262
263 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
264 t = p1;
265 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
266 t = p2;
267 GNUNET_assert (t != NULL);
268
269 char *ps = strdup (GNUNET_i2s (&p->id));
270
271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
272 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
273 t->no, GNUNET_i2s (peer));
274 GNUNET_free (ps);
275
276 if ((restarted == GNUNET_YES) && (c == 4))
277 end ();
278}
279
280
281static void
282notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
283{
284 struct PeerContext *p = cls;
285 char *ps = strdup (GNUNET_i2s (&p->id));
286
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
289 GNUNET_i2s (peer));
290
291 if (th != NULL)
292 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
293 th = NULL;
294}
295
296static void
297testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
298{
299 cc = NULL;
300 char *p1_c = strdup (GNUNET_i2s (&p1->id));
301
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
303 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
304 GNUNET_free (p1_c);
305
306 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
307}
308
309
310
311void
312start_cb (struct PeerContext *p, void *cls)
313{
314 static int started;
315
316 started++;
317
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
319 GNUNET_i2s (&p->id));
320
321 if (started != 2)
322 return;
323
324 char *sender_c = strdup (GNUNET_i2s (&p1->id));
325
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
328 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
329
330 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
331 NULL);
332
333}
334
335static void
336run (void *cls, char *const *args, const char *cfgfile,
337 const struct GNUNET_CONFIGURATION_Handle *cfg)
338{
339 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
340
341 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
342 &notify_receive, &notify_connect,
343 &notify_disconnect, &start_cb,
344 NULL);
345
346 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
347 &notify_receive, &notify_connect,
348 &notify_disconnect, &start_cb,
349 NULL);
350
351 if ((p1 == NULL) || (p2 == NULL))
352 {
353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
354 if (die_task != GNUNET_SCHEDULER_NO_TASK)
355 GNUNET_SCHEDULER_cancel (die_task);
356 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
357 return;
358 }
359}
360
361
362static int
363check ()
364{
365 static char *const argv[] = { "test-transport-api",
366 "-c",
367 "test_transport_api_data.conf",
368#if VERBOSE
369 "-L", "DEBUG",
370#endif
371 NULL
372 };
373 static struct GNUNET_GETOPT_CommandLineOption options[] = {
374 GNUNET_GETOPT_OPTION_END
375 };
376
377#if WRITECONFIG
378 setTransportOptions ("test_transport_api_data.conf");
379#endif
380 send_task = GNUNET_SCHEDULER_NO_TASK;
381
382 ok = 1;
383 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
384 "nohelp", options, &run, &ok);
385
386 return ok;
387}
388
389int
390main (int argc, char *argv[])
391{
392 int ret;
393 int nat_res;
394
395 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
396 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
397 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
398 &test_plugin);
399
400 GNUNET_log_setup (test_name,
401#if VERBOSE
402 "DEBUG",
403#else
404 "WARNING",
405#endif
406 NULL);
407
408 tth = GNUNET_TRANSPORT_TESTING_init ();
409
410 if ((strcmp (test_plugin, "tcp_nat") == 0) ||
411 (strcmp (test_plugin, "udp_nat") == 0))
412 {
413 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
414 if (GNUNET_NO == nat_res)
415 {
416 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
417 "gnunet-nat-server", "SUID not set");
418 return 0;
419 }
420 if (GNUNET_SYSERR == nat_res)
421 {
422 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
423 "gnunet-nat-server", "file not found");
424 return 0;
425 }
426 }
427
428 GNUNET_asprintf(&cfg_file_p1,"test_transport_api_tcp_peer1.conf");
429 GNUNET_asprintf(&cfg_file_p2,"test_transport_api_tcp_peer2.conf");
430
431
432 ret = check ();
433
434 GNUNET_free (cfg_file_p1);
435 GNUNET_free (cfg_file_p2);
436
437 GNUNET_free (test_source);
438 GNUNET_free (test_plugin);
439 GNUNET_free (test_name);
440
441 GNUNET_TRANSPORT_TESTING_done (tth);
442
443 return ret;
444}
445
446/* end of test_transport_api_restart_1peer.c */