-improve indentation, reduce duplication of PIDs in core's neighbour map
[oweals/gnunet.git] / src / transport / test_transport_testing.c
1 /*
2      This file is part of GNUnet.
3      Copyright (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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file transport/test_transport_testing.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 static struct GNUNET_SCHEDULER_Task * timeout_task;
38
39 static struct PeerContext *p1;
40
41 static struct PeerContext *p2;
42
43 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
44
45 struct GNUNET_TRANSPORT_TESTING_handle *tth;
46
47 static int connected = GNUNET_NO;
48
49 static int ret = 0;
50
51
52 static void
53 end ()
54 {
55   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
56               "Stopping peers\n");
57
58   if (timeout_task != NULL)
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
68 static void
69 end_badly ()
70 {
71   timeout_task = NULL;
72   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
73               "Timeout! Stopping peers\n");
74
75   if (NULL != cc)
76   {
77     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
78     cc = NULL;
79   }
80
81   if (p1 != NULL)
82     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
83   if (p2 != NULL)
84     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
85
86   if (NULL != tth)
87     GNUNET_TRANSPORT_TESTING_done (tth);
88
89   ret = GNUNET_SYSERR;
90 }
91
92
93 static void
94 testing_connect_cb (struct PeerContext *p1, 
95                     struct PeerContext *p2, 
96                     void *cls)
97 {
98   char *ps = GNUNET_strdup (GNUNET_i2s (&p1->id));
99
100   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
101               "Peer %u (`%4s') connected to peer %u (`%s')!\n", p1->no, ps,
102               p2->no, GNUNET_i2s (&p2->id));
103   GNUNET_free (ps);
104   GNUNET_SCHEDULER_add_now (&end, NULL);
105 }
106
107
108 static void
109 notify_connect (void *cls,
110                 const struct GNUNET_PeerIdentity *peer)
111 {
112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
113               "Peer `%s' connected \n",
114               GNUNET_i2s (peer));
115   connected++;
116 }
117
118
119 static void
120 notify_disconnect (void *cls, 
121                    const struct GNUNET_PeerIdentity *peer)
122 {
123   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
124               "Peer `%s' disconnected \n",
125               GNUNET_i2s (peer));
126 }
127
128
129 static void
130 notify_receive (void *cls, 
131                 const struct GNUNET_PeerIdentity *peer,
132                 const struct GNUNET_MessageHeader *message)
133 {
134   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
135               "Receiving\n");
136 }
137
138
139 static void
140 start_cb (struct PeerContext *p, void *cls)
141 {
142   static int started;
143
144   started++;
145
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
147               "Peer %u (`%s') started\n", p->no,
148               GNUNET_i2s (&p->id));
149
150   if (started != 2)
151     return;
152
153   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
154
155   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
157               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
158   GNUNET_free (sender_c);
159
160   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, 
161                                                &testing_connect_cb,
162                                                NULL);
163 }
164
165
166 static void
167 run (void *cls, char *const *args, const char *cfgfile,
168      const struct GNUNET_CONFIGURATION_Handle *cfg)
169 {
170   tth = GNUNET_TRANSPORT_TESTING_init ();
171
172   timeout_task =
173       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &end_badly, NULL);
174
175   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting peer\n");
176   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
177                                             "test_transport_api_tcp_peer1.conf",
178                                             1, &notify_receive, &notify_connect,
179                                             &notify_disconnect, &start_cb, p1);
180
181   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
182                                             "test_transport_api_tcp_peer2.conf",
183                                             2, &notify_receive, &notify_connect,
184                                             &notify_disconnect, &start_cb, p2);
185
186   if (p1 == NULL)
187   {
188     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189                 "Peer1 was not started successfully\n");
190     GNUNET_SCHEDULER_shutdown ();
191   }
192   if (p2 == NULL)
193   {
194     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195                 "Peer2 was not started successfully\n");
196     GNUNET_SCHEDULER_shutdown ();
197   }
198 }
199
200
201 int
202 main (int argc, char *argv[])
203 {
204   char *const argv_1[] = { 
205     "test_transport_testing",
206     "-c",
207     "test_transport_api_data.conf",
208     NULL
209   };
210   struct GNUNET_GETOPT_CommandLineOption options[] = {
211     GNUNET_GETOPT_OPTION_END
212   };
213
214   GNUNET_log_setup ("test_transport_testing",
215                     "WARNING",
216                     NULL);
217   GNUNET_PROGRAM_run ((sizeof (argv_1) / sizeof (char *)) - 1, argv_1,
218                       "test_transport_testing", "nohelp", options, 
219                       &run, &ret);
220
221   return ret;
222 }
223
224 /* end of test_transport_testing.c */