2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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 3, or (at your
8 option) any later version.
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.
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.
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
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"
40 struct GNUNET_CONFIGURATION_Handle *cfg;
41 struct GNUNET_CORE_Handle *ch;
42 struct GNUNET_PeerIdentity id;
43 struct GNUNET_OS_Process *arm_proc;
46 static struct PeerContext p1;
48 static struct PeerContext p2;
50 static GNUNET_SCHEDULER_TaskIdentifier timeout_task_id;
55 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
57 #define OKPP do { ok++; } while (0)
63 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
69 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
75 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
76 const struct GNUNET_MessageHeader *message)
83 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
84 const struct GNUNET_MessageHeader *message)
90 static struct GNUNET_CORE_MessageHandler handlers[] = {
96 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98 GNUNET_CORE_disconnect (p1.ch);
100 GNUNET_CORE_disconnect (p2.ch);
107 init_notify (void *cls,
108 const struct GNUNET_PeerIdentity *my_identity)
110 struct PeerContext *p = cls;
116 GNUNET_CORE_connect (p2.cfg, &p2, &init_notify, &connect_notify,
117 &disconnect_notify, &inbound_notify, GNUNET_YES,
118 &outbound_notify, GNUNET_YES, handlers);
122 GNUNET_assert (p == &p2);
123 GNUNET_SCHEDULER_cancel (timeout_task_id);
124 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
130 setup_peer (struct PeerContext *p, const char *cfgname)
134 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
135 p->cfg = GNUNET_CONFIGURATION_create ();
137 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
140 "gnunet-service-arm",
141 "-c", cfgname, NULL);
142 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
143 GNUNET_free (binary);
148 timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
150 FPRINTF (stderr, "%s", "Timeout.\n");
153 GNUNET_CORE_disconnect (p1.ch);
158 GNUNET_CORE_disconnect (p2.ch);
166 run (void *cls, char *const *args, const char *cfgfile,
167 const struct GNUNET_CONFIGURATION_Handle *cfg)
169 GNUNET_assert (ok == 1);
171 setup_peer (&p1, "test_core_api_peer1.conf");
172 setup_peer (&p2, "test_core_api_peer2.conf");
174 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
175 (GNUNET_TIME_UNIT_MINUTES, TIMEOUT),
176 &timeout_task, NULL);
178 GNUNET_CORE_connect (p1.cfg, &p1, &init_notify, &connect_notify,
179 &disconnect_notify, &inbound_notify, GNUNET_YES,
180 &outbound_notify, GNUNET_YES, handlers);
185 stop_arm (struct PeerContext *p)
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peer\n");
188 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
189 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
190 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
191 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
193 GNUNET_OS_process_get_pid (p->arm_proc));
194 GNUNET_OS_process_destroy (p->arm_proc);
196 GNUNET_CONFIGURATION_destroy (p->cfg);
203 char *const argv[] = { "test-core-api-start-only",
205 "test_core_api_data.conf",
208 struct GNUNET_GETOPT_CommandLineOption options[] = {
209 GNUNET_GETOPT_OPTION_END
211 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
212 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
215 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
216 "test-core-api-start-only", "nohelp", options, &run, &ok);
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test finished\n");
225 main (int argc, char *argv[])
229 GNUNET_log_setup ("test-core-api-start-only",
233 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
234 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
238 /* end of test_core_api_start_only.c */