complete state reset functionality
[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_EXTRA_LOGGING
41
42 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
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 GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
57
58 struct GNUNET_TRANSPORT_TESTING_handle * tth;
59
60 static int connected = GNUNET_NO;
61
62 static int ret = 0;
63
64 static void
65 end ()
66 {
67   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
68
69   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
70     GNUNET_SCHEDULER_cancel (timeout_task);
71
72   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
73   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
74
75   GNUNET_TRANSPORT_TESTING_done (tth);
76 }
77
78 static void
79 end_badly ()
80 {
81   timeout_task = GNUNET_SCHEDULER_NO_TASK;
82   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
83
84   if (p1 != NULL)
85     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
86   if (p2 != NULL)
87     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
88
89   GNUNET_TRANSPORT_TESTING_done (tth);
90
91   ret = GNUNET_SYSERR;
92 }
93
94 static void
95 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
96 {
97   char * ps = strdup (GNUNET_i2s(&p1->id));
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') connected to peer %u (`%s')!\n",
99               p1->no, ps,
100               p2->no, GNUNET_i2s (&p2->id));
101   GNUNET_free (ps);
102   GNUNET_SCHEDULER_add_now (&end, NULL);
103 }
104
105 static void
106 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
107                 const struct GNUNET_ATS_Information *ats,
108                 uint32_t ats_count)
109 {
110   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' connected \n",
111               GNUNET_i2s (peer));
112   connected++;
113 }
114
115 static void
116 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
117 {
118   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' disconnected \n",
119               GNUNET_i2s (peer));
120 }
121
122 static void
123 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
124                 const struct GNUNET_MessageHeader *message,
125                 const struct GNUNET_ATS_Information *ats,
126                 uint32_t ats_count)
127 {
128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving\n");
129 }
130
131 void start_cb (struct PeerContext * p,
132                void *cls)
133 {
134   static int started;
135   started++;
136
137   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n",
138        p->no,
139        GNUNET_i2s (&p->id));
140
141   if (started != 2)
142     return;
143
144   char *sender_c = strdup (GNUNET_i2s (&p1->id));
145   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
146               p1->no, sender_c,
147               p2->no, GNUNET_i2s (&p2->id));
148
149   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
150                                                NULL);
151
152 }
153
154 static void
155 run (void *cls, char *const *args, const char *cfgfile,
156      const struct GNUNET_CONFIGURATION_Handle *cfg)
157 {
158   tth = GNUNET_TRANSPORT_TESTING_init ();
159
160   timeout_task =
161       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &end_badly, NULL);
162
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting peer\n");
164   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer1.conf",
165                                             1,
166                                             &notify_receive, &notify_connect,
167                                             &notify_disconnect,
168                                             &start_cb,
169                                             p1);
170   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer2.conf",
171                                             2,
172                                             &notify_receive, &notify_connect,
173                                             &notify_disconnect,
174                                             &start_cb,
175                                             p2);
176
177   if (p1 == NULL)
178   {
179     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer1 was not started successfully\n");
180     if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
181       GNUNET_SCHEDULER_cancel(timeout_task);
182     timeout_task =
183         GNUNET_SCHEDULER_add_now (&end_badly, NULL);
184   }
185   if (p2 == NULL)
186   {
187     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer2 was not started successfully\n");
188     if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
189       GNUNET_SCHEDULER_cancel(timeout_task);
190     timeout_task =
191         GNUNET_SCHEDULER_add_now (&end_badly, NULL);
192   }
193 }
194
195 int
196 main (int argc, char *argv[])
197 {
198   GNUNET_log_setup ("test_transport_testing",
199 #if VERBOSE
200                     "DEBUG",
201 #else
202                     "WARNING",
203 #endif
204                     NULL);
205
206   char *const argv_1[] = { "test_transport_testing",
207     "-c",
208     "test_transport_api_data.conf",
209 #if VERBOSE
210     "-L", "DEBUG",
211 #endif
212     NULL
213   };
214
215   struct GNUNET_GETOPT_CommandLineOption options[] = {
216     GNUNET_GETOPT_OPTION_END
217   };
218
219   GNUNET_PROGRAM_run ((sizeof (argv_1) / sizeof (char *)) - 1, argv_1,
220                       "test_transport_testing", "nohelp", options, &run, &ret);
221
222   return ret;
223 }
224
225 /* end of test_transport_api.c */