7cec78f9808d2a76eee307c3626a9b01c77b105b
[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_YES
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                                   GNUNET_YES,
151                                   &my_connect_complete, NULL);
152 }
153
154
155 static void
156 my_cb1 (void *cls,
157         const struct GNUNET_PeerIdentity *id,
158         const struct GNUNET_CONFIGURATION_Handle *cfg,
159         struct GNUNET_TESTING_Daemon *d, const char *emsg)
160 {
161   GNUNET_assert (id != NULL);
162 #if VERBOSE
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164               "Daemon `%s' started.\n", GNUNET_i2s (id));
165 #endif
166   d2 =
167     GNUNET_TESTING_daemon_start (c2, TIMEOUT, GNUNET_NO, NULL, NULL, 0, NULL, NULL, NULL,
168                                  &my_cb2, NULL);
169   GNUNET_assert (d2 != NULL);
170 }
171
172
173 static void
174 run (void *cls,
175      char *const *args,
176      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
177 {
178   ok = 1;
179 #if VERBOSE
180   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemon.\n");
181 #endif
182   c1 = GNUNET_CONFIGURATION_create ();
183   GNUNET_CONFIGURATION_parse (c1, "test_testing_connect_peer1.conf");
184   c2 = GNUNET_CONFIGURATION_create ();
185   GNUNET_CONFIGURATION_parse (c2, "test_testing_connect_peer2.conf");
186   run_phase ();
187 }
188
189 static void 
190 run_phase ()
191 {
192   phase++;
193   d1 =
194     GNUNET_TESTING_daemon_start (c1, TIMEOUT, GNUNET_NO, NULL, NULL, 0, NULL, NULL, NULL,
195                                  &my_cb1, NULL);
196   GNUNET_assert (d1 != NULL);
197 }
198
199 static int
200 check ()
201 {
202   char *const argv[] = { "test-testing-reconnect",
203     "-c",
204     "test_testing_data.conf",
205 #if VERBOSE
206     "-L", "DEBUG",
207 #endif
208     NULL
209   };
210   struct GNUNET_GETOPT_CommandLineOption options[] = {
211     GNUNET_GETOPT_OPTION_END
212   };
213   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
214                       argv, "test-testing-reconnect", "nohelp",
215                       options, &run, &ok);
216   return ok;
217 }
218
219 int
220 main (int argc, char *argv[])
221 {
222   int ret;
223
224   GNUNET_log_setup ("test-testing-reconnect",
225 #if VERBOSE
226                     "DEBUG",
227 #else
228                     "WARNING",
229 #endif
230                     NULL);
231   ret = check ();
232   return ret;
233 }
234
235 /* end of test_testing_reconnect.c */