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