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