f335978364647556f0011ca53d508096f68720bd
[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_YES
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                            &connect_notify,
135                            &disconnect_notify,
136                            &inbound_notify,
137                            GNUNET_YES,
138                            &outbound_notify, GNUNET_YES, handlers);
139     }
140   else
141     {
142       GNUNET_assert (cls == &p2);
143       GNUNET_CORE_disconnect (p1.ch);
144       GNUNET_CORE_disconnect (p2.ch);
145       GNUNET_ARM_stop_services (p1.cfg, sched, "core", NULL);
146       GNUNET_ARM_stop_services (p2.cfg, sched, "core", NULL);
147
148       ok = 0;
149     }
150 }
151
152
153 static void
154 setup_peer (struct PeerContext *p, const char *cfgname)
155 {
156   p->cfg = GNUNET_CONFIGURATION_create ();
157 #if START_ARM
158   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
159                                         "gnunet-service-arm",
160 #if VERBOSE
161                                         "-L", "DEBUG",
162 #endif
163                                         "-c", cfgname, NULL);
164 #endif
165   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
166   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
167 }
168
169
170 static void
171 run (void *cls,
172      struct GNUNET_SCHEDULER_Handle *s,
173      char *const *args,
174      const char *cfgfile, 
175      const struct GNUNET_CONFIGURATION_Handle *cfg)
176 {
177   GNUNET_assert (ok == 1);
178   OKPP;
179   sched = s;
180   setup_peer (&p1, "test_core_api_peer1.conf");
181   setup_peer (&p2, "test_core_api_peer2.conf");
182   GNUNET_CORE_connect (sched,
183                        p1.cfg,
184                        TIMEOUT,
185                        &p1,
186                        &init_notify,
187                        &connect_notify,
188                        &disconnect_notify,
189                        &inbound_notify,
190                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
191 }
192
193
194 static void
195 stop_arm (struct PeerContext *p)
196 {
197 #if START_ARM
198   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
199     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
200   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
201     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
202   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203               "ARM process %u stopped\n", p->arm_pid);
204 #endif
205   GNUNET_CONFIGURATION_destroy (p->cfg);
206 }
207
208
209 static int
210 check ()
211 {
212   char *const argv[] = { "test-core-api",
213     "-c",
214     "test_core_api_data.conf",
215 #if VERBOSE
216     "-L", "DEBUG",
217 #endif
218     NULL
219   };
220   struct GNUNET_GETOPT_CommandLineOption options[] = {
221     GNUNET_GETOPT_OPTION_END
222   };
223
224   ok = 1;
225   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
226                       argv, "test-core-api", "nohelp", options, &run, &ok);
227   stop_arm (&p1);
228   stop_arm (&p2);
229   return ok;
230 }
231
232 int
233 main (int argc, char *argv[])
234 {
235   int ret;
236
237   GNUNET_log_setup ("test-core-api",
238 #if VERBOSE
239                     "DEBUG",
240 #else
241                     "WARNING",
242 #endif
243                     NULL);
244   ret = check ();
245
246   return ret;
247 }
248
249 /* end of test_core_api_start_only.c */