- implementation for mantis 0002485
[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_transport_service.h"
31 #include "transport-testing.h"
32 /**
33  * How long until we give up on transmitting the message?
34  */
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
36
37 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
38
39 static struct PeerContext *p1;
40 static struct PeerContext *p2;
41
42 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
43
44 struct GNUNET_TRANSPORT_TESTING_handle *tth;
45
46 static int connected = GNUNET_NO;
47
48 static int ret = 0;
49
50 static void
51 end ()
52 {
53   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
54
55   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
56     GNUNET_SCHEDULER_cancel (timeout_task);
57
58   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
59   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
60
61   GNUNET_TRANSPORT_TESTING_done (tth);
62 }
63
64 static void
65 end_badly ()
66 {
67   timeout_task = GNUNET_SCHEDULER_NO_TASK;
68   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
69
70   if (NULL != cc)
71   {
72     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
73     cc = NULL;
74   }
75
76   if (p1 != NULL)
77     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
78   if (p2 != NULL)
79     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
80
81   GNUNET_TRANSPORT_TESTING_done (tth);
82
83   ret = GNUNET_SYSERR;
84 }
85
86 static void
87 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
88 {
89   char *ps = GNUNET_strdup (GNUNET_i2s (&p1->id));
90
91   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
92               "Peer %u (`%4s') connected to peer %u (`%s')!\n", p1->no, ps,
93               p2->no, GNUNET_i2s (&p2->id));
94   GNUNET_free (ps);
95   GNUNET_SCHEDULER_add_now (&end, NULL);
96 }
97
98 static void
99 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
100                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
101 {
102   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' connected \n",
103               GNUNET_i2s (peer));
104   connected++;
105 }
106
107 static void
108 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
109 {
110   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' disconnected \n",
111               GNUNET_i2s (peer));
112 }
113
114 static void
115 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
116                 const struct GNUNET_MessageHeader *message,
117                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
118 {
119   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving\n");
120 }
121
122 void
123 start_cb (struct PeerContext *p, void *cls)
124 {
125   static int started;
126
127   started++;
128
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
130               GNUNET_i2s (&p->id));
131
132   if (started != 2)
133     return;
134
135   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
136
137   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
139               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
140   GNUNET_free (sender_c);
141
142   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
143                                                NULL);
144
145 }
146
147 static void
148 run (void *cls, char *const *args, const char *cfgfile,
149      const struct GNUNET_CONFIGURATION_Handle *cfg)
150 {
151   tth = GNUNET_TRANSPORT_TESTING_init ();
152
153   timeout_task =
154       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &end_badly, NULL);
155
156   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting peer\n");
157   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
158                                             "test_transport_api_tcp_peer1.conf",
159                                             1, &notify_receive, &notify_connect,
160                                             &notify_disconnect, &start_cb, p1);
161
162   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
163                                             "test_transport_api_tcp_peer2.conf",
164                                             2, &notify_receive, &notify_connect,
165                                             &notify_disconnect, &start_cb, p2);
166
167   if (p1 == NULL)
168   {
169     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170                 "Peer1 was not started successfully\n");
171     if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
172       GNUNET_SCHEDULER_cancel (timeout_task);
173     timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
174   }
175   if (p2 == NULL)
176   {
177     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178                 "Peer2 was not started successfully\n");
179     if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
180       GNUNET_SCHEDULER_cancel (timeout_task);
181     timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
182   }
183 }
184
185 int
186 main (int argc, char *argv[])
187 {
188   GNUNET_log_setup ("test_transport_testing",
189                     "WARNING",
190                     NULL);
191
192   char *const argv_1[] = { "test_transport_testing",
193     "-c",
194     "test_transport_api_data.conf",
195     NULL
196   };
197
198   struct GNUNET_GETOPT_CommandLineOption options[] = {
199     GNUNET_GETOPT_OPTION_END
200   };
201
202   GNUNET_PROGRAM_run ((sizeof (argv_1) / sizeof (char *)) - 1, argv_1,
203                       "test_transport_testing", "nohelp", options, &run, &ret);
204
205   return ret;
206 }
207
208 /* end of test_transport_api.c */