d0a5aedc4bde25ebbb951d2737302e44bd87c07e
[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, 
90               size_t padding)
91 {
92   return 0;
93 }
94
95
96 static int
97 inbound_notify (void *cls,
98                 const struct GNUNET_PeerIdentity *other,
99                 const struct GNUNET_MessageHeader *message)
100 {
101   return GNUNET_OK;
102 }
103
104
105 static int
106 outbound_notify (void *cls,
107                  const struct GNUNET_PeerIdentity *other,
108                  const struct GNUNET_MessageHeader *message)
109 {
110   return GNUNET_OK;
111 }
112
113
114 static struct GNUNET_CORE_MessageHandler handlers[] = {
115   {NULL, 0, 0}
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   p->ch = server;
130   if (cls == &p1)
131     {
132       /* connect p2 */
133       GNUNET_CORE_connect (sched,
134                            p2.cfg,
135                            TIMEOUT,
136                            &p2,
137                            &init_notify,
138                            &connect_notify,
139                            &disconnect_notify,
140                            &bfc_callback,
141                            &inbound_notify,
142                            GNUNET_YES,
143                            &outbound_notify, GNUNET_YES, handlers);
144     }
145   else
146     {
147       GNUNET_assert (cls == &p2);
148       GNUNET_CORE_disconnect (p1.ch);
149       GNUNET_CORE_disconnect (p2.ch);
150       GNUNET_ARM_stop_service ("core", p1.cfg, sched, TIMEOUT, NULL, NULL);
151       GNUNET_ARM_stop_service ("core", p2.cfg, sched, TIMEOUT, NULL, NULL);
152
153       ok = 0;
154     }
155 }
156
157
158 static void
159 setup_peer (struct PeerContext *p, const char *cfgname)
160 {
161   p->cfg = GNUNET_CONFIGURATION_create ();
162 #if START_ARM
163   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
164                                         "gnunet-service-arm",
165 #if VERBOSE
166                                         "-L", "DEBUG",
167 #endif
168                                         "-c", cfgname, NULL);
169   sleep (1);                    /* allow ARM to start */
170 #endif
171   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
172   GNUNET_ARM_start_service ("core", p->cfg, sched, TIMEOUT, NULL, NULL);
173 }
174
175
176 static void
177 run (void *cls,
178      struct GNUNET_SCHEDULER_Handle *s,
179      char *const *args,
180      const char *cfgfile, 
181      const struct GNUNET_CONFIGURATION_Handle *cfg)
182 {
183   GNUNET_assert (ok == 1);
184   OKPP;
185   sched = s;
186   setup_peer (&p1, "test_core_api_peer1.conf");
187   setup_peer (&p2, "test_core_api_peer2.conf");
188   GNUNET_CORE_connect (sched,
189                        p1.cfg,
190                        TIMEOUT,
191                        &p1,
192                        &init_notify,
193                        &connect_notify,
194                        &disconnect_notify,
195                        &bfc_callback,
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 #if START_ARM
205   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
206     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
207   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
208     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
209   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210               "ARM process %u stopped\n", p->arm_pid);
211 #endif
212   GNUNET_CONFIGURATION_destroy (p->cfg);
213 }
214
215
216 static int
217 check ()
218 {
219   char *const argv[] = { "test-core-api",
220     "-c",
221     "test_core_api_data.conf",
222 #if VERBOSE
223     "-L", "DEBUG",
224 #endif
225     NULL
226   };
227   struct GNUNET_GETOPT_CommandLineOption options[] = {
228     GNUNET_GETOPT_OPTION_END
229   };
230
231   ok = 1;
232   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
233                       argv, "test-core-api", "nohelp", options, &run, &ok);
234   stop_arm (&p1);
235   stop_arm (&p2);
236   return ok;
237 }
238
239 int
240 main (int argc, char *argv[])
241 {
242   int ret;
243
244   GNUNET_log_setup ("test-core-api",
245 #if VERBOSE
246                     "DEBUG",
247 #else
248                     "WARNING",
249 #endif
250                     NULL);
251   ret = check ();
252
253   return ret;
254 }
255
256 /* end of test_core_api_start_only.c */