diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2011-11-21 22:02:20 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2011-11-21 22:02:20 +0000 |
commit | ffabb052fa64eded69dffa1387b72df0ad1b0211 (patch) | |
tree | fb5fb6a36a0f885adcc7c44e84347a1cfa7e75b8 /src | |
parent | d3871e80fec5e7fcd5ff7cc7ff64284bb7051b38 (diff) | |
download | gnunet-ffabb052fa64eded69dffa1387b72df0ad1b0211.tar.gz gnunet-ffabb052fa64eded69dffa1387b72df0ad1b0211.zip |
transport testing:
peer restart functionality
+ testcases if peers reconnect when restarted
Diffstat (limited to 'src')
-rw-r--r-- | src/transport/Makefile.am | 22 | ||||
-rw-r--r-- | src/transport/test_transport_api_restart_1peer.c | 446 | ||||
-rw-r--r-- | src/transport/test_transport_api_restart_2peers.c | 447 | ||||
-rw-r--r-- | src/transport/transport-testing.c | 101 | ||||
-rw-r--r-- | src/transport/transport-testing.h | 11 |
5 files changed, 1024 insertions, 3 deletions
diff --git a/src/transport/Makefile.am b/src/transport/Makefile.am index 32fe682cc..4b95545ac 100644 --- a/src/transport/Makefile.am +++ b/src/transport/Makefile.am | |||
@@ -260,6 +260,8 @@ check_PROGRAMS = \ | |||
260 | test_transport_api_disconnect_tcp \ | 260 | test_transport_api_disconnect_tcp \ |
261 | test_transport_api_bidirectional_connect \ | 261 | test_transport_api_bidirectional_connect \ |
262 | test_transport_api_tcp \ | 262 | test_transport_api_tcp \ |
263 | test_transport_api_restart_1peer \ | ||
264 | test_transport_api_restart_2peers \ | ||
263 | test_transport_api_timeout_tcp \ | 265 | test_transport_api_timeout_tcp \ |
264 | test_transport_api_limited_sockets_tcp \ | 266 | test_transport_api_limited_sockets_tcp \ |
265 | test_transport_api_tcp_nat \ | 267 | test_transport_api_tcp_nat \ |
@@ -299,6 +301,8 @@ TESTS = \ | |||
299 | test_transport_api_disconnect_tcp \ | 301 | test_transport_api_disconnect_tcp \ |
300 | test_transport_api_bidirectional_connect \ | 302 | test_transport_api_bidirectional_connect \ |
301 | test_transport_api_tcp \ | 303 | test_transport_api_tcp \ |
304 | test_transport_api_restart_1peer \ | ||
305 | test_transport_api_restart_2peers \ | ||
302 | test_transport_api_timeout_tcp \ | 306 | test_transport_api_timeout_tcp \ |
303 | test_transport_api_limited_sockets_tcp \ | 307 | test_transport_api_limited_sockets_tcp \ |
304 | test_transport_api_tcp_nat \ | 308 | test_transport_api_tcp_nat \ |
@@ -381,6 +385,24 @@ test_transport_api_bidirectional_connect_LDADD = \ | |||
381 | $(top_builddir)/src/hello/libgnunethello.la \ | 385 | $(top_builddir)/src/hello/libgnunethello.la \ |
382 | $(top_builddir)/src/util/libgnunetutil.la \ | 386 | $(top_builddir)/src/util/libgnunetutil.la \ |
383 | $(top_builddir)/src/transport/libgnunettransporttesting.la | 387 | $(top_builddir)/src/transport/libgnunettransporttesting.la |
388 | |||
389 | test_transport_api_restart_1peer_SOURCES = \ | ||
390 | test_transport_api_restart_1peer.c | ||
391 | test_transport_api_restart_1peer_LDADD = \ | ||
392 | $(top_builddir)/src/transport/libgnunettransport.la \ | ||
393 | $(top_builddir)/src/hello/libgnunethello.la \ | ||
394 | $(top_builddir)/src/statistics/libgnunetstatistics.la \ | ||
395 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
396 | $(top_builddir)/src/transport/libgnunettransporttesting.la | ||
397 | |||
398 | test_transport_api_restart_2peers_SOURCES = \ | ||
399 | test_transport_api_restart_2peers.c | ||
400 | test_transport_api_restart_2peers_LDADD = \ | ||
401 | $(top_builddir)/src/transport/libgnunettransport.la \ | ||
402 | $(top_builddir)/src/hello/libgnunethello.la \ | ||
403 | $(top_builddir)/src/statistics/libgnunetstatistics.la \ | ||
404 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
405 | $(top_builddir)/src/transport/libgnunettransporttesting.la | ||
384 | 406 | ||
385 | test_transport_api_limited_sockets_tcp_SOURCES = \ | 407 | test_transport_api_limited_sockets_tcp_SOURCES = \ |
386 | test_transport_api_limited_sockets.c | 408 | test_transport_api_limited_sockets.c |
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 | |||
56 | static char *test_source; | ||
57 | |||
58 | static char *test_plugin; | ||
59 | |||
60 | static char *test_name; | ||
61 | |||
62 | static int ok; | ||
63 | |||
64 | static GNUNET_SCHEDULER_TaskIdentifier die_task; | ||
65 | |||
66 | static GNUNET_SCHEDULER_TaskIdentifier send_task; | ||
67 | |||
68 | struct PeerContext *p1; | ||
69 | |||
70 | struct PeerContext *p2; | ||
71 | |||
72 | static GNUNET_TRANSPORT_TESTING_ConnectRequest cc; | ||
73 | |||
74 | struct GNUNET_TRANSPORT_TransmitHandle *th; | ||
75 | |||
76 | struct GNUNET_TRANSPORT_TESTING_handle *tth; | ||
77 | |||
78 | char *cfg_file_p1; | ||
79 | |||
80 | char *cfg_file_p2; | ||
81 | |||
82 | static 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 | |||
91 | static void | ||
92 | end () | ||
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 | |||
110 | static void | ||
111 | end_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 | |||
145 | static | ||
146 | void 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 | |||
157 | static void | ||
158 | notify_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 | |||
195 | static size_t | ||
196 | notify_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 | |||
233 | static void | ||
234 | sendtask (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, ¬ify_ready, | ||
249 | p1); | ||
250 | } | ||
251 | |||
252 | |||
253 | static void | ||
254 | notify_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 | |||
281 | static void | ||
282 | notify_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 | |||
296 | static void | ||
297 | testing_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 | |||
311 | void | ||
312 | start_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 | |||
335 | static void | ||
336 | run (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 | ¬ify_receive, ¬ify_connect, | ||
343 | ¬ify_disconnect, &start_cb, | ||
344 | NULL); | ||
345 | |||
346 | p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2, | ||
347 | ¬ify_receive, ¬ify_connect, | ||
348 | ¬ify_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 | |||
362 | static int | ||
363 | check () | ||
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 | |||
389 | int | ||
390 | main (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 */ | ||
diff --git a/src/transport/test_transport_api_restart_2peers.c b/src/transport/test_transport_api_restart_2peers.c new file mode 100644 index 000000000..af9b46385 --- /dev/null +++ b/src/transport/test_transport_api_restart_2peers.c | |||
@@ -0,0 +1,447 @@ | |||
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_2peers.c | ||
22 | * @brief base test case for transport implementations | ||
23 | * | ||
24 | * This test case starts 2 peers, connects and exchanges a message | ||
25 | * boths peer are 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 | |||
56 | static char *test_source; | ||
57 | |||
58 | static char *test_plugin; | ||
59 | |||
60 | static char *test_name; | ||
61 | |||
62 | static int ok; | ||
63 | |||
64 | static GNUNET_SCHEDULER_TaskIdentifier die_task; | ||
65 | |||
66 | static GNUNET_SCHEDULER_TaskIdentifier send_task; | ||
67 | |||
68 | struct PeerContext *p1; | ||
69 | |||
70 | struct PeerContext *p2; | ||
71 | |||
72 | static GNUNET_TRANSPORT_TESTING_ConnectRequest cc; | ||
73 | |||
74 | struct GNUNET_TRANSPORT_TransmitHandle *th; | ||
75 | |||
76 | struct GNUNET_TRANSPORT_TESTING_handle *tth; | ||
77 | |||
78 | char *cfg_file_p1; | ||
79 | |||
80 | char *cfg_file_p2; | ||
81 | |||
82 | static 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 | |||
91 | static void | ||
92 | end () | ||
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 | |||
110 | static void | ||
111 | end_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, "Peers were restarted\n"); | ||
119 | |||
120 | if (restarted == GNUNET_NO) | ||
121 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers were NOT 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 | |||
145 | static | ||
146 | void 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, p, cfg_file); | ||
154 | return; | ||
155 | } | ||
156 | |||
157 | static void | ||
158 | notify_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 | restart (p2, cfg_file_p2); | ||
184 | } | ||
185 | else | ||
186 | { | ||
187 | GNUNET_break (0); | ||
188 | ok = 1; | ||
189 | if (die_task != GNUNET_SCHEDULER_NO_TASK) | ||
190 | GNUNET_SCHEDULER_cancel (die_task); | ||
191 | die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); | ||
192 | } | ||
193 | } | ||
194 | |||
195 | |||
196 | static size_t | ||
197 | notify_ready (void *cls, size_t size, void *buf) | ||
198 | { | ||
199 | struct PeerContext *p = cls; | ||
200 | struct GNUNET_MessageHeader *hdr; | ||
201 | |||
202 | th = NULL; | ||
203 | |||
204 | if (buf == NULL) | ||
205 | { | ||
206 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
207 | "Timeout occurred while waiting for transmit_ready\n"); | ||
208 | if (GNUNET_SCHEDULER_NO_TASK != die_task) | ||
209 | GNUNET_SCHEDULER_cancel (die_task); | ||
210 | die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); | ||
211 | ok = 42; | ||
212 | return 0; | ||
213 | } | ||
214 | |||
215 | GNUNET_assert (size >= 256); | ||
216 | |||
217 | if (buf != NULL) | ||
218 | { | ||
219 | hdr = buf; | ||
220 | hdr->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
221 | hdr->type = htons (MTYPE); | ||
222 | } | ||
223 | char *ps = strdup (GNUNET_i2s (&p2->id)); | ||
224 | |||
225 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
226 | "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n", | ||
227 | p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no, | ||
228 | GNUNET_i2s (&p->id)); | ||
229 | GNUNET_free (ps); | ||
230 | return sizeof (struct GNUNET_MessageHeader); | ||
231 | } | ||
232 | |||
233 | |||
234 | static void | ||
235 | sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
236 | { | ||
237 | send_task = GNUNET_SCHEDULER_NO_TASK; | ||
238 | |||
239 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | ||
240 | return; | ||
241 | char *receiver_s = strdup (GNUNET_i2s (&p1->id)); | ||
242 | |||
243 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
244 | "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n", | ||
245 | p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s); | ||
246 | GNUNET_free (receiver_s); | ||
247 | |||
248 | th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0, | ||
249 | TIMEOUT_TRANSMIT, ¬ify_ready, | ||
250 | p1); | ||
251 | } | ||
252 | |||
253 | |||
254 | static void | ||
255 | notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
256 | const struct GNUNET_ATS_Information *ats, uint32_t ats_count) | ||
257 | { | ||
258 | static int c; | ||
259 | |||
260 | c++; | ||
261 | struct PeerContext *p = cls; | ||
262 | struct PeerContext *t = NULL; | ||
263 | |||
264 | if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity))) | ||
265 | t = p1; | ||
266 | if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity))) | ||
267 | t = p2; | ||
268 | GNUNET_assert (t != NULL); | ||
269 | |||
270 | char *ps = strdup (GNUNET_i2s (&p->id)); | ||
271 | |||
272 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
273 | "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps, | ||
274 | t->no, GNUNET_i2s (peer)); | ||
275 | GNUNET_free (ps); | ||
276 | |||
277 | if ((restarted == GNUNET_YES) && (c == 4)) | ||
278 | end (); | ||
279 | } | ||
280 | |||
281 | |||
282 | static void | ||
283 | notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
284 | { | ||
285 | struct PeerContext *p = cls; | ||
286 | char *ps = strdup (GNUNET_i2s (&p->id)); | ||
287 | |||
288 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
289 | "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps, | ||
290 | GNUNET_i2s (peer)); | ||
291 | |||
292 | if (th != NULL) | ||
293 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
294 | th = NULL; | ||
295 | } | ||
296 | |||
297 | static void | ||
298 | testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls) | ||
299 | { | ||
300 | cc = NULL; | ||
301 | char *p1_c = strdup (GNUNET_i2s (&p1->id)); | ||
302 | |||
303 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n", | ||
304 | p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id)); | ||
305 | GNUNET_free (p1_c); | ||
306 | |||
307 | send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL); | ||
308 | } | ||
309 | |||
310 | |||
311 | |||
312 | void | ||
313 | start_cb (struct PeerContext *p, void *cls) | ||
314 | { | ||
315 | static int started; | ||
316 | |||
317 | started++; | ||
318 | |||
319 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no, | ||
320 | GNUNET_i2s (&p->id)); | ||
321 | |||
322 | if (started != 2) | ||
323 | return; | ||
324 | |||
325 | char *sender_c = strdup (GNUNET_i2s (&p1->id)); | ||
326 | |||
327 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
328 | "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n", | ||
329 | p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id)); | ||
330 | |||
331 | cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb, | ||
332 | NULL); | ||
333 | |||
334 | } | ||
335 | |||
336 | static void | ||
337 | run (void *cls, char *const *args, const char *cfgfile, | ||
338 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
339 | { | ||
340 | die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL); | ||
341 | |||
342 | p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1, | ||
343 | ¬ify_receive, ¬ify_connect, | ||
344 | ¬ify_disconnect, &start_cb, | ||
345 | NULL); | ||
346 | |||
347 | p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2, | ||
348 | ¬ify_receive, ¬ify_connect, | ||
349 | ¬ify_disconnect, &start_cb, | ||
350 | NULL); | ||
351 | |||
352 | if ((p1 == NULL) || (p2 == NULL)) | ||
353 | { | ||
354 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n"); | ||
355 | if (die_task != GNUNET_SCHEDULER_NO_TASK) | ||
356 | GNUNET_SCHEDULER_cancel (die_task); | ||
357 | die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); | ||
358 | return; | ||
359 | } | ||
360 | } | ||
361 | |||
362 | |||
363 | static int | ||
364 | check () | ||
365 | { | ||
366 | static char *const argv[] = { "test-transport-api", | ||
367 | "-c", | ||
368 | "test_transport_api_data.conf", | ||
369 | #if VERBOSE | ||
370 | "-L", "DEBUG", | ||
371 | #endif | ||
372 | NULL | ||
373 | }; | ||
374 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
375 | GNUNET_GETOPT_OPTION_END | ||
376 | }; | ||
377 | |||
378 | #if WRITECONFIG | ||
379 | setTransportOptions ("test_transport_api_data.conf"); | ||
380 | #endif | ||
381 | send_task = GNUNET_SCHEDULER_NO_TASK; | ||
382 | |||
383 | ok = 1; | ||
384 | GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name, | ||
385 | "nohelp", options, &run, &ok); | ||
386 | |||
387 | return ok; | ||
388 | } | ||
389 | |||
390 | int | ||
391 | main (int argc, char *argv[]) | ||
392 | { | ||
393 | int ret; | ||
394 | int nat_res; | ||
395 | |||
396 | GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name); | ||
397 | GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source); | ||
398 | GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source, | ||
399 | &test_plugin); | ||
400 | |||
401 | GNUNET_log_setup (test_name, | ||
402 | #if VERBOSE | ||
403 | "DEBUG", | ||
404 | #else | ||
405 | "WARNING", | ||
406 | #endif | ||
407 | NULL); | ||
408 | |||
409 | tth = GNUNET_TRANSPORT_TESTING_init (); | ||
410 | |||
411 | if ((strcmp (test_plugin, "tcp_nat") == 0) || | ||
412 | (strcmp (test_plugin, "udp_nat") == 0)) | ||
413 | { | ||
414 | nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server"); | ||
415 | if (GNUNET_NO == nat_res) | ||
416 | { | ||
417 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n", | ||
418 | "gnunet-nat-server", "SUID not set"); | ||
419 | return 0; | ||
420 | } | ||
421 | if (GNUNET_SYSERR == nat_res) | ||
422 | { | ||
423 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n", | ||
424 | "gnunet-nat-server", "file not found"); | ||
425 | return 0; | ||
426 | } | ||
427 | } | ||
428 | |||
429 | GNUNET_asprintf(&cfg_file_p1,"test_transport_api_tcp_peer1.conf"); | ||
430 | GNUNET_asprintf(&cfg_file_p2,"test_transport_api_tcp_peer2.conf"); | ||
431 | |||
432 | |||
433 | ret = check (); | ||
434 | |||
435 | GNUNET_free (cfg_file_p1); | ||
436 | GNUNET_free (cfg_file_p2); | ||
437 | |||
438 | GNUNET_free (test_source); | ||
439 | GNUNET_free (test_plugin); | ||
440 | GNUNET_free (test_name); | ||
441 | |||
442 | GNUNET_TRANSPORT_TESTING_done (tth); | ||
443 | |||
444 | return ret; | ||
445 | } | ||
446 | |||
447 | /* end of test_transport_api_restart_2peers.c */ | ||
diff --git a/src/transport/transport-testing.c b/src/transport/transport-testing.c index 6892b2556..28d0db256 100644 --- a/src/transport/transport-testing.c +++ b/src/transport/transport-testing.c | |||
@@ -283,11 +283,10 @@ GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle | |||
283 | struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext)); | 283 | struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext)); |
284 | 284 | ||
285 | p->cfg = GNUNET_CONFIGURATION_create (); | 285 | p->cfg = GNUNET_CONFIGURATION_create (); |
286 | |||
287 | GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); | 286 | GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); |
288 | 287 | ||
289 | 288 | if (GNUNET_CONFIGURATION_have_value (p-> | |
290 | if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME")) | 289 | cfg, "PATHS", "SERVICEHOME")) |
291 | GNUNET_assert (GNUNET_OK == | 290 | GNUNET_assert (GNUNET_OK == |
292 | GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", | 291 | GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", |
293 | "SERVICEHOME", | 292 | "SERVICEHOME", |
@@ -350,6 +349,100 @@ GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle | |||
350 | } | 349 | } |
351 | 350 | ||
352 | /** | 351 | /** |
352 | * Restart the given peer | ||
353 | * @param tth testing handle | ||
354 | * @param p the peer | ||
355 | */ | ||
356 | void | ||
357 | GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth, | ||
358 | struct PeerContext *p, | ||
359 | const char *cfgname) | ||
360 | { | ||
361 | GNUNET_assert (p != NULL); | ||
362 | /* shutdown */ | ||
363 | #if VERBOSE | ||
364 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing", | ||
365 | "Stopping peer %u (`%s')\n", p->no, | ||
366 | GNUNET_i2s (&p->id)); | ||
367 | #endif | ||
368 | if (p->ghh != NULL) | ||
369 | GNUNET_TRANSPORT_get_hello_cancel (p->ghh); | ||
370 | p->ghh = NULL; | ||
371 | |||
372 | if (p->th != NULL) | ||
373 | GNUNET_TRANSPORT_disconnect (p->th); | ||
374 | |||
375 | if (NULL != p->arm_proc) | ||
376 | { | ||
377 | if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM)) | ||
378 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
379 | GNUNET_OS_process_wait (p->arm_proc); | ||
380 | GNUNET_OS_process_close (p->arm_proc); | ||
381 | p->arm_proc = NULL; | ||
382 | } | ||
383 | if (p->hello != NULL) | ||
384 | GNUNET_free (p->hello); | ||
385 | p->hello = NULL; | ||
386 | |||
387 | if (p->cfg != NULL) | ||
388 | GNUNET_CONFIGURATION_destroy (p->cfg); | ||
389 | p->cfg = NULL; | ||
390 | |||
391 | /* start */ | ||
392 | #if VERBOSE | ||
393 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing", | ||
394 | "Restarting peer %u (`%s')\n", p->no, | ||
395 | GNUNET_i2s (&p->id)); | ||
396 | #endif | ||
397 | struct GNUNET_DISK_FileHandle *fn; | ||
398 | |||
399 | GNUNET_assert (tth != NULL); | ||
400 | if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO) | ||
401 | { | ||
402 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing", | ||
403 | "File not found: `%s' \n", cfgname); | ||
404 | return; | ||
405 | } | ||
406 | |||
407 | p->cfg = GNUNET_CONFIGURATION_create (); | ||
408 | GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); | ||
409 | if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME")) | ||
410 | GNUNET_assert (GNUNET_OK == | ||
411 | GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", | ||
412 | "SERVICEHOME", | ||
413 | &p->servicehome)); | ||
414 | |||
415 | GNUNET_assert (p->hostkeyfile != NULL); | ||
416 | GNUNET_asprintf (&p->hostkeyfile, "%s/.hostkey", p->servicehome); | ||
417 | fn = GNUNET_DISK_file_open (p->hostkeyfile, | ||
418 | GNUNET_DISK_OPEN_READWRITE | | ||
419 | GNUNET_DISK_OPEN_CREATE, | ||
420 | GNUNET_DISK_PERM_USER_READ | | ||
421 | GNUNET_DISK_PERM_USER_WRITE); | ||
422 | GNUNET_assert (fn != NULL); | ||
423 | GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn)); | ||
424 | |||
425 | p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm", | ||
426 | "gnunet-service-arm", "-c", cfgname, | ||
427 | #if VERBOSE_PEERS | ||
428 | "-L", "DEBUG", | ||
429 | #else | ||
430 | "-L", "ERROR", | ||
431 | #endif | ||
432 | NULL); | ||
433 | |||
434 | p->th = | ||
435 | GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive, | ||
436 | ¬ify_connect, ¬ify_disconnect); | ||
437 | GNUNET_assert (p->th != NULL); | ||
438 | |||
439 | p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p); | ||
440 | GNUNET_assert (p->ghh != NULL); | ||
441 | |||
442 | return ; | ||
443 | } | ||
444 | |||
445 | /** | ||
353 | * shutdown the given peer | 446 | * shutdown the given peer |
354 | * @param p the peer | 447 | * @param p the peer |
355 | */ | 448 | */ |
@@ -389,9 +482,11 @@ GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth, | |||
389 | 482 | ||
390 | if (p->hello != NULL) | 483 | if (p->hello != NULL) |
391 | GNUNET_free (p->hello); | 484 | GNUNET_free (p->hello); |
485 | p->hello = NULL; | ||
392 | 486 | ||
393 | if (p->cfg != NULL) | 487 | if (p->cfg != NULL) |
394 | GNUNET_CONFIGURATION_destroy (p->cfg); | 488 | GNUNET_CONFIGURATION_destroy (p->cfg); |
489 | p->cfg = NULL; | ||
395 | 490 | ||
396 | GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p); | 491 | GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p); |
397 | 492 | ||
diff --git a/src/transport/transport-testing.h b/src/transport/transport-testing.h index cec7eb724..32641d8bc 100644 --- a/src/transport/transport-testing.h +++ b/src/transport/transport-testing.h | |||
@@ -58,6 +58,7 @@ typedef void (*GNUNET_TRANSPORT_TESTING_connect_cb) (struct PeerContext * p1, | |||
58 | void *cls); | 58 | void *cls); |
59 | 59 | ||
60 | 60 | ||
61 | |||
61 | struct GNUNET_TRANSPORT_TESTING_handle; | 62 | struct GNUNET_TRANSPORT_TESTING_handle; |
62 | 63 | ||
63 | /** | 64 | /** |
@@ -160,6 +161,16 @@ GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth, | |||
160 | 161 | ||
161 | 162 | ||
162 | /** | 163 | /** |
164 | * Restart the given peer | ||
165 | * @param tth testing handle | ||
166 | * @param p the peer | ||
167 | */ | ||
168 | void | ||
169 | GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth, | ||
170 | struct PeerContext *p, | ||
171 | const char *cfgname); | ||
172 | |||
173 | /** | ||
163 | * Connect the given peers and call the callback when both peers report the | 174 | * Connect the given peers and call the callback when both peers report the |
164 | * inbound connection. Remarks: start_peer's notify_connect callback can be called | 175 | * inbound connection. Remarks: start_peer's notify_connect callback can be called |
165 | * before. | 176 | * before. |