paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / core / test_core_api_start_only.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file transport/test_core_api_start_only.c
20  * @brief testcase for core_api.c that only starts two peers,
21  *        connects to the core service and shuts down again
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_arm_service.h"
26 #include "gnunet_core_service.h"
27 #include "gnunet_util_lib.h"
28
29 #define TIMEOUT 5
30
31 #define MTYPE 12345
32
33 struct PeerContext
34 {
35   struct GNUNET_CONFIGURATION_Handle *cfg;
36   struct GNUNET_CORE_Handle *ch;
37   struct GNUNET_PeerIdentity id;
38   struct GNUNET_OS_Process *arm_proc;
39 };
40
41 static struct PeerContext p1;
42
43 static struct PeerContext p2;
44
45 static struct GNUNET_SCHEDULER_Task *timeout_task_id;
46
47 static int ok;
48
49
50 static void *
51 connect_notify (void *cls,
52                 const struct GNUNET_PeerIdentity *peer,
53                 struct GNUNET_MQ_Handle *mq)
54 {
55   return NULL;
56 }
57
58
59 static void
60 disconnect_notify (void *cls,
61                    const struct GNUNET_PeerIdentity *peer,
62                    void *internal_cls)
63 {
64 }
65
66
67 static struct GNUNET_MQ_MessageHandler handlers[] = {
68   GNUNET_MQ_handler_end ()
69 };
70
71
72 static void
73 shutdown_task (void *cls)
74 {
75   GNUNET_CORE_disconnect (p1.ch);
76   p1.ch = NULL;
77   GNUNET_CORE_disconnect (p2.ch);
78   p2.ch = NULL;
79   ok = 0;
80 }
81
82
83 static void
84 init_notify (void *cls,
85              const struct GNUNET_PeerIdentity *my_identity)
86 {
87   struct PeerContext *p = cls;
88
89   if (p == &p1)
90   {
91     /* connect p2 */
92     p2.ch = GNUNET_CORE_connect (p2.cfg,
93                                  &p2,
94                                  &init_notify,
95                                  &connect_notify,
96                                  &disconnect_notify,
97                                  handlers);
98   }
99   else
100   {
101     GNUNET_assert (p == &p2);
102     GNUNET_SCHEDULER_cancel (timeout_task_id);
103     timeout_task_id = NULL;
104     GNUNET_SCHEDULER_add_now (&shutdown_task,
105                               NULL);
106   }
107 }
108
109
110 static void
111 setup_peer (struct PeerContext *p,
112             const char *cfgname)
113 {
114   char *binary;
115
116   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
117   p->cfg = GNUNET_CONFIGURATION_create ();
118   p->arm_proc =
119     GNUNET_OS_start_process (GNUNET_YES,
120                              GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
121                              NULL, NULL, NULL,
122                              binary,
123                              "gnunet-service-arm",
124                              "-c", cfgname,
125                              NULL);
126   GNUNET_assert (GNUNET_OK ==
127                  GNUNET_CONFIGURATION_load (p->cfg,
128                                             cfgname));
129   GNUNET_free (binary);
130 }
131
132
133 static void
134 timeout_task (void *cls)
135 {
136   FPRINTF (stderr,
137            "%s",
138            "Timeout.\n");
139   if (NULL != p1.ch)
140   {
141     GNUNET_CORE_disconnect (p1.ch);
142     p1.ch = NULL;
143   }
144   if (NULL != p2.ch)
145   {
146     GNUNET_CORE_disconnect (p2.ch);
147     p2.ch = NULL;
148   }
149   ok = 42;
150 }
151
152
153 static void
154 run (void *cls,
155      char *const *args,
156      const char *cfgfile,
157      const struct GNUNET_CONFIGURATION_Handle *cfg)
158 {
159   GNUNET_assert (ok == 1);
160   ok++;
161   setup_peer (&p1, "test_core_api_peer1.conf");
162   setup_peer (&p2, "test_core_api_peer2.conf");
163   timeout_task_id =
164       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
165                                     (GNUNET_TIME_UNIT_MINUTES,
166                                      TIMEOUT),
167                                     &timeout_task,
168                                     NULL);
169   p1.ch = GNUNET_CORE_connect (p1.cfg,
170                                &p1,
171                                &init_notify,
172                                &connect_notify,
173                                &disconnect_notify,
174                                handlers);
175 }
176
177
178 static void
179 stop_arm (struct PeerContext *p)
180 {
181   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
182               "Stopping peer\n");
183   if (0 != GNUNET_OS_process_kill (p->arm_proc,
184                                    GNUNET_TERM_SIG))
185     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
186                          "kill");
187   if (GNUNET_OK !=
188       GNUNET_OS_process_wait (p->arm_proc))
189     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
190                          "waitpid");
191   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192               "ARM process %u stopped\n",
193               (unsigned int) GNUNET_OS_process_get_pid (p->arm_proc));
194   GNUNET_OS_process_destroy (p->arm_proc);
195   p->arm_proc = NULL;
196   GNUNET_CONFIGURATION_destroy (p->cfg);
197 }
198
199
200 static int
201 check ()
202 {
203   char *const argv[] = {
204     "test-core-api-start-only",
205     "-c",
206     "test_core_api_data.conf",
207     NULL
208   };
209   struct GNUNET_GETOPT_CommandLineOption options[] = {
210     GNUNET_GETOPT_OPTION_END
211   };
212   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
213   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
214
215   ok = 1;
216   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
217                       argv,
218                       "test-core-api-start-only",
219                       "nohelp",
220                       options,
221                       &run,
222                       &ok);
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224               "Test finished\n");
225   stop_arm (&p1);
226   stop_arm (&p2);
227   return ok;
228 }
229
230
231 int
232 main (int argc,
233       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 */