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