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