free in proper place
[oweals/gnunet.git] / src / testing / test_testing_reconnect.c
1 /*
2      This file is part of GNUnet.
3      (C) 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 testing/test_testing_reconnect.c
22  * @brief testcase for functions to connect two peers in testing.c
23  */
24 #include "platform.h"
25 #include "gnunet_testing_lib.h"
26
27 #define VERBOSE GNUNET_NO
28
29 /**
30  * How long until we give up on connecting the peers?
31  */
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
33
34 #define CONNECT_ATTEMPTS 3
35
36 static int ok;
37
38 static struct GNUNET_TESTING_Daemon *d1;
39
40 static struct GNUNET_TESTING_Daemon *d2;
41
42 static struct GNUNET_CONFIGURATION_Handle *c1;
43
44 static struct GNUNET_CONFIGURATION_Handle *c2;
45
46 /**
47  * How many start-connect-stop iterations should we do?
48  */
49 #define NUM_PHASES 2
50
51 static int phase;
52
53 /**
54  * Run the next phase of starting daemons, connecting them and
55  * stopping them again.
56  */
57 static void 
58 run_phase ();
59
60 static void
61 end2_cb (void *cls, const char *emsg)
62 {
63
64   if (emsg != NULL)
65     {
66       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Ending with error: %s\n", emsg);
67       ok = 1;
68     }
69   else
70     {
71       if (phase < NUM_PHASES)
72         {
73           fprintf (stderr, ".");
74           run_phase();
75           return;
76         }
77       fprintf (stderr, ".\n");
78 #if VERBOSE
79       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80                   "Both daemons terminated, will now exit.\n");
81 #endif
82       ok = 0;
83     }
84 }
85
86 static void
87 end1_cb (void *cls, const char *emsg)
88 {
89   if (emsg != NULL)
90     {
91       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping daemon 1 gave: %s\n",
92                   emsg);
93       ok = 1;
94     }
95   else
96     {
97       ok = 0;
98     }
99
100   GNUNET_TESTING_daemon_stop (d2, TIMEOUT, &end2_cb, NULL,
101                               (phase == NUM_PHASES) ? GNUNET_YES : GNUNET_NO,
102                               GNUNET_NO);
103   d2 = NULL;
104 }
105
106 static void
107 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108 {
109   GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &end1_cb, NULL, 
110                               (phase == NUM_PHASES) ? GNUNET_YES : GNUNET_NO,
111                               GNUNET_NO);
112   d1 = NULL;
113 }
114
115
116 static void
117 my_connect_complete (void *cls,
118                      const struct GNUNET_PeerIdentity *first,
119                      const struct GNUNET_PeerIdentity *second,
120                      unsigned int distance,
121                      const struct GNUNET_CONFIGURATION_Handle *first_cfg,
122                      const struct GNUNET_CONFIGURATION_Handle *second_cfg,
123                      struct GNUNET_TESTING_Daemon *first_daemon,
124                      struct GNUNET_TESTING_Daemon *second_daemon,
125                      const char *emsg)
126 {
127 #if VERBOSE
128   fprintf (stderr, "Peer %s ", GNUNET_i2s (first));
129   fprintf (stderr, "connected to %s\n", GNUNET_i2s (second));
130 #endif
131   GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
132 }
133
134
135
136
137 static void
138 my_cb2 (void *cls,
139         const struct GNUNET_PeerIdentity *id,
140         const struct GNUNET_CONFIGURATION_Handle *cfg,
141         struct GNUNET_TESTING_Daemon *d, const char *emsg)
142 {
143   GNUNET_assert (id != NULL);
144 #if VERBOSE
145   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146               "Daemon `%s' started.\n", GNUNET_i2s (id));
147 #endif
148   GNUNET_TESTING_daemons_connect (d1, d2,
149                                   TIMEOUT, CONNECT_ATTEMPTS,
150                                   &my_connect_complete, NULL);
151 }
152
153
154 static void
155 my_cb1 (void *cls,
156         const struct GNUNET_PeerIdentity *id,
157         const struct GNUNET_CONFIGURATION_Handle *cfg,
158         struct GNUNET_TESTING_Daemon *d, const char *emsg)
159 {
160   GNUNET_assert (id != NULL);
161 #if VERBOSE
162   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163               "Daemon `%s' started.\n", GNUNET_i2s (id));
164 #endif
165   d2 =
166     GNUNET_TESTING_daemon_start (c2, TIMEOUT, NULL, NULL, 0, NULL, NULL, NULL,
167                                  &my_cb2, NULL);
168   GNUNET_assert (d2 != NULL);
169 }
170
171
172 static void
173 run (void *cls,
174      char *const *args,
175      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
176 {
177   ok = 1;
178 #if VERBOSE
179   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemon.\n");
180 #endif
181   c1 = GNUNET_CONFIGURATION_create ();
182   GNUNET_CONFIGURATION_parse (c1, "test_testing_connect_peer1.conf");
183   c2 = GNUNET_CONFIGURATION_create ();
184   GNUNET_CONFIGURATION_parse (c2, "test_testing_connect_peer2.conf");
185   run_phase ();
186 }
187
188 static void 
189 run_phase ()
190 {
191   phase++;
192   d1 =
193     GNUNET_TESTING_daemon_start (c1, TIMEOUT, NULL, NULL, 0, NULL, NULL, NULL,
194                                  &my_cb1, NULL);
195   GNUNET_assert (d1 != NULL);
196 }
197
198 static int
199 check ()
200 {
201   char *const argv[] = { "test-testing-reconnect",
202     "-c",
203     "test_testing_data.conf",
204 #if VERBOSE
205     "-L", "DEBUG",
206 #endif
207     NULL
208   };
209   struct GNUNET_GETOPT_CommandLineOption options[] = {
210     GNUNET_GETOPT_OPTION_END
211   };
212   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
213                       argv, "test-testing-reconnect", "nohelp",
214                       options, &run, &ok);
215   return ok;
216 }
217
218 int
219 main (int argc, char *argv[])
220 {
221   int ret;
222
223   GNUNET_log_setup ("test-testing-reconnect",
224 #if VERBOSE
225                     "DEBUG",
226 #else
227                     "WARNING",
228 #endif
229                     NULL);
230   ret = check ();
231   return ret;
232 }
233
234 /* end of test_testing_reconnect.c */