getting service_new.c to compile, albeit it is not yet complete
[oweals/gnunet.git] / src / util / test_service.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2013, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file util/test_service.c
22  * @brief tests for service.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27
28
29 #define PORT 12435
30
31 /**
32  * Message type we use for testing.
33  */
34 #define MY_TYPE 256
35
36 static struct GNUNET_SERVICE_Context *sctx;
37
38 static int ok = 1;
39
40 static struct GNUNET_MQ_Handle *mq;
41
42
43 static void
44 do_stop (void *cls)
45 {
46   if (NULL != mq)
47   {
48     GNUNET_MQ_destroy (mq);
49     mq = NULL;
50   }
51   if (NULL != sctx)
52   {
53     GNUNET_SERVICE_stop (sctx);
54     sctx = NULL;
55   }
56   else
57   {
58     GNUNET_SCHEDULER_shutdown ();
59   }
60 }
61
62
63 static void
64 ready (void *cls,
65        int result)
66 {
67   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
68   struct GNUNET_MQ_Envelope *env;
69   struct GNUNET_MessageHeader *msg;
70
71   GNUNET_assert (GNUNET_YES == result);
72   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
73               "Service confirmed running\n");
74   mq = GNUNET_CLIENT_connecT (cfg,
75                               "test_service",
76                               NULL,
77                               NULL,
78                               NULL);
79   GNUNET_assert (NULL != mq);
80   env = GNUNET_MQ_msg (msg,
81                        MY_TYPE);
82   GNUNET_MQ_send (mq,
83                   env);
84 }
85
86
87 static void
88 recv_cb (void *cls,
89          struct GNUNET_SERVER_Client *sc,
90          const struct GNUNET_MessageHeader *message)
91 {
92   GNUNET_assert (NULL != message);
93   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
94   GNUNET_SERVER_receive_done (sc, GNUNET_OK);
95   GNUNET_SCHEDULER_add_now (&do_stop, NULL);
96   ok = 0;
97 }
98
99
100 static struct GNUNET_SERVER_MessageHandler myhandlers[] = {
101   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
102   {NULL, NULL, 0, 0}
103 };
104
105
106 static void
107 runner (void *cls,
108         struct GNUNET_SERVER_Handle *server,
109         const struct GNUNET_CONFIGURATION_Handle *cfg)
110 {
111   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112               "Service initializing\n");
113   GNUNET_SERVER_add_handlers (server,
114                               myhandlers);
115   GNUNET_CLIENT_service_test ("test_service", cfg, GNUNET_TIME_UNIT_SECONDS,
116                               &ready, (void *) cfg);
117 }
118
119
120 /**
121  * Main method, starts scheduler with task1,
122  * checks that "ok" is correct at the end.
123  */
124 static int
125 check ()
126 {
127   ok = 1;
128   char *const argv[] = {
129     "test_service",
130     "-c",
131     "test_service_data.conf",
132     NULL
133   };
134   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n");
135   GNUNET_assert (GNUNET_OK ==
136                  GNUNET_SERVICE_run (3, argv, "test_service",
137                                      GNUNET_SERVICE_OPTION_NONE, &runner, &ok));
138   GNUNET_assert (0 == ok);
139   return ok;
140 }
141
142
143 static void
144 ready6 (void *cls,
145         int result)
146 {
147   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
148   struct GNUNET_MQ_Envelope *env;
149   struct GNUNET_MessageHeader *msg;
150
151   GNUNET_assert (GNUNET_YES == result);
152   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153               "V6 ready\n");
154   mq = GNUNET_CLIENT_connecT (cfg,
155                               "test_service6",
156                               NULL,
157                               NULL,
158                               NULL);
159   GNUNET_assert (NULL != mq);
160   env = GNUNET_MQ_msg (msg,
161                        MY_TYPE);
162   GNUNET_MQ_send (mq,
163                   env);
164 }
165
166
167 static void
168 runner6 (void *cls,
169          struct GNUNET_SERVER_Handle *server,
170          const struct GNUNET_CONFIGURATION_Handle *cfg)
171 {
172   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173               "Initializing v6 service\n");
174   GNUNET_SERVER_add_handlers (server,
175                               myhandlers);
176   GNUNET_CLIENT_service_test ("test_service6", cfg, GNUNET_TIME_UNIT_SECONDS,
177                               &ready6, (void *) cfg);
178 }
179
180
181 /**
182  * Main method, starts scheduler with task1,
183  * checks that "ok" is correct at the end.
184  */
185 static int
186 check6 ()
187 {
188   char *const argv[] = {
189     "test_service6",
190     "-c",
191     "test_service_data.conf",
192     NULL
193   };
194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n");
195   GNUNET_assert (GNUNET_OK ==
196                  GNUNET_SERVICE_run (3, argv, "test_service6",
197                                      GNUNET_SERVICE_OPTION_NONE, &runner6,
198                                      &ok));
199   GNUNET_assert (0 == ok);
200   return ok;
201 }
202
203
204 static void
205 start_stop_main (void *cls,
206                  char *const *args,
207                  const char *cfgfile,
208                  const struct GNUNET_CONFIGURATION_Handle *cfg)
209 {
210   int *ret = cls;
211
212   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213               "Starting service using start method\n");
214   sctx = GNUNET_SERVICE_start ("test_service",
215                                cfg,
216                                GNUNET_SERVICE_OPTION_NONE);
217   GNUNET_assert (NULL != sctx);
218   runner (cls, GNUNET_SERVICE_get_server (sctx), cfg);
219   *ret = 0;
220 }
221
222
223 static int
224 check_start_stop ()
225 {
226   char *const argv[] = {
227     "test-service-program",
228     "-c",
229     "test_service_data.conf",
230     NULL
231   };
232   const struct GNUNET_GETOPT_CommandLineOption options[] = {
233     GNUNET_GETOPT_OPTION_END
234   };
235   int ret = 1;
236
237   GNUNET_assert (GNUNET_OK ==
238                  GNUNET_PROGRAM_run (3, argv, "test-service-program", "no help",
239                                      options, &start_stop_main, &ret));
240
241   GNUNET_break (0 == ret);
242   return ret;
243 }
244
245
246 int
247 main (int argc, char *argv[])
248 {
249   int ret = 0;
250   struct GNUNET_NETWORK_Handle *s = NULL;
251
252   GNUNET_log_setup ("test-service",
253                     "WARNING",
254                     NULL);
255   ret += check ();
256   ret += check ();
257   // FIXME
258 #ifndef MINGW
259   s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
260 #endif
261   if (NULL == s)
262   {
263     if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
264         (errno == EACCES))
265     {
266       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
267       return 1;
268     }
269     FPRINTF (stderr,
270              "IPv6 support seems to not be available (%s), not testing it!\n",
271              strerror (errno));
272   }
273   else
274   {
275     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
276     ret += check6 ();
277   }
278   ret += check_start_stop ();
279   return ret;
280 }
281
282 /* end of test_service.c */