2 This file is part of GNUnet.
3 (C) 2009 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 2, 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 testcase for transport_api.c
25 #include "gnunet_common.h"
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_program_lib.h"
30 #include "gnunet_scheduler_lib.h"
31 #include "gnunet_transport_service.h"
32 #include "transport.h"
34 #define VERBOSE GNUNET_NO
36 #define VERBOSE_ARM GNUNET_NO
38 #define START_ARM GNUNET_YES
41 * How long until we give up on transmitting the message?
43 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
49 struct GNUNET_CONFIGURATION_Handle *cfg;
50 struct GNUNET_TRANSPORT_Handle *th;
51 struct GNUNET_PeerIdentity id;
57 static struct PeerContext p1;
59 static struct PeerContext p2;
61 static struct GNUNET_SCHEDULER_Handle *sched;
70 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
72 #define OKPP do { ok++; } while (0)
80 GNUNET_assert (ok == 8);
81 GNUNET_TRANSPORT_disconnect (p1.th);
82 GNUNET_TRANSPORT_disconnect (p2.th);
88 * Function called by the transport for each received message.
91 * @param latency estimated latency for communicating with the
93 * @param peer (claimed) identity of the other peer
94 * @param message the message
97 notify_receive (void *cls,
98 struct GNUNET_TIME_Relative latency,
99 const struct GNUNET_PeerIdentity *peer,
100 const struct GNUNET_MessageHeader *message)
102 GNUNET_assert (ok == 7);
104 GNUNET_assert (MTYPE == ntohs (message->type));
105 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
106 ntohs (message->size));
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
114 * Function called to notify transport users that another
115 * peer connected to us.
118 * @param transport the transport service handle
119 * @param peer the peer that disconnected
120 * @param latency current latency of the connection
123 notify_connect (void *cls,
124 const struct GNUNET_PeerIdentity *peer,
125 struct GNUNET_TIME_Relative latency)
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
129 GNUNET_assert ((ok >= 1) && (ok <= 6));
135 * Function called to notify transport users that another
136 * peer disconnected from us.
139 * @param transport the transport service handle
140 * @param peer the peer that disconnected
143 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
150 setup_peer (struct PeerContext *p, const char *cfgname)
152 p->cfg = GNUNET_CONFIGURATION_create ();
154 p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
155 "gnunet-service-arm",
159 "-c", cfgname, NULL);
161 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
162 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
165 ¬ify_connect, ¬ify_disconnect);
166 GNUNET_assert (p->th != NULL);
171 notify_ready (void *cls, size_t size, void *buf)
173 struct GNUNET_MessageHeader *hdr;
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "Transmitting message to peer (%p) - %u!\n", cls, size);
177 GNUNET_assert (size >= 256);
178 GNUNET_assert ((ok >= 5) && (ok <= 6));
181 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
182 hdr->type = htons (MTYPE);
183 return sizeof (struct GNUNET_MessageHeader);
188 exchange_hello_last (void *cls,
189 struct GNUNET_TIME_Relative latency,
190 const struct GNUNET_PeerIdentity *peer,
191 const struct GNUNET_MessageHeader *message)
193 struct PeerContext *me = cls;
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 "Exchanging HELLO with peer (%p)!\n", cls);
197 GNUNET_assert (ok >= 3);
199 GNUNET_assert (message != NULL);
200 GNUNET_assert (GNUNET_OK ==
201 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
203 GNUNET_TRANSPORT_offer_hello (p1.th, message);
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 "Finished exchanging HELLOs, now waiting for transmission!\n");
206 /* both HELLOs exchanged, get ready to test transmission! */
207 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
209 256, 0, TIMEOUT, ¬ify_ready,
215 exchange_hello (void *cls,
216 struct GNUNET_TIME_Relative latency,
217 const struct GNUNET_PeerIdentity *peer,
218 const struct GNUNET_MessageHeader *message)
220 struct PeerContext *me = cls;
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Exchanging HELLO with peer (%p)!\n", cls);
224 GNUNET_assert (ok >= 2);
226 GNUNET_assert (message != NULL);
227 GNUNET_assert (GNUNET_OK ==
228 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
230 GNUNET_TRANSPORT_get_hello (p2.th, TIMEOUT, &exchange_hello_last, &p2);
236 struct GNUNET_SCHEDULER_Handle *s,
238 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
240 GNUNET_assert (ok == 1);
243 setup_peer (&p1, "test_transport_api_peer1.conf");
244 setup_peer (&p2, "test_transport_api_peer2.conf");
245 GNUNET_TRANSPORT_get_hello (p1.th, TIMEOUT, &exchange_hello, &p1);
250 stop_arm (struct PeerContext *p)
253 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
254 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
255 GNUNET_OS_process_wait (p->arm_pid);
257 GNUNET_CONFIGURATION_destroy (p->cfg);
264 char *const argv[] = { "test-transport-api",
266 "test_transport_api_data.conf",
272 struct GNUNET_GETOPT_CommandLineOption options[] = {
273 GNUNET_GETOPT_OPTION_END
277 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
278 argv, "test-transport-api", "nohelp",
286 main (int argc, char *argv[])
290 if (strstr(argv[0], "test_transport_api_tcp") == 0)
294 else if (strstr(argv[0], "test_transport_api_udp") == 0)
300 GNUNET_log_setup ("test-transport-api",
308 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
309 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
313 /* end of test_transport_api.c */