36d96479699d71e41b2d76a14f5fd888c32ba9fb
[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, const struct GNUNET_PeerIdentity *peer)
78 {
79 }
80
81
82 static int
83 inbound_notify (void *cls,
84                 const struct GNUNET_PeerIdentity *other,
85                 const struct GNUNET_MessageHeader *message,
86                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
87 {
88   return GNUNET_OK;
89 }
90
91
92 static int
93 outbound_notify (void *cls,
94                  const struct GNUNET_PeerIdentity *other,
95                  const struct GNUNET_MessageHeader *message,
96                  const struct GNUNET_TRANSPORT_ATS_Information *atsi)
97 {
98   return GNUNET_OK;
99 }
100
101
102 static struct GNUNET_CORE_MessageHandler handlers[] = {
103   {NULL, 0, 0}
104 };
105
106
107 static void
108 shutdown_task (void *cls, 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, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 {
171   fprintf (stderr, "Timeout.\n");
172   if (p1.ch != NULL)
173   {
174     GNUNET_CORE_disconnect (p1.ch);
175     p1.ch = NULL;
176   }
177   if (p2.ch != NULL)
178   {
179     GNUNET_CORE_disconnect (p2.ch);
180     p2.ch = NULL;
181   }
182   ok = 42;
183 }
184
185
186
187 static void
188 run (void *cls,
189      char *const *args,
190      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
191 {
192   GNUNET_assert (ok == 1);
193   OKPP;
194   setup_peer (&p1, "test_core_api_peer1.conf");
195   setup_peer (&p2, "test_core_api_peer2.conf");
196   timeout_task_id =
197       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
198                                     (GNUNET_TIME_UNIT_MINUTES, TIMEOUT),
199                                     &timeout_task, NULL);
200   p1.ch =
201       GNUNET_CORE_connect (p1.cfg, 1, &p1, &init_notify, &connect_notify,
202                            &disconnect_notify, NULL, &inbound_notify,
203                            GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
204 }
205
206
207 static void
208 stop_arm (struct PeerContext *p)
209 {
210   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peer\n");
211 #if START_ARM
212   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
213     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
214   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
215     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
216   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217               "ARM process %u stopped\n",
218               GNUNET_OS_process_get_pid (p->arm_proc));
219   GNUNET_OS_process_close (p->arm_proc);
220   p->arm_proc = NULL;
221 #endif
222   GNUNET_CONFIGURATION_destroy (p->cfg);
223 }
224
225
226 static int
227 check ()
228 {
229   char *const argv[] = { "test-core-api-start-only",
230     "-c",
231     "test_core_api_data.conf",
232 #if VERBOSE
233     "-L", "DEBUG",
234 #endif
235     NULL
236   };
237   struct GNUNET_GETOPT_CommandLineOption options[] = {
238     GNUNET_GETOPT_OPTION_END
239   };
240   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
241   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
242
243   ok = 1;
244   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
245                       argv, "test-core-api-start-only", "nohelp", options, &run,
246                       &ok);
247   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test finished\n");
248   stop_arm (&p1);
249   stop_arm (&p2);
250   return ok;
251 }
252
253 int
254 main (int argc, char *argv[])
255 {
256   int ret;
257
258   GNUNET_log_setup ("test-core-api-start-only",
259 #if VERBOSE
260                     "DEBUG",
261 #else
262                     "WARNING",
263 #endif
264                     NULL);
265   ret = check ();
266   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
267   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
268   return ret;
269 }
270
271 /* end of test_core_api_start_only.c */