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