Fix perf_crypto_rsa.c after various changes
[oweals/gnunet.git] / src / util / test_service.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2013 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_CLIENT_Connection *client;
41
42
43 static void
44 do_stop (void *cls)
45 {
46   if (NULL != client)
47   {
48     GNUNET_CLIENT_disconnect (client);
49     client = 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 size_t
64 build_msg (void *cls, size_t size, void *buf)
65 {
66   struct GNUNET_MessageHeader *msg = buf;
67
68   if (size < sizeof (struct GNUNET_MessageHeader))
69   {
70     /* timeout */
71     GNUNET_break (0);
72     GNUNET_SCHEDULER_add_now (&do_stop, NULL);
73     ok = 1;
74     return 0;
75   }
76
77   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected, transmitting\n");
78   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
79   msg->type = htons (MY_TYPE);
80   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
81   return sizeof (struct GNUNET_MessageHeader);
82 }
83
84
85 static void
86 ready (void *cls,
87        int result)
88 {
89   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
90
91   GNUNET_assert (GNUNET_YES == result);
92   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service confirmed running\n");
93   client = GNUNET_CLIENT_connect ("test_service", cfg);
94   GNUNET_assert (client != NULL);
95   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96               "Client connecting, waiting to transmit\n");
97   GNUNET_CLIENT_notify_transmit_ready (client,
98                                        sizeof (struct GNUNET_MessageHeader),
99                                        GNUNET_TIME_UNIT_SECONDS, GNUNET_NO,
100                                        &build_msg, NULL);
101 }
102
103
104 static void
105 recv_cb (void *cls, struct GNUNET_SERVER_Client *sc,
106          const struct GNUNET_MessageHeader *message)
107 {
108   GNUNET_assert (NULL != message);
109   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
110   GNUNET_SERVER_receive_done (sc, GNUNET_OK);
111   GNUNET_SCHEDULER_add_now (&do_stop, NULL);
112   ok = 0;
113 }
114
115
116 static struct GNUNET_SERVER_MessageHandler myhandlers[] = {
117   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
118   {NULL, NULL, 0, 0}
119 };
120
121
122 static void
123 runner (void *cls, struct GNUNET_SERVER_Handle *server,
124         const struct GNUNET_CONFIGURATION_Handle *cfg)
125 {
126   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n");
127   GNUNET_SERVER_add_handlers (server, myhandlers);
128   GNUNET_CLIENT_service_test ("test_service", cfg, GNUNET_TIME_UNIT_SECONDS,
129                               &ready, (void *) cfg);
130 }
131
132
133 /**
134  * Main method, starts scheduler with task1,
135  * checks that "ok" is correct at the end.
136  */
137 static int
138 check ()
139 {
140   ok = 1;
141   char *const argv[] = {
142     "test_service",
143     "-c",
144     "test_service_data.conf",
145     NULL
146   };
147   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n");
148   GNUNET_assert (GNUNET_OK ==
149                  GNUNET_SERVICE_run (3, argv, "test_service",
150                                      GNUNET_SERVICE_OPTION_NONE, &runner, &ok));
151   GNUNET_assert (0 == ok);
152   return ok;
153 }
154
155
156 static void
157 ready6 (void *cls,
158         int result)
159 {
160   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
161
162   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 ready\n");
163   GNUNET_assert (GNUNET_YES == result);
164   client = GNUNET_CLIENT_connect ("test_service6", cfg);
165   GNUNET_assert (client != NULL);
166   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n");
167   GNUNET_CLIENT_notify_transmit_ready (client,
168                                        sizeof (struct GNUNET_MessageHeader),
169                                        GNUNET_TIME_UNIT_SECONDS, GNUNET_NO,
170                                        &build_msg, NULL);
171 }
172
173
174 static void
175 runner6 (void *cls, struct GNUNET_SERVER_Handle *server,
176          const struct GNUNET_CONFIGURATION_Handle *cfg)
177 {
178   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing v6 service\n");
179   GNUNET_SERVER_add_handlers (server, myhandlers);
180   GNUNET_CLIENT_service_test ("test_service6", cfg, GNUNET_TIME_UNIT_SECONDS,
181                               &ready6, (void *) cfg);
182 }
183
184
185 /**
186  * Main method, starts scheduler with task1,
187  * checks that "ok" is correct at the end.
188  */
189 static int
190 check6 ()
191 {
192   char *const argv[] = {
193     "test_service6",
194     "-c",
195     "test_service_data.conf",
196     NULL
197   };
198   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n");
199   GNUNET_assert (GNUNET_OK ==
200                  GNUNET_SERVICE_run (3, argv, "test_service6",
201                                      GNUNET_SERVICE_OPTION_NONE, &runner6,
202                                      &ok));
203   GNUNET_assert (0 == ok);
204   return ok;
205 }
206
207
208 static void
209 start_stop_main (void *cls, char *const *args, const char *cfgfile,
210                  const struct GNUNET_CONFIGURATION_Handle *cfg)
211 {
212   int *ret = cls;
213
214   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service using start method\n");
215   sctx = GNUNET_SERVICE_start ("test_service", cfg, GNUNET_SERVICE_OPTION_NONE);
216   GNUNET_assert (NULL != sctx);
217   runner (cls, GNUNET_SERVICE_get_server (sctx), cfg);
218   *ret = 0;
219 }
220
221
222 static int
223 check_start_stop ()
224 {
225   char *const argv[] = {
226     "test-service-program",
227     "-c",
228     "test_service_data.conf",
229     NULL
230   };
231   const struct GNUNET_GETOPT_CommandLineOption options[] = {
232     GNUNET_GETOPT_OPTION_END
233   };
234   int ret = 1;
235
236   GNUNET_assert (GNUNET_OK ==
237                  GNUNET_PROGRAM_run (3, argv, "test-service-program", "no help",
238                                      options, &start_stop_main, &ret));
239
240   GNUNET_break (0 == ret);
241   return ret;
242 }
243
244
245 int
246 main (int argc, char *argv[])
247 {
248   int ret = 0;
249   struct GNUNET_NETWORK_Handle *s = NULL;
250
251   GNUNET_log_setup ("test-service",
252                     "WARNING",
253                     NULL);
254   ret += check ();
255   ret += check ();
256   // FIXME
257 #ifndef MINGW
258   s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
259 #endif
260   if (NULL == s)
261   {
262     if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
263         (errno == EACCES))
264     {
265       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
266       return 1;
267     }
268     FPRINTF (stderr,
269              "IPv6 support seems to not be available (%s), not testing it!\n",
270              strerror (errno));
271   }
272   else
273   {
274     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
275     ret += check6 ();
276   }
277   ret += check_start_stop ();
278   return ret;
279 }
280
281 /* end of test_service.c */