2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file transport/test_transport_api.c
22 * @brief base test case for transport implementations
24 * This test case serves as a base for tcp, udp, and udp-nat
25 * transport test cases. Based on the executable being run
26 * the correct test case will be performed. Conservation of
30 #include "gnunet_common.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "transport.h"
38 #include "transport-testing.h"
40 #define VERBOSE GNUNET_NO
42 #define VERBOSE_ARM GNUNET_NO
44 #define START_ARM GNUNET_YES
47 * How long until we give up on transmitting the message?
49 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
52 * How long until we give up on transmitting the message?
54 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
58 static struct PeerContext p1;
60 static struct PeerContext p2;
66 static GNUNET_SCHEDULER_TaskIdentifier die_task;
68 static GNUNET_SCHEDULER_TaskIdentifier tct;
72 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
74 #define OKPP do { ok++; } while (0)
80 stop_arm (struct PeerContext *p)
83 if (NULL != p->arm_proc)
85 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
86 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
87 GNUNET_OS_process_wait (p->arm_proc);
88 GNUNET_OS_process_close (p->arm_proc);
92 GNUNET_CONFIGURATION_destroy (p->cfg);
99 exchange_hello_last (void *cls,
100 const struct GNUNET_MessageHeader *message)
102 struct PeerContext *me = cls;
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105 "Exchanging HELLO of size %d with peer (%s)!\n",
106 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
107 GNUNET_i2s (&me->id));
108 GNUNET_assert (message != NULL);
109 GNUNET_assert (GNUNET_OK ==
110 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
112 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
117 exchange_hello (void *cls,
118 const struct GNUNET_MessageHeader *message)
120 struct PeerContext *me = cls;
122 GNUNET_assert (message != NULL);
123 GNUNET_assert (GNUNET_OK ==
124 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "Exchanging HELLO of size %d from peer %s!\n",
128 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
129 GNUNET_i2s (&me->id));
130 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
135 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
139 die_task = GNUNET_SCHEDULER_NO_TASK;
140 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
141 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
142 GNUNET_TRANSPORT_disconnect (p1.th);
143 GNUNET_TRANSPORT_disconnect (p2.th);
144 if (GNUNET_SCHEDULER_NO_TASK != tct)
146 GNUNET_SCHEDULER_cancel (tct);
147 tct = GNUNET_SCHEDULER_NO_TASK;
153 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
156 die_task = GNUNET_SCHEDULER_NO_TASK;
157 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
158 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
159 GNUNET_TRANSPORT_disconnect (p1.th);
160 GNUNET_TRANSPORT_disconnect (p2.th);
161 if (GNUNET_SCHEDULER_NO_TASK != tct)
163 GNUNET_SCHEDULER_cancel (tct);
164 tct = GNUNET_SCHEDULER_NO_TASK;
170 notify_connect (void *cls,
171 const struct GNUNET_PeerIdentity *peer,
172 const struct GNUNET_TRANSPORT_ATS_Information *ats,
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "Peer `%4s' connected to us (%p)!\n",
181 GNUNET_assert (ok >= 2);
184 if (GNUNET_SCHEDULER_NO_TASK != die_task)
185 GNUNET_SCHEDULER_cancel (die_task);
186 if (GNUNET_SCHEDULER_NO_TASK != tct)
187 GNUNET_SCHEDULER_cancel (tct);
188 tct = GNUNET_SCHEDULER_NO_TASK;
189 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT,
190 &end_normally, NULL);
196 notify_disconnect (void *cls,
197 const struct GNUNET_PeerIdentity *peer)
200 (GNUNET_SCHEDULER_NO_TASK != die_task) )
202 GNUNET_SCHEDULER_cancel (die_task);
203 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Peer `%4s' disconnected (%p)!\n",
207 GNUNET_i2s (peer), cls);
212 setup_peer (struct PeerContext *p,
215 p->cfg = GNUNET_CONFIGURATION_create ();
217 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
218 if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
219 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
220 GNUNET_DISK_directory_remove (p->servicehome);
223 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
224 "gnunet-service-arm",
230 if (0 == stat (cert_file_p1, &sbuf ))
232 if (0 == remove(cert_file_p1))
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "Successfully removed existing certificate file `%s'\n",cert_file_p1);
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 "Failed to remove certfile `%s'\n",cert_file_p1);
240 if (0 == stat (key_file_p1, &sbuf ))
242 if (0 == remove(key_file_p1))
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244 "Successfully removed private key file `%s'\n",key_file_p1);
246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
247 "Failed to private key file `%s'\n",key_file_p1);
250 if (0 == stat (cert_file_p2, &sbuf ))
252 if (0 == remove(cert_file_p2))
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Successfully removed existing certificate file `%s'\n",cert_file_p2);
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
257 "Failed to remove certfile `%s'\n",cert_file_p2);
260 if (0 == stat (key_file_p2, &sbuf ))
262 if (0 == remove(key_file_p2))
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "Successfully removed private key file `%s'\n",key_file_p2);
266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
267 "Failed to private key file `%s'\n",key_file_p2);
269 GNUNET_free(key_file_p1);
270 GNUNET_free(key_file_p2);
271 GNUNET_free(cert_file_p1);
272 GNUNET_free(cert_file_p2);
275 "-c", cfgname, NULL);
279 if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
280 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
282 p->th = GNUNET_TRANSPORT_connect (p->cfg,
285 ¬ify_connect, ¬ify_disconnect);
286 GNUNET_assert (p->th != NULL);
291 try_connect (void *cls,
292 const struct GNUNET_SCHEDULER_TaskContext *tc)
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Asking peers to connect...\n");
296 /* FIXME: 'pX.id' may still be all-zeros here... */
297 GNUNET_TRANSPORT_try_connect (p2.th,
299 GNUNET_TRANSPORT_try_connect (p1.th,
301 tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
310 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
312 GNUNET_assert (ok == 1);
314 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
318 setup_peer (&p1, "test_transport_api_multiaddress_tcp_peer1.conf");
319 setup_peer (&p2, "test_transport_api_multiaddress_tcp_peer2.conf");
321 GNUNET_assert(p1.th != NULL);
322 GNUNET_assert(p2.th != NULL);
324 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
325 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
326 tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
332 static char *const argv[] = { "test-transport-api",
334 "test_transport_api_data.conf",
340 static struct GNUNET_GETOPT_CommandLineOption options[] = {
341 GNUNET_GETOPT_OPTION_END
345 setTransportOptions("test_transport_api_data.conf");
348 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
349 argv, "test-transport-api", "nohelp",
354 if ((p1.servicehome != NULL) && (p2.servicehome != NULL))
356 GNUNET_DISK_directory_remove (p1.servicehome);
357 GNUNET_DISK_directory_remove (p2.servicehome);
358 GNUNET_free(p1.servicehome);
359 GNUNET_free(p2.servicehome);
366 main (int argc, char *argv[])
370 GNUNET_log_setup ("test-transport-api",
378 if (strstr(argv[0], "tcp") != NULL)
388 /* end of test_transport_api.c */