installing all service, daemon and helper binaries to lib/gnunet/libexec/; updating...
[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 3, 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 TIMEOUT 5
35
36 #define MTYPE 12345
37
38 struct PeerContext
39 {
40   struct GNUNET_CONFIGURATION_Handle *cfg;
41   struct GNUNET_CORE_Handle *ch;
42   struct GNUNET_PeerIdentity id;
43   struct GNUNET_OS_Process *arm_proc;
44 };
45
46 static struct PeerContext p1;
47
48 static struct PeerContext p2;
49
50 static GNUNET_SCHEDULER_TaskIdentifier timeout_task_id;
51
52 static int ok;
53
54 #if VERBOSE
55 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
56 #else
57 #define OKPP do { ok++; } while (0)
58 #endif
59
60
61
62 static void
63 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
64                 const struct GNUNET_ATS_Information *atsi,
65                 unsigned int atsi_count)
66 {
67 }
68
69
70 static void
71 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
72 {
73 }
74
75
76 static int
77 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
78                 const struct GNUNET_MessageHeader *message,
79                 const struct GNUNET_ATS_Information *atsi,
80                 unsigned int atsi_count)
81 {
82   return GNUNET_OK;
83 }
84
85
86 static int
87 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
88                  const struct GNUNET_MessageHeader *message,
89                  const struct GNUNET_ATS_Information *atsi,
90                  unsigned int atsi_count)
91 {
92   return GNUNET_OK;
93 }
94
95
96 static struct GNUNET_CORE_MessageHandler handlers[] = {
97   {NULL, 0, 0}
98 };
99
100
101 static void
102 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 {
104   GNUNET_CORE_disconnect (p1.ch);
105   p1.ch = NULL;
106   GNUNET_CORE_disconnect (p2.ch);
107   p2.ch = NULL;
108   ok = 0;
109 }
110
111
112 static void
113 init_notify (void *cls, struct GNUNET_CORE_Handle *server,
114              const struct GNUNET_PeerIdentity *my_identity)
115 {
116   struct PeerContext *p = cls;
117
118   GNUNET_assert (server != NULL);
119   GNUNET_assert (p->ch == server);
120   if (cls == &p1)
121   {
122     /* connect p2 */
123     p2.ch =
124         GNUNET_CORE_connect (p2.cfg, &p2, &init_notify, &connect_notify,
125                              &disconnect_notify, &inbound_notify, GNUNET_YES,
126                              &outbound_notify, GNUNET_YES, handlers);
127   }
128   else
129   {
130     GNUNET_assert (cls == &p2);
131     GNUNET_SCHEDULER_cancel (timeout_task_id);
132     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
133   }
134 }
135
136
137 static void
138 setup_peer (struct PeerContext *p, const char *cfgname)
139 {
140   char *binary;
141
142   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
143   p->cfg = GNUNET_CONFIGURATION_create ();
144   p->arm_proc =
145     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
146                              NULL, NULL,
147                              binary,
148                              "gnunet-service-arm",
149                              "-c", cfgname, NULL);
150   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
151   GNUNET_free (binary);
152 }
153
154
155 static void
156 timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 {
158   FPRINTF (stderr, "%s",  "Timeout.\n");
159   if (p1.ch != NULL)
160   {
161     GNUNET_CORE_disconnect (p1.ch);
162     p1.ch = NULL;
163   }
164   if (p2.ch != NULL)
165   {
166     GNUNET_CORE_disconnect (p2.ch);
167     p2.ch = NULL;
168   }
169   ok = 42;
170 }
171
172
173 static void
174 run (void *cls, char *const *args, const char *cfgfile,
175      const struct GNUNET_CONFIGURATION_Handle *cfg)
176 {
177   GNUNET_assert (ok == 1);
178   OKPP;
179   setup_peer (&p1, "test_core_api_peer1.conf");
180   setup_peer (&p2, "test_core_api_peer2.conf");
181   timeout_task_id =
182       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
183                                     (GNUNET_TIME_UNIT_MINUTES, TIMEOUT),
184                                     &timeout_task, NULL);
185   p1.ch =
186       GNUNET_CORE_connect (p1.cfg, &p1, &init_notify, &connect_notify,
187                            &disconnect_notify, &inbound_notify, GNUNET_YES,
188                            &outbound_notify, GNUNET_YES, handlers);
189 }
190
191
192 static void
193 stop_arm (struct PeerContext *p)
194 {
195   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peer\n");
196   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
197     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
198   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
199     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
200   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
201               GNUNET_OS_process_get_pid (p->arm_proc));
202   GNUNET_OS_process_destroy (p->arm_proc);
203   p->arm_proc = NULL;
204   GNUNET_CONFIGURATION_destroy (p->cfg);
205 }
206
207
208 static int
209 check ()
210 {
211   char *const argv[] = { "test-core-api-start-only",
212     "-c",
213     "test_core_api_data.conf",
214     NULL
215   };
216   struct GNUNET_GETOPT_CommandLineOption options[] = {
217     GNUNET_GETOPT_OPTION_END
218   };
219   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
220   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
221
222   ok = 1;
223   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
224                       "test-core-api-start-only", "nohelp", options, &run, &ok);
225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test finished\n");
226   stop_arm (&p1);
227   stop_arm (&p2);
228   return ok;
229 }
230
231
232 int
233 main (int argc, char *argv[])
234 {
235   int ret;
236
237   GNUNET_log_setup ("test-core-api-start-only",
238                     "WARNING",
239                     NULL);
240   ret = check ();
241   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
242   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
243   return ret;
244 }
245
246 /* end of test_core_api_start_only.c */