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