aboutsummaryrefslogtreecommitdiff
path: root/src/core/test_core_api_start_only.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/test_core_api_start_only.c')
-rw-r--r--src/core/test_core_api_start_only.c257
1 files changed, 257 insertions, 0 deletions
diff --git a/src/core/test_core_api_start_only.c b/src/core/test_core_api_start_only.c
new file mode 100644
index 000000000..dc4bfea98
--- /dev/null
+++ b/src/core/test_core_api_start_only.c
@@ -0,0 +1,257 @@
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
46struct 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
56static struct PeerContext p1;
57
58static struct PeerContext p2;
59
60static struct GNUNET_SCHEDULER_Handle *sched;
61
62static 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
72static void
73connect_notify (void *cls,
74 const struct GNUNET_PeerIdentity *peer,
75 unsigned int bpm, struct GNUNET_TIME_Absolute last_activity)
76{
77}
78
79
80static void
81disconnect_notify (void *cls,
82 const struct GNUNET_PeerIdentity *peer,
83 unsigned int bpm,
84 struct GNUNET_TIME_Absolute last_activity)
85{
86}
87
88
89static unsigned int
90bfc_callback (void *cls,
91 const struct GNUNET_PeerIdentity *receiver,
92 void *position, unsigned int padding)
93{
94 return 0;
95}
96
97
98static int
99inbound_notify (void *cls,
100 const struct GNUNET_PeerIdentity *other,
101 const struct GNUNET_MessageHeader *message)
102{
103 return GNUNET_OK;
104}
105
106
107static int
108outbound_notify (void *cls,
109 const struct GNUNET_PeerIdentity *other,
110 const struct GNUNET_MessageHeader *message)
111{
112 return GNUNET_OK;
113}
114
115
116static struct GNUNET_CORE_MessageHandler handlers[] = {
117 {NULL, 0, 0}
118};
119
120
121
122static void
123init_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
160static void
161setup_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
178static void
179run (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
202static void
203stop_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
217static int
218check ()
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
240int
241main (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 */