aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/test_transport_api.c')
-rw-r--r--src/transport/test_transport_api.c305
1 files changed, 305 insertions, 0 deletions
diff --git a/src/transport/test_transport_api.c b/src/transport/test_transport_api.c
new file mode 100644
index 000000000..02c28b09c
--- /dev/null
+++ b/src/transport/test_transport_api.c
@@ -0,0 +1,305 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 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 2, 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.c
22 * @brief testcase for transport_api.c
23 */
24#include "platform.h"
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"
33
34#define VERBOSE GNUNET_NO
35
36#define START_ARM GNUNET_YES
37
38/**
39 * How long until we give up on transmitting the message?
40 */
41#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
42
43#define MTYPE 12345
44
45struct PeerContext
46{
47 struct GNUNET_CONFIGURATION_Handle *cfg;
48 struct GNUNET_TRANSPORT_Handle *th;
49 struct GNUNET_PeerIdentity id;
50#if START_ARM
51 pid_t arm_pid;
52#endif
53};
54
55static struct PeerContext p1;
56
57static struct PeerContext p2;
58
59static struct GNUNET_SCHEDULER_Handle *sched;
60
61static int ok;
62
63#if VERBOSE
64#define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
65#else
66#define OKPP do { ok++; } while (0)
67#endif
68
69
70static void
71end ()
72{
73 /* do work here */
74 GNUNET_assert (ok == 8);
75 GNUNET_TRANSPORT_disconnect (p1.th);
76 GNUNET_TRANSPORT_disconnect (p2.th);
77 ok = 0;
78}
79
80
81/**
82 * Function called by the transport for each received message.
83 *
84 * @param cls closure
85 * @param latency estimated latency for communicating with the
86 * given peer
87 * @param peer (claimed) identity of the other peer
88 * @param message the message
89 */
90static void
91notify_receive (void *cls,
92 struct GNUNET_TIME_Relative latency,
93 const struct GNUNET_PeerIdentity *peer,
94 const struct GNUNET_MessageHeader *message)
95{
96 GNUNET_assert (ok == 7);
97 OKPP;
98 GNUNET_assert (MTYPE == ntohs (message->type));
99 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
100 ntohs (message->size));
101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
102 cls);
103 end ();
104}
105
106
107/**
108 * Function called to notify transport users that another
109 * peer connected to us.
110 *
111 * @param cls closure
112 * @param transport the transport service handle
113 * @param peer the peer that disconnected
114 * @param latency current latency of the connection
115 */
116static void
117notify_connect (void *cls,
118 const struct GNUNET_PeerIdentity *peer,
119 struct GNUNET_TIME_Relative latency)
120{
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer connected to us (%p)!\n", cls);
122 GNUNET_assert ((ok >= 1) && (ok <= 6));
123 OKPP;
124}
125
126
127/**
128 * Function called to notify transport users that another
129 * peer disconnected from us.
130 *
131 * @param cls closure
132 * @param transport the transport service handle
133 * @param peer the peer that disconnected
134 */
135static void
136notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
137{
138 GNUNET_assert (0);
139}
140
141
142static void
143setup_peer (struct PeerContext *p, const char *cfgname)
144{
145 p->cfg = GNUNET_CONFIGURATION_create ();
146#if START_ARM
147 p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
148 "gnunet-service-arm",
149#if VERBOSE
150 "-L", "DEBUG",
151#endif
152 "-c", cfgname, NULL);
153 sleep (1); /* allow ARM to start */
154#endif
155 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
156 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
157 p,
158 &notify_receive,
159 &notify_connect, &notify_disconnect);
160 GNUNET_assert (p->th != NULL);
161}
162
163
164static size_t
165notify_ready (void *cls, size_t size, void *buf)
166{
167 struct GNUNET_MessageHeader *hdr;
168
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "Transmitting message to peer (%p) - %u!\n", cls, size);
171 GNUNET_assert (size >= 256);
172 GNUNET_assert ((ok >= 5) && (ok <= 6));
173 OKPP;
174 hdr = buf;
175 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
176 hdr->type = htons (MTYPE);
177 return sizeof (struct GNUNET_MessageHeader);
178}
179
180
181static void
182exchange_hello_last (void *cls,
183 struct GNUNET_TIME_Relative latency,
184 const struct GNUNET_PeerIdentity *peer,
185 const struct GNUNET_MessageHeader *message)
186{
187 struct PeerContext *me = cls;
188 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
189
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191 "Exchanging HELLO with peer (%p)!\n", cls);
192 GNUNET_assert (ok >= 3);
193 OKPP;
194 GNUNET_assert (message != NULL);
195 GNUNET_assert (GNUNET_OK ==
196 GNUNET_HELLO_get_key ((const struct GNUNET_HELLO_Message *)
197 message, &pk));
198 GNUNET_CRYPTO_hash (&pk,
199 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
200 &me->id.hashPubKey);
201 GNUNET_TRANSPORT_offer_hello (p1.th, message);
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "Finished exchanging HELLOs, now waiting for transmission!\n");
204 /* both HELLOs exchanged, get ready to test transmission! */
205 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
206 &p2.id,
207 256, TIMEOUT, &notify_ready, &p1);
208}
209
210
211static void
212exchange_hello (void *cls,
213 struct GNUNET_TIME_Relative latency,
214 const struct GNUNET_PeerIdentity *peer,
215 const struct GNUNET_MessageHeader *message)
216{
217 struct PeerContext *me = cls;
218 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
219
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "Exchanging HELLO with peer (%p)!\n", cls);
222 GNUNET_assert (ok >= 2);
223 OKPP;
224 GNUNET_assert (message != NULL);
225 GNUNET_assert (GNUNET_OK ==
226 GNUNET_HELLO_get_key ((const struct GNUNET_HELLO_Message *)
227 message, &pk));
228 GNUNET_CRYPTO_hash (&pk,
229 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
230 &me->id.hashPubKey);
231 GNUNET_TRANSPORT_get_hello (p2.th, GNUNET_TIME_UNIT_MINUTES,
232 &exchange_hello_last, &p2);
233}
234
235
236static void
237run (void *cls,
238 struct GNUNET_SCHEDULER_Handle *s,
239 char *const *args,
240 const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
241{
242 GNUNET_assert (ok == 1);
243 OKPP;
244 sched = s;
245 setup_peer (&p1, "test_transport_api_peer1.conf");
246 setup_peer (&p2, "test_transport_api_peer2.conf");
247 GNUNET_TRANSPORT_get_hello (p1.th,
248 GNUNET_TIME_UNIT_MINUTES, &exchange_hello, &p1);
249}
250
251
252static void
253stop_arm (struct PeerContext *p)
254{
255#if START_ARM
256 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
257 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
258 waitpid (p->arm_pid, NULL, 0);
259#endif
260 GNUNET_CONFIGURATION_destroy (p->cfg);
261}
262
263
264static int
265check ()
266{
267 char *const argv[] = { "test-transport-api",
268 "-c",
269 "test_transport_api_data.conf",
270#if VERBOSE
271 "-L", "DEBUG",
272#endif
273 NULL
274 };
275 struct GNUNET_GETOPT_CommandLineOption options[] = {
276 GNUNET_GETOPT_OPTION_END
277 };
278
279 ok = 1;
280 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
281 argv, "test-transport-api", "nohelp",
282 options, &run, &ok);
283 stop_arm (&p1);
284 stop_arm (&p2);
285 return ok;
286}
287
288int
289main (int argc, char *argv[])
290{
291 int ret;
292
293 GNUNET_log_setup ("test-transport-api",
294#if VERBOSE
295 "DEBUG",
296#else
297 "WARNING",
298#endif
299 NULL);
300 ret = check ();
301
302 return ret;
303}
304
305/* end of test_transport_api.c */