codesonar fixes
[oweals/gnunet.git] / src / testing / test_testing_connect.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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_connect.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 static void
47 end2_cb (void *cls, const char *emsg)
48 {
49
50   if (emsg != NULL)
51     {
52       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Ending with error: %s\n", emsg);
53       ok = 1;
54     }
55   else
56     {
57 #if VERBOSE
58       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
59                   "Both daemons terminated, will now exit.\n");
60 #endif
61       ok = 0;
62     }
63 }
64
65 static void
66 end1_cb (void *cls, const char *emsg)
67 {
68   if (emsg != NULL)
69     {
70       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Stopping daemon 1 gave: %s\n", emsg);
71       ok = 1;
72     }
73   else
74     {
75       ok = 0;
76     }
77
78   GNUNET_TESTING_daemon_stop (d2, TIMEOUT, &end2_cb, NULL, GNUNET_YES, GNUNET_NO);
79   d2 = NULL;
80 }
81
82 static void
83 finish_testing(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
84 {
85   GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &end1_cb, NULL, GNUNET_YES, GNUNET_NO);
86   d1 = NULL;
87 }
88
89 static void
90 my_connect_complete (void *cls,
91                      const struct GNUNET_PeerIdentity *first,
92                      const struct GNUNET_PeerIdentity *second,
93                      unsigned int distance,
94                      const struct GNUNET_CONFIGURATION_Handle *first_cfg,
95                      const struct GNUNET_CONFIGURATION_Handle *second_cfg,
96                      struct GNUNET_TESTING_Daemon *first_daemon,
97                      struct GNUNET_TESTING_Daemon *second_daemon,
98                      const char *emsg)
99 {
100   GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
101 }
102
103
104 static void
105 my_cb2 (void *cls,
106         const struct GNUNET_PeerIdentity *id,
107         const struct GNUNET_CONFIGURATION_Handle *cfg,
108         struct GNUNET_TESTING_Daemon *d, const char *emsg)
109 {
110   GNUNET_assert (id != NULL);
111 #if VERBOSE
112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113               "Daemon `%s' started.\n", GNUNET_i2s (id));
114 #endif
115   GNUNET_TESTING_daemons_connect (d1, d2,
116                                   TIMEOUT, CONNECT_ATTEMPTS, &my_connect_complete, NULL);
117 }
118
119
120 static void
121 my_cb1 (void *cls,
122         const struct GNUNET_PeerIdentity *id,
123         const struct GNUNET_CONFIGURATION_Handle *cfg,
124         struct GNUNET_TESTING_Daemon *d, const char *emsg)
125 {
126   GNUNET_assert (id != NULL);
127 #if VERBOSE
128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129               "Daemon `%s' started.\n", GNUNET_i2s (id));
130 #endif
131   d2 = GNUNET_TESTING_daemon_start (c2, TIMEOUT, NULL, NULL, 0, NULL, NULL, &my_cb2, NULL);
132   GNUNET_assert (d2 != NULL);
133
134 }
135
136
137 static void
138 run (void *cls,
139      char *const *args,
140      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
141 {
142   ok = 1;
143 #if VERBOSE
144   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemon.\n");
145 #endif
146   c1 = GNUNET_CONFIGURATION_create ();
147   GNUNET_CONFIGURATION_parse (c1, "test_testing_connect_peer1.conf");
148   c2 = GNUNET_CONFIGURATION_create ();
149   GNUNET_CONFIGURATION_parse (c2, "test_testing_connect_peer2.conf");
150   d1 = GNUNET_TESTING_daemon_start (c1, TIMEOUT, NULL, NULL, 0, NULL, NULL, &my_cb1, NULL);
151   GNUNET_assert (d1 != NULL);
152 }
153
154 static int
155 check ()
156 {
157   char *const argv[] = { "test-testing",
158     "-c",
159     "test_testing_data.conf",
160 #if VERBOSE
161     "-L", "DEBUG",
162 #endif
163     NULL
164   };
165   struct GNUNET_GETOPT_CommandLineOption options[] = {
166     GNUNET_GETOPT_OPTION_END
167   };
168   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
169                       argv, "test-testing-connect", "nohelp",
170                       options, &run, &ok);
171   return ok;
172 }
173
174 int
175 main (int argc, char *argv[])
176 {
177   int ret;
178
179   GNUNET_log_setup ("test-testing-connect",
180 #if VERBOSE
181                     "DEBUG",
182 #else
183                     "WARNING",
184 #endif
185                     NULL);
186   ret = check ();
187   return ret;
188 }
189
190 /* end of test_testing_connect.c */