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