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