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