b2ac507d28e3b367b90460383bf1efc881963c31
[oweals/gnunet.git] / src / transport / test_transport_testing.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 3, 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 base test case for transport implementations
23  *
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
27  * C code apparently.
28  */
29 #include "platform.h"
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"
39
40 #define VERBOSE GNUNET_NO
41
42 #define VERBOSE_ARM GNUNET_NO
43
44 #define START_ARM GNUNET_YES
45
46 /**
47  * How long until we give up on transmitting the message?
48  */
49 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
50
51 /**
52  * How long until we give up on transmitting the message?
53  */
54 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
55
56 #define MTYPE 12345
57
58 static struct PeerContext p1;
59
60 static struct PeerContext p2;
61
62 static int ok;
63
64 static int is_tcp;
65
66 static  GNUNET_SCHEDULER_TaskIdentifier die_task;
67
68 static  GNUNET_SCHEDULER_TaskIdentifier tct;
69
70
71 #if VERBOSE
72 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
73 #else
74 #define OKPP do { ok++; } while (0)
75 #endif
76
77
78
79 static void
80 stop_arm (struct PeerContext *p)
81 {
82 #if START_ARM
83   if (NULL != p->arm_proc)
84     {
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);
89       p->arm_proc = NULL;
90     }
91 #endif
92   GNUNET_CONFIGURATION_destroy (p->cfg);
93 }
94
95
96
97
98 static void
99 exchange_hello_last (void *cls,
100                      const struct GNUNET_MessageHeader *message)
101 {
102   struct PeerContext *me = cls;
103
104   GNUNET_assert (message != NULL);
105   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
106               "Exchanging HELLO of size %d with peer (%s)!\n", 
107               (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
108               GNUNET_i2s (&me->id));
109   GNUNET_assert (GNUNET_OK ==
110                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
111                                       message, &me->id));
112   GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
113 }
114
115
116 static void
117 exchange_hello (void *cls,
118                 const struct GNUNET_MessageHeader *message)
119 {
120   struct PeerContext *me = cls;
121
122   GNUNET_assert (message != NULL);
123   GNUNET_assert (GNUNET_OK ==
124                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
125                                       message, &me->id));
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);
131 }
132
133
134 static void
135 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 {
137   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
138   GNUNET_break (0);
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)
145     {
146       GNUNET_SCHEDULER_cancel (tct);
147       tct = GNUNET_SCHEDULER_NO_TASK;
148     }
149   ok = 1;
150 }
151
152 static void
153 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
154 {
155   ok = 60;
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)
162     {
163       GNUNET_SCHEDULER_cancel (tct);
164       tct = GNUNET_SCHEDULER_NO_TASK;
165     }
166   ok = 0;
167 }
168
169 static void
170 notify_connect (void *cls,
171                 const struct GNUNET_PeerIdentity *peer,
172                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
173                 uint32_t ats_count)
174 {
175   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176               "Peer `%4s' connected to us (%p)!\n", 
177               GNUNET_i2s (peer), 
178               cls);
179   if (cls == &p1)
180     {
181       GNUNET_assert (ok >= 2);
182       OKPP;
183       OKPP;
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);
191     }
192 }
193
194
195 static void
196 notify_disconnect (void *cls,
197                    const struct GNUNET_PeerIdentity *peer)
198 {
199    if ( (ok != 60) &&
200          (GNUNET_SCHEDULER_NO_TASK != die_task) )
201      {
202        GNUNET_SCHEDULER_cancel (die_task);
203        die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
204      }
205   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206               "Peer `%4s' disconnected (%p)!\n",
207               GNUNET_i2s (peer), cls);
208 }
209
210
211 static void
212 setup_peer (struct PeerContext *p, 
213             const char *cfgname)
214 {
215   p->cfg = GNUNET_CONFIGURATION_create ();
216
217   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
218   if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
219     {
220       GNUNET_assert (GNUNET_OK == 
221                      GNUNET_CONFIGURATION_get_value_string (p->cfg, 
222                                                             "PATHS", "SERVICEHOME", 
223                                                             &p->servicehome));
224       GNUNET_DISK_directory_remove (p->servicehome);
225     }
226
227 #if START_ARM
228   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
229                                         "gnunet-service-arm",
230 #if VERBOSE_ARM
231                                         "-L", "DEBUG",
232 #endif
233                                         "-c", cfgname, NULL);
234 #endif
235   p->th = GNUNET_TRANSPORT_connect (p->cfg,
236                                     NULL, p,
237                                     NULL,
238                                     &notify_connect, &notify_disconnect);
239   GNUNET_assert (p->th != NULL);
240 }
241
242
243 static void
244 try_connect (void *cls,
245              const struct GNUNET_SCHEDULER_TaskContext *tc)
246 {
247   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248               "Asking peers to connect...\n");
249   /* FIXME: 'pX.id' may still be all-zeros here... */
250   GNUNET_TRANSPORT_try_connect (p2.th,
251                                 &p1.id);
252   GNUNET_TRANSPORT_try_connect (p1.th,
253                                 &p2.id);
254   tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
255                                       &try_connect,
256                                       NULL);
257 }
258
259
260 static void
261 run (void *cls,
262      char *const *args,
263      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
264 {
265   GNUNET_assert (ok == 1);
266   OKPP;
267   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
268                                            &end_badly, NULL);
269   if (is_tcp)
270     {
271       setup_peer (&p1, "test_transport_api_multiaddress_tcp_peer1.conf");
272       setup_peer (&p2, "test_transport_api_multiaddress_tcp_peer2.conf");
273     }
274   GNUNET_assert(p1.th != NULL);
275   GNUNET_assert(p2.th != NULL);
276
277   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
278   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
279   tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
280 }
281
282 static int
283 check ()
284 {
285   static char *const argv[] = { "test-transport-api",
286     "-c",
287     "test_transport_api_data.conf",
288 #if VERBOSE
289     "-L", "DEBUG",
290 #endif
291     NULL
292   };
293   static struct GNUNET_GETOPT_CommandLineOption options[] = {
294     GNUNET_GETOPT_OPTION_END
295   };
296
297 #if WRITECONFIG
298   setTransportOptions("test_transport_api_data.conf");
299 #endif
300   ok = 1;
301   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
302                       argv, "test-transport-api", "nohelp",
303                       options, &run, &ok);
304   stop_arm (&p1);
305   stop_arm (&p2);
306
307   if (p1.servicehome != NULL)
308     {
309       GNUNET_DISK_directory_remove (p1.servicehome);
310       GNUNET_free(p1.servicehome);
311     }
312   if (p2.servicehome != NULL)
313     {
314       GNUNET_DISK_directory_remove (p2.servicehome);
315       GNUNET_free(p2.servicehome);
316     }
317   return ok;
318 }
319
320
321 int
322 main (int argc, char *argv[])
323 {
324   int ret;
325
326   GNUNET_log_setup ("test-transport-api",
327 #if VERBOSE
328                     "DEBUG",
329 #else
330                     "WARNING",
331 #endif
332                     NULL);
333
334   if (strstr(argv[0], "tcp") != NULL)
335     {
336       is_tcp = GNUNET_YES;
337     }
338
339   ret = check ();
340
341   return ret;
342 }
343
344 /* end of test_transport_api.c */