- fix for mantis 0002618
[oweals/gnunet.git] / src / transport / test_transport_startonly.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, 90)
37
38 #define ITERATIONS 10
39
40 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
41
42 static struct PeerContext *p1;
43
44 struct GNUNET_TRANSPORT_TESTING_handle *tth;
45
46 static int connected = GNUNET_NO;
47
48 static int ret = 0;
49
50 static int i;
51
52 static void
53 end ()
54 {
55   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
56
57   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
58   {
59     GNUNET_SCHEDULER_cancel (timeout_task);
60     timeout_task = GNUNET_SCHEDULER_NO_TASK;
61   }
62   GNUNET_TRANSPORT_TESTING_done (tth);
63   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting\n");
64 }
65
66 static void
67 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
68 {
69   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
70
71   timeout_task = GNUNET_SCHEDULER_NO_TASK;
72   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
73     return;
74
75   if (p1 != NULL)
76     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
77   if (NULL != tth)
78     GNUNET_TRANSPORT_TESTING_done (tth);
79   ret = GNUNET_SYSERR;
80 }
81
82 static void
83 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
84                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
85 {
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' connected \n",
87               GNUNET_i2s (peer));
88   connected++;
89 }
90
91 static void
92 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
93 {
94   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' disconnected \n",
95               GNUNET_i2s (peer));
96 }
97
98 static void
99 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
100                 const struct GNUNET_MessageHeader *message,
101                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
102 {
103   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving\n");
104 }
105
106
107 static void
108 run (void *cls, char *const *args, const char *cfgfile,
109      const struct GNUNET_CONFIGURATION_Handle *cfg)
110 {
111   tth = GNUNET_TRANSPORT_TESTING_init ();
112
113   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
114
115   i = 1;
116   FPRINTF (stderr, "%i", i);
117   while (i <= ITERATIONS)
118   {
119     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting peer\n");
120     p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
121                                               "test_transport_startonly.conf",
122                                               1, &notify_receive,
123                                               &notify_connect,
124                                               &notify_disconnect, NULL, p1);
125
126
127     if (p1 != NULL)
128       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer was successfully started\n");
129     else
130       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131                   "Peer1 was not started successfully\n");
132     GNUNET_assert (p1 != NULL);
133     GNUNET_assert (p1->th != NULL);
134
135     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
136
137     i++;
138     if (i <= ITERATIONS)
139       FPRINTF (stderr, "..%i", i);
140   }
141
142   FPRINTF (stderr, "%s",  "\n");
143   end ();
144 }
145
146 int
147 main (int argc, char *argv[])
148 {
149   GNUNET_log_setup ("test_transport_testing",
150                     "WARNING",
151
152                     NULL);
153
154   char *const argv_1[] = { "test_transport_testing",
155     "-c",
156     "test_transport_api_data.conf",
157     NULL
158   };
159
160   struct GNUNET_GETOPT_CommandLineOption options[] = {
161     GNUNET_GETOPT_OPTION_END
162   };
163
164   GNUNET_PROGRAM_run ((sizeof (argv_1) / sizeof (char *)) - 1, argv_1,
165                       "test_transport_testing", "nohelp", options, &run, &ret);
166
167   return ret;
168 }
169
170 /* end of test_transport_api.c */