move GNUNET_TRANSPORT_ATS_ to GNUNET_ATS_
[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_common.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "transport.h"
38 #include "transport-testing.h"
39
40 #define VERBOSE GNUNET_EXTRA_LOGGING
41
42 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
43
44 #define START_ARM GNUNET_YES
45
46 /**
47  * How long until we give up on transmitting the message?
48  */
49 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
50
51 #define ITERATIONS 10
52
53 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
54
55 static struct PeerContext *p1;
56
57 struct GNUNET_TRANSPORT_TESTING_handle * tth;
58
59 static int connected = GNUNET_NO;
60
61 static int ret = 0;
62
63 static int i;
64
65 static void
66 end ()
67 {
68   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
69
70   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
71   {
72     GNUNET_SCHEDULER_cancel (timeout_task);
73     timeout_task = GNUNET_SCHEDULER_NO_TASK;
74   }
75   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting\n");
76 }
77
78 static void
79 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 {
81   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
82
83   timeout_task = GNUNET_SCHEDULER_NO_TASK;
84   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
85     return;
86
87   if (p1 != NULL)
88     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
89
90   ret = GNUNET_SYSERR;
91 }
92
93 static void
94 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
95                 const struct GNUNET_ATS_Information *ats,
96                 uint32_t ats_count)
97 {
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' connected \n",
99               GNUNET_i2s (peer));
100   connected++;
101 }
102
103 static void
104 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
105 {
106   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' disconnected \n",
107               GNUNET_i2s (peer));
108 }
109
110 static void
111 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
112                 const struct GNUNET_MessageHeader *message,
113                 const struct GNUNET_ATS_Information *ats,
114                 uint32_t ats_count)
115 {
116   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving\n");
117 }
118
119
120 static void
121 run (void *cls, char *const *args, const char *cfgfile,
122      const struct GNUNET_CONFIGURATION_Handle *cfg)
123 {
124   tth = GNUNET_TRANSPORT_TESTING_init ();
125
126   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
127
128   i = 1;
129   fprintf (stderr, "%i", i);
130   while (i <= ITERATIONS)
131   {
132     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting peer\n");
133     p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_startonly.conf", 1,
134                                               &notify_receive, &notify_connect,
135                                               &notify_disconnect, NULL, p1);
136
137
138     if (p1 != NULL)
139       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer was successfully started\n");
140     else
141       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142                   "Peer1 was not started successfully\n");
143     GNUNET_assert (p1 != NULL);
144     GNUNET_assert (p1->th != NULL);
145
146     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
147
148     i++;
149     if (i <= ITERATIONS)
150       fprintf (stderr, "..%i", i);
151   }
152
153   GNUNET_TRANSPORT_TESTING_init (tth);
154
155   fprintf (stderr, "\n");
156   end ();
157 }
158
159 int
160 main (int argc, char *argv[])
161 {
162   GNUNET_log_setup ("test_transport_testing",
163 #if VERBOSE
164                     "DEBUG",
165 #else
166                     "WARNING",
167 #endif
168                     NULL);
169
170   char *const argv_1[] = { "test_transport_testing",
171     "-c",
172     "test_transport_api_data.conf",
173 #if VERBOSE
174     "-L", "DEBUG",
175 #endif
176     NULL
177   };
178
179   struct GNUNET_GETOPT_CommandLineOption options[] = {
180     GNUNET_GETOPT_OPTION_END
181   };
182
183   GNUNET_PROGRAM_run ((sizeof (argv_1) / sizeof (char *)) - 1, argv_1,
184                       "test_transport_testing", "nohelp", options, &run, &ret);
185
186   return ret;
187 }
188
189 /* end of test_transport_api.c */