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