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