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