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