misc bugfixes
[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_YES
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
45 struct 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
55 static struct PeerContext p1;
56
57 static struct PeerContext p2;
58
59 static struct GNUNET_SCHEDULER_Handle *sched;
60
61 static 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
70 static void
71 end ()
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  */
90 static void
91 notify_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  */
116 static void
117 notify_connect (void *cls,
118                 const struct GNUNET_PeerIdentity *peer,
119                 struct GNUNET_TIME_Relative latency)
120 {
121   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
122               "Peer `%4s' connected to us (%p)!\n", 
123               GNUNET_i2s(peer),
124               cls);
125   GNUNET_assert ((ok >= 1) && (ok <= 6));
126   OKPP;
127 }
128
129
130 /**
131  * Function called to notify transport users that another
132  * peer disconnected from us.
133  *
134  * @param cls closure
135  * @param transport the transport service handle
136  * @param peer the peer that disconnected
137  */
138 static void
139 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
140 {
141   ok--;
142 }
143
144
145 static void
146 setup_peer (struct PeerContext *p, const char *cfgname)
147 {
148   p->cfg = GNUNET_CONFIGURATION_create ();
149 #if START_ARM
150   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
151                                         "gnunet-service-arm",
152 #if VERBOSE
153                                         "-L", "DEBUG",
154 #endif
155                                         "-c", cfgname, NULL);
156   sleep (1);                    /* allow ARM to start */
157 #endif
158   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
159   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
160                                     p,
161                                     &notify_receive,
162                                     &notify_connect, &notify_disconnect);
163   GNUNET_assert (p->th != NULL);
164 }
165
166
167 static size_t
168 notify_ready (void *cls, size_t size, void *buf)
169 {
170   struct GNUNET_MessageHeader *hdr;
171
172   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173               "Transmitting message to peer (%p) - %u!\n", cls, size);
174   GNUNET_assert (size >= 256);
175   GNUNET_assert ((ok >= 5) && (ok <= 6));
176   OKPP;
177   hdr = buf;
178   hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
179   hdr->type = htons (MTYPE);
180   return sizeof (struct GNUNET_MessageHeader);
181 }
182
183
184 static void
185 exchange_hello_last (void *cls,
186                      struct GNUNET_TIME_Relative latency,
187                      const struct GNUNET_PeerIdentity *peer,
188                      const struct GNUNET_MessageHeader *message)
189 {
190   struct PeerContext *me = cls;
191
192   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193               "Exchanging HELLO with peer (%p)!\n", cls);
194   GNUNET_assert (ok >= 3);
195   OKPP;
196   GNUNET_assert (message != NULL);
197   GNUNET_assert (GNUNET_OK ==
198                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
199                                       message, &me->id));
200   GNUNET_TRANSPORT_offer_hello (p1.th, message);
201   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202               "Finished exchanging HELLOs, now waiting for transmission!\n");
203   /* both HELLOs exchanged, get ready to test transmission! */
204   GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
205                                           &p2.id,
206                                           256, 0, TIMEOUT, &notify_ready, &p1);
207 }
208
209
210 static void
211 exchange_hello (void *cls,
212                 struct GNUNET_TIME_Relative latency,
213                 const struct GNUNET_PeerIdentity *peer,
214                 const struct GNUNET_MessageHeader *message)
215 {
216   struct PeerContext *me = cls;
217
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219               "Exchanging HELLO with peer (%p)!\n", cls);
220   GNUNET_assert (ok >= 2);
221   OKPP;
222   GNUNET_assert (message != NULL);
223   GNUNET_assert (GNUNET_OK ==
224                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
225                                       message, &me->id));
226   GNUNET_TRANSPORT_get_hello (p2.th,
227                               TIMEOUT,
228                               &exchange_hello_last, &p2);
229 }
230
231
232 static void
233 run (void *cls,
234      struct GNUNET_SCHEDULER_Handle *s,
235      char *const *args,
236      const char *cfgfile,
237      const struct GNUNET_CONFIGURATION_Handle *cfg)
238 {
239   GNUNET_assert (ok == 1);
240   OKPP;
241   sched = s;
242   setup_peer (&p1, "test_transport_api_peer1.conf");
243   setup_peer (&p2, "test_transport_api_peer2.conf");
244   GNUNET_TRANSPORT_get_hello (p1.th,
245                               TIMEOUT, &exchange_hello, &p1);
246 }
247
248
249 static void
250 stop_arm (struct PeerContext *p)
251 {
252 #if START_ARM
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);
256 #endif
257   GNUNET_CONFIGURATION_destroy (p->cfg);
258 }
259
260
261 static int
262 check ()
263 {
264   char *const argv[] = { "test-transport-api",
265     "-c",
266     "test_transport_api_data.conf",
267 #if VERBOSE
268     "-L", "DEBUG",
269 #endif
270     NULL
271   };
272   struct GNUNET_GETOPT_CommandLineOption options[] = {
273     GNUNET_GETOPT_OPTION_END
274   };
275
276   ok = 1;
277   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
278                       argv, "test-transport-api", "nohelp",
279                       options, &run, &ok);
280   stop_arm (&p1);
281   stop_arm (&p2);
282   return ok;
283 }
284
285 int
286 main (int argc, char *argv[])
287 {
288   int ret;
289
290   GNUNET_log_setup ("test-transport-api",
291 #if VERBOSE
292                     "DEBUG",
293 #else
294                     "WARNING",
295 #endif
296                     NULL);
297   ret = check ();
298   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1"); 
299   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
300   return ret;
301 }
302
303 /* end of test_transport_api.c */