8b5e867bcb9642bd64401edcbcbb419a0e07812b
[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 int
87 inbound_notify (void *cls,
88                 const struct GNUNET_PeerIdentity *other,
89                 const struct GNUNET_MessageHeader *message)
90 {
91   return GNUNET_OK;
92 }
93
94
95 static int
96 outbound_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 struct GNUNET_CORE_MessageHandler handlers[] = {
105   {NULL, 0, 0}
106 };
107
108
109
110 static void
111 init_notify (void *cls,
112              struct GNUNET_CORE_Handle *server,
113              const struct GNUNET_PeerIdentity *my_identity,
114              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
115 {
116   struct PeerContext *p = cls;
117
118   GNUNET_assert (server != NULL);
119   p->ch = server;
120   if (cls == &p1)
121     {
122       /* connect p2 */
123       GNUNET_CORE_connect (sched,
124                            p2.cfg,
125                            TIMEOUT,
126                            &p2,
127                            &init_notify,
128                            NULL,
129                            &connect_notify,
130                            &disconnect_notify,
131                            &inbound_notify,
132                            GNUNET_YES,
133                            &outbound_notify, GNUNET_YES, handlers);
134     }
135   else
136     {
137       GNUNET_assert (cls == &p2);
138       GNUNET_CORE_disconnect (p1.ch);
139       GNUNET_CORE_disconnect (p2.ch);
140       GNUNET_ARM_stop_services (p1.cfg, sched, "core", NULL);
141       GNUNET_ARM_stop_services (p2.cfg, sched, "core", NULL);
142
143       ok = 0;
144     }
145 }
146
147
148 static void
149 setup_peer (struct PeerContext *p, const char *cfgname)
150 {
151   p->cfg = GNUNET_CONFIGURATION_create ();
152 #if START_ARM
153   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
154                                         "gnunet-service-arm",
155 #if VERBOSE
156                                         "-L", "DEBUG",
157 #endif
158                                         "-c", cfgname, NULL);
159 #endif
160   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
161   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
162 }
163
164
165 static void
166 run (void *cls,
167      struct GNUNET_SCHEDULER_Handle *s,
168      char *const *args,
169      const char *cfgfile, 
170      const struct GNUNET_CONFIGURATION_Handle *cfg)
171 {
172   GNUNET_assert (ok == 1);
173   OKPP;
174   sched = s;
175   setup_peer (&p1, "test_core_api_peer1.conf");
176   setup_peer (&p2, "test_core_api_peer2.conf");
177   GNUNET_CORE_connect (sched,
178                        p1.cfg,
179                        TIMEOUT,
180                        &p1,
181                        &init_notify,
182                        NULL,
183                        &connect_notify,
184                        &disconnect_notify,
185                        &inbound_notify,
186                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
187 }
188
189
190 static void
191 stop_arm (struct PeerContext *p)
192 {
193 #if START_ARM
194   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
195     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
196   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
197     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
198   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199               "ARM process %u stopped\n", p->arm_pid);
200 #endif
201   GNUNET_CONFIGURATION_destroy (p->cfg);
202 }
203
204
205 static int
206 check ()
207 {
208   char *const argv[] = { "test-core-api",
209     "-c",
210     "test_core_api_data.conf",
211 #if VERBOSE
212     "-L", "DEBUG",
213 #endif
214     NULL
215   };
216   struct GNUNET_GETOPT_CommandLineOption options[] = {
217     GNUNET_GETOPT_OPTION_END
218   };
219
220   ok = 1;
221   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
222                       argv, "test-core-api", "nohelp", options, &run, &ok);
223   stop_arm (&p1);
224   stop_arm (&p2);
225   return ok;
226 }
227
228 int
229 main (int argc, char *argv[])
230 {
231   int ret;
232
233   GNUNET_log_setup ("test-core-api",
234 #if VERBOSE
235                     "DEBUG",
236 #else
237                     "WARNING",
238 #endif
239                     NULL);
240   ret = check ();
241
242   return ret;
243 }
244
245 /* end of test_core_api_start_only.c */