better logging since important messages are info
[oweals/gnunet.git] / src / transport / test_transport_api.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 /**
34  * How long until we give up on transmitting the message?
35  */
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
37
38 /**
39  * How long until we give up on transmitting the message?
40  */
41 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
42
43 #define TEST_MESSAGE_SIZE 2600
44
45 #define TEST_MESSAGE_TYPE 12345
46
47 static char *test_source;
48
49 static char *test_plugin;
50
51 static char *test_name;
52
53 static int ok;
54
55 static int s_started;
56
57 static int s_connected;
58
59 static int s_sending;
60
61 static GNUNET_SCHEDULER_TaskIdentifier die_task;
62
63 static GNUNET_SCHEDULER_TaskIdentifier send_task;
64
65 static struct PeerContext *p1;
66
67 static struct PeerContext *p2;
68
69 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
70
71 static struct GNUNET_TRANSPORT_TransmitHandle *th;
72
73 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
74
75 static char *cfg_file_p1;
76
77 static char *cfg_file_p2;
78
79
80 static void
81 end ()
82 {
83   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping peers\n");
84
85   if (send_task != GNUNET_SCHEDULER_NO_TASK)
86     GNUNET_SCHEDULER_cancel (send_task);
87
88   if (die_task != GNUNET_SCHEDULER_NO_TASK)
89     GNUNET_SCHEDULER_cancel (die_task);
90
91   if (th != NULL)
92     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
93   th = NULL;
94
95   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
96   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
97 }
98
99 static void
100 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 {
102   die_task = GNUNET_SCHEDULER_NO_TASK;
103
104   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Fail! Stopping peers\n");
105
106
107   if (send_task != GNUNET_SCHEDULER_NO_TASK)
108     GNUNET_SCHEDULER_cancel (send_task);
109
110   if (cc != NULL)
111   {
112     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
113     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
114     cc = NULL;
115   }
116
117   if (th != NULL)
118     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
119   else
120     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
121
122   if (s_started == GNUNET_NO)
123     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
124   else
125     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
126
127   if (s_connected == GNUNET_NO)
128     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
129   else
130     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
131
132   if (s_sending == GNUNET_NO)
133     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
134   else
135     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
136
137   th = NULL;
138
139   if (p1 != NULL)
140     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
141   else
142     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
143   if (p2 != NULL)
144     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
145   else
146     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
147
148   ok = GNUNET_SYSERR;
149 }
150
151
152 static void
153 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
154                 const struct GNUNET_MessageHeader *message)
155 {
156   struct PeerContext *p = cls;
157   struct PeerContext *t = NULL;
158
159   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
160     t = p1;
161   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
162     t = p2;
163   GNUNET_assert (t != NULL);
164
165   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
166
167   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
168               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
169               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
170               GNUNET_i2s (&t->id));
171   GNUNET_free (ps);
172
173   if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
174       (TEST_MESSAGE_SIZE == ntohs (message->size)))
175   {
176     ok = 0;
177     end ();
178   }
179   else
180   {
181     GNUNET_break (0);
182     ok = 1;
183     end ();
184   }
185 }
186
187
188 static size_t
189 notify_ready (void *cls, size_t size, void *buf)
190 {
191   struct PeerContext *p = cls;
192   struct GNUNET_MessageHeader *hdr;
193
194   th = NULL;
195
196   if (buf == NULL)
197   {
198     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199                 "Timeout occurred while waiting for transmit_ready\n");
200     if (GNUNET_SCHEDULER_NO_TASK != die_task)
201       GNUNET_SCHEDULER_cancel (die_task);
202     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
203     ok = 42;
204     return 0;
205   }
206
207   GNUNET_assert (size >= TEST_MESSAGE_SIZE);
208   if (buf != NULL)
209   {
210     memset (buf, '\0', TEST_MESSAGE_SIZE);
211     hdr = buf;
212     hdr->size = htons (TEST_MESSAGE_SIZE);
213     hdr->type = htons (TEST_MESSAGE_TYPE);
214   }
215
216   char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
217   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
218               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
219               p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
220               GNUNET_i2s (&p->id));
221   GNUNET_free (ps);
222
223   return TEST_MESSAGE_SIZE;
224 }
225
226
227 static void
228 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
229 {
230   send_task = GNUNET_SCHEDULER_NO_TASK;
231
232   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
233     return;
234   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
235
236   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
237               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
238               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
239   GNUNET_free (receiver_s);
240   s_sending = GNUNET_YES;
241   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
242                                                TIMEOUT_TRANSMIT, &notify_ready,
243                                                p1);
244 }
245
246
247 static void
248 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
249 {
250   static int c;
251
252   c++;
253   struct PeerContext *p = cls;
254   struct PeerContext *t = NULL;
255
256   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
257     t = p1;
258   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
259     t = p2;
260   GNUNET_assert (t != NULL);
261
262   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
263
264   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
265               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
266               t->no, GNUNET_i2s (peer));
267   GNUNET_free (ps);
268 }
269
270
271 static void
272 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
273 {
274   struct PeerContext *p = cls;
275   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
276
277   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
278               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
279               GNUNET_i2s (peer));
280
281   GNUNET_free (ps);
282
283   if (th != NULL)
284     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
285   th = NULL;
286 }
287
288
289 static void
290 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
291 {
292   cc = NULL;
293   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
294
295   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
296               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
297   GNUNET_free (p1_c);
298
299   s_connected = GNUNET_YES;
300   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
301 }
302
303
304 static void
305 start_cb (struct PeerContext *p, void *cls)
306 {
307   static int started;
308
309   started++;
310
311   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer %u (`%s') started\n", p->no,
312               GNUNET_i2s (&p->id));
313
314   if (started != 2)
315     return;
316   else
317     s_started = GNUNET_YES;
318   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
319
320   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
321               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
322               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
323   GNUNET_free (sender_c);
324
325   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
326                                                NULL);
327
328 }
329
330
331 static void
332 run (void *cls, char *const *args, const char *cfgfile,
333      const struct GNUNET_CONFIGURATION_Handle *cfg)
334 {
335   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
336
337   s_started = GNUNET_NO;
338   s_connected = GNUNET_NO;
339   s_sending = GNUNET_NO;
340
341   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
342                                             &notify_receive, &notify_connect,
343                                             &notify_disconnect, &start_cb,
344                                             NULL);
345
346   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
347                                             &notify_receive, &notify_connect,
348                                             &notify_disconnect, &start_cb,
349                                             NULL);
350
351   if ((p1 == NULL) || (p2 == NULL))
352   {
353     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
354     if (die_task != GNUNET_SCHEDULER_NO_TASK)
355       GNUNET_SCHEDULER_cancel (die_task);
356     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
357     return;
358   }
359 }
360
361
362 static int
363 check ()
364 {
365   static char *const argv[] = { "test-transport-api",
366     "-c",
367     "test_transport_api_data.conf",
368     NULL
369   };
370   static struct GNUNET_GETOPT_CommandLineOption options[] = {
371     GNUNET_GETOPT_OPTION_END
372   };
373
374   send_task = GNUNET_SCHEDULER_NO_TASK;
375
376   ok = 1;
377   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
378                       "nohelp", options, &run, &ok);
379
380   return ok;
381 }
382
383 int
384 main (int argc, char *argv[])
385 {
386   int ret;
387
388   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
389   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
390   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
391                                                  &test_plugin);
392
393   GNUNET_log_setup (test_name,
394                     "WARNING",
395                     NULL);
396   tth = GNUNET_TRANSPORT_TESTING_init ();
397
398   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
399   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
400
401   ret = check ();
402
403   GNUNET_free (cfg_file_p1);
404   GNUNET_free (cfg_file_p2);
405
406   GNUNET_free (test_source);
407   GNUNET_free (test_plugin);
408   GNUNET_free (test_name);
409
410   GNUNET_TRANSPORT_TESTING_done (tth);
411
412   return ret;
413 }
414
415 /* end of test_transport_api.c */