fix memory-leaks
[oweals/gnunet.git] / src / core / test_core_api_start_only.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 transport/test_core_api_start_only.c
22  * @brief testcase for core_api.c that only starts two peers,
23  *        connects to the core service and shuts down again
24  */
25 #include "platform.h"
26 #include "gnunet_common.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_core_service.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_scheduler_lib.h"
33
34 #define VERBOSE GNUNET_NO
35
36 #define TIMEOUT 3
37
38 #define START_ARM GNUNET_YES
39
40 #define MTYPE 12345
41
42 struct PeerContext
43 {
44   struct GNUNET_CONFIGURATION_Handle *cfg;
45   struct GNUNET_CORE_Handle *ch;
46   struct GNUNET_PeerIdentity id;
47 #if START_ARM
48   struct GNUNET_OS_Process *arm_proc;
49 #endif
50 };
51
52 static struct PeerContext p1;
53
54 static struct PeerContext p2;
55
56 static GNUNET_SCHEDULER_TaskIdentifier timeout_task_id;
57
58 static int ok;
59
60 #if VERBOSE
61 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
62 #else
63 #define OKPP do { ok++; } while (0)
64 #endif
65
66
67
68 static void
69 connect_notify (void *cls,
70                 const struct GNUNET_PeerIdentity *peer,
71                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
72 {
73 }
74
75
76 static void
77 disconnect_notify (void *cls,
78                    const struct GNUNET_PeerIdentity *peer)
79 {
80 }
81
82
83 static int
84 inbound_notify (void *cls,
85                 const struct GNUNET_PeerIdentity *other,
86                 const struct GNUNET_MessageHeader *message,
87                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
88 {
89   return GNUNET_OK;
90 }
91
92
93 static int
94 outbound_notify (void *cls,
95                  const struct GNUNET_PeerIdentity *other,
96                  const struct GNUNET_MessageHeader *message,
97                  const struct GNUNET_TRANSPORT_ATS_Information *atsi)
98 {
99   return GNUNET_OK;
100 }
101
102
103 static struct GNUNET_CORE_MessageHandler handlers[] = {
104   {NULL, 0, 0}
105 };
106
107
108 static void
109 shutdown_task (void *cls, 
110                const struct GNUNET_SCHEDULER_TaskContext *tc)
111 {
112   GNUNET_CORE_disconnect (p1.ch);
113   p1.ch = NULL;
114   GNUNET_CORE_disconnect (p2.ch);
115   p2.ch = NULL;
116   ok = 0;
117 }
118
119
120
121
122 static void
123 init_notify (void *cls,
124              struct GNUNET_CORE_Handle *server,
125              const struct GNUNET_PeerIdentity *my_identity,
126              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
127 {
128   struct PeerContext *p = cls;
129
130   GNUNET_assert (server != NULL);
131   GNUNET_assert (p->ch == server);
132   if (cls == &p1)
133     {
134       /* connect p2 */
135       p2.ch = GNUNET_CORE_connect (p2.cfg, 1,
136                                    &p2,
137                                    &init_notify,                         
138                                    &connect_notify,
139                                    &disconnect_notify,
140                                    NULL,
141                                    &inbound_notify,
142                                    GNUNET_YES,
143                                    &outbound_notify, GNUNET_YES, handlers);
144     }
145   else
146     {
147       GNUNET_assert (cls == &p2);
148       GNUNET_SCHEDULER_cancel (timeout_task_id);
149       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
150     }
151 }
152
153
154 static void
155 setup_peer (struct PeerContext *p, const char *cfgname)
156 {
157   p->cfg = GNUNET_CONFIGURATION_create ();
158 #if START_ARM
159   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
160                                         "gnunet-service-arm",
161 #if VERBOSE
162                                         "-L", "DEBUG",
163 #endif
164                                         "-c", cfgname, NULL);
165 #endif
166   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
167 }
168
169
170 static void
171 timeout_task (void *cls, 
172               const struct GNUNET_SCHEDULER_TaskContext *tc)
173 {
174   fprintf (stderr, "Timeout.\n");
175   if (p1.ch != NULL)
176     {
177       GNUNET_CORE_disconnect (p1.ch);
178       p1.ch = NULL;
179     }
180   if (p2.ch != NULL)
181     {
182       GNUNET_CORE_disconnect (p2.ch);
183       p2.ch = NULL;
184     }
185   ok = 42;
186 }
187
188
189
190 static void
191 run (void *cls,
192      char *const *args,
193      const char *cfgfile, 
194      const struct GNUNET_CONFIGURATION_Handle *cfg)
195 {
196   GNUNET_assert (ok == 1);
197   OKPP;
198   setup_peer (&p1, "test_core_api_peer1.conf");
199   setup_peer (&p2, "test_core_api_peer2.conf");
200   timeout_task_id = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, TIMEOUT),
201                                                   &timeout_task,
202                                                   NULL);
203   p1.ch = GNUNET_CORE_connect (p1.cfg, 1,
204                                &p1,
205                                &init_notify,
206                                &connect_notify,
207                                &disconnect_notify,
208                                NULL,
209                                &inbound_notify,
210                                GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
211 }
212
213
214 static void
215 stop_arm (struct PeerContext *p)
216 {
217   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218               "Stopping peer\n");
219 #if START_ARM
220   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
221     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
222   if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
223     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
224   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225               "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
226   GNUNET_OS_process_close (p->arm_proc);
227   p->arm_proc = NULL;
228 #endif
229   GNUNET_CONFIGURATION_destroy (p->cfg);
230 }
231
232
233 static int
234 check ()
235 {
236   char *const argv[] = { "test-core-api-start-only",
237     "-c",
238     "test_core_api_data.conf",
239 #if VERBOSE
240     "-L", "DEBUG",
241 #endif
242     NULL
243   };
244   struct GNUNET_GETOPT_CommandLineOption options[] = {
245     GNUNET_GETOPT_OPTION_END
246   };
247   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
248   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
249
250   ok = 1;
251   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
252                       argv, "test-core-api-start-only", "nohelp", options, &run, &ok);
253   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254               "Test finished\n");
255   stop_arm (&p1);
256   stop_arm (&p2);
257   return ok;
258 }
259
260 int
261 main (int argc, char *argv[])
262 {
263   int ret;
264
265   GNUNET_log_setup ("test-core-api-start-only",
266 #if VERBOSE
267                     "DEBUG",
268 #else
269                     "WARNING",
270 #endif
271                     NULL);
272   ret = check ();
273   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
274   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
275   return ret;
276 }
277
278 /* end of test_core_api_start_only.c */