-typo
[oweals/gnunet.git] / src / util / test_service.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_client_lib.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_service_lib.h"
30 #include "gnunet_scheduler_lib.h"
31 #include "gnunet_time_lib.h"
32
33
34 #define PORT 12435
35
36 #define MY_TYPE 256
37
38 static struct GNUNET_SERVICE_Context *sctx;
39
40 static int ok = 1;
41
42 static struct GNUNET_CLIENT_Connection *client;
43
44
45
46
47 static void
48 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
49 {
50   if (NULL != client)
51   {
52     GNUNET_CLIENT_disconnect (client);
53     client = NULL;
54   }
55   if (NULL != sctx)
56   {
57     GNUNET_SERVICE_stop (sctx);
58     sctx = NULL;
59   }
60   else
61   {
62     GNUNET_SCHEDULER_shutdown ();
63   }
64 }
65
66
67 static size_t
68 build_msg (void *cls, size_t size, void *buf)
69 {
70   struct GNUNET_MessageHeader *msg = buf;
71
72   if (size < sizeof (struct GNUNET_MessageHeader))
73   {
74     /* timeout */
75     GNUNET_break (0);
76     GNUNET_SCHEDULER_add_now (&do_stop, NULL);
77     ok = 1;
78     return 0;
79   }
80
81   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected, transmitting\n");
82   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
83   msg->type = htons (MY_TYPE);
84   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
85   return sizeof (struct GNUNET_MessageHeader);
86 }
87
88
89 static void
90 ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 {
92   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
93
94   GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
95   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service confirmed running\n");
96   client = GNUNET_CLIENT_connect ("test_service", cfg);
97   GNUNET_assert (client != NULL);
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99               "Client connecting, waiting to transmit\n");
100   GNUNET_CLIENT_notify_transmit_ready (client,
101                                        sizeof (struct GNUNET_MessageHeader),
102                                        GNUNET_TIME_UNIT_SECONDS, GNUNET_NO,
103                                        &build_msg, NULL);
104 }
105
106
107 static void
108 recv_cb (void *cls, struct GNUNET_SERVER_Client *sc,
109          const struct GNUNET_MessageHeader *message)
110 {
111   if (NULL == message)
112     return;
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
114   GNUNET_SERVER_receive_done (sc, GNUNET_OK);
115   GNUNET_SCHEDULER_add_now (&do_stop, NULL);
116   ok = 0;
117 }
118
119
120 static struct GNUNET_SERVER_MessageHandler myhandlers[] = {
121   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
122   {NULL, NULL, 0, 0}
123 };
124
125
126 static void
127 runner (void *cls, struct GNUNET_SERVER_Handle *server,
128         const struct GNUNET_CONFIGURATION_Handle *cfg)
129 {
130   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n");
131   GNUNET_SERVER_add_handlers (server, myhandlers);
132   GNUNET_CLIENT_service_test ("test_service", cfg, GNUNET_TIME_UNIT_SECONDS,
133                               &ready, (void *) cfg);
134 }
135
136
137 /**
138  * Main method, starts scheduler with task1,
139  * checks that "ok" is correct at the end.
140  */
141 static int
142 check ()
143 {
144   ok = 1;
145   char *const argv[] = {
146     "test_service",
147     "-c",
148     "test_service_data.conf",
149     NULL
150   };
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n");
152   GNUNET_assert (GNUNET_OK ==
153                  GNUNET_SERVICE_run (3, argv, "test_service",
154                                      GNUNET_SERVICE_OPTION_NONE, &runner, &ok));
155   GNUNET_assert (0 == ok);
156   return ok;
157 }
158
159 static void
160 ready6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 {
162   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
163
164   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 ready\n");
165   GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
166   client = GNUNET_CLIENT_connect ("test_service6", cfg);
167   GNUNET_assert (client != NULL);
168   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n");
169   GNUNET_CLIENT_notify_transmit_ready (client,
170                                        sizeof (struct GNUNET_MessageHeader),
171                                        GNUNET_TIME_UNIT_SECONDS, GNUNET_NO,
172                                        &build_msg, NULL);
173 }
174
175 static void
176 runner6 (void *cls, struct GNUNET_SERVER_Handle *server,
177          const struct GNUNET_CONFIGURATION_Handle *cfg)
178 {
179   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing v6 service\n");
180   GNUNET_SERVER_add_handlers (server, myhandlers);
181   GNUNET_CLIENT_service_test ("test_service6", cfg, GNUNET_TIME_UNIT_SECONDS,
182                               &ready6, (void *) cfg);
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
209 static void
210 start_stop_main (void *cls, char *const *args, const char *cfgfile,
211                  const struct GNUNET_CONFIGURATION_Handle *cfg)
212 {
213   int *ret = cls;
214
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service using start method\n");
216   sctx = GNUNET_SERVICE_start ("test_service", cfg, 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 */