d9789d98044d1f59ec7ce379793837db3373a97e
[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 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
52
53 static struct PeerContext * p1;
54 static struct PeerContext * p2;
55
56 static int connected = GNUNET_NO;
57
58 static int ret = 0;
59
60 static void
61 end ()
62 {
63   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
64
65   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
66     GNUNET_SCHEDULER_cancel(timeout_task);
67
68   GNUNET_TRANSPORT_TESTING_stop_peer(p1);
69   GNUNET_TRANSPORT_TESTING_stop_peer(p2);
70 }
71
72 static void
73 end_badly ()
74 {
75   timeout_task = GNUNET_SCHEDULER_NO_TASK;
76   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
77
78   GNUNET_TRANSPORT_TESTING_stop_peer(p1);
79   GNUNET_TRANSPORT_TESTING_stop_peer(p2);
80
81   ret = GNUNET_SYSERR;
82 }
83
84 static void
85 testing_connect_cb (struct PeerContext * p1, struct PeerContext * p2, void *cls)
86 {
87   end();
88 }
89
90 static void
91 notify_connect (void *cls,
92                 const struct GNUNET_PeerIdentity *peer,
93                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
94                 uint32_t ats_count)
95 {
96   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' connected \n",
97        GNUNET_i2s (peer));
98   connected++;
99
100   if (connected == 2)
101     end ();
102 }
103
104 static void
105 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
106 {
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' disconnected \n",
108        GNUNET_i2s (peer));
109 }
110
111 static void
112 notify_receive (void *cls,
113                 const struct GNUNET_PeerIdentity *peer,
114                 const struct GNUNET_MessageHeader *message,
115                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
116                 uint32_t ats_count)
117 {
118   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving\n");
119 }
120
121
122 static void
123 run (void *cls,
124      char *const *args,
125      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
126 {
127   timeout_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MINUTES, &end_badly, NULL);
128
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting peer\n");
130   p1 = GNUNET_TRANSPORT_TESTING_start_peer("test_transport_api_tcp_peer1.conf",
131       &notify_receive,
132       &notify_connect,
133       &notify_disconnect,
134       p1);
135
136   p2 = GNUNET_TRANSPORT_TESTING_start_peer("test_transport_api_tcp_peer2.conf",
137       &notify_receive,
138       &notify_connect,
139       &notify_disconnect,
140       p2);
141
142   if (p1 != NULL)
143     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer1 was successfully started\n");
144   else
145     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer1 was not started successfully\n");
146   GNUNET_assert (p1 != NULL);
147   GNUNET_assert (p1->th != NULL);
148
149   if (p2 != NULL)
150     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer2 was successfully started\n");
151   else
152     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer2 was not started successfully\n");
153   GNUNET_assert (p2 != NULL);
154   GNUNET_assert (p2->th != NULL);
155
156
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peers\n");
158   GNUNET_TRANSPORT_TESTING_connect_peers(p1, p2, NULL, NULL);
159 }
160
161 int
162 main (int argc, char *argv[])
163 {
164   GNUNET_log_setup ("test_transport_testing",
165 #if VERBOSE
166                     "DEBUG",
167 #else
168                     "WARNING",
169 #endif
170                     NULL);
171
172   char *const argv_1[] = { "test_transport_testing",
173     "-c",
174     "test_transport_api_data.conf",
175 #if VERBOSE
176     "-L", "DEBUG",
177 #endif
178     NULL
179   };
180
181   struct GNUNET_GETOPT_CommandLineOption options[] = {
182     GNUNET_GETOPT_OPTION_END
183   };
184
185   GNUNET_PROGRAM_run ((sizeof (argv_1) / sizeof (char *)) - 1,
186                       argv_1, "test_transport_testing", "nohelp",
187                       options, &run, &ret);
188
189   return ret;
190 }
191
192 /* end of test_transport_api.c */